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

View File

@@ -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,

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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;