From 59dd27f1233cfd9bc342f2b9c502036e5677f7cc Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Sat, 25 Apr 2026 16:27:42 +0200 Subject: [PATCH 1/4] 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; From 3a8706603c6d2cf592ada1c1a39c2eb4664db520 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Sat, 25 Apr 2026 17:34:28 +0200 Subject: [PATCH 2/4] Add: JavaDOC in ServerLauncher Fix: Action error handling in the entire network stack Modified: TCP's broadcast methods name --- .../ingsw/gc14/Network/NetworkEvent.java | 17 +++- .../RMI/Client/ClientCallbackImpl.java | 14 ++- .../Network/RMI/Common/IClientCallback.java | 1 - .../gc14/Network/RMI/Server/RMIServer.java | 4 - .../gc14/Network/TCP/Client/TCPClient.java | 17 +++- .../Network/TCP/Server/ClientHandler.java | 6 +- .../gc14/Network/TCP/Server/TCPServer.java | 4 +- .../it/polimi/ingsw/gc14/ServerLauncher.java | 89 ++++++++++++------- 8 files changed, 100 insertions(+), 52 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java index abc510c..1a5d50c 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java @@ -1,7 +1,6 @@ package it.polimi.ingsw.gc14.Network; import it.polimi.ingsw.gc14.Controller.GameController; -import javafx.event.Event; import java.io.Serializable; @@ -12,9 +11,23 @@ public abstract class NetworkEvent implements Serializable { } protected EventType eventType; public EventType getEventType() {return eventType;} - protected NetworkEvent(String username, EventType eventType) { + protected boolean isError; + public boolean getIsError() {return isError;} + public void setIsError(boolean isError) {this.isError = isError;} + + protected NetworkEvent(String username, EventType eventType, boolean isError) { this.username = username; this.eventType = eventType; + this.isError = isError; + } + + @Override + public String toString() { + if(isError) { + return ("ERROR: action " + eventType.toString()); + } else { + return ("ACTION: action " + eventType.toString()); + } } public abstract boolean apply(GameController gameController); diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java index d6b54d7..871b653 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java @@ -23,13 +23,11 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa @Override public void onAction(NetworkEvent event) throws RemoteException { - event.apply(clientController.localController); // delega tutto al controller - //clientController.view.update(); TODO + if(event.getIsError()) { + System.out.println(event.toString()); + } else { + event.apply(clientController.localController); // delega tutto al controller + //clientController.view.update(); TODO + } } - - @Override - public void onError(String message) throws RemoteException { - clientController.onError(message); - } - } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java index 5ff2b0b..0292fcc 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java @@ -8,5 +8,4 @@ import java.rmi.*; public interface IClientCallback extends Remote { void onGameInit(Game model) throws RemoteException; void onAction(NetworkEvent action) throws RemoteException; - void onError(String message) throws RemoteException; } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java index fc74f3c..e575a8f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java @@ -83,10 +83,6 @@ public class RMIServer implements IGameServer { cb.onGameInit(model); } } - public void notifyError(String username, String message) throws RemoteException { - IClientCallback cb = clients.get(username); - if (cb != null) cb.onError(message); - } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java index d29de4b..9a39808 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Client/TCPClient.java @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14.Network.TCP.Client; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer; @@ -46,7 +47,21 @@ public class TCPClient implements Serializable{ private void ReceiveMessage(){ while(true){ try{ - ((NetworkEvent)(socketReceive.readObject())).apply(controller); + Object read = socketReceive.readObject(); + + if (read instanceof NetworkEvent) { //TODO non fare con instanceof + NetworkEvent event = (NetworkEvent) read; + if(event.getIsError()) { + System.out.println(event.toString()); + } else { + event.apply(controller); + //clientController.view.update(); TODO + } + } + else if (read instanceof Game) { + controller.setModel((Game) read); + } + } catch(IOException e){ e.printStackTrace(); diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java index d471ab0..a989868 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/ClientHandler.java @@ -43,8 +43,8 @@ public class ClientHandler implements Runnable { while(true){ try{ input = (NetworkEvent) (in.readObject()); - if(actionQueue.add(input)){ - server.broadcastUpdate(input); + if(!actionQueue.add(input)){ + System.out.println("An error occurred in inserting an action into queue"); } } catch(java.io.IOException e){ @@ -53,8 +53,6 @@ public class ClientHandler implements Runnable { catch (ClassNotFoundException e){ throw new RuntimeException(e); } - - } } catch (IOException e) { diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java index 7c928c3..2a79695 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java @@ -118,11 +118,11 @@ public class TCPServer { this.playerList = players; } - public void broadcastUpdate(NetworkEvent event){ + public void notifAll(NetworkEvent event){ clientHandlers.forEach((x) -> x.notifyEvent(event)); } - public void broadcastModel(Game model){ + public void notifyAll(Game model){ clientHandlers.forEach((x) -> x.notifyModel(model)); } } diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 04de37c..dabe7a0 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -12,34 +12,58 @@ import java.rmi.RemoteException; 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 - */ +/** + * 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 + */ +public class ServerLauncher { + + /** + * List containing the events that have to be applied to the game's model. + * Thread safe by design. + */ BlockingQueue actionQueue; + + /** Game controller. Used to apply events */ GameController gameController; + + /** Server RMI. Handles RMI clients */ RMIServer serverRMI; + + /** Server TCP. Handles TCP clients */ TCPServer serverTCP; + + /** + * List containing the username of joined players. + * {@link LimitedList}'s limit defines at which size the list calls its action + * Both limit and action can be set with {@link LimitedList#setLimit(int)} and {@link LimitedList#setAction(Runnable)} + * The limit is set by the first player joining the game. The action consists in calling {@link #run()} + */ static LimitedList playerList; - + /** + * Class constructor that initializes the attributes. + * @param actionQueue is the list containing the events + * @param gameController is the game controller + * @param serverRMI is the server RMI + * @param serverTCP is the server TCP + */ public ServerLauncher(BlockingQueue actionQueue, GameController gameController, RMIServer serverRMI, TCPServer serverTCP) { this.actionQueue = actionQueue; this.serverRMI = serverRMI; @@ -47,17 +71,22 @@ public class ServerLauncher { this.serverTCP = serverTCP; } + + /** + * Takes the first event in the actionQueue and attempt to apply it to the game controller. + * If the event can be applied, all clients (both TCP and RMI) are notified with the event. Otherwise, the user who sent the action will be notified with an error. + * @return the outcome of attempting to apply the event to the controller + * @throws InterruptedException if any problem in accessing actionQueue is issued + * @throws RemoteException if any RMI problem is issued + */ public boolean doFirstEvent() throws InterruptedException, RemoteException { NetworkEvent event = actionQueue.take(); - if(event.apply(gameController)) { - serverRMI.notifyAll(event); - serverTCP.broadcastUpdate(event); - return true; - } else { - serverRMI.notifyError(event.getUsername(), "Mossa non valida"); // TODO Converrebbe mettere in network event un booleano che dice se รจ stato accettato e fare una notifyAll anche per errori - //serverTCP // TODO non esiste un notify error (guarda sopra) - return false; - } + event.setIsError(!event.apply(gameController)); + + serverRMI.notifyAll(event); + serverTCP.notifAll(event); + + return !event.getIsError(); } @@ -91,7 +120,7 @@ public class ServerLauncher { public void run() throws InterruptedException, RemoteException { serverRMI.notifyAll(gameController.getModel()); - serverTCP.broadcastModel(gameController.getModel()); + serverTCP.notifyAll(gameController.getModel()); // Game execution From 27903f1738756cd6f911ff2907a09b32bd90e476 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Sat, 25 Apr 2026 17:36:24 +0200 Subject: [PATCH 3/4] Fix: compilation errors due to wrong number of parameters --- .../it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java | 2 +- .../ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java | 2 +- .../ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java | 2 +- .../ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java | 2 +- .../ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java | 2 +- .../gc14/Network/NetworkEvents/PickOptionalBuildingCard.java | 2 +- .../ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java | 2 +- .../it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java index d8ab797..8466942 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java @@ -13,7 +13,7 @@ public class AddPlayer extends NetworkEvent implements Serializable { return proposedNPlayer; } public AddPlayer(String username, int proposedNPlayer) { - super(username, EventType.ADD_PLAYER); + super(username, EventType.ADD_PLAYER, false); this.proposedNPlayer = proposedNPlayer; } @Override diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java index 2456a6a..5656ac8 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java @@ -11,7 +11,7 @@ public class DrawLowerBuildingCard extends NetworkEvent implements Serializable private int pos; public DrawLowerBuildingCard(String username, int pos){ - super(username, EventType.DRAW_LOWER_BUILD); + super(username, EventType.DRAW_LOWER_BUILD, false); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java index 64b0a17..277627a 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java @@ -11,7 +11,7 @@ public class DrawLowerTribeCard extends NetworkEvent implements Serializable{ private int pos; public DrawLowerTribeCard(String username, int pos){ - super(username, EventType.DRAW_LOWER_TRIBE); + super(username, EventType.DRAW_LOWER_TRIBE, false); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java index 5f0a985..6fb06cf 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java @@ -11,7 +11,7 @@ public class DrawUpperBuildingCard extends NetworkEvent implements Serializable private int pos; public DrawUpperBuildingCard(String username, int pos){ - super(username, EventType.DRAW_UPPER_BUILD); + super(username, EventType.DRAW_UPPER_BUILD, false); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java index 69ab281..8d37f02 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java @@ -11,7 +11,7 @@ public class DrawUpperTribeCard extends NetworkEvent implements Serializable{ private int pos; public DrawUpperTribeCard(String username, int pos){ - super(username, EventType.DRAW_UPPER_TRIBE); + super(username, EventType.DRAW_UPPER_TRIBE, false); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java index a1fde73..36be45c 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java @@ -11,7 +11,7 @@ public class PickOptionalBuildingCard extends NetworkEvent implements Serializa private int pos; public PickOptionalBuildingCard(String username, int pos){ - super(username, EventType.PICK_OPTIONAL_BUILD); + super(username, EventType.PICK_OPTIONAL_BUILD, false); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java index d739e3c..f2dadac 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java @@ -11,7 +11,7 @@ public class PickOptionalTribeCard extends NetworkEvent implements Serializable private int pos; public PickOptionalTribeCard(String username, int pos){ - super(username, EventType.PICK_OPTIONAL_TRIBE); + super(username, EventType.PICK_OPTIONAL_TRIBE, false); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java index 97000d9..4bf0d12 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java @@ -11,7 +11,7 @@ public class SlotChoice extends NetworkEvent implements Serializable { private int pos; public SlotChoice(String username, int pos) { - super(username, EventType.SLOT_CHOICE); + super(username, EventType.SLOT_CHOICE, false); this.pos = pos; } From 6ebfd4d0260780548f597d7c82a15d0e690a6388 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Sat, 25 Apr 2026 18:16:13 +0200 Subject: [PATCH 4/4] Add: complete JavaDOC to ServerLauncher --- .../gc14/Network/RMI/Server/RMIServer.java | 2 +- .../gc14/Network/TCP/Server/TCPServer.java | 2 +- .../it/polimi/ingsw/gc14/ServerLauncher.java | 60 +++++++++++-------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java index e575a8f..1770da4 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java @@ -52,7 +52,7 @@ public class RMIServer implements IGameServer { if (controller.addPlayer(username)) { if(playerList.size()==0) { - Game game= new Game(preferredInt); + Game game = new Game(preferredInt); controller.setModel(game); playerList.setLimit(preferredInt); } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java index 2a79695..fc86f84 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java @@ -118,7 +118,7 @@ public class TCPServer { this.playerList = players; } - public void notifAll(NetworkEvent event){ + public void notifyAll(NetworkEvent event){ clientHandlers.forEach((x) -> x.notifyEvent(event)); } diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index dabe7a0..df3c776 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -23,18 +23,18 @@ import java.util.concurrent.LinkedBlockingQueue; * - 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} + * - All players are notified of 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 + * - If the event cannot be successfully applied to the model, its {@code isError} flag is set to {@code true} + * - All players are notified of the event */ public class ServerLauncher { /** - * List containing the events that have to be applied to the game's model. + * Queue containing the events to be applied to the game model. * Thread safe by design. */ BlockingQueue actionQueue; @@ -49,9 +49,9 @@ public class ServerLauncher { TCPServer serverTCP; /** - * List containing the username of joined players. + * List containing the usernames of joined players. * {@link LimitedList}'s limit defines at which size the list calls its action - * Both limit and action can be set with {@link LimitedList#setLimit(int)} and {@link LimitedList#setAction(Runnable)} + * Both the limit and the action can be set using {@link LimitedList#setLimit(int)} and {@link LimitedList#setAction(Runnable)} * The limit is set by the first player joining the game. The action consists in calling {@link #run()} */ static LimitedList playerList; @@ -59,10 +59,10 @@ public class ServerLauncher { /** * Class constructor that initializes the attributes. - * @param actionQueue is the list containing the events - * @param gameController is the game controller - * @param serverRMI is the server RMI - * @param serverTCP is the server TCP + * @param actionQueue The queue containing the events + * @param gameController The game controller + * @param serverRMI The server RMI + * @param serverTCP The server TCP */ public ServerLauncher(BlockingQueue actionQueue, GameController gameController, RMIServer serverRMI, TCPServer serverTCP) { this.actionQueue = actionQueue; @@ -73,29 +73,34 @@ public class ServerLauncher { /** - * Takes the first event in the actionQueue and attempt to apply it to the game controller. - * If the event can be applied, all clients (both TCP and RMI) are notified with the event. Otherwise, the user who sent the action will be notified with an error. - * @return the outcome of attempting to apply the event to the controller - * @throws InterruptedException if any problem in accessing actionQueue is issued - * @throws RemoteException if any RMI problem is issued + * Takes the first event in the actionQueue and attempts to apply it to the game controller. + * If the event cannot be applied, its isError flag is set to true; otherwise, it is set to false. + * All clients (both TCP and RMI) are notified of the event + * @return the outcome of applying the event to the controller + * @throws InterruptedException if an error occurs while accessing the actionQueue + * @throws RemoteException if an RMI error occurs */ public boolean doFirstEvent() throws InterruptedException, RemoteException { NetworkEvent event = actionQueue.take(); event.setIsError(!event.apply(gameController)); serverRMI.notifyAll(event); - serverTCP.notifAll(event); + serverTCP.notifyAll(event); return !event.getIsError(); } - public static void main() throws RemoteException, InterruptedException { - // TODO - // Deve avere una coda con gli eventi. - // 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 - + /** + * The first method executed when the server program is launched. + * It creates all the objects needed: playerList, actionQueue, gameController, serverRMI, serverTCP, launcher. + * Then sets the playerList's action to execute launcher.run() and starts the TCP/RMI servers. + * Note: the model is initialized and set in the controller in TCP/RMI servers when the first user decides the number of players. + * + * @throws InterruptedException if this exception is issued by run method + * @throws RemoteException if this exception is issued by run method + */ + public static void main(String[] args) throws InterruptedException, RemoteException { playerList = new LimitedList<>(5, ()->{}); BlockingQueue actionQueue = new LinkedBlockingQueue<>(); GameController gameController = new GameController(); @@ -117,12 +122,19 @@ public class ServerLauncher { new Thread(()->{serverTCP.start();}).start(); } - public void run() throws InterruptedException, RemoteException { + /** + * Creates and executes the game. + * Game creation: TCP/RMI servers send the game model to all players. + * Game execution: repeatedly calls doFirstEvent() to process the events in the actionQueue. + * @throws InterruptedException if the TCP server thread is interrupted + * @throws RemoteException if an RMI error occurs + */ + public void run() throws InterruptedException, RemoteException { + // Game creation serverRMI.notifyAll(gameController.getModel()); serverTCP.notifyAll(gameController.getModel()); - // Game execution while (true) { try {