ServerManager logging

This commit is contained in:
Bas de Jong
2025-09-18 11:45:39 +02:00
parent 0cc57f558d
commit 0f6f8baee7
8 changed files with 95 additions and 23 deletions

10
.idea/workspace.xml generated
View File

@@ -6,9 +6,13 @@
<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/Logging.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/Logging.java" 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/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/eventbus/GlobalEventBus.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/eventbus/GlobalEventBus.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/org/toop/UI/GameSelectorWindow.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/UI/GameSelectorWindow.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/UI/UIGameBoard.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/UI/UIGameBoard.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/server/backend/ServerManager.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/server/backend/ServerManager.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/org/toop/server/backend/tictactoe/ParsedCommand.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/server/backend/tictactoe/ParsedCommand.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/backend/tictactoe/TicTacToeServerCommand.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/server/backend/tictactoe/TicTacToeServerCommand.java" 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" />
@@ -70,6 +74,8 @@
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager": "true", "RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager": "true",
"RunOnceActivity.git.unshallow": "true", "RunOnceActivity.git.unshallow": "true",
"UI_DESIGNER_EDITOR_MODE.PaletteManager.STATE": "right",
"UI_DESIGNER_EDITOR_MODE.UIDesignerToolWindowManager.STATE": "left",
"UI_DESIGNER_EDITOR_MODE.UIDesignerToolWindowManager.WIDTH": "421", "UI_DESIGNER_EDITOR_MODE.UIDesignerToolWindowManager.WIDTH": "421",
"git-widget-placeholder": "#25 on Ticho", "git-widget-placeholder": "#25 on Ticho",
"node.js.detected.package.eslint": "true", "node.js.detected.package.eslint": "true",

View File

@@ -1,5 +1,6 @@
package org.toop; package org.toop;
import org.apache.logging.log4j.Level;
import org.toop.UI.GameSelectorWindow; import org.toop.UI.GameSelectorWindow;
import org.toop.eventbus.EventRegistry; import org.toop.eventbus.EventRegistry;
import org.toop.eventbus.Events; import org.toop.eventbus.Events;
@@ -17,8 +18,8 @@ public class Main {
private static boolean running = false; private static boolean running = false;
public static void main(String[] args) throws ExecutionException, InterruptedException { public static void main(String[] args) throws ExecutionException, InterruptedException {
// Logging.disableAllLogs(); Logging.disableAllLogs();
// Logging.enableAllLogsForClass(EventRegistry.class); Logging.enableLogsForClass(ServerManager.class, Level.ALL);
initSystems(); initSystems();
registerEvents(); registerEvents();

View File

@@ -1,15 +1,20 @@
package org.toop.UI; package org.toop.UI;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.eventbus.Events; import org.toop.eventbus.Events;
import org.toop.eventbus.GlobalEventBus; import org.toop.eventbus.GlobalEventBus;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
public class GameSelectorWindow extends JFrame { public class GameSelectorWindow extends JFrame {
private static final Logger logger = LogManager.getLogger(GameSelectorWindow.class);
private JPanel mainMenu; private JPanel mainMenu;
private JTextField nameTextField; private JTextField nameTextField;
private JTextField name2TextField; private JTextField name2TextField;
@@ -70,22 +75,36 @@ public class GameSelectorWindow extends JFrame {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} // TODO: Better error handling to not crash the system. } // TODO: Better error handling to not crash the system.
CompletableFuture<String> ticTacToeGame = new CompletableFuture<>(); GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ReceivedMessage.class,
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest( // TODO: Make this happen through commands send through the connection, instead of an event. event -> {
serverId, if (event.message().equalsIgnoreCase("ok")) {
nameTextField.getText(), logger.info("received ok from server.");
name2TextField.getText(), } else if (event.message().toLowerCase().startsWith("gameid")) {
ticTacToeGame String gameId = event.message().toLowerCase().replace("gameid ", "");
)); GlobalEventBus.post(new Events.ServerEvents.SendCommand("start_game " + gameId));
String ticTacToeGameId; }
try { else {
ticTacToeGameId = ticTacToeGame.get(); logger.info("{}", event.message());
} catch (InterruptedException | ExecutionException ex) { }
throw new RuntimeException(ex); }
} // TODO: Better error handling to not crash the system. );
GlobalEventBus.post(new Events.ServerEvents.SendCommand(connectionId, "create_game", nameTextField.getText(), name2TextField.getText()));
// CompletableFuture<String> ticTacToeGame = new CompletableFuture<>();
// GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGameRequest( // TODO: Make this happen through commands send through the connection, instead of an event.
// serverId,
// nameTextField.getText(),
// name2TextField.getText(),
// ticTacToeGame
// ));
// String ticTacToeGameId;
// try {
// ticTacToeGameId = ticTacToeGame.get();
// } catch (InterruptedException | ExecutionException ex) {
// throw new RuntimeException(ex);
// } // TODO: Better error handling to not crash the system.
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, ticTacToeGameId)); // TODO: attempt connecting to the server with given ip, port and name.
frame.remove(mainMenu); frame.remove(mainMenu);
UIGameBoard ttt = new UIGameBoard(gameSelectorBox.getSelectedItem().toString(),this); UIGameBoard ttt = new UIGameBoard(gameSelectorBox.getSelectedItem().toString(),this);

View File

@@ -11,9 +11,10 @@ import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
public class UIGameBoard extends JFrame { public class UIGameBoard extends JFrame {
private static final Logger logger = LogManager.getLogger(UIGameBoard.class);
private final int TICTACTOE_SIZE = 3; private final int TICTACTOE_SIZE = 3;
private final int REVERSI_SIZE = 8; private final int REVERSI_SIZE = 8;
private static final Logger logger = LogManager.getLogger(Main.class);
private JLabel name; private JLabel name;
private JLabel ip; private JLabel ip;
private JLabel gameName; private JLabel gameName;

View File

@@ -45,12 +45,12 @@ public class ServerManager {
server = new TicTacToeServer(Integer.parseInt(port)); server = new TicTacToeServer(Integer.parseInt(port));
} }
else { else {
logger.error("Manager could not create a TcpServer for {}", gameType); logger.error("Manager could not create a server for game type: {}", gameType);
return null; return null;
} }
this.servers.put(serverId, server); this.servers.put(serverId, server);
new Thread(server, "Server-" + serverId).start(); new Thread(server, "Server-" + serverId).start();
logger.info("Connected to server {} at {}", serverId, port); logger.info("Created server with id: {}, port: {}", serverId, port);
return serverId; return serverId;
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to start server", e); logger.error("Failed to start server", e);

View File

@@ -76,6 +76,33 @@ public class ParsedCommand {
TicTacToeServerCommand commandEnum = TicTacToeServerCommand.getCommand(segments[0]); TicTacToeServerCommand commandEnum = TicTacToeServerCommand.getCommand(segments[0]);
switch (commandEnum) { switch (commandEnum) {
case CREATE_GAME -> {
if (segments.length == 3 && !segments[1].isEmpty() && !segments[2].isEmpty()) {
this.command = commandEnum;
this.arguments = new ArrayList<>(2);
this.arguments.add(segments[1]);
this.arguments.add(segments[2]);
this.returnMessage = TicTacToeServerMessage.OK;
this.isValidCommand = true;
this.isServerCommand = true;
this.errorMessage = null;
this.originalCommand = receivedCommand;
return;
}
}
case END_GAME, START_GAME -> {
if (segments.length == 2 && !segments[1].isEmpty()) {
this.command = commandEnum;
this.arguments = new ArrayList<>(1);
this.arguments.add(segments[1]);
this.returnMessage = TicTacToeServerMessage.OK;
this.isValidCommand = true;
this.isServerCommand = true;
this.errorMessage = null;
this.originalCommand = receivedCommand;
return;
}
}
case MOVE -> { case MOVE -> {
if (segments.length == 2 && !segments[1].isEmpty()) { if (segments.length == 2 && !segments[1].isEmpty()) {
this.command = commandEnum; this.command = commandEnum;

View File

@@ -104,9 +104,24 @@ public class TicTacToeServer extends TcpServer {
} }
private void handleServerCommand(ParsedCommand command) { private void handleServerCommand(ParsedCommand command) {
if (command.isValidCommand) {
this.sendQueue.offer("ok");
}
// TODO if (command.command == TicTacToeServerCommand.CREATE_GAME) {
String gameId = this.newGame((String) command.arguments.getFirst(), (String) command.arguments.get(1));
this.sendQueue.offer("game created successfully|gameid " + gameId);
} else if (command.command == TicTacToeServerCommand.START_GAME) {
this.runGame((String) command.arguments.getFirst());
this.sendQueue.offer("svr game is running successfully");
} else if (command.command == TicTacToeServerCommand.END_GAME) {
this.endGame((String) command.arguments.getFirst());
this.sendQueue.offer("svr game ended successfully");
} else if (command.command == TicTacToeServerCommand.LOGIN) {
this.sendQueue.offer("svr login successful");
} else if (command.command == TicTacToeServerCommand.SUBSCRIBE) {
this.sendQueue.offer("svr added to ");
}
} }
public void forwardGameMessages(TicTacToe game) { public void forwardGameMessages(TicTacToe game) {

View File

@@ -3,6 +3,9 @@ package org.toop.server.backend.tictactoe;
import java.util.EnumSet; import java.util.EnumSet;
public enum TicTacToeServerCommand { public enum TicTacToeServerCommand {
CREATE_GAME,
START_GAME,
END_GAME,
/** /**
* Login, "username" * Login, "username"
*/ */