Created event for ReceivedMessage from connection

This commit is contained in:
lieght
2025-09-17 16:52:47 +02:00
parent 867eddb0ff
commit 4378852394
5 changed files with 18 additions and 4 deletions

7
.idea/workspace.xml generated
View File

@@ -6,6 +6,11 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="997b32da-b4d4-48ac-ab51-52d65f364f81" name="Changes" comment=""> <list default="true" id="997b32da-b4d4-48ac-ab51-52d65f364f81" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/game/tictactoe/TicTacToe.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/game/tictactoe/TicTacToe.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/server/backend/tictactoe/TicTacToeServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/server/backend/tictactoe/TicTacToeServer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/server/frontend/ServerConnection.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/server/frontend/ServerConnection.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/log4j2.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/log4j2.xml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -82,7 +87,7 @@
<option name="number" value="Default" /> <option name="number" value="Default" />
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1758117514311</updated> <updated>1758117514311</updated>
<workItem from="1758117515668" duration="1821000" /> <workItem from="1758117515668" duration="2660000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -13,6 +13,7 @@ import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@@ -35,6 +36,13 @@ public class Main {
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, "J", "P", ticTacToeGame)); GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest(serverId, "J", "P", ticTacToeGame));
String ticTacToeGameId = ticTacToeGame.get(); String ticTacToeGameId = ticTacToeGame.get();
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId)); 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( GlobalEventBus.post(new Events.ServerEvents.SendCommand(
connectionId, connectionId,

View File

@@ -34,6 +34,7 @@ public class TicTacToe extends GameBase implements Runnable {
*/ */
public TicTacToe(String player1, String player2, String gameId) { public TicTacToe(String player1, String player2, String gameId) {
super(3, new Player(player1, 'X'), new Player(player2, 'O')); super(3, new Player(player1, 'X'), new Player(player2, 'O'));
this.gameId = gameId;
movesLeft = size * size; movesLeft = size * size;
} }
@@ -59,7 +60,7 @@ public class TicTacToe extends GameBase implements Runnable {
// if (command == null) { continue; } // if (command == null) { continue; }
try { try {
ParsedCommand cmd = this.commandQueue.take(); 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"); this.addSendToQueue("OK");
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.error("Game {} has crashed.", this.gameId); logger.error("Game {} has crashed.", this.gameId);

View File

@@ -114,7 +114,7 @@ public class TicTacToeServer extends TcpServer {
try { try {
while (isRunning()) { while (isRunning()) {
String msg = game.sendQueue.take(); // blocks until a message is added to the queue 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 this.sendQueue.put(msg); // push to network layer
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@@ -108,7 +108,7 @@ public final class ServerConnection implements Runnable {
while (running) { while (running) {
String received = tcpClient.readLine(); // blocks String received = tcpClient.readLine(); // blocks
if (received != null) { if (received != null) {
logger.info("Received: '{}'", received); logger.info("Connection: {} received: '{}'", this.uuid, received);
GlobalEventBus.post(new Events.ServerEvents.ReceivedMessage(this.uuid, received)); GlobalEventBus.post(new Events.ServerEvents.ReceivedMessage(this.uuid, received));
} else { } else {
break; break;