From b0ebb2db3e305d5a352e620fb5940679ca9cd4cb Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 12 Mar 2020 11:17:42 +0100 Subject: [PATCH] =?UTF-8?q?Legger=20til=20en=20del=20objekter=20for=20?= =?UTF-8?q?=C3=A5=20teste=20kryonet=20funksjonalitet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roborally/networking/RoboRallyClient.java | 33 ++++++++++ .../roborally/networking/RoboRallyServer.java | 62 +++++++++++++++++++ .../roborally/networking/SomeRequest.java | 8 +++ .../roborally/networking/SomeResponse.java | 8 +++ .../fiasko/roborally/utility/NetworkUtil.java | 17 +++++ 5 files changed, 128 insertions(+) create mode 100644 src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java create mode 100644 src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java create mode 100644 src/main/java/inf112/fiasko/roborally/networking/SomeRequest.java create mode 100644 src/main/java/inf112/fiasko/roborally/networking/SomeResponse.java create mode 100644 src/main/java/inf112/fiasko/roborally/utility/NetworkUtil.java diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java new file mode 100644 index 0000000..02fbd5e --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyClient.java @@ -0,0 +1,33 @@ +package inf112.fiasko.roborally.networking; + +import com.esotericsoftware.kryonet.Client; +import com.esotericsoftware.kryonet.Connection; +import com.esotericsoftware.kryonet.Listener; +import inf112.fiasko.roborally.utility.NetworkUtil; + +import java.io.IOException; + +public class RoboRallyClient { + public RoboRallyClient() throws IOException { + Client client = new Client(); + client.start(); + NetworkUtil.registerClasses(client.getKryo()); + client.connect(5000, "127.0.0.1", 54555, 54777); + + SomeRequest request = new SomeRequest(); + request.text = "Here is the request"; + client.sendTCP(request); + + client.addListener(new RoboRallyClientListener()); + } +} + +class RoboRallyClientListener extends Listener { + @Override + public void received (Connection connection, Object object) { + if (object instanceof SomeResponse) { + SomeResponse response = (SomeResponse)object; + System.out.println("Client received: " + response.text); + } + } +} \ No newline at end of file diff --git a/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java new file mode 100644 index 0000000..4b3813b --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/networking/RoboRallyServer.java @@ -0,0 +1,62 @@ +package inf112.fiasko.roborally.networking; + +import com.esotericsoftware.kryonet.Connection; +import com.esotericsoftware.kryonet.Listener; +import com.esotericsoftware.kryonet.Server; +import inf112.fiasko.roborally.utility.NetworkUtil; + +import java.io.IOException; + +public class RoboRallyServer { + private Server server; + + public RoboRallyServer() throws IOException { + server = new Server(); + server.start(); + NetworkUtil.registerClasses(server.getKryo()); + server.bind(54555, 54777); + server.addListener(new RoboRallyServerListener()); + } + + /** + * Sends an object to all clients + * @param object The object to send + */ + public void sendToAllClients(Object object) { + server.sendToAllTCP(object); + } +} + +class RoboRallyServerListener extends Listener { + Connection host; + + public RoboRallyServerListener() { + super(); + } + + @Override + public void received (Connection connection, Object object) { + if (object instanceof SomeRequest) { + SomeRequest request = (SomeRequest)object; + System.out.println(request.text); + + SomeResponse response = new SomeResponse(); + response.text = "Thanks"; + connection.sendTCP(response); + } + } + + @Override + public void connected(Connection connection) { + //The first client to connect is assumed to be the host + if (host == null) { + host = connection; + } + System.out.println(connection.getRemoteAddressTCP() + " connected"); + } + + @Override + public void disconnected(Connection connection) { + System.out.println(connection.getRemoteAddressTCP() + " disconnected"); + } +} \ No newline at end of file diff --git a/src/main/java/inf112/fiasko/roborally/networking/SomeRequest.java b/src/main/java/inf112/fiasko/roborally/networking/SomeRequest.java new file mode 100644 index 0000000..fff7135 --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/networking/SomeRequest.java @@ -0,0 +1,8 @@ +package inf112.fiasko.roborally.networking; + +/** + * Represents a request to the server + */ +public class SomeRequest { + public String text; +} \ No newline at end of file diff --git a/src/main/java/inf112/fiasko/roborally/networking/SomeResponse.java b/src/main/java/inf112/fiasko/roborally/networking/SomeResponse.java new file mode 100644 index 0000000..b5ad826 --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/networking/SomeResponse.java @@ -0,0 +1,8 @@ +package inf112.fiasko.roborally.networking; + +/** + * Represents a response from a client + */ +public class SomeResponse { + public String text; +} diff --git a/src/main/java/inf112/fiasko/roborally/utility/NetworkUtil.java b/src/main/java/inf112/fiasko/roborally/utility/NetworkUtil.java new file mode 100644 index 0000000..5d12d7e --- /dev/null +++ b/src/main/java/inf112/fiasko/roborally/utility/NetworkUtil.java @@ -0,0 +1,17 @@ +package inf112.fiasko.roborally.utility; + +import com.esotericsoftware.kryo.Kryo; +import inf112.fiasko.roborally.networking.SomeRequest; +import inf112.fiasko.roborally.networking.SomeResponse; + +public final class NetworkUtil { + + /** + * Registers all classes which can be sent between a server and a client + * @param kryo The kryo object to register the classes to + */ + public static void registerClasses(Kryo kryo) { + kryo.register(SomeRequest.class); + kryo.register(SomeResponse.class); + } +}