From 59dd27f1233cfd9bc342f2b9c502036e5677f7cc Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Sat, 25 Apr 2026 16:27:42 +0200 Subject: [PATCH] Add: JavaDOC explaining the network stack workflow --- .../it/polimi/ingsw/gc14/ServerLauncher.java | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 67d4dfd..04de37c 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -13,12 +13,30 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class ServerLauncher { + /** + * Main server launcher that handles both TCP and RMI connections. + * The workflow is divided into two parts: game creation and game execution. + * + * The process flow for game creation is as follows: + * - The first client (TCP/RMI) requests to join the game by providing a username and the desired number of players + * - The TCP/RMI server checks {@link #playerList} and, if it is empty, sets the number of players according to the first user's request using {@link LimitedList#setLimit(int)} + * - The TCP/RMI server creates the {@link Game} with the requested number of players and adds the player to {@link #playerList} + * - Other players request to join the game (their requested number of players is ignored) + * - When the number of players in {@link #playerList} reaches the {@link LimitedList}'s limit, the list calls {@link #run()} + * - All players are notified with the {@link Game} + * + * The process flow for game execution is as follows: + * - The TCP/RMI server receives a {@link NetworkEvent} from a client and adds it to the {@link #actionQueue} + * - The {@link #run()} method repeatedly calls {@link #doFirstEvent()}, which takes the first event in the {@link #actionQueue} and tries to apply it + * - If the event is successfully applied to the model, all players receive the event + * - Otherwise, the player who sent the action receives an error notification + */ BlockingQueue actionQueue; GameController gameController; RMIServer serverRMI; TCPServer serverTCP; - static LimitedList players; + static LimitedList playerList; @@ -29,7 +47,7 @@ public class ServerLauncher { this.serverTCP = serverTCP; } - public boolean doFirstEven() throws InterruptedException, RemoteException { + public boolean doFirstEvent() throws InterruptedException, RemoteException { NetworkEvent event = actionQueue.take(); if(event.apply(gameController)) { serverRMI.notifyAll(event); @@ -49,14 +67,14 @@ public class ServerLauncher { // Il server RMI e il server TCP quando ricevono un doEvent devono aggiungere l'evento alla coda condivisa // Questa classe esegue gli eventi nella coda e chiama il notify all e notify error sia RMI che TCP - players = new LimitedList<>(5, ()->{}); + playerList = new LimitedList<>(5, ()->{}); BlockingQueue actionQueue = new LinkedBlockingQueue<>(); GameController gameController = new GameController(); - RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, players); - TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, players); + RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, playerList); + TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, playerList); ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); - players.setAction(()->{ + playerList.setAction(()->{ try { launcher.run(); } catch (InterruptedException e) { @@ -76,9 +94,10 @@ public class ServerLauncher { serverTCP.broadcastModel(gameController.getModel()); + // Game execution while (true) { try { - this.doFirstEven(); + this.doFirstEvent(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break;