A bunch of Packet changes
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package dev.wiing.gossip.lib;
|
||||
|
||||
import dev.wiing.gossip.lib.packets.CreateTopicPacket;
|
||||
import dev.wiing.gossip.lib.packets.Packet;
|
||||
import dev.wiing.gossip.lib.packets.TopicAddedPacket;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -36,6 +38,14 @@ public class PacketHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public void runPacket(Packet packet) {
|
||||
switch (packet.getType()) {
|
||||
case TopicAddedPacket.TYPE:
|
||||
runPacket(TopicAddedPacket.class, (TopicAddedPacket)packet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public <T extends Packet> Set<PacketListener<T>> getListeners(Class<T> packetClass) {
|
||||
if (!listeners.containsKey(packetClass))
|
||||
return null;
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package dev.wiing.gossip.lib;
|
||||
|
||||
import dev.wiing.gossip.lib.packets.CredentialsPacket;
|
||||
import dev.wiing.gossip.lib.packets.RegisterPacket;
|
||||
import dev.wiing.gossip.lib.packets.Packet;
|
||||
import dev.wiing.gossip.lib.packets.TestPacket;
|
||||
import dev.wiing.gossip.lib.packets.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -20,6 +17,12 @@ public class PacketManager {
|
||||
|
||||
add(new RegisterPacket());
|
||||
add(new CredentialsPacket());
|
||||
|
||||
add(new CreateTopicPacket());
|
||||
add(new TopicAddedPacket());
|
||||
|
||||
add(new FetchUserPacket());
|
||||
add(new UserDataPacket());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -33,6 +36,9 @@ public class PacketManager {
|
||||
packetMap.clear();
|
||||
|
||||
for (Packet packet : packetList) {
|
||||
if (packetMap.containsKey(packet.getType())) {
|
||||
throw new RuntimeException("Packet type already exists");
|
||||
}
|
||||
packetMap.put(packet.getType(), packet);
|
||||
}
|
||||
}
|
||||
@@ -40,6 +46,8 @@ public class PacketManager {
|
||||
public Packet readPacket(BufferedInputStream stream) throws IOException {
|
||||
ByteBuffer buffer = ByteBuffer.wrap(stream.readNBytes(6));
|
||||
|
||||
if (buffer.capacity() < 6) return null;
|
||||
|
||||
short type = buffer.getShort();
|
||||
int size = buffer.getInt();
|
||||
|
||||
@@ -71,4 +79,10 @@ public class PacketManager {
|
||||
writePacket(outputStream, packet);
|
||||
outputStream.flush();
|
||||
}
|
||||
|
||||
public void replyPacket(Packet source, Packet reply) throws IOException {
|
||||
BufferedOutputStream outputStream = new BufferedOutputStream(source.getSource().getOutputStream());
|
||||
writePacket(outputStream, reply);
|
||||
outputStream.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class StringType implements DataType<String> {
|
||||
|
||||
private String value;
|
||||
private String value = "";
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package dev.wiing.gossip.lib.models;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class Message {
|
||||
private User author;
|
||||
private String message;
|
||||
private LocalDateTime postTime;
|
||||
|
||||
public Message(User author, String message, LocalDateTime postTime) {
|
||||
this.author = author;
|
||||
this.message = message;
|
||||
this.postTime = postTime;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package dev.wiing.gossip.lib.models;
|
||||
|
||||
import java.beans.PropertyChangeListener;
|
||||
import java.beans.PropertyChangeSupport;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class Topic {
|
||||
private PropertyChangeSupport changeSupport;
|
||||
|
||||
private final long id;
|
||||
private String name;
|
||||
private String description;
|
||||
private final User host;
|
||||
private final Map<Long, User> users = new ConcurrentHashMap<>();
|
||||
private short color;
|
||||
|
||||
public Topic(long id, String name, String description, User host, short color) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.host = host;
|
||||
this.color = color;
|
||||
|
||||
this.changeSupport = new PropertyChangeSupport(this);
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
String prev = this.name;
|
||||
this.name = name;
|
||||
this.changeSupport.firePropertyChange("name", prev, name);
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
String prev = this.description;
|
||||
this.description = description;
|
||||
this.changeSupport.firePropertyChange("description", prev, description);
|
||||
}
|
||||
|
||||
public User getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public short getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(short color) {
|
||||
short prev = this.color;
|
||||
this.color = color;
|
||||
this.changeSupport.firePropertyChange("color", prev, color);
|
||||
}
|
||||
|
||||
public Map<Long, User> getUsersReadOnly() {
|
||||
return Collections.unmodifiableMap(users);
|
||||
}
|
||||
|
||||
public void addUser(User user) {
|
||||
if (this.users.containsKey(user.getUserID())) return;
|
||||
|
||||
this.users.put(user.getUserID(), user);
|
||||
this.changeSupport.firePropertyChange("userAdd", null, getUsersReadOnly());
|
||||
}
|
||||
|
||||
public void removeUser(User user) {
|
||||
if (!this.users.containsKey(user.getUserID())) return;
|
||||
|
||||
this.users.remove(user.getUserID());
|
||||
this.changeSupport.firePropertyChange("userRemove", null, getUsersReadOnly());
|
||||
}
|
||||
|
||||
public boolean hasUser(User user) {
|
||||
return this.users.containsKey(user.getUserID());
|
||||
}
|
||||
|
||||
public void addChangeListener(PropertyChangeListener listener) {
|
||||
this.changeSupport.addPropertyChangeListener(listener);
|
||||
}
|
||||
|
||||
public void removeChangeListener(PropertyChangeListener listener) {
|
||||
this.changeSupport.removePropertyChangeListener(listener);
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,12 @@ public class User {
|
||||
private final PropertyChangeSupport change;
|
||||
|
||||
private String username;
|
||||
private int iconID;
|
||||
private int avatarID;
|
||||
private long userID;
|
||||
|
||||
public User(String username, int iconID, long userID) {
|
||||
public User(String username, int avatarID, long userID) {
|
||||
this.username = username;
|
||||
this.iconID = iconID;
|
||||
this.avatarID = avatarID;
|
||||
this.userID = userID;
|
||||
|
||||
this.change = new PropertyChangeSupport(this);
|
||||
@@ -22,20 +22,24 @@ public class User {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getUsernameDisplay() {
|
||||
return "@ " + getUsername();
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
String previous = this.username;
|
||||
this.username = username;
|
||||
change.firePropertyChange("username", previous, username);
|
||||
}
|
||||
|
||||
public int getIconID() {
|
||||
return iconID;
|
||||
public int getAvatarID() {
|
||||
return avatarID;
|
||||
}
|
||||
|
||||
public void setIconID(int iconID) {
|
||||
int previous = this.iconID;
|
||||
this.iconID = iconID;
|
||||
change.firePropertyChange("iconID", previous, iconID);
|
||||
public void setAvatarID(int avatarID) {
|
||||
int previous = this.avatarID;
|
||||
this.avatarID = avatarID;
|
||||
change.firePropertyChange("iconID", previous, avatarID);
|
||||
}
|
||||
|
||||
public long getUserID() {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringType;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class CreateTopicPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x11;
|
||||
public static final int LENGTH = 0x022;
|
||||
|
||||
private byte[] userSecret = new byte[32];
|
||||
private final StringType topicName = new StringType();
|
||||
private final StringType topicDescription = new StringType();
|
||||
|
||||
public CreateTopicPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
private void updateLength() {
|
||||
setLength(
|
||||
32 + // User secret
|
||||
topicName.getLength() +
|
||||
topicDescription.getLength()
|
||||
);
|
||||
}
|
||||
|
||||
public byte[] getUserSecret() {
|
||||
return userSecret;
|
||||
}
|
||||
|
||||
public void setUserSecret(byte[] userSecret) {
|
||||
this.userSecret = userSecret;
|
||||
}
|
||||
|
||||
public String getTopicName() {
|
||||
return topicName.getValue();
|
||||
}
|
||||
|
||||
public String getTopicDescription() {
|
||||
return topicDescription.getValue();
|
||||
}
|
||||
|
||||
public void setTopicName(String topicName) {
|
||||
this.topicName.setValue(topicName);
|
||||
updateLength();
|
||||
}
|
||||
|
||||
public void setTopicDescription(String topicDescription) {
|
||||
this.topicDescription.setValue(topicDescription);
|
||||
updateLength();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
CreateTopicPacket packet = new CreateTopicPacket();
|
||||
packet.setLength(size);
|
||||
|
||||
buffer.get(packet.userSecret);
|
||||
packet.topicName.setBytes(buffer);
|
||||
packet.topicDescription.setBytes(buffer);
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.put(userSecret);
|
||||
buffer.put(topicName.getBytes());
|
||||
buffer.put(topicDescription.getBytes());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class FetchUserPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x21;
|
||||
public static final int LENGTH = 0x008;
|
||||
|
||||
private long userID;
|
||||
|
||||
public FetchUserPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
public long getUserID() {
|
||||
return userID;
|
||||
}
|
||||
|
||||
public void setUserID(long userID) {
|
||||
this.userID = userID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
FetchUserPacket packet = new FetchUserPacket();
|
||||
|
||||
packet.userID = buffer.getLong();
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.putLong(userID);
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,10 @@ import java.nio.ByteBuffer;
|
||||
public class RegisterPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x02;
|
||||
public static final int SIZE = 0x0001;
|
||||
public static final int SIZE = 0x0005;
|
||||
|
||||
private final StringType username = new StringType();
|
||||
private char iconID;
|
||||
private byte avatarID;
|
||||
|
||||
public RegisterPacket() {
|
||||
super(TYPE, SIZE);
|
||||
@@ -22,6 +22,7 @@ public class RegisterPacket extends Packet {
|
||||
packet.setLength(size);
|
||||
|
||||
packet.username.setBytes(buffer);
|
||||
packet.avatarID = buffer.get();
|
||||
|
||||
return packet;
|
||||
}
|
||||
@@ -29,24 +30,23 @@ public class RegisterPacket extends Packet {
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.put(username.getBytes());
|
||||
buffer.put(avatarID);
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username.getValue();
|
||||
}
|
||||
|
||||
public RegisterPacket setUsername(String username) {
|
||||
public void setUsername(String username) {
|
||||
this.username.setValue(username);
|
||||
this.setLength(1 + this.username.getLength());
|
||||
return this;
|
||||
}
|
||||
|
||||
public char getIconID() {
|
||||
return iconID;
|
||||
public byte getAvatarID() {
|
||||
return avatarID;
|
||||
}
|
||||
|
||||
public RegisterPacket setIconID(char iconID) {
|
||||
this.iconID = iconID;
|
||||
return this;
|
||||
public void setAvatarID(byte avatarID) {
|
||||
this.avatarID = avatarID;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringType;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class TopicAddedPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x12;
|
||||
public static final int LENGTH = 0x014;
|
||||
|
||||
private long topicID;
|
||||
private long hostID;
|
||||
private final StringType topicName = new StringType();
|
||||
private final StringType topicDescription = new StringType();
|
||||
private short topicColor;
|
||||
|
||||
public TopicAddedPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
private void updateLength() {
|
||||
setLength(
|
||||
8 + // topicID
|
||||
8 + // hostID
|
||||
topicName.getLength() +
|
||||
topicDescription.getLength() +
|
||||
2 // topicColor
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
TopicAddedPacket packet = new TopicAddedPacket();
|
||||
setLength(size);
|
||||
|
||||
packet.topicID = buffer.getLong();
|
||||
packet.hostID = buffer.getLong();
|
||||
packet.topicName.setBytes(buffer);
|
||||
packet.topicDescription.setBytes(buffer);
|
||||
packet.topicColor = buffer.getShort();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringType;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class UserDataPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x22;
|
||||
public static final int LENGTH = 0x00a;
|
||||
|
||||
private long userID;
|
||||
private final StringType username = new StringType();
|
||||
private byte avatarID;
|
||||
|
||||
public UserDataPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
public long getUserID() {
|
||||
return userID;
|
||||
}
|
||||
|
||||
public void setUserID(long userID) {
|
||||
this.userID = userID;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username.getValue();
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username.setValue(username);
|
||||
setLength(9 + this.username.getLength());
|
||||
}
|
||||
|
||||
public byte getAvatarID() {
|
||||
return avatarID;
|
||||
}
|
||||
|
||||
public void setAvatarID(byte avatarID) {
|
||||
this.avatarID = avatarID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
UserDataPacket packet = new UserDataPacket();
|
||||
packet.setLength(size);
|
||||
|
||||
packet.userID = buffer.getLong();
|
||||
packet.username.setBytes(buffer);
|
||||
packet.avatarID = buffer.get();
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.putLong(userID);
|
||||
buffer.put(username.getBytes());
|
||||
buffer.put(avatarID);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user