From 1ecd8d1d527b6b66b4746e44023b6ab3cb2ca4cc Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 21 Jan 2025 03:17:15 +0500 Subject: feat: using other serializer and gdx-websocket (because the old one doesn't support gwt) --- core/build.gradle | 3 +- .../frogartha/sessions/SessionClient.java | 75 ++++++++++++-------- .../frogartha/sessions/SessionHandlers.java | 3 +- gradle.properties | 2 + html/build.gradle | 19 ++--- .../kz/ilotterytea/frogartha/GdxDefinition.gwt.xml | 60 ++++++++-------- .../kz/ilotterytea/frogartha/gwt/GwtLauncher.java | 2 + lwjgl3/build.gradle | 82 +++++++++++----------- .../frogartha/lwjgl3/Lwjgl3Launcher.java | 2 + .../frogartha/server/FrogarthaServer.java | 22 ++++-- .../frogartha/server/PlayerConnection.java | 14 ++-- .../frogartha/server/ServerHandlers.java | 2 +- shared/build.gradle | 3 +- .../kz/ilotterytea/frogartha/domain/Identity.java | 22 +++++- .../frogartha/domain/server/Acknowledge.java | 36 +++++++--- .../frogartha/exceptions/PlayerKickException.java | 20 +++++- .../frogartha/utils/SerializerUtils.java | 14 ++++ 17 files changed, 240 insertions(+), 141 deletions(-) create mode 100644 shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java diff --git a/core/build.gradle b/core/build.gradle index eac5714..a731b5d 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -2,9 +2,8 @@ eclipse.project.name = appName + '-core' dependencies { - api "com.badlogicgames.gdx:gdx:$gdxVersion" api "com.github.mgsx-dev.gdx-gltf:gltf:$gdxGltfVersion" - api "org.java-websocket:Java-WebSocket:$javaWebsocketVersion" + api "com.github.MrStahlfelge.gdx-websockets:core:$gdxWsVersion" api "ch.qos.logback:logback-classic:$logbackVersion" api project(':shared') diff --git a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java index 555c287..67afd69 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java @@ -1,49 +1,63 @@ package kz.ilotterytea.frogartha.sessions; +import com.github.czyzby.websocket.WebSocket; +import com.github.czyzby.websocket.WebSocketListener; +import com.github.czyzby.websocket.WebSockets; +import com.github.czyzby.websocket.serialization.impl.ManualSerializer; import kz.ilotterytea.frogartha.FrogarthaConstants; import kz.ilotterytea.frogartha.FrogarthaGame; import kz.ilotterytea.frogartha.domain.Identity; import kz.ilotterytea.frogartha.domain.server.Acknowledge; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.handshake.ServerHandshake; +import kz.ilotterytea.frogartha.utils.SerializerUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.net.URI; -import java.nio.ByteBuffer; - -public class SessionClient extends WebSocketClient { +public class SessionClient implements WebSocketListener { private final Logger log; private final FrogarthaGame game; + private final WebSocket connection; + private final ManualSerializer serializer; public SessionClient() { - super(URI.create(FrogarthaConstants.URLS.SESSION_WSS)); + this.connection = WebSockets.newSocket(FrogarthaConstants.URLS.SESSION_WSS); + this.connection.addListener(this); + + this.serializer = new ManualSerializer(); + SerializerUtils.registerTypes(serializer); + connection.setSerializer(serializer); + this.log = LoggerFactory.getLogger(SessionClient.class); this.game = FrogarthaGame.getInstance(); } @Override - public void onOpen(ServerHandshake handshakedata) { + public boolean onOpen(WebSocket webSocket) { log.info("Connected"); updateIdentity(); + return true; + } + + @Override + public boolean onClose(WebSocket webSocket, int closeCode, String reason) { + log.info("Connection closed! Reason: {} {}", closeCode, reason); + game.getIdentityClient().setAuthorized(false); + return true; } @Override - public void onMessage(String message) { + public boolean onMessage(WebSocket webSocket, String packet) { + return false; } @Override - public void onMessage(ByteBuffer bytes) { + public boolean onMessage(WebSocket webSocket, byte[] packet) { try { - // Deserialize the object - ByteArrayInputStream bais = new ByteArrayInputStream(bytes.array()); - ObjectInputStream ois = new ObjectInputStream(bais); - Object obj = ois.readObject(); + Object obj = serializer.deserialize(packet); + + if (obj == null) { + throw new RuntimeException("Deserialized packet is null"); + } if (obj instanceof Acknowledge) SessionHandlers.handleAcknowledge((Acknowledge) obj); else if (obj instanceof PlayerKickException) throw (PlayerKickException) obj; @@ -52,30 +66,31 @@ public class SessionClient extends WebSocketClient { } catch (Exception e) { log.error("An exception was thrown while processing message", e); } + return true; } @Override - public void onClose(int code, String reason, boolean remote) { - log.info("Connection closed! Reason: {} {}", code, reason); - game.getIdentityClient().setAuthorized(false); - } - - @Override - public void onError(Exception ex) { - log.error("Failed to connect", ex); + public boolean onError(WebSocket webSocket, Throwable error) { + log.error("An exception was thrown on session connection", error); + return true; } public void send(Object object) { try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(object); - send(baos.toByteArray()); + connection.send(serializer.serialize(object)); } catch (Exception e) { log.error("Failed to serialize and send an object", e); } } + public void connect() { + connection.connect(); + } + + public void close() { + connection.close(); + } + public void updateIdentity() { send(new Identity(game.getIdentityClient().getUsername())); } diff --git a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java index 0a298dc..cb6457d 100644 --- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java +++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java @@ -1,7 +1,6 @@ package kz.ilotterytea.frogartha.sessions; import kz.ilotterytea.frogartha.FrogarthaGame; -import kz.ilotterytea.frogartha.domain.Identity; import kz.ilotterytea.frogartha.domain.server.Acknowledge; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +13,7 @@ public class SessionHandlers { public static void handleAcknowledge(Acknowledge acknowledge) { log.info("The server was acknowledged: {}", acknowledge); - if (acknowledge.getPayload() instanceof Identity) { + if (acknowledge.getCode() == Acknowledge.AcknowledgeCode.IDENTIFIED) { game.getIdentityClient().setAuthorized(true); } } diff --git a/gradle.properties b/gradle.properties index 015fb0f..5e93e6d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,6 +7,8 @@ gwtFrameworkVersion=2.11.0 gwtPluginVersion=1.1.29 enableGraalNative=false gdxVersion=1.13.1 +gdxWsVersion=1.9.10.3 +gdxWsLibVersion=1.9.1.9.6 javaWebsocketVersion=1.6.0 logbackVersion=1.5.15 projectVersion=1.0.0 diff --git a/html/build.gradle b/html/build.gradle index 2036b11..89d07bd 100644 --- a/html/build.gradle +++ b/html/build.gradle @@ -1,4 +1,3 @@ - buildscript { repositories { mavenCentral() @@ -14,7 +13,8 @@ apply plugin: "war" apply plugin: "org.gretty" gwt { - gwtVersion = "$gwtFrameworkVersion" // Should match the version used for building the GWT backend. See gradle.properties. + gwtVersion = "$gwtFrameworkVersion" + // Should match the version used for building the GWT backend. See gradle.properties. maxHeapSize = '1G' // Default 256m is not enough for the GWT compiler. GWT is HUNGRY. minHeapSize = '1G' @@ -36,6 +36,9 @@ dependencies { implementation "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources" implementation "com.badlogicgames.gdx:gdx:$gdxVersion:sources" implementation "com.github.mgsx-dev.gdx-gltf:gltf:$gdxGltfVersion:sources" + implementation "com.github.MrStahlfelge.gdx-websockets:core:$gdxWsVersion:sources" + implementation "com.github.MrStahlfelge.gdx-websockets:html:$gdxWsVersion" + implementation "com.github.MrStahlfelge.gdx-websockets:html:$gdxWsVersion:sources" implementation project(':core') } @@ -49,7 +52,7 @@ gretty.resourceBase = "${project.layout.buildDirectory.asFile.get().absolutePath gretty.contextPath = "/" gretty.portPropertiesFileName = "TEMP_PORTS.properties" -task startHttpServer (dependsOn: [draftCompileGwt]) { +task startHttpServer(dependsOn: [draftCompileGwt]) { doFirst { copy { from "webapp" @@ -99,7 +102,7 @@ task dist(dependsOn: [clean, compileGwt]) { file(outputPath).mkdirs() copy { - from("build/gwt/out"){ + from("build/gwt/out") { exclude '**/*.symbolMap' // Not used by a dist, and these can be large. } into outputPath @@ -110,7 +113,7 @@ task dist(dependsOn: [clean, compileGwt]) { exclude 'refresh.png' // We don't need this button; this saves some bytes. } into outputPath - } + } copy { from("webapp") { // These next two lines take the index.html page and remove the superdev refresh button. @@ -121,7 +124,7 @@ task dist(dependsOn: [clean, compileGwt]) { // either remove or comment out only the "filter" line above this. } into outputPath - } + } copy { from "war" into outputPath @@ -137,7 +140,7 @@ task addSource { } } -task distZip(type: Zip, dependsOn: dist){ +task distZip(type: Zip, dependsOn: dist) { //// This uses the output of the dist task, which removes the superdev button from index.html . from(outputPath) archiveVersion = projectVersion @@ -152,6 +155,6 @@ tasks.checkGwt.dependsOn(addSource) java.sourceCompatibility = JavaVersion.VERSION_11 java.targetCompatibility = JavaVersion.VERSION_11 -sourceSets.main.java.srcDirs = [ "src/main/java/" ] +sourceSets.main.java.srcDirs = ["src/main/java/"] eclipse.project.name = appName + "-html" diff --git a/html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml b/html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml index 3f287d1..266f4a8 100644 --- a/html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml +++ b/html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml @@ -1,35 +1,39 @@ - + - - + + - - - - - + + + + + + + + - - + + - - - - + + + + - - - - - - + + + + + + - - - - - - - - \ No newline at end of file + + + + + + + + diff --git a/html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java b/html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java index a4e7ea3..bafa596 100644 --- a/html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java +++ b/html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java @@ -3,6 +3,7 @@ package kz.ilotterytea.frogartha.gwt; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.backends.gwt.GwtApplication; import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration; +import com.github.czyzby.websocket.GwtWebSockets; import kz.ilotterytea.frogartha.FrogarthaGame; /** @@ -23,6 +24,7 @@ public class GwtLauncher extends GwtApplication { @Override public ApplicationListener createApplicationListener() { + GwtWebSockets.initiate(); return FrogarthaGame.getInstance(); } } diff --git a/lwjgl3/build.gradle b/lwjgl3/build.gradle index e2986c6..ac9b29f 100644 --- a/lwjgl3/build.gradle +++ b/lwjgl3/build.gradle @@ -1,11 +1,10 @@ - buildscript { repositories { gradlePluginPortal() } dependencies { classpath "io.github.fourlastor:construo:1.5.1" - if(enableGraalNative == 'true') { + if (enableGraalNative == 'true') { classpath "org.graalvm.buildtools.native:org.graalvm.buildtools.native.gradle.plugin:0.9.28" } } @@ -18,24 +17,25 @@ apply plugin: 'io.github.fourlastor.construo' import io.github.fourlastor.construo.Target -sourceSets.main.resources.srcDirs += [ rootProject.file('assets').path ] +sourceSets.main.resources.srcDirs += [rootProject.file('assets').path] mainClassName = 'kz.ilotterytea.frogartha.lwjgl3.Lwjgl3Launcher' application.setMainClass(mainClassName) eclipse.project.name = appName + '-lwjgl3' java.sourceCompatibility = 11 java.targetCompatibility = 11 if (JavaVersion.current().isJava9Compatible()) { - compileJava.options.release.set(11) + compileJava.options.release.set(11) } dependencies { implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion" implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" + implementation "com.github.MrStahlfelge.gdx-websockets:common:$gdxWsVersion" implementation project(':core') - if(enableGraalNative == 'true') { + if (enableGraalNative == 'true') { implementation "io.github.berstanio:gdx-svmhelper-backend-lwjgl3:$graalHelperVersion" - } + } } @@ -72,41 +72,41 @@ jar { } construo { - // name of the executable - name.set(appName) - // human-readable name, used for example in the `.app` name for macOS - humanName.set(appName) - // Optional, defaults to project version property - version.set("$projectVersion") - - targets.configure { - create("linuxX64", Target.Linux) { - architecture.set(Target.Architecture.X86_64) - jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_linux_hotspot_17.0.12_7.tar.gz") - } - create("macM1", Target.MacOs) { - architecture.set(Target.Architecture.AARCH64) - jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.12_7.tar.gz") - // macOS needs an identifier - identifier.set("kz.ilotterytea.frogartha." + appName) - // Optional: icon for macOS - macIcon.set(project.file("icons/logo.icns")) - } - create("macX64", Target.MacOs) { - architecture.set(Target.Architecture.X86_64) - jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_mac_hotspot_17.0.12_7.tar.gz") - // macOS needs an identifier - identifier.set("kz.ilotterytea.frogartha." + appName) - // Optional: icon for macOS - macIcon.set(project.file("icons/logo.icns")) - } - create("winX64", Target.Windows) { - architecture.set(Target.Architecture.X86_64) - jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_windows_hotspot_17.0.12_7.zip") - // Uncomment the next line to show a console when the game runs, to print messages. - //useConsole.set(true) - } + // name of the executable + name.set(appName) + // human-readable name, used for example in the `.app` name for macOS + humanName.set(appName) + // Optional, defaults to project version property + version.set("$projectVersion") + + targets.configure { + create("linuxX64", Target.Linux) { + architecture.set(Target.Architecture.X86_64) + jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_linux_hotspot_17.0.12_7.tar.gz") + } + create("macM1", Target.MacOs) { + architecture.set(Target.Architecture.AARCH64) + jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.12_7.tar.gz") + // macOS needs an identifier + identifier.set("kz.ilotterytea.frogartha." + appName) + // Optional: icon for macOS + macIcon.set(project.file("icons/logo.icns")) + } + create("macX64", Target.MacOs) { + architecture.set(Target.Architecture.X86_64) + jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_mac_hotspot_17.0.12_7.tar.gz") + // macOS needs an identifier + identifier.set("kz.ilotterytea.frogartha." + appName) + // Optional: icon for macOS + macIcon.set(project.file("icons/logo.icns")) } + create("winX64", Target.Windows) { + architecture.set(Target.Architecture.X86_64) + jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_windows_hotspot_17.0.12_7.zip") + // Uncomment the next line to show a console when the game runs, to print messages. + //useConsole.set(true) + } + } } // Equivalent to the jar task; here for compatibility with gdx-setup. @@ -131,6 +131,6 @@ distributions { startScripts.dependsOn(':lwjgl3:jar') startScripts.classpath = project.tasks.jar.outputs.files -if(enableGraalNative == 'true') { +if (enableGraalNative == 'true') { apply from: file("nativeimage.gradle") } diff --git a/lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java b/lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java index 4767186..86a6985 100644 --- a/lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java +++ b/lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java @@ -2,6 +2,7 @@ package kz.ilotterytea.frogartha.lwjgl3; import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application; import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration; +import com.github.czyzby.websocket.CommonWebSockets; import kz.ilotterytea.frogartha.FrogarthaGame; /** @@ -10,6 +11,7 @@ import kz.ilotterytea.frogartha.FrogarthaGame; public class Lwjgl3Launcher { public static void main(String[] args) { if (StartupHelper.startNewJvmIfRequired()) return; // This handles macOS support and helps on Windows. + CommonWebSockets.initiate(); new Lwjgl3Application(FrogarthaGame.getInstance(), getDefaultConfiguration()); } diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java index c3fcd77..1dbb4bd 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java @@ -1,7 +1,9 @@ package kz.ilotterytea.frogartha.server; +import com.github.czyzby.websocket.serialization.impl.ManualSerializer; import kz.ilotterytea.frogartha.domain.Identity; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; +import kz.ilotterytea.frogartha.utils.SerializerUtils; import org.java_websocket.WebSocket; import org.java_websocket.framing.CloseFrame; import org.java_websocket.handshake.ClientHandshake; @@ -9,8 +11,6 @@ import org.java_websocket.server.WebSocketServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.ByteArrayInputStream; -import java.io.ObjectInputStream; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -22,10 +22,15 @@ public class FrogarthaServer extends WebSocketServer { private final Logger log; private final ArrayList players; + private final ManualSerializer serializer; + private FrogarthaServer() { super(new InetSocketAddress(20015)); this.log = LoggerFactory.getLogger(FrogarthaServer.class); this.players = new ArrayList<>(); + + this.serializer = new ManualSerializer(); + SerializerUtils.registerTypes(serializer); } @Override @@ -66,10 +71,11 @@ public class FrogarthaServer extends WebSocketServer { PlayerConnection player = optionalPlayer.get(); try { - // Deserializing the object - ByteArrayInputStream bais = new ByteArrayInputStream(message.array()); - ObjectInputStream ois = new ObjectInputStream(bais); - Object obj = ois.readObject(); + Object obj = serializer.deserialize(message.array()); + + if (obj == null) { + throw PlayerKickException.internalServerError(); + } if (obj instanceof Identity) ServerHandlers.handleIdentity(player, (Identity) obj); else throw PlayerKickException.internalServerError(); @@ -108,4 +114,8 @@ public class FrogarthaServer extends WebSocketServer { public ArrayList getPlayers() { return players; } + + public ManualSerializer getSerializer() { + return serializer; + } } diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java b/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java index c62dce5..d9326a6 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java @@ -3,9 +3,6 @@ package kz.ilotterytea.frogartha.server; import kz.ilotterytea.frogartha.domain.Identity; import org.java_websocket.WebSocket; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectOutputStream; import java.sql.Timestamp; public class PlayerConnection { @@ -27,15 +24,12 @@ public class PlayerConnection { } public void send(Object object) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { - oos.writeObject(object); - } catch (IOException ignored) { + FrogarthaServer server = FrogarthaServer.getInstance(); + try { + connection.send(server.getSerializer().serialize(object)); + } catch (Exception ignored) { } - - this.connection.send(baos.toByteArray()); } public int getId() { diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java index 7b4a868..7ad2f25 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java @@ -20,7 +20,7 @@ public class ServerHandlers { } player.setIdentity(identity); - player.send(new Acknowledge(identity)); + player.send(new Acknowledge(Acknowledge.AcknowledgeCode.IDENTIFIED)); log.debug("Successfully identified {} for {}", identity, player); } } diff --git a/shared/build.gradle b/shared/build.gradle index 1eafc3b..d2344a1 100644 --- a/shared/build.gradle +++ b/shared/build.gradle @@ -1,5 +1,6 @@ eclipse.project.name = appName + '-shared' dependencies { - + api "com.badlogicgames.gdx:gdx:$gdxVersion" + api "com.github.czyzby:gdx-websocket-serialization:$gdxWsLibVersion" } diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java b/shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java index 9a72268..03c0ef4 100644 --- a/shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java +++ b/shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java @@ -1,9 +1,15 @@ package kz.ilotterytea.frogartha.domain; -import java.io.Serializable; +import com.github.czyzby.websocket.serialization.SerializationException; +import com.github.czyzby.websocket.serialization.Transferable; +import com.github.czyzby.websocket.serialization.impl.Deserializer; +import com.github.czyzby.websocket.serialization.impl.Serializer; -public class Identity implements Serializable { - private final String username; +public class Identity implements Transferable { + private String username; + + public Identity() { + } public Identity(String username) { this.username = username; @@ -27,4 +33,14 @@ public class Identity implements Serializable { "username='" + username + '\'' + '}'; } + + @Override + public void serialize(Serializer serializer) throws SerializationException { + serializer.serializeString(username); + } + + @Override + public Identity deserialize(Deserializer deserializer) throws SerializationException { + return new Identity(deserializer.deserializeString()); + } } diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java b/shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java index f82c1c9..8ca0f66 100644 --- a/shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java +++ b/shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java @@ -1,22 +1,42 @@ package kz.ilotterytea.frogartha.domain.server; -import java.io.Serializable; +import com.github.czyzby.websocket.serialization.SerializationException; +import com.github.czyzby.websocket.serialization.Transferable; +import com.github.czyzby.websocket.serialization.impl.Deserializer; +import com.github.czyzby.websocket.serialization.impl.Serializer; -public class Acknowledge implements Serializable { - private final Object payload; +public class Acknowledge implements Transferable { + public enum AcknowledgeCode { + IDENTIFIED + } + + private AcknowledgeCode code; - public Acknowledge(Object payload) { - this.payload = payload; + public Acknowledge() { } - public Object getPayload() { - return payload; + public Acknowledge(AcknowledgeCode code) { + this.code = code; + } + + public AcknowledgeCode getCode() { + return code; } @Override public String toString() { return "Acknowledge{" + - "payload=" + payload + + "code=" + code.toString() + '}'; } + + @Override + public void serialize(Serializer serializer) throws SerializationException { + serializer.serializeInt(code.ordinal()); + } + + @Override + public Acknowledge deserialize(Deserializer deserializer) throws SerializationException { + return new Acknowledge(AcknowledgeCode.values()[deserializer.deserializeInt()]); + } } diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java b/shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java index a6d8c0a..4eaf389 100644 --- a/shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java +++ b/shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java @@ -1,6 +1,14 @@ package kz.ilotterytea.frogartha.exceptions; -public class PlayerKickException extends RuntimeException { +import com.github.czyzby.websocket.serialization.SerializationException; +import com.github.czyzby.websocket.serialization.Transferable; +import com.github.czyzby.websocket.serialization.impl.Deserializer; +import com.github.czyzby.websocket.serialization.impl.Serializer; + +public class PlayerKickException extends RuntimeException implements Transferable { + public PlayerKickException() { + } + private PlayerKickException(String message) { super(message); } @@ -12,4 +20,14 @@ public class PlayerKickException extends RuntimeException { public static PlayerKickException internalServerError() { return new PlayerKickException("Internal Server Error"); } + + @Override + public void serialize(Serializer serializer) throws SerializationException { + serializer.serializeString(getMessage()); + } + + @Override + public PlayerKickException deserialize(Deserializer deserializer) throws SerializationException { + return new PlayerKickException(deserializer.deserializeString()); + } } diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java b/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java new file mode 100644 index 0000000..bf0c3f3 --- /dev/null +++ b/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java @@ -0,0 +1,14 @@ +package kz.ilotterytea.frogartha.utils; + +import com.github.czyzby.websocket.serialization.impl.ManualSerializer; +import kz.ilotterytea.frogartha.domain.Identity; +import kz.ilotterytea.frogartha.domain.server.Acknowledge; +import kz.ilotterytea.frogartha.exceptions.PlayerKickException; + +public class SerializerUtils { + public static void registerTypes(ManualSerializer serializer) { + serializer.register(new Acknowledge()); + serializer.register(new Identity()); + serializer.register(new PlayerKickException()); + } +} -- cgit v1.2.3