Add: JavaDOC explaining the network stack workflow

This commit is contained in:
2026-04-25 16:27:42 +02:00
parent de4738beba
commit 59dd27f123
@@ -13,12 +13,30 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
public class ServerLauncher { 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<NetworkEvent> actionQueue; BlockingQueue<NetworkEvent> actionQueue;
GameController gameController; GameController gameController;
RMIServer serverRMI; RMIServer serverRMI;
TCPServer serverTCP; TCPServer serverTCP;
static LimitedList<String> players; static LimitedList<String> playerList;
@@ -29,7 +47,7 @@ public class ServerLauncher {
this.serverTCP = serverTCP; this.serverTCP = serverTCP;
} }
public boolean doFirstEven() throws InterruptedException, RemoteException { public boolean doFirstEvent() throws InterruptedException, RemoteException {
NetworkEvent event = actionQueue.take(); NetworkEvent event = actionQueue.take();
if(event.apply(gameController)) { if(event.apply(gameController)) {
serverRMI.notifyAll(event); 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 // 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 // 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<NetworkEvent> actionQueue = new LinkedBlockingQueue<>(); BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
GameController gameController = new GameController(); GameController gameController = new GameController();
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, players); RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, playerList);
TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, players); TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, playerList);
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
players.setAction(()->{ playerList.setAction(()->{
try { try {
launcher.run(); launcher.run();
} catch (InterruptedException e) { } catch (InterruptedException e) {
@@ -76,9 +94,10 @@ public class ServerLauncher {
serverTCP.broadcastModel(gameController.getModel()); serverTCP.broadcastModel(gameController.getModel());
// Game execution
while (true) { while (true) {
try { try {
this.doFirstEven(); this.doFirstEvent();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
break; break;