Late-pulling

This commit is contained in:
2023-12-13 11:25:41 +02:00
parent 0b1270a916
commit fff9fb90fa
27 changed files with 1260 additions and 193 deletions
@@ -1,9 +1,6 @@
package dev.wiing.gossip.lib;
import dev.wiing.gossip.lib.packets.MessageCreatedPacket;
import dev.wiing.gossip.lib.packets.Packet;
import dev.wiing.gossip.lib.packets.TopicCreatedPacket;
import dev.wiing.gossip.lib.packets.TopicUpdatePacket;
import dev.wiing.gossip.lib.packets.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -42,17 +39,14 @@ public class PacketHandler {
}
public boolean runPacket(Packet packet) {
switch (packet.getType()) {
case TopicCreatedPacket.TYPE:
return runPacket(TopicCreatedPacket.class, (TopicCreatedPacket)packet);
case TopicUpdatePacket.TYPE:
return runPacket(TopicUpdatePacket.class, (TopicUpdatePacket)packet);
case MessageCreatedPacket.TYPE:
return runPacket(MessageCreatedPacket.class, (MessageCreatedPacket)packet);
}
return false;
return switch (packet.getType()) {
case TopicCreatedPacket.TYPE -> runPacket(TopicCreatedPacket.class, (TopicCreatedPacket) packet);
case TopicUpdatePacket.TYPE -> runPacket(TopicUpdatePacket.class, (TopicUpdatePacket) packet);
case TopicListDataPacket.TYPE -> runPacket(TopicListDataPacket.class, (TopicListDataPacket) packet);
case MessageCreatedPacket.TYPE -> runPacket(MessageCreatedPacket.class, (MessageCreatedPacket) packet);
case MessageSystemPacket.TYPE -> runPacket(MessageSystemPacket.class, (MessageSystemPacket) packet);
default -> false;
};
}
public <T extends Packet> Set<PacketListener<T>> getListeners(Class<T> packetClass) {
@@ -69,7 +63,9 @@ public class PacketHandler {
{
add(TopicCreatedPacket.TYPE);
add(TopicUpdatePacket.TYPE);
add(TopicListDataPacket.TYPE);
add(MessageCreatedPacket.TYPE);
add(MessageSystemPacket.TYPE);
}
};
}
@@ -23,12 +23,21 @@ public class PacketManager {
add(new TopicCreatedPacket());
add(new TopicJoinPacket());
add(new TopicUpdatePacket());
add(new TopicListFetchPacket());
add(new TopicListDataPacket());
add(new TopicFetchPacket());
add(new TopicDataPacket());
add(new UserFetchPacket());
add(new UserDataPacket());
add(new MessagePushPacket());
add(new MessageCreatedPacket());
add(new MessageSystemPacket());
add(new MessageListFetchPacket());
add(new MessageListDataPacket());
add(new MessageFetchPacket());
add(new MessageDataPacket());
}
};
@@ -49,7 +58,7 @@ public class PacketManager {
}
}
public Packet readPacket(BufferedInputStream stream) throws IOException {
public Packet readPacket(InputStream stream) throws IOException {
ByteBuffer buffer = ByteBuffer.wrap(stream.readNBytes(6));
if (buffer.capacity() < 6) return null;
@@ -64,10 +73,6 @@ public class PacketManager {
return packet.readBytes(ByteBuffer.wrap(stream.readNBytes(size)), size);
}
public Packet readPacket(InputStream stream) throws IOException {
return readPacket(new BufferedInputStream(stream));
}
public void writePacket(BufferedOutputStream stream, Packet packet) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(packet.getTotalLength());
@@ -77,7 +82,6 @@ public class PacketManager {
packet.writeBytes(buffer);
stream.write(buffer.array());
stream.flush();
}
public void writePacket(OutputStream stream, Packet packet) throws IOException {
@@ -86,6 +90,14 @@ public class PacketManager {
outputStream.flush();
}
public void writeAllPackets(OutputStream stream, Packet ...packets) throws IOException {
BufferedOutputStream outputStream = new BufferedOutputStream(stream);
for (Packet packet : packets) {
writePacket(outputStream, packet);
}
outputStream.flush();
}
public void replyPacket(Packet source, Packet reply) throws IOException {
BufferedOutputStream outputStream = new BufferedOutputStream(source.getSource().getOutputStream());
writePacket(outputStream, reply);
@@ -3,34 +3,32 @@ package dev.wiing.gossip.lib.models;
import java.time.LocalDateTime;
public class Message {
private final User author;
private long id;
private final Topic topic;
private final String contents;
private final LocalDateTime postTime;
public Message(User author, Topic topic, String contents, LocalDateTime postTime) {
this.author = author;
public Message(long id, Topic topic, LocalDateTime postTime) {
this.id = id;
this.topic = topic;
this.contents = contents;
this.postTime = postTime;
}
public Message(User author, Topic topic, String contents) {
this(author, topic, contents, LocalDateTime.now());
public Message(long id, Topic topic) {
this(id, topic, LocalDateTime.now());
}
public User getAuthor() {
return author;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Topic getTopic() {
return topic;
}
public String getContents() {
return contents;
}
public LocalDateTime getPostTime() {
return postTime;
}
@@ -0,0 +1,43 @@
package dev.wiing.gossip.lib.models;
import java.time.LocalDateTime;
import java.util.Objects;
public class SystemMessage extends Message {
public static class SystemType {
public static final short USER_JOIN = 1;
public static final short USER_LEAVE = 2;
public static final short NAME_CHANGE = 3;
public static final short DESCRIPTION_CHANGE = 4;
public static final short HOST_CHANGE = 5;
public static final short EXPIRE = 10;
}
private final short type;
private final User user;
private final String contents;
public SystemMessage(long id, Topic topic, short type, User user, String contents, LocalDateTime postTime) {
super(id, topic, postTime);
this.type = type;
this.user = user;
this.contents = contents;
}
public SystemMessage(long id, Topic topic, short type, User user, String contents) {
this(id, topic, type, user, contents, LocalDateTime.now());
}
public short getType() {
return type;
}
public User getUser() {
return user;
}
public String getContents() {
return contents;
}
}
@@ -16,6 +16,9 @@ public class Topic {
private short color;
private final List<Message> messages = Collections.synchronizedList(new ArrayList<>());
private final Map<Long, Message> messageByIDs = new ConcurrentHashMap<>();
private long messageTrackerID = 1;
public Topic(long id, String name, String description, User host, short color) {
this.id = id;
@@ -93,10 +96,19 @@ public class Topic {
public void addMessage(Message message) {
messages.add(message);
messageByIDs.put(message.getId(), message);
this.changeSupport.firePropertyChange("messageAdd", null, message);
}
public Message getMessageByID(long messageID) {
return messageByIDs.getOrDefault(messageID, null);
}
public long getNextMessageID() {
return messageTrackerID++;
}
public void addChangeListener(PropertyChangeListener listener) {
this.changeSupport.addPropertyChangeListener(listener);
}
@@ -0,0 +1,26 @@
package dev.wiing.gossip.lib.models;
import java.time.LocalDateTime;
public class UserMessage extends Message {
private final User author;
private final String contents;
public UserMessage(long id, User author, Topic topic, String contents, LocalDateTime postTime) {
super(id, topic, postTime);
this.author = author;
this.contents = contents;
}
public UserMessage(long id, User author, Topic topic, String contents) {
this(id, author, topic, contents, LocalDateTime.now());
}
public User getAuthor() {
return author;
}
public String getContents() {
return contents;
}
}
@@ -7,8 +7,9 @@ import java.nio.ByteBuffer;
public class MessageCreatedPacket extends Packet {
public static final short TYPE = 0x32;
public static final int LENGTH = 0x014;
public static final int LENGTH = 0x01C;
private long messageID;
private long authorID;
private long topicID;
private final StringData contents = new StringData();
@@ -17,6 +18,14 @@ public class MessageCreatedPacket extends Packet {
super(TYPE, LENGTH);
}
public long getMessageID() {
return messageID;
}
public void setMessageID(long messageID) {
this.messageID = messageID;
}
public long getAuthorID() {
return authorID;
}
@@ -39,7 +48,7 @@ public class MessageCreatedPacket extends Packet {
public void setContents(String contents) {
this.contents.setValue(contents);
setLength(16 + this.contents.getLength());
setLength(24 + this.contents.getLength());
}
@Override
@@ -47,6 +56,7 @@ public class MessageCreatedPacket extends Packet {
MessageCreatedPacket packet = new MessageCreatedPacket();
packet.setLength(size);
packet.messageID = buffer.getLong();
packet.authorID = buffer.getLong();
packet.topicID = buffer.getLong();
packet.contents.setBytes(buffer);
@@ -56,6 +66,7 @@ public class MessageCreatedPacket extends Packet {
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.putLong(messageID);
buffer.putLong(authorID);
buffer.putLong(topicID);
buffer.put(contents.getBytes());
@@ -0,0 +1,151 @@
package dev.wiing.gossip.lib.packets;
import dev.wiing.gossip.lib.data.StringData;
import java.nio.ByteBuffer;
public class MessageDataPacket extends Packet {
public static final short TYPE = 0x37;
public static final int LENGTH = 0x000;
public static class MessageType {
public static final byte USER = 1;
public static final byte SYSTEM = 2;
}
private long messageID;
private long topicID;
private byte messageType;
// USER TYPE
private long userAuthorID;
private final StringData userContents = new StringData();
// SYSTEM TYPE
private short systemType;
private long systemUserID = 0;
private final StringData systemContents = new StringData();
public MessageDataPacket() {
super(TYPE, LENGTH);
}
private void updateLength() {
setLength(17 +
(isUserMessage() ? (8 + userContents.getLength()) : 0) +
(isSystemMessage() ? (16 + systemContents.getLength()) : 0));
}
public boolean isUserMessage() {
return messageType == MessageType.USER;
}
public boolean isSystemMessage() {
return messageType == MessageType.SYSTEM;
}
public long getMessageID() {
return messageID;
}
public void setMessageID(long messageID) {
this.messageID = messageID;
}
public long getTopicID() {
return topicID;
}
public void setTopicID(long topicID) {
this.topicID = topicID;
}
public byte getMessageType() {
return messageType;
}
public void setMessageType(byte messageType) {
this.messageType = messageType;
}
public long getUserAuthorID() {
return userAuthorID;
}
public void setUserAuthorID(long userAuthorID) {
this.userAuthorID = userAuthorID;
}
public String getUserContents() {
return userContents.getValue();
}
public void setUserContents(String userContents) {
this.userContents.setValue(userContents);
updateLength();
}
public short getSystemType() {
return systemType;
}
public void setSystemType(short systemType) {
this.systemType = systemType;
}
public long getSystemUserID() {
return systemUserID;
}
public void setSystemUserID(long systemUserID) {
this.systemUserID = systemUserID;
}
public String getSystemContents() {
return systemContents.getValue();
}
public void setSystemContents(String systemContents) {
this.systemContents.setValue(systemContents);
updateLength();
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
MessageDataPacket packet = new MessageDataPacket();
packet.setLength(size);
packet.messageID = buffer.getLong();
packet.topicID = buffer.getLong();
packet.messageType = buffer.get();
if (packet.isUserMessage()) {
packet.userAuthorID = buffer.getLong();
packet.userContents.setBytes(buffer);
} else if (packet.isSystemMessage()) {
packet.systemType = buffer.getShort();
packet.systemUserID = buffer.getLong();
packet.systemContents.setBytes(buffer);
}
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.putLong(messageID);
buffer.putLong(topicID);
buffer.put(messageType);
if (isUserMessage()) {
buffer.putLong(userAuthorID);
buffer.put(userContents.getBytes());
} else if (isSystemMessage()) {
buffer.putShort(systemType);
buffer.putLong(systemUserID);
buffer.put(systemContents.getBytes());
}
}
}
@@ -0,0 +1,48 @@
package dev.wiing.gossip.lib.packets;
import java.nio.ByteBuffer;
public class MessageFetchPacket extends Packet {
public static final short TYPE = 0x36;
public static final int LENGTH = 0x010;
private long topicID;
private long messageID;
public MessageFetchPacket() {
super(TYPE, LENGTH);
}
public long getTopicID() {
return topicID;
}
public void setTopicID(long topicID) {
this.topicID = topicID;
}
public long getMessageID() {
return messageID;
}
public void setMessageID(long messageID) {
this.messageID = messageID;
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
MessageFetchPacket packet = new MessageFetchPacket();
packet.topicID = buffer.getLong();
packet.messageID = buffer.getLong();
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.putLong(topicID);
buffer.putLong(messageID);
}
}
@@ -0,0 +1,59 @@
package dev.wiing.gossip.lib.packets;
import dev.wiing.gossip.lib.data.ListData;
import dev.wiing.gossip.lib.data.LongData;
import java.nio.ByteBuffer;
import java.util.List;
public class MessageListDataPacket extends Packet {
public static final short TYPE = 0x35;
public static final int LENGTH = 0x00C;
private long topicID;
private final ListData<LongData> messageIDs = new ListData<>(LongData::new);
public MessageListDataPacket() {
super(TYPE, LENGTH);
}
private void updateLength() {
setLength(8 + messageIDs.getLength());
}
@Override
public int getLength() {
updateLength();
return super.getLength();
}
public long getTopicID() {
return topicID;
}
public void setTopicID(long topicID) {
this.topicID = topicID;
}
public List<LongData> getMessageIDs() {
return messageIDs.getData();
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
MessageListDataPacket packet = new MessageListDataPacket();
packet.setLength(size);
packet.topicID = buffer.getLong();
packet.messageIDs.setBytes(buffer);
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.putLong(topicID);
buffer.put(messageIDs.getBytes());
}
}
@@ -0,0 +1,37 @@
package dev.wiing.gossip.lib.packets;
import java.nio.ByteBuffer;
public class MessageListFetchPacket extends Packet {
public static final short TYPE = 0x34;
public static final int LENGTH = 0x008;
private long topicID;
public MessageListFetchPacket() {
super(TYPE, LENGTH);
}
public long getTopicID() {
return topicID;
}
public void setTopicID(long topicID) {
this.topicID = topicID;
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
MessageListFetchPacket packet = new MessageListFetchPacket();
packet.topicID = buffer.getLong();
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.putLong(topicID);
}
}
@@ -0,0 +1,86 @@
package dev.wiing.gossip.lib.packets;
import dev.wiing.gossip.lib.data.StringData;
import java.nio.ByteBuffer;
import java.util.Objects;
public class MessageSystemPacket extends Packet {
public static final short TYPE = 0x33;
public static final int LENGTH = 0x01E;
private long messageID;
private long topicID;
private short systemType = 0;
private long userID = 0;
private final StringData content = new StringData();
public MessageSystemPacket() {
super(TYPE, LENGTH);
}
public long getMessageID() {
return messageID;
}
public void setMessageID(long messageID) {
this.messageID = messageID;
}
public long getTopicID() {
return topicID;
}
public void setTopicID(long topicID) {
this.topicID = topicID;
}
public short getSystemType() {
return systemType;
}
public void setSystemType(short systemType) {
this.systemType = systemType;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
public String getContent() {
return content.getValue();
}
public void setContent(String content) {
this.content.setValue(content);
setLength(26 + this.content.getLength());
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
MessageSystemPacket packet = new MessageSystemPacket();
packet.setLength(size);
packet.messageID = buffer.getLong();
packet.topicID = buffer.getLong();
packet.systemType = buffer.getShort();
packet.userID = buffer.getLong();
packet.content.setBytes(buffer);
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.putLong(messageID);
buffer.putLong(topicID);
buffer.putShort(systemType);
buffer.putLong(userID);
buffer.put(content.getBytes());
}
}
@@ -0,0 +1,109 @@
package dev.wiing.gossip.lib.packets;
import dev.wiing.gossip.lib.data.ListData;
import dev.wiing.gossip.lib.data.LongData;
import dev.wiing.gossip.lib.data.StringData;
import java.nio.ByteBuffer;
import java.util.List;
public class TopicDataPacket extends Packet {
public static final short TYPE = 0x18;
public static final int LENGTH = 0x01E;
private long topicID;
private long hostID;
private final StringData topicName = new StringData();
private final StringData topicDescription = new StringData();
private short topicColor;
private final ListData<LongData> userIDs = new ListData<>(LongData::new);
public TopicDataPacket() {
super(TYPE, LENGTH);
}
private void updateLength() {
super.setLength(18 +
topicName.getLength() +
topicDescription.getLength() +
userIDs.getLength());
}
@Override
public int getLength() {
updateLength();
return super.getLength();
}
public long getTopicID() {
return topicID;
}
public void setTopicID(long topicID) {
this.topicID = topicID;
}
public long getHostID() {
return hostID;
}
public void setHostID(long hostID) {
this.hostID = hostID;
}
public String getTopicName() {
return topicName.getValue();
}
public void setTopicName(String topicName) {
this.topicName.setValue(topicName);
updateLength();
}
public String getTopicDescription() {
return topicDescription.getValue();
}
public void setTopicDescription(String topicDescription) {
this.topicDescription.setValue(topicDescription);
updateLength();
}
public short getTopicColor() {
return topicColor;
}
public void setTopicColor(short topicColor) {
this.topicColor = topicColor;
}
public List<LongData> getUserIDs() {
return userIDs.getData();
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
TopicDataPacket packet = new TopicDataPacket();
packet.setLength(size);
packet.topicID = buffer.getLong();
packet.hostID = buffer.getLong();
packet.topicName.setBytes(buffer);
packet.topicDescription.setBytes(buffer);
packet.topicColor = buffer.getShort();
packet.userIDs.setBytes(buffer);
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.putLong(topicID);
buffer.putLong(hostID);
buffer.put(topicName.getBytes());
buffer.put(topicDescription.getBytes());
buffer.putShort(topicColor);
buffer.put(userIDs.getBytes());
}
}
@@ -0,0 +1,37 @@
package dev.wiing.gossip.lib.packets;
import java.nio.ByteBuffer;
public class TopicFetchPacket extends Packet {
public static final short TYPE = 0x17;
public static final int LENGTH = 8;
private long topicID;
public TopicFetchPacket() {
super(TYPE, LENGTH);
}
public long getTopicID() {
return topicID;
}
public void setTopicID(long topicID) {
this.topicID = topicID;
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
TopicFetchPacket packet = new TopicFetchPacket();
packet.topicID = buffer.getLong();
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.putLong(topicID);
}
}
@@ -0,0 +1,48 @@
package dev.wiing.gossip.lib.packets;
import dev.wiing.gossip.lib.data.ListData;
import dev.wiing.gossip.lib.data.LongData;
import java.nio.ByteBuffer;
import java.util.List;
public class TopicListDataPacket extends Packet {
public static final short TYPE = 0x16;
public static final int LENGTH = 0x004;
private final ListData<LongData> topicIDs = new ListData<>(LongData::new);
public TopicListDataPacket() {
super(TYPE, LENGTH);
}
private void updateLength() {
setLength(this.topicIDs.getLength());
}
@Override
public int getLength() {
updateLength();
return super.getLength();
}
public List<LongData> getTopicIDs() {
return topicIDs.getData();
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
TopicListDataPacket packet = new TopicListDataPacket();
packet.setLength(size);
packet.topicIDs.setBytes(buffer);
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
buffer.put(topicIDs.getBytes());
}
}
@@ -0,0 +1,26 @@
package dev.wiing.gossip.lib.packets;
import java.nio.ByteBuffer;
public class TopicListFetchPacket extends Packet {
public static final short TYPE = 0x15;
public static final int LENGTH = 0x000;
public TopicListFetchPacket() {
super(TYPE, LENGTH);
}
@Override
public Packet readBytes(ByteBuffer buffer, int size) {
TopicListFetchPacket packet = new TopicListFetchPacket();
packet.setLength(size);
return packet;
}
@Override
public void writeBytes(ByteBuffer buffer) {
}
}