From f97b39768461c49057b48967dc461351bc07b1f9 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 31 Jul 2022 16:28:33 +0800 Subject: Implement message serializing. --- .../remote/psmb/PsmbMessageSerializer.java | 26 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main/java/com/keuin/crosslink/messaging/endpoint/remote/psmb/PsmbMessageSerializer.java b/src/main/java/com/keuin/crosslink/messaging/endpoint/remote/psmb/PsmbMessageSerializer.java index cc8b220..cb02d7f 100644 --- a/src/main/java/com/keuin/crosslink/messaging/endpoint/remote/psmb/PsmbMessageSerializer.java +++ b/src/main/java/com/keuin/crosslink/messaging/endpoint/remote/psmb/PsmbMessageSerializer.java @@ -8,14 +8,18 @@ import com.keuin.crosslink.messaging.sender.ISender; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; -import org.bson.BsonBinaryReader; -import org.bson.BsonType; +import org.bson.*; +import org.bson.codecs.BsonDocumentCodec; +import org.bson.codecs.EncoderContext; +import org.bson.io.BasicOutputBuffer; import org.jetbrains.annotations.NotNull; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; import java.util.Objects; import java.util.UUID; import java.util.zip.GZIPInputStream; @@ -68,9 +72,23 @@ public class PsmbMessageSerializer { } public static byte[] serialize(@NotNull IMessage message) { - // TODO Objects.requireNonNull(message); - throw new RuntimeException("not implemented"); + var doc = new BsonDocument() + .append("endpoint", new BsonString(message.source().friendlyName())) + .append("sender", new BsonString(message.sender().plainTextId())) + .append("msg", new BsonArray(Collections.singletonList( + new BsonArray(Arrays.asList( + new BsonInt32(0), + new BsonBinary(message.pureString().getBytes(StandardCharsets.UTF_8)) + )) + ))) + .append("time", new BsonInt64(System.currentTimeMillis())); // FIXME current time != message time + var codec = new BsonDocumentCodec(); + try (var writeBuffer = new BasicOutputBuffer(); + var writer = new BsonBinaryWriter(writeBuffer)) { + codec.encode(writer, doc, EncoderContext.builder().build()); + return Arrays.copyOf(writeBuffer.getInternalBuffer(), writeBuffer.getSize()); + } } public static @NotNull IMessage deserialize(@NotNull ByteBuffer buffer, @NotNull IEndpoint source) throws IllegalPackedMessageException { -- cgit v1.2.3