Is null, needs fix wip

This commit is contained in:
Bas de Jong
2025-09-16 18:17:56 +02:00
parent 5e3ba40bed
commit dcbdfc9185
6 changed files with 66 additions and 25 deletions

View File

@@ -24,12 +24,20 @@ public class Main {
initSystems(); initSystems();
GlobalEventBus.post(new Events.ServerEvents.StartServer("5001")); GlobalEventBus.post(new Events.ServerEvents.StartServer("5001", "tictactoe"));
CompletableFuture<String> future = new CompletableFuture<>(); CompletableFuture<String> future = new CompletableFuture<>();
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", future)); GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", future));
String serverId = future.get(); String serverId = future.get();
CompletableFuture<String> future2 = new CompletableFuture<>();
GlobalEventBus.post(new Events.ServerEvents.CreateTicTacToeGame(serverId, "John", "Pim", future2));
String gameId = future.get();
GlobalEventBus.post(new Events.ServerEvents.RunTicTacToeGame(serverId, gameId));
GlobalEventBus.post(new Events.ServerEvents.Command(serverId, "MOVE", "0"));
GlobalEventBus.post(new Events.ServerEvents.EndTicTacToeGame(serverId, gameId));
// for (int i = 0; i < 1; i++) { // for (int i = 0; i < 1; i++) {
// Thread thread = new Thread(() -> { // Thread thread = new Thread(() -> {
//// logger.info("Server ID: {}", serverId); //// logger.info("Server ID: {}", serverId);
@@ -38,10 +46,8 @@ public class Main {
// thread.start(); // thread.start();
// } // }
GlobalEventBus.post(new Events.ServerEvents.Command(serverId, "HELP", "TEST")); // GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllConnections());
// GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllServers());
GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllConnections());
GlobalEventBus.post(new Events.ServerEvents.ForceCloseAllServers());
// //
// CompletableFuture<String> future2 = new CompletableFuture<>(); // CompletableFuture<String> future2 = new CompletableFuture<>();

View File

@@ -84,13 +84,17 @@ public class Events implements IEvents {
public record ForceCloseAllServers() {} public record ForceCloseAllServers() {}
public record StartServer(String port) {} public record StartServer(String port, String gameType) {}
public record StartServerRequest(String port, CompletableFuture<String> future) {} public record StartServerRequest(String port, String gameType, CompletableFuture<String> future) {}
public record ServerStarted(String uuid, String port) {} public record ServerStarted(String uuid, String port) {}
public record StartTicTacToeGame(String id, String port) {} public record CreateTicTacToeGame(String serverUuid, String playerA, String playerB, CompletableFuture<String> future) {}
public record RunTicTacToeGame(String serverUuid, String gameUuid) {}
public record EndTicTacToeGame(String serverUuid, String gameUuid) {}
/** /**
* *

View File

@@ -5,9 +5,11 @@ import org.toop.eventbus.GlobalEventBus;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.toop.server.backend.tictactoe.TicTacToeServer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@@ -29,13 +31,23 @@ public class ServerManager {
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServerRequest.class, this::handleStartServerRequest); GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServerRequest.class, this::handleStartServerRequest);
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServer.class, this::handleStartServer); GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartServer.class, this::handleStartServer);
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ForceCloseAllServers.class, _ -> shutdownAll()); GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ForceCloseAllServers.class, _ -> shutdownAll());
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.StartTicTacToeGame.class, this::handleStartTicTacToeGameOnAServer); GlobalEventBus.subscribeAndRegister(Events.ServerEvents.CreateTicTacToeGame.class, this::handleStartTicTacToeGameOnAServer);
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.RunTicTacToeGame.class, this::handleRunTicTacToeGameOnAServer);
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.EndTicTacToeGame.class, this::handleEndTicTacToeGameOnAServer);
} }
private String startServer(String port) { private String startServer(String port, String gameType) {
String serverId = UUID.randomUUID().toString(); String serverId = UUID.randomUUID().toString();
gameType = gameType.toLowerCase();
try { try {
TcpServer server = new TcpServer(Integer.parseInt(port)); TcpServer server = null;
if (Objects.equals(gameType, "tictactoe")) {
server = new TicTacToeServer(Integer.parseInt(port));
}
else {
logger.error("Manager could not create a TcpServer for {}", gameType);
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("Connected to server {} at {}", serverId, port);
@@ -47,28 +59,39 @@ public class ServerManager {
} }
private void handleStartServerRequest(Events.ServerEvents.StartServerRequest request) { private void handleStartServerRequest(Events.ServerEvents.StartServerRequest request) {
request.future().complete(this.startServer(request.port())); // TODO: Maybe post StartServer event. request.future().complete(this.startServer(request.port(), request.gameType())); // TODO: Maybe post StartServer event.
} }
private void handleStartServer(Events.ServerEvents.StartServer event) { private void handleStartServer(Events.ServerEvents.StartServer event) {
GlobalEventBus.post(new Events.ServerEvents.ServerStarted( GlobalEventBus.post(new Events.ServerEvents.ServerStarted(
this.startServer(event.port()), this.startServer(event.port(), event.gameType()),
event.port() event.port()
)); ));
} }
private void handleStartTicTacToeGameOnAServer(Events.ServerEvents.StartTicTacToeGame event) { private void handleStartTicTacToeGameOnAServer(Events.ServerEvents.CreateTicTacToeGame event) {
TcpServer serverThing = this.servers.get(event.id()); TicTacToeServer serverThing = (TicTacToeServer) this.servers.get(event.serverUuid());
String gameId = null;
if (serverThing != null) { if (serverThing != null) {
try { try {
serverThing.runGame(); gameId = serverThing.newGame(event.playerA(), event.playerB());
logger.info("Started game on server {}", event.id()); logger.info("Created game on server {}", event.serverUuid());
} }
catch (Exception e) { catch (Exception e) { // TODO: Error handling
logger.info("Could not start game on server {}", event.id()); logger.info("Could not create game on server {}", event.serverUuid());
} }
} }
event.future().complete(gameId);
}
private void handleRunTicTacToeGameOnAServer(Events.ServerEvents.RunTicTacToeGame event) {
TicTacToeServer gameServer = (TicTacToeServer) this.servers.get(event.serverUuid());
gameServer.runGame(event.gameUuid());
}
private void handleEndTicTacToeGameOnAServer(Events.ServerEvents.EndTicTacToeGame event) {
TicTacToeServer gameServer = (TicTacToeServer) this.servers.get(event.serverUuid());
gameServer.endGame(event.gameUuid());
} }
private void getAllServers(Events.ServerEvents.RequestsAllServers request) { private void getAllServers(Events.ServerEvents.RequestsAllServers request) {

View File

@@ -50,6 +50,10 @@ public class TcpServer implements Runnable {
public void runGame() {} public void runGame() {}
public void endGame() {}
public void newGame() {}
protected String sendServerMessage() { protected String sendServerMessage() {
try { return sendQueue.poll(this.WAIT_TIME, TimeUnit.MILLISECONDS); } try { return sendQueue.poll(this.WAIT_TIME, TimeUnit.MILLISECONDS); }
catch (InterruptedException e) { catch (InterruptedException e) {

View File

@@ -1,16 +1,18 @@
package org.toop.server.backend.tictactoe; package org.toop.server.backend.tictactoe;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.server.backend.tictactoe.game.TicTacToe; import org.toop.server.backend.tictactoe.game.TicTacToe;
import org.toop.server.backend.TcpServer; import org.toop.server.backend.TcpServer;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectStreamException;
import java.net.Socket; import java.net.Socket;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class TicTacToeServer extends TcpServer { public class TicTacToeServer extends TcpServer {
protected static final Logger logger = LogManager.getLogger(TicTacToeServer.class);
/** /**
* Map of gameId -> Game instances * Map of gameId -> Game instances
*/ */
@@ -36,11 +38,13 @@ public class TicTacToeServer extends TcpServer {
} }
} }
public void newGame(String playerA, String playerB) { public String newGame(String playerA, String playerB) {
logger.info("Creating a new game: {} vs {}", playerA, playerB);
String gameId = UUID.randomUUID().toString(); String gameId = UUID.randomUUID().toString();
TicTacToe game = new TicTacToe(playerA, playerB); TicTacToe game = new TicTacToe(playerA, playerB);
this.games.put(gameId, game); this.games.put(gameId, game);
logger.info("Created a new game: {}. {} vs {}", gameId, playerA, playerB); logger.info("Created a new game: {}. {} vs {}", gameId, playerA, playerB);
return gameId;
} }
public void runGame(String gameId) { public void runGame(String gameId) {
@@ -52,7 +56,7 @@ public class TicTacToeServer extends TcpServer {
public void endGame(String gameId) { public void endGame(String gameId) {
TicTacToe game = this.games.get(gameId); TicTacToe game = this.games.get(gameId);
this.games.remove(gameId); this.games.remove(gameId);
logger.info("Removed game: {}", gameId); logger.info("Ended game: {}", gameId);
// TODO: Multithreading, close game in a graceful matter, etc. // TODO: Multithreading, close game in a graceful matter, etc.
} }

View File

@@ -27,9 +27,9 @@ public class TicTacToe extends GameBase implements Runnable {
private void gameThread() { private void gameThread() {
while (true) { while (true) {
String command = getNewestCommand(); // String command = getNewestCommand();
command = this.parseCommand(command).toString(); // command = this.parseCommand(command).toString();
if (command == null) { continue; } // if (command == null) { continue; }
// TODO: Game // TODO: Game
} }