This commit is contained in:
ramollia
2025-09-17 17:28:30 +02:00
6 changed files with 129 additions and 19 deletions

24
.idea/workspace.xml generated
View File

@@ -5,7 +5,7 @@
</component> </component>
<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$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/org/toop/eventbus/Events.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/org/toop/eventbus/Events.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" />
@@ -38,15 +38,35 @@
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent"><![CDATA[{
"keyToString": { "keyToString": {
"Application.Main.executor": "Run",
"ModuleVcsDetector.initialDetectionPerformed": "true", "ModuleVcsDetector.initialDetectionPerformed": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager": "true", "RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager": "true",
"RunOnceActivity.git.unshallow": "true", "RunOnceActivity.git.unshallow": "true",
"git-widget-placeholder": "ServerManager", "git-widget-placeholder": "ServerManager",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm", "nodejs_package_manager_path": "npm",
"vue.rearranger.settings.migration": "true" "vue.rearranger.settings.migration": "true"
} }
}]]></component> }]]></component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="org.toop.Main" />
<module name="pism" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="org.toop.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
<component name="SharedIndexes"> <component name="SharedIndexes">
<attachedChunks> <attachedChunks>
<set> <set>
@@ -62,7 +82,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="225000" /> <workItem from="1758117515668" duration="4457000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -76,96 +76,185 @@ public class Events implements IEvents {
public static class ServerEvents { 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<String> future) {} public record RequestsAllConnections(CompletableFuture<String> 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<String> future) {} public record RequestsAllServers(CompletableFuture<String> future) {}
/**
* Forces closing all active connections immediately.
*/
public record ForceCloseAllConnections() {} public record ForceCloseAllConnections() {}
/**
* Forces closing all active servers immediately.
*/
public record ForceCloseAllServers() {} 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) {} 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<String> future) {} public record StartServerRequest(String port, String gameType, CompletableFuture<String> 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) {} 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<String> future) {} public record CreateTicTacToeGameRequest(String serverUuid, String playerA, String playerB, CompletableFuture<String> 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) {} 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) {} public record EndTicTacToeGame(String serverUuid, String gameUuid) {}
/** /**
* *
* Triggers starting a server connection. * Triggers starting a server connection.
* *
* @param ip * @param ip The IP address of the server to connect to.
* @param port * @param port The port of the server to connect to.
*/ */
public record StartConnection(String ip, String port) {} public record StartConnection(String ip, String port) {}
/** /**
* Triggers starting a server connection, returns a future. * BLOCKING
* WARNING: This is a blocking operation. * Triggers starting a server connection and returns a future.
* *
* @param ip * @param ip The IP address of the server to connect to.
* @param port * @param port The port of the server to connect to.
* @param future * @param future Returns the UUID of the connection, when connection is established.
*/ */
public record StartConnectionRequest(String ip, String port, CompletableFuture<String> future) {} public record StartConnectionRequest(String ip, String port, CompletableFuture<String> future) {}
// public record StartGameConnectionRequest(String ip, String port, CompletableFuture<String> future) {} // public record StartGameConnectionRequest(String ip, String port, CompletableFuture<String> future) {}
/** /**
* Triggers when a connection to a server is established. * BLOCKING
* Triggers starting a server connection and returns a future.
* *
* @param connectionId * @param ip The IP address of the server to connect to.
* @param ip * @param port The port of the server to connect to.
* @param port * @param future The CompletableFuture that will complete when the connection is established.
*/ */
public record ConnectionEstablished(Object connectionId, String ip, String port) {} public record ConnectionEstablished(Object connectionId, String ip, String port) {}
/** /**
* Triggers sending a command to a server. * 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) { } public record SendCommand(String connectionId, String... args) { }
/** /**
* WIP
* Triggers when a command is sent to a server. * 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) {} public record OnCommand(TicTacToeServer command, String[] args, String result) {}
/** /**
* Triggers when the server client receives a message. * 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) {} 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) {} public record OnChangingServerIp(String ip) {}
/** /**
* Triggers on changing the server port. * Triggers on changing the server port.
*
* @param port The new port.
*/ */
public record OnChangingServerPort(String 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) {} public record Reconnect(Object connectionId) {}
/** /**
* Triggers changing connection to a new address. * 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) {} public record ChangeConnection(Object connectionId, String ip, String port) {}
/** /**
* Triggers when the server couldn't connect to the desired address. * 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) {} public record CouldNotConnect(Object connectionId) {}
/**
* WIP
* Triggers when a connection closes.
*
*/
public record ClosedConnection() {} public record ClosedConnection() {}
} }

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;

View File

@@ -2,7 +2,7 @@
<Configuration status="debug" name="AppConfig"> <Configuration status="debug" name="AppConfig">
<Appenders> <Appenders>
<Console name="Console" target="SYSTEM_OUT"> <Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%17.17t] %-5level %logger{36} - %msg%n"/>
</Console> </Console>
</Appenders> </Appenders>
<Loggers> <Loggers>