From 07584ec5a3450200a95a1245da9214c9dd643df8 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Sun, 26 Apr 2026 16:33:10 +0200 Subject: [PATCH] Fix: removed unused class in ClientHandler Add: complete JavaDOC to TCPServer --- .../gc14/Network/RMI/Server/RMIServer.java | 2 +- .../Network/TCP/Server/ClientHandler.java | 3 - .../gc14/Network/TCP/Server/TCPServer.java | 62 +++++++++++++++---- 3 files changed, 51 insertions(+), 16 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 3578350..05152c1 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 @@ -122,7 +122,7 @@ public class RMIServer implements IGameServer { /** - * Sends a model game to all RMI clients. + * Sends a game model to all RMI clients. * @param model The desired model */ public void notifyAll(Game model) throws RemoteException { 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 1a98f12..d6de535 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 @@ -15,9 +15,6 @@ public class ClientHandler implements Runnable { List clientHandlers; BlockingQueue actionQueue; - public Socket getClientSocket() { - return clientSocket; - } public ClientHandler(Socket clientSocket, List clientHandlers, BlockingQueue actionQueue) { this.clientSocket = clientSocket; 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 a8698c2..3c1d442 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 @@ -13,24 +13,50 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; - +/** + * Server TCP. Accepts connections and manages all client handlers. + */ public class TCPServer { + + /** TCP port */ int port; + + /** Number of currently connected clients */ int ConnectedPlayers; - ServerSocket serverTCP; + + /** Socket TCP */ + ServerSocket socketTCP; + + /** Server game's controller */ GameController controller; + + /** 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; + + /** List containing all client's handlers */ private List clientHandlers; - private int getConnectedPlayers(){ - return ConnectedPlayers; - } + /** + * Starts the TCP server. + * If the first event is not AddPlayer, the request is rejected. + * If the desired number of player is invalid, the request is rejected. + * + * If this is the first player to connect, a new game model is created and passed to the controller. Additionally, the playerList's limit is set. + * If the controller successfully adds the player, the username is added to {@link #playerList} and the handler is added to {@link #clientHandlers}. + * + * If any error occurs, the server sends -1 back to the client. Otherwise, it sends 1. + */ public void start(){ try{ - serverTCP = new ServerSocket(port); + socketTCP = new ServerSocket(port); } catch (IOException e){ System.out.println("Could not start the server TCP on port: " + port); @@ -38,12 +64,12 @@ public class TCPServer { return; } System.out.println("Server TCP started on port: " + port); + Socket clientSocket; while(true){ - Socket clientSocket = null; try{ - clientSocket = serverTCP.accept(); + clientSocket = socketTCP.accept(); ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream()); NetworkEvent event = (NetworkEvent) clientSocketObj.readObject(); @@ -93,20 +119,32 @@ public class TCPServer { } } - public TCPServer(GameController gameController, int port, BlockingQueue actionQueue, LimitedList players){ + + /** + * Class constructor that initializes the attributes. + * @param controller The game controller + * @param port The TCP port + * @param actionQueue The action queue + * @param playerList The player's usernames list + */ + public TCPServer(GameController controller, int port, BlockingQueue actionQueue, LimitedList playerList){ this.port = port; this.ConnectedPlayers = 0; - this.serverTCP = null; - this.controller = gameController; + this.socketTCP = null; + this.controller = controller; this.actionQueue = actionQueue; - this.playerList = players; + this.playerList = playerList; this.clientHandlers = new ArrayList<>(); } + + /** Sends an action to all TCP clients */ public void notifyAll(NetworkEvent event){ clientHandlers.forEach((x) -> x.notifyEvent(event)); } + + /** Sends a game model to all TCP clients */ public void notifyAll(Game model){ clientHandlers.forEach((x) -> x.notifyModel(model)); }