From 867eddb0ff63bf001ffe73cb0e153ead6f0989a6 Mon Sep 17 00:00:00 2001 From: lieght Date: Wed, 17 Sep 2025 16:51:56 +0200 Subject: [PATCH 1/4] Easier to read log pattern --- .idea/workspace.xml | 24 ++++++++++++++++++++++-- src/main/resources/log4j2.xml | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f89020b..a7fb959 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,7 +5,7 @@ - + + + + + @@ -62,7 +82,7 @@ diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index 21163f9..18686f2 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ - + From 437885239467bcaf7ff6e8fe87021807e530639a Mon Sep 17 00:00:00 2001 From: lieght Date: Wed, 17 Sep 2025 16:52:47 +0200 Subject: [PATCH 2/4] Created event for ReceivedMessage from connection --- .idea/workspace.xml | 7 ++++++- src/main/java/org/toop/Main.java | 8 ++++++++ src/main/java/org/toop/game/tictactoe/TicTacToe.java | 3 ++- .../toop/server/backend/tictactoe/TicTacToeServer.java | 2 +- .../java/org/toop/server/frontend/ServerConnection.java | 2 +- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a7fb959..7e0b642 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,6 +6,11 @@ + + + + + diff --git a/src/main/java/org/toop/Main.java b/src/main/java/org/toop/Main.java index 5ba1913..b22f187 100644 --- a/src/main/java/org/toop/Main.java +++ b/src/main/java/org/toop/Main.java @@ -13,6 +13,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; import java.io.IOException; +import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -35,6 +36,13 @@ public class Main { GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, "J", "P", ticTacToeGame)); String ticTacToeGameId = ticTacToeGame.get(); GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId)); + GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ReceivedMessage.class, + event -> { + if (Objects.equals(event.ConnectionUuid(), connectionId)) { + logger.info("Received '{}'", event.message()); + } + } + ); GlobalEventBus.post(new Events.ServerEvents.SendCommand( connectionId, diff --git a/src/main/java/org/toop/game/tictactoe/TicTacToe.java b/src/main/java/org/toop/game/tictactoe/TicTacToe.java index 576731d..127f535 100644 --- a/src/main/java/org/toop/game/tictactoe/TicTacToe.java +++ b/src/main/java/org/toop/game/tictactoe/TicTacToe.java @@ -34,6 +34,7 @@ public class TicTacToe extends GameBase implements Runnable { */ public TicTacToe(String player1, String player2, String gameId) { super(3, new Player(player1, 'X'), new Player(player2, 'O')); + this.gameId = gameId; movesLeft = size * size; } @@ -59,7 +60,7 @@ public class TicTacToe extends GameBase implements Runnable { // if (command == null) { continue; } try { ParsedCommand cmd = this.commandQueue.take(); - logger.info("Game {}, took command: {}", this.gameId, cmd.originalCommand); // TODO: Fix null gameid + logger.info("Game {}, took command: {}", this.gameId, cmd.originalCommand); this.addSendToQueue("OK"); } catch (InterruptedException e) { logger.error("Game {} has crashed.", this.gameId); diff --git a/src/main/java/org/toop/server/backend/tictactoe/TicTacToeServer.java b/src/main/java/org/toop/server/backend/tictactoe/TicTacToeServer.java index e887003..9e1df12 100644 --- a/src/main/java/org/toop/server/backend/tictactoe/TicTacToeServer.java +++ b/src/main/java/org/toop/server/backend/tictactoe/TicTacToeServer.java @@ -114,7 +114,7 @@ public class TicTacToeServer extends TcpServer { try { while (isRunning()) { String msg = game.sendQueue.take(); // blocks until a message is added to the queue - logger.info("Adding: {} to the send queue", msg); + logger.info("Games: {}, Adding: {} to the send queue", game.gameId, msg); this.sendQueue.put(msg); // push to network layer } } catch (InterruptedException e) { diff --git a/src/main/java/org/toop/server/frontend/ServerConnection.java b/src/main/java/org/toop/server/frontend/ServerConnection.java index 5413e20..2f8a99f 100644 --- a/src/main/java/org/toop/server/frontend/ServerConnection.java +++ b/src/main/java/org/toop/server/frontend/ServerConnection.java @@ -108,7 +108,7 @@ public final class ServerConnection implements Runnable { while (running) { String received = tcpClient.readLine(); // blocks if (received != null) { - logger.info("Received: '{}'", received); + logger.info("Connection: {} received: '{}'", this.uuid, received); GlobalEventBus.post(new Events.ServerEvents.ReceivedMessage(this.uuid, received)); } else { break; From 0d4472c81428beb794bb75577d4d6da78e0ca519 Mon Sep 17 00:00:00 2001 From: lieght Date: Wed, 17 Sep 2025 17:19:29 +0200 Subject: [PATCH 3/4] Documentation added for server events --- .idea/workspace.xml | 8 +- src/main/java/org/toop/eventbus/Events.java | 116 +++++++++++++++++--- 2 files changed, 105 insertions(+), 19 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7e0b642..722a9f4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,11 +6,7 @@ - - - - - + diff --git a/src/main/java/org/toop/eventbus/Events.java b/src/main/java/org/toop/eventbus/Events.java index a0a3667..86de31a 100644 --- a/src/main/java/org/toop/eventbus/Events.java +++ b/src/main/java/org/toop/eventbus/Events.java @@ -76,96 +76,186 @@ public class Events implements IEvents { public static class ServerEvents { + /** + * BLOCKING + * Requests all active connections. The result is returned via the provided CompletableFuture. + * + * @param future List of all connections in string form. + */ public record RequestsAllConnections(CompletableFuture future) {} + /** + * BLOCKING + * Requests all active servers. The result is returned via the provided CompletableFuture. + * + * @param future List of all servers in string form. + */ public record RequestsAllServers(CompletableFuture future) {} + /** + * Forces closing all active connections immediately. + */ public record ForceCloseAllConnections() {} + /** + * Forces closing all active servers immediately. + */ public record ForceCloseAllServers() {} + /** + * Requests starting a server with a specific port and game type. + * + * @param port The port to open the server. + * @param gameType Either "tictactoe" or ... + */ public record StartServer(String port, String gameType) {} + + /** + * BLOCKING + * Requests starting a server with a specific port and game type, and returns a CompletableFuture + * that completes when the server has started. + * + * @param port The port to open the server. + * @param gameType Either "tictactoe" or ... + * @param future The uuid of the server. + */ public record StartServerRequest(String port, String gameType, CompletableFuture future) {} + /** + * Represents a server that has successfully started. + * + * @param uuid The unique identifier of the server. + * @param port The port the server is listening on. + */ public record ServerStarted(String uuid, String port) {} + /** + * BLOCKING + * Requests creation of a TicTacToe game on a specific server. + * + * @param serverUuid The unique identifier of the server where the game will be created. + * @param playerA The name of the first player. + * @param playerB The name of the second player. + * @param future The game UUID when the game is created. + */ public record CreateTicTacToeGameRequest(String serverUuid, String playerA, String playerB, CompletableFuture future) {} + /** + * Requests running a TicTacToe game on a specific server. + * + * @param serverUuid The unique identifier of the server. + * @param gameUuid The UUID of the game to run. + */ public record RunTicTacToeGame(String serverUuid, String gameUuid) {} + /** + * Requests ending a TicTacToe game on a specific server. + * + * @param serverUuid The UUID of the server the game is running on. + * @param gameUuid The UUID of the game to end. + */ public record EndTicTacToeGame(String serverUuid, String gameUuid) {} /** * * Triggers starting a server connection. * - * @param ip - * @param port + * @param ip The IP address of the server to connect to. + * @param port The port of the server to connect to. */ public record StartConnection(String ip, String port) {} /** - * Triggers starting a server connection, returns a future. - * WARNING: This is a blocking operation. + * BLOCKING + * Triggers starting a server connection and returns a future. * - * @param ip - * @param port - * @param future + * @param ip The IP address of the server to connect to. + * @param port The port of the server to connect to. + * @param future Returns the UUID of the connection, when connection is established. */ public record StartConnectionRequest(String ip, String port, CompletableFuture future) {} // public record StartGameConnectionRequest(String ip, String port, CompletableFuture future) {} /** - * Triggers when a connection to a server is established. + * BLOCKING + * Triggers starting a server connection and returns a future. * - * @param connectionId - * @param ip - * @param port + * @param ip The IP address of the server to connect to. + * @param port The port of the server to connect to. + * @param future The CompletableFuture that will complete when the connection is established. */ public record ConnectionEstablished(Object connectionId, String ip, String port) {} /** * Triggers sending a command to a server. + * + * @param connectionId The UUID of the connection to send the command on. + * @param args The command arguments. */ public record SendCommand(String connectionId, String... args) { } /** + * WIP * Triggers when a command is sent to a server. + * + * @param command The TicTacToeServer instance that executed the command. + * @param args The command arguments. + * @param result The result returned from executing the command. */ public record OnCommand(TicTacToeServer command, String[] args, String result) {} /** * Triggers when the server client receives a message. + * + * @param ConnectionUuid The UUID of the connection that received the message. + * @param message The message received. */ public record ReceivedMessage(String ConnectionUuid, String message) {} /** - * Triggers on changing the server ip. + * Triggers on changing the server IP. + * + * @param ip The new IP address. */ public record OnChangingServerIp(String ip) {} /** * Triggers on changing the server port. + * + * @param port The new port. */ public record OnChangingServerPort(String port) {} /** - * Triggers reconnecting to previous address. + * Triggers reconnecting to a previous address. + * + * @param connectionId The identifier of the connection being reconnected. */ public record Reconnect(Object connectionId) {} /** * Triggers changing connection to a new address. + * + * @param connectionId The identifier of the connection being changed. + * @param ip The new IP address. + * @param port The new port. */ public record ChangeConnection(Object connectionId, String ip, String port) {} /** * Triggers when the server couldn't connect to the desired address. + * + * @param connectionId The identifier of the connection that failed. */ public record CouldNotConnect(Object connectionId) {} + /** + * WIP + * Triggers when a connection closes. + * + */ public record ClosedConnection() {} } From 17a3827ef0196533e7af36b0c0ac4c006347cd16 Mon Sep 17 00:00:00 2001 From: lieght Date: Wed, 17 Sep 2025 17:20:00 +0200 Subject: [PATCH 4/4] Removed unecessary space --- .idea/workspace.xml | 3 +-- src/main/java/org/toop/eventbus/Events.java | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 722a9f4..181c215 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,7 +5,6 @@ - diff --git a/src/main/java/org/toop/eventbus/Events.java b/src/main/java/org/toop/eventbus/Events.java index 86de31a..439bff4 100644 --- a/src/main/java/org/toop/eventbus/Events.java +++ b/src/main/java/org/toop/eventbus/Events.java @@ -110,7 +110,6 @@ public class Events implements IEvents { */ public record StartServer(String port, String gameType) {} - /** * BLOCKING * Requests starting a server with a specific port and game type, and returns a CompletableFuture