Packet, Auth reworks & Messages
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package dev.wiing.gossip.lib;
|
||||
|
||||
import dev.wiing.gossip.lib.packets.CreateTopicPacket;
|
||||
import dev.wiing.gossip.lib.packets.MessageCreatedPacket;
|
||||
import dev.wiing.gossip.lib.packets.Packet;
|
||||
import dev.wiing.gossip.lib.packets.TopicAddedPacket;
|
||||
import dev.wiing.gossip.lib.packets.TopicCreatedPacket;
|
||||
import dev.wiing.gossip.lib.packets.TopicUpdatePacket;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -28,22 +29,30 @@ public class PacketHandler {
|
||||
listeners.get(type).remove(listener);
|
||||
}
|
||||
|
||||
public <T extends Packet> void runPacket(Class<T> packetClass, T packet) {
|
||||
public <T extends Packet> boolean runPacket(Class<T> packetClass, T packet) {
|
||||
Set<PacketListener<T>> result = getListeners(packetClass);
|
||||
|
||||
if (result == null) return;
|
||||
if (result == null || result.isEmpty()) return false;
|
||||
|
||||
for (PacketListener<T> packetListener : result) {
|
||||
packetListener.onReceive(packet);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void runPacket(Packet packet) {
|
||||
public boolean runPacket(Packet packet) {
|
||||
switch (packet.getType()) {
|
||||
case TopicAddedPacket.TYPE:
|
||||
runPacket(TopicAddedPacket.class, (TopicAddedPacket)packet);
|
||||
break;
|
||||
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;
|
||||
}
|
||||
|
||||
public <T extends Packet> Set<PacketListener<T>> getListeners(Class<T> packetClass) {
|
||||
@@ -55,4 +64,14 @@ public class PacketHandler {
|
||||
}).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public List<Short> getListeningTypes() {
|
||||
return new ArrayList<>() {
|
||||
{
|
||||
add(TopicCreatedPacket.TYPE);
|
||||
add(TopicUpdatePacket.TYPE);
|
||||
add(MessageCreatedPacket.TYPE);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,15 +14,21 @@ public class PacketManager {
|
||||
private final List<Packet> packetList = new ArrayList<>() {
|
||||
{
|
||||
add(new TestPacket());
|
||||
add(new AckPacket());
|
||||
|
||||
add(new RegisterPacket());
|
||||
add(new CredentialsPacket());
|
||||
add(new RegisterRequestPacket());
|
||||
add(new RegisterCredentialsPacket());
|
||||
|
||||
add(new CreateTopicPacket());
|
||||
add(new TopicAddedPacket());
|
||||
add(new TopicPushPacket());
|
||||
add(new TopicCreatedPacket());
|
||||
add(new TopicJoinPacket());
|
||||
add(new TopicUpdatePacket());
|
||||
|
||||
add(new FetchUserPacket());
|
||||
add(new UserFetchPacket());
|
||||
add(new UserDataPacket());
|
||||
|
||||
add(new MessagePushPacket());
|
||||
add(new MessageCreatedPacket());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package dev.wiing.gossip.lib.data;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
public class AuthSecret {
|
||||
|
||||
public static final int LENGTH = 32;
|
||||
|
||||
private byte[] value = new byte[LENGTH];
|
||||
|
||||
public AuthSecret(byte... value) {
|
||||
if (value.length != LENGTH)
|
||||
throw new RuntimeException("Invalid length");
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public AuthSecret(Byte... value) {
|
||||
if (value.length != LENGTH)
|
||||
throw new RuntimeException("Invalid length");
|
||||
|
||||
for (int i = 0; i < value.length; i++) {
|
||||
this.value[i] = value[i];
|
||||
}
|
||||
}
|
||||
|
||||
public AuthSecret(List<Byte> value) {
|
||||
this(value.toArray(new Byte[32]));
|
||||
}
|
||||
|
||||
public AuthSecret(String value) {
|
||||
this(value.getBytes());
|
||||
}
|
||||
|
||||
public AuthSecret(ByteBuffer buffer) {
|
||||
buffer.get(value);
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return new String(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package dev.wiing.gossip.lib.data;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ListData<T extends PacketData> implements PacketData {
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ListDataInitializer<U> {
|
||||
U create();
|
||||
}
|
||||
|
||||
private final ListDataInitializer<T> entryInitializer;
|
||||
|
||||
private final List<T> data = new ArrayList<>();
|
||||
|
||||
public ListData(ListDataInitializer<T> entryInitializer) {
|
||||
this.entryInitializer = entryInitializer;
|
||||
}
|
||||
|
||||
public List<T> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 4 + data.stream()
|
||||
.map(PacketData::getLength)
|
||||
.reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer getBytes() {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(getLength());
|
||||
|
||||
buffer.putInt(data.size());
|
||||
|
||||
for (T datum : data) {
|
||||
buffer.put(datum.getBytes());
|
||||
}
|
||||
|
||||
return buffer.rewind();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBytes(ByteBuffer buffer) {
|
||||
this.data.clear();
|
||||
|
||||
int size = buffer.getInt();
|
||||
|
||||
for (; size > 0; --size) {
|
||||
T entry = entryInitializer.create();
|
||||
entry.setBytes(buffer);
|
||||
this.data.add(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package dev.wiing.gossip.lib.data;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class LongData implements PacketAtomicData<Long> {
|
||||
|
||||
private long value = 0;
|
||||
|
||||
@Override
|
||||
public Long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongData setValue(Long value) {
|
||||
this.value = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 8;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer getBytes() {
|
||||
return ByteBuffer.allocate(getLength()).putLong(value).rewind();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBytes(ByteBuffer buffer) {
|
||||
value = buffer.getLong();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package dev.wiing.gossip.lib.data;
|
||||
|
||||
public interface PacketAtomicData<T> extends PacketData {
|
||||
T getValue();
|
||||
PacketAtomicData<T> setValue(T value);
|
||||
}
|
||||
+1
-6
@@ -1,13 +1,8 @@
|
||||
package dev.wiing.gossip.lib.data;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public interface DataType<T> {
|
||||
|
||||
T getValue();
|
||||
void setValue(T val);
|
||||
|
||||
public interface PacketData {
|
||||
int getLength();
|
||||
|
||||
ByteBuffer getBytes();
|
||||
+3
-4
@@ -1,11 +1,9 @@
|
||||
package dev.wiing.gossip.lib.data;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class StringType implements DataType<String> {
|
||||
public class StringData implements PacketAtomicData<String> {
|
||||
|
||||
private String value = "";
|
||||
|
||||
@@ -15,8 +13,9 @@ public class StringType implements DataType<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(String val) {
|
||||
public StringData setValue(String val) {
|
||||
this.value = val;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -3,15 +3,35 @@ package dev.wiing.gossip.lib.models;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class Message {
|
||||
private User author;
|
||||
private String message;
|
||||
private LocalDateTime postTime;
|
||||
private final User author;
|
||||
private final Topic topic;
|
||||
private final String contents;
|
||||
private final LocalDateTime postTime;
|
||||
|
||||
public Message(User author, String message, LocalDateTime postTime) {
|
||||
public Message(User author, Topic topic, String contents, LocalDateTime postTime) {
|
||||
this.author = author;
|
||||
this.message = message;
|
||||
this.topic = topic;
|
||||
this.contents = contents;
|
||||
this.postTime = postTime;
|
||||
}
|
||||
|
||||
public Message(User author, Topic topic, String contents) {
|
||||
this(author, topic, contents, LocalDateTime.now());
|
||||
}
|
||||
|
||||
public User getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public Topic getTopic() {
|
||||
return topic;
|
||||
}
|
||||
|
||||
public String getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
public LocalDateTime getPostTime() {
|
||||
return postTime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package dev.wiing.gossip.lib.models;
|
||||
|
||||
public class SecretUser extends User {
|
||||
private final byte[] userSecret;
|
||||
import dev.wiing.gossip.lib.data.AuthSecret;
|
||||
import dev.wiing.gossip.lib.models.User;
|
||||
|
||||
public SecretUser(String username, int iconID, long userID, byte[] userSecret) {
|
||||
public class SecretUser extends User {
|
||||
private final AuthSecret userSecret;
|
||||
|
||||
public SecretUser(String username, int iconID, long userID, AuthSecret userSecret) {
|
||||
super(username, iconID, userID);
|
||||
this.userSecret = userSecret;
|
||||
}
|
||||
|
||||
public byte[] getUserSecret() {
|
||||
public AuthSecret getUserSecret() {
|
||||
return userSecret;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@ 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.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class Topic {
|
||||
@@ -16,6 +15,8 @@ public class Topic {
|
||||
private final Map<Long, User> users = new ConcurrentHashMap<>();
|
||||
private short color;
|
||||
|
||||
private final List<Message> messages = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
public Topic(long id, String name, String description, User host, short color) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
@@ -72,20 +73,30 @@ public class Topic {
|
||||
if (this.users.containsKey(user.getUserID())) return;
|
||||
|
||||
this.users.put(user.getUserID(), user);
|
||||
this.changeSupport.firePropertyChange("userAdd", null, getUsersReadOnly());
|
||||
this.changeSupport.firePropertyChange("userAdd", user, null);
|
||||
}
|
||||
|
||||
public void removeUser(User user) {
|
||||
if (!this.users.containsKey(user.getUserID())) return;
|
||||
|
||||
this.users.remove(user.getUserID());
|
||||
this.changeSupport.firePropertyChange("userRemove", null, getUsersReadOnly());
|
||||
this.changeSupport.firePropertyChange("userRemove", null, user);
|
||||
}
|
||||
|
||||
public boolean hasUser(User user) {
|
||||
return this.users.containsKey(user.getUserID());
|
||||
}
|
||||
|
||||
public List<Message> getMessagesReadOnly() {
|
||||
return Collections.unmodifiableList(messages);
|
||||
}
|
||||
|
||||
public void addMessage(Message message) {
|
||||
messages.add(message);
|
||||
|
||||
this.changeSupport.firePropertyChange("messageAdd", null, message);
|
||||
}
|
||||
|
||||
public void addChangeListener(PropertyChangeListener listener) {
|
||||
this.changeSupport.addPropertyChangeListener(listener);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class AckPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x02;
|
||||
public static final int LENGTH = 0x002;
|
||||
|
||||
private short acknowledgement;
|
||||
|
||||
public AckPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
public short getAcknowledgement() {
|
||||
return acknowledgement;
|
||||
}
|
||||
|
||||
public void setAcknowledgement(short acknowledgement) {
|
||||
this.acknowledgement = acknowledgement;
|
||||
}
|
||||
|
||||
public void setAcknowledgement(Packet packet) {
|
||||
setAcknowledgement(packet.getType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
AckPacket packet = new AckPacket();
|
||||
|
||||
packet.acknowledgement = buffer.getShort();
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.putShort(acknowledgement);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.AuthSecret;
|
||||
|
||||
public abstract class AuthRequiredPacket extends Packet {
|
||||
private AuthSecret auth;
|
||||
|
||||
public AuthRequiredPacket(short type, int length) {
|
||||
super(type, length);
|
||||
}
|
||||
|
||||
public AuthSecret getAuth() {
|
||||
return auth;
|
||||
}
|
||||
|
||||
public void setAuth(AuthSecret auth) {
|
||||
this.auth = auth;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringData;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class MessageCreatedPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x32;
|
||||
public static final int LENGTH = 0x014;
|
||||
|
||||
private long authorID;
|
||||
private long topicID;
|
||||
private final StringData contents = new StringData();
|
||||
|
||||
public MessageCreatedPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
public long getAuthorID() {
|
||||
return authorID;
|
||||
}
|
||||
|
||||
public void setAuthorID(long authorID) {
|
||||
this.authorID = authorID;
|
||||
}
|
||||
|
||||
public long getTopicID() {
|
||||
return topicID;
|
||||
}
|
||||
|
||||
public void setTopicID(long topicID) {
|
||||
this.topicID = topicID;
|
||||
}
|
||||
|
||||
public String getContents() {
|
||||
return contents.getValue();
|
||||
}
|
||||
|
||||
public void setContents(String contents) {
|
||||
this.contents.setValue(contents);
|
||||
setLength(16 + this.contents.getLength());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
MessageCreatedPacket packet = new MessageCreatedPacket();
|
||||
packet.setLength(size);
|
||||
|
||||
packet.authorID = buffer.getLong();
|
||||
packet.topicID = buffer.getLong();
|
||||
packet.contents.setBytes(buffer);
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.putLong(authorID);
|
||||
buffer.putLong(topicID);
|
||||
buffer.put(contents.getBytes());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.AuthSecret;
|
||||
import dev.wiing.gossip.lib.data.StringData;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class MessagePushPacket extends AuthRequiredPacket {
|
||||
public static final short TYPE = 0x31;
|
||||
public static final int LENGTH = 0x02C;
|
||||
|
||||
private long topicID;
|
||||
private final StringData message = new StringData();
|
||||
|
||||
public MessagePushPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
public long getTopicID() {
|
||||
return topicID;
|
||||
}
|
||||
|
||||
public void setTopicID(long topicID) {
|
||||
this.topicID = topicID;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message.getValue();
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message.setValue(message);
|
||||
setLength(40 + this.message.getLength());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
MessagePushPacket packet = new MessagePushPacket();
|
||||
packet.setLength(size);
|
||||
|
||||
packet.setAuth(new AuthSecret(buffer));
|
||||
packet.topicID = buffer.getLong();
|
||||
packet.message.setBytes(buffer);
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.put(getAuth().getBytes());
|
||||
buffer.putLong(topicID);
|
||||
buffer.put(message.getBytes());
|
||||
}
|
||||
}
|
||||
+11
-9
@@ -1,24 +1,26 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.AuthSecret;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class CredentialsPacket extends Packet {
|
||||
public class RegisterCredentialsPacket extends Packet {
|
||||
|
||||
public static short TYPE = 0x03;
|
||||
public static short TYPE = 0x04;
|
||||
public static int LENGTH = 0x028;
|
||||
|
||||
private byte[] secret = new byte[32];
|
||||
private AuthSecret secret;
|
||||
private long uID;
|
||||
|
||||
public CredentialsPacket() {
|
||||
public RegisterCredentialsPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
public byte[] getSecret() {
|
||||
public AuthSecret getSecret() {
|
||||
return secret;
|
||||
}
|
||||
|
||||
public void setSecret(byte[] secret) {
|
||||
public void setSecret(AuthSecret secret) {
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
@@ -32,9 +34,9 @@ public class CredentialsPacket extends Packet {
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
CredentialsPacket packet = new CredentialsPacket();
|
||||
RegisterCredentialsPacket packet = new RegisterCredentialsPacket();
|
||||
|
||||
buffer.get(packet.secret);
|
||||
packet.secret = new AuthSecret(buffer);
|
||||
packet.setUID(buffer.getLong());
|
||||
|
||||
return packet;
|
||||
@@ -42,7 +44,7 @@ public class CredentialsPacket extends Packet {
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.put(getSecret());
|
||||
buffer.put(getSecret().getBytes());
|
||||
buffer.putLong(getUID());
|
||||
}
|
||||
}
|
||||
+6
-6
@@ -1,24 +1,24 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringType;
|
||||
import dev.wiing.gossip.lib.data.StringData;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class RegisterPacket extends Packet {
|
||||
public class RegisterRequestPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x02;
|
||||
public static final short TYPE = 0x03;
|
||||
public static final int SIZE = 0x0005;
|
||||
|
||||
private final StringType username = new StringType();
|
||||
private final StringData username = new StringData();
|
||||
private byte avatarID;
|
||||
|
||||
public RegisterPacket() {
|
||||
public RegisterRequestPacket() {
|
||||
super(TYPE, SIZE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
RegisterPacket packet = new RegisterPacket();
|
||||
RegisterRequestPacket packet = new RegisterRequestPacket();
|
||||
packet.setLength(size);
|
||||
|
||||
packet.username.setBytes(buffer);
|
||||
@@ -1,8 +1,7 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringType;
|
||||
import dev.wiing.gossip.lib.data.StringData;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class TestPacket extends Packet {
|
||||
@@ -10,7 +9,7 @@ public class TestPacket extends Packet {
|
||||
public static final short TYPE = 0x01;
|
||||
public static final int SIZE = 0x0000;
|
||||
|
||||
private final StringType message = new StringType();
|
||||
private final StringData message = new StringData();
|
||||
|
||||
public TestPacket() {
|
||||
super(TYPE, SIZE);
|
||||
|
||||
+6
-6
@@ -1,21 +1,21 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringType;
|
||||
import dev.wiing.gossip.lib.data.StringData;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class TopicAddedPacket extends Packet {
|
||||
public class TopicCreatedPacket 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 final StringData topicName = new StringData();
|
||||
private final StringData topicDescription = new StringData();
|
||||
private short topicColor;
|
||||
|
||||
public TopicAddedPacket() {
|
||||
public TopicCreatedPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ public class TopicAddedPacket extends Packet {
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
TopicAddedPacket packet = new TopicAddedPacket();
|
||||
TopicCreatedPacket packet = new TopicCreatedPacket();
|
||||
setLength(size);
|
||||
|
||||
packet.topicID = buffer.getLong();
|
||||
@@ -0,0 +1,41 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.AuthSecret;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class TopicJoinPacket extends AuthRequiredPacket {
|
||||
|
||||
public static final short TYPE = 0x13;
|
||||
public static final int LENGTH = 0x028;
|
||||
|
||||
private long topicID;
|
||||
|
||||
public TopicJoinPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
public long getTopicID() {
|
||||
return topicID;
|
||||
}
|
||||
|
||||
public void setTopicID(long topicID) {
|
||||
this.topicID = topicID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
TopicJoinPacket packet = new TopicJoinPacket();
|
||||
|
||||
packet.setAuth(new AuthSecret(buffer));
|
||||
packet.topicID = buffer.getLong();
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.put(getAuth().getBytes());
|
||||
buffer.putLong(topicID);
|
||||
}
|
||||
}
|
||||
+9
-17
@@ -1,19 +1,19 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringType;
|
||||
import dev.wiing.gossip.lib.data.AuthSecret;
|
||||
import dev.wiing.gossip.lib.data.StringData;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class CreateTopicPacket extends Packet {
|
||||
public class TopicPushPacket extends AuthRequiredPacket {
|
||||
|
||||
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();
|
||||
private final StringData topicName = new StringData();
|
||||
private final StringData topicDescription = new StringData();
|
||||
|
||||
public CreateTopicPacket() {
|
||||
public TopicPushPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
@@ -25,14 +25,6 @@ public class CreateTopicPacket extends Packet {
|
||||
);
|
||||
}
|
||||
|
||||
public byte[] getUserSecret() {
|
||||
return userSecret;
|
||||
}
|
||||
|
||||
public void setUserSecret(byte[] userSecret) {
|
||||
this.userSecret = userSecret;
|
||||
}
|
||||
|
||||
public String getTopicName() {
|
||||
return topicName.getValue();
|
||||
}
|
||||
@@ -53,10 +45,10 @@ public class CreateTopicPacket extends Packet {
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
CreateTopicPacket packet = new CreateTopicPacket();
|
||||
TopicPushPacket packet = new TopicPushPacket();
|
||||
packet.setLength(size);
|
||||
|
||||
buffer.get(packet.userSecret);
|
||||
packet.setAuth(new AuthSecret(buffer));
|
||||
packet.topicName.setBytes(buffer);
|
||||
packet.topicDescription.setBytes(buffer);
|
||||
|
||||
@@ -65,7 +57,7 @@ public class CreateTopicPacket extends Packet {
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
buffer.put(userSecret);
|
||||
buffer.put(getAuth().getBytes());
|
||||
buffer.put(topicName.getBytes());
|
||||
buffer.put(topicDescription.getBytes());
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
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 TopicUpdatePacket extends Packet {
|
||||
public static final short TYPE = 0x14;
|
||||
public static final int LENGTH = 0x009;
|
||||
|
||||
private long topicID;
|
||||
private byte updateFlags = 0;
|
||||
|
||||
// FLAG 0b0001 == 0x1
|
||||
public static final byte FLAG_TOPIC_NAME_UPDATED = 0b1;
|
||||
private final StringData topicName = new StringData();
|
||||
|
||||
// FLAG 0b0010 == 0x2
|
||||
public static final byte FLAG_TOPIC_DESCRIPTION_UPDATED = 0b10;
|
||||
private final StringData topicDescription = new StringData();
|
||||
|
||||
// FLAG 0b0100 == 0x4
|
||||
public static final byte FLAG_USERS_JOINED = 0b100;
|
||||
private final ListData<LongData> usersJoined = new ListData<>(LongData::new);
|
||||
|
||||
// FLAG 0b1000 == 0x8
|
||||
public static final byte FLAG_USERS_LEFT = 0b1000;
|
||||
private final ListData<LongData> usersLeft = new ListData<>(LongData::new);
|
||||
|
||||
public TopicUpdatePacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
private void updateLength() {
|
||||
if (!usersJoined.getData().isEmpty()) {
|
||||
updateFlags |= FLAG_USERS_JOINED;
|
||||
}
|
||||
if (!usersLeft.getData().isEmpty()) {
|
||||
updateFlags |= FLAG_USERS_LEFT;
|
||||
}
|
||||
|
||||
setLength(9 +
|
||||
(isTopicNameModified() ? topicName.getLength() : 0) +
|
||||
(isTopicDescriptionModified() ? topicDescription.getLength() : 0) +
|
||||
(haveUsersJoined() ? usersJoined.getLength() : 0) +
|
||||
(haveUsersLeft() ? usersLeft.getLength() : 0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
updateLength();
|
||||
return super.getLength();
|
||||
}
|
||||
|
||||
public long getTopicID() {
|
||||
return topicID;
|
||||
}
|
||||
|
||||
public void setTopicID(long topicID) {
|
||||
this.topicID = topicID;
|
||||
}
|
||||
|
||||
public boolean isTopicNameModified() {
|
||||
return (updateFlags & FLAG_TOPIC_NAME_UPDATED) > 0;
|
||||
}
|
||||
|
||||
public boolean isTopicDescriptionModified() {
|
||||
return (updateFlags & FLAG_TOPIC_DESCRIPTION_UPDATED) > 0;
|
||||
}
|
||||
|
||||
public boolean haveUsersJoined() {
|
||||
return (updateFlags & FLAG_USERS_JOINED) > 0;
|
||||
}
|
||||
|
||||
public boolean haveUsersLeft() {
|
||||
return (updateFlags & FLAG_USERS_LEFT) > 0;
|
||||
}
|
||||
|
||||
public String getTopicName() {
|
||||
return topicName.getValue();
|
||||
}
|
||||
|
||||
public void setTopicName(String topicName) {
|
||||
this.topicName.setValue(topicName);
|
||||
this.updateFlags |= FLAG_TOPIC_NAME_UPDATED;
|
||||
updateLength();
|
||||
}
|
||||
|
||||
public String getTopicDescription() {
|
||||
return topicDescription.getValue();
|
||||
}
|
||||
|
||||
public void setTopicDescription(String topicDescription) {
|
||||
this.topicDescription.setValue(topicDescription);
|
||||
this.updateFlags |= FLAG_TOPIC_DESCRIPTION_UPDATED;
|
||||
updateLength();
|
||||
}
|
||||
|
||||
public List<LongData> getUsersJoined() {
|
||||
return usersJoined.getData();
|
||||
}
|
||||
|
||||
public List<LongData> getUsersLeft() {
|
||||
return usersLeft.getData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
TopicUpdatePacket packet = new TopicUpdatePacket();
|
||||
packet.setLength(size);
|
||||
|
||||
packet.topicID = buffer.getLong();
|
||||
packet.updateFlags = buffer.get();
|
||||
|
||||
if (packet.isTopicNameModified()) packet.topicName.setBytes(buffer);
|
||||
if (packet.isTopicDescriptionModified()) packet.topicDescription.setBytes(buffer);
|
||||
if (packet.haveUsersJoined()) packet.usersJoined.setBytes(buffer);
|
||||
if (packet.haveUsersLeft()) packet.usersLeft.setBytes(buffer);
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeBytes(ByteBuffer buffer) {
|
||||
updateLength();
|
||||
|
||||
buffer.putLong(topicID);
|
||||
buffer.put(updateFlags);
|
||||
|
||||
if (isTopicNameModified()) buffer.put(topicName.getBytes());
|
||||
if (isTopicDescriptionModified()) buffer.put(topicDescription.getBytes());
|
||||
if (haveUsersJoined()) buffer.put(usersJoined.getBytes());
|
||||
if (haveUsersLeft()) buffer.put(usersLeft.getBytes());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import dev.wiing.gossip.lib.data.StringType;
|
||||
import dev.wiing.gossip.lib.data.StringData;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@@ -10,7 +10,7 @@ public class UserDataPacket extends Packet {
|
||||
public static final int LENGTH = 0x00a;
|
||||
|
||||
private long userID;
|
||||
private final StringType username = new StringType();
|
||||
private final StringData username = new StringData();
|
||||
private byte avatarID;
|
||||
|
||||
public UserDataPacket() {
|
||||
|
||||
+3
-3
@@ -2,14 +2,14 @@ package dev.wiing.gossip.lib.packets;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class FetchUserPacket extends Packet {
|
||||
public class UserFetchPacket extends Packet {
|
||||
|
||||
public static final short TYPE = 0x21;
|
||||
public static final int LENGTH = 0x008;
|
||||
|
||||
private long userID;
|
||||
|
||||
public FetchUserPacket() {
|
||||
public UserFetchPacket() {
|
||||
super(TYPE, LENGTH);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class FetchUserPacket extends Packet {
|
||||
|
||||
@Override
|
||||
public Packet readBytes(ByteBuffer buffer, int size) {
|
||||
FetchUserPacket packet = new FetchUserPacket();
|
||||
UserFetchPacket packet = new UserFetchPacket();
|
||||
|
||||
packet.userID = buffer.getLong();
|
||||
|
||||
Reference in New Issue
Block a user