From 269ce0d13bf1419267a46d908df5c892e2461e1e Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Sat, 25 Apr 2026 18:55:28 +0200 Subject: [PATCH] Add: partial JavaDOC to RMIServer --- .../gc14/Network/RMI/Server/RMIServer.java | 75 ++++++++++++------- 1 file changed, 50 insertions(+), 25 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 1770da4..d960cc1 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 @@ -15,19 +15,47 @@ import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; - import java.rmi.*; + +/** + * Server RMI. Exposes a method to join the game and one to execute an event. + */ public class RMIServer implements IGameServer { + /** Server game's controller */ private GameController controller; + + /** Server game's model */ + private Game model; + + /** RMI registry */ private Registry registry; + + /** RMI port */ private int nPort; + + /** Map containing the associations between a player's username and its callback */ private final Map clients = new ConcurrentHashMap<>(); + + /** Queue containing the events to be applied to the game model */ BlockingQueue actionQueue; + + /** + * List containing the usernames of joined players. + * {@link LimitedList}'s limit defines at which size the list calls its action. The limit can be set using {@link LimitedList#setLimit(int)}. + */ private LimitedList playerList; - // Costruttore + + /** + * Class constructor that initializes the attributes. + * @param controller The game controller + * @param nPort The RMI port + * @param actionQueue The action queue + * @param playerList The player's usernames list + * @throws RemoteException if an RMI error occurs + */ public RMIServer(GameController controller, int nPort, BlockingQueue actionQueue,LimitedList playerList) throws RemoteException { this.controller = controller; this.nPort = nPort; @@ -36,26 +64,29 @@ public class RMIServer implements IGameServer { } - - - - - // Metodi esposti RMI - public void setController (GameController controller) { - this.controller = controller; - } + // RMI's exposed methods + /** + * Allows a player to join the game. + * If the desired number of player is invalid, the request is rejected. + * If this is the first player, a new game model is created and passed to the controller. Additionally, the playerList's limit is set. + * Then, if the controller successfully adds the player, the username is added to {@link #playerList} and {@link #clients}. + * @param username The player's name + * @param preferredInt The desired number of players + * @param callback The client's callback interface + * @return true if the player successfully joined the game, false otherwise + */ @Override - public boolean joinGame(String username, int preferredInt,IClientCallback callback) { + public boolean joinGame(String username, int preferredInt, IClientCallback callback) { + if (preferredInt<2 || preferredInt>5) { + return false; + } synchronized (controller) { - if(playerList.size()==0 && (preferredInt<2||preferredInt>5)) - return false; + if(playerList.isEmpty()){ + model = new Game(preferredInt); + controller.setModel(model); + playerList.setLimit(preferredInt); + } if (controller.addPlayer(username)) { - if(playerList.size()==0) - { - Game game = new Game(preferredInt); - controller.setModel(game); - playerList.setLimit(preferredInt); - } playerList.add(username); clients.put(username, callback); return true; @@ -69,9 +100,6 @@ public class RMIServer implements IGameServer { } - - - // Metodi interni del server public void notifyAll(NetworkEvent action) throws RemoteException { for (IClientCallback cb : clients.values()) { @@ -85,9 +113,6 @@ public class RMIServer implements IGameServer { } - - - // Metodi per avviare server RMI public boolean start() { try {