From 2fe430900d49abb73c9f47601d56f513d1aeef21 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 24 Apr 2026 16:56:53 +0200 Subject: [PATCH 1/9] Add: ClientLauncher Fix: changed management of addPlayer in ServerLauncher --- .../it/polimi/ingsw/gc14/ClientLauncher.java | 28 +++++++++++++ .../it/polimi/ingsw/gc14/ServerLauncher.java | 40 ++++++++++++++----- 2 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 src/main/java/it/polimi/ingsw/gc14/ClientLauncher.java diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncher.java new file mode 100644 index 0000000..fd8a3f8 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncher.java @@ -0,0 +1,28 @@ +package it.polimi.ingsw.gc14; +import java.util.Scanner; + +public class ClientLauncher { + public void main() { + Scanner scanner = new Scanner(System.in); + + System.out.println("Selezionare nome utente: "); + String username = scanner.next(); + System.out.println(username); + + System.out.println("Selezionare numero di giocatori desiderato: "); + int proposedNumPlayers = scanner.nextInt(); + System.out.println(proposedNumPlayers); + + System.out.println("Selezionare RMI[0] o TCP[1]: "); + int networkType = scanner.nextInt(); + System.out.println(networkType); + + scanner.close(); // chiudi solo alla fine + + if (networkType == 0) { + return; + } else if (networkType == 1) { + return; + } + } +} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index b3c14d7..36b57bc 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -4,6 +4,7 @@ package it.polimi.ingsw.gc14; 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; import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer; import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer; @@ -26,7 +27,7 @@ public class ServerLauncher { this.serverTCP = serverTCP; } - public boolean doFirstEvent() throws InterruptedException, RemoteException { + public boolean doFirstEven() throws InterruptedException, RemoteException { NetworkEvent event = actionQueue.take(); if(event.apply(gameController)) { serverRMI.notifyAll(event); @@ -40,24 +41,45 @@ public class ServerLauncher { } - public static void main() throws RemoteException { + 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 + BlockingQueue actionQueue = new LinkedBlockingQueue<>(); - Game model = new Game(); - GameController gameController = new GameController(model); + GameController gameController = new GameController(); RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue); TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue); - serverRMI.start(); - new Thread (()->{serverTCP.start();}).start(); - ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); launcher.run(); } - public void run() { + public void run() throws InterruptedException, RemoteException { + + // Aggiunge il primo player e imposta il numero di giocatori voluti + AddPlayer player1 = (AddPlayer) actionQueue.take(); + Game model = new Game(player1.getProposedNPlayer()); + gameController.setModel(model); + player1.apply(gameController); + + for(int i=1; i Date: Fri, 24 Apr 2026 18:19:15 +0200 Subject: [PATCH 2/9] Add: LimitedList --- .../it/polimi/ingsw/gc14/LimitedList.java | 26 +++++++++++++++++++ .../it/polimi/ingsw/gc14/ServerLauncher.java | 26 ++++++++----------- 2 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 src/main/java/it/polimi/ingsw/gc14/LimitedList.java diff --git a/src/main/java/it/polimi/ingsw/gc14/LimitedList.java b/src/main/java/it/polimi/ingsw/gc14/LimitedList.java new file mode 100644 index 0000000..fb582c3 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/LimitedList.java @@ -0,0 +1,26 @@ +package it.polimi.ingsw.gc14; + +import java.util.ArrayList; + +public class LimitedList extends ArrayList { + private int limit; + private final Runnable action; + + public LimitedList(int limit, Runnable action) { + this.limit = limit; + this.action = action; + } + + @Override + public boolean add(Player element) { + boolean result = super.add(element); + if (size() >= limit) { + action.run(); + } + return result; + } + + public void setElement(int num) { + this.limit=num; + } +} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 36b57bc..2761ab8 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -17,6 +17,7 @@ public class ServerLauncher { GameController gameController; RMIServer serverRMI; TCPServer serverTCP; + LimitedList players; @@ -25,6 +26,16 @@ public class ServerLauncher { this.serverRMI = serverRMI; this.gameController = gameController; this.serverTCP = serverTCP; + this.players = new LimitedList<>(5, ()->{ + synchronized (gameController) { + try { + serverRMI.notifyAll(gameController.getModel()); + } catch (RemoteException e) { + throw new RuntimeException(e); + } + } + + }); } public boolean doFirstEven() throws InterruptedException, RemoteException { @@ -59,21 +70,6 @@ public class ServerLauncher { public void run() throws InterruptedException, RemoteException { // Aggiunge il primo player e imposta il numero di giocatori voluti - AddPlayer player1 = (AddPlayer) actionQueue.take(); - Game model = new Game(player1.getProposedNPlayer()); - gameController.setModel(model); - player1.apply(gameController); - - for(int i=1; i Date: Fri, 24 Apr 2026 18:24:11 +0200 Subject: [PATCH 3/9] Fix: Fixed Initial Socket Connection Logic. Add: "broadcastModel" Method In TCPServer.java, "notifyModel" Method In ClientHandler.java. --- .../gc14/Network/TCP/Client/TCPClient.java | 4 +-- .../Network/TCP/Server/ClientHandler.java | 25 +++++++++++++++---- .../gc14/Network/TCP/Server/TCPServer.java | 21 +++++++++++++--- 3 files changed, 40 insertions(+), 10 deletions(-) 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 113cd2e..d29de4b 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 @@ -22,13 +22,13 @@ public class TCPClient implements Serializable{ this.port = port; } - public boolean start(String user){ + public boolean start(String user, int players){ try{ communicationSocket = new Socket(hostname, port); socketSend = new ObjectOutputStream(communicationSocket.getOutputStream()); socketReceive = new ObjectInputStream(communicationSocket.getInputStream()); - socketSend.writeObject(new AddPlayer(user)); + socketSend.writeObject(new AddPlayer(user, players)); if(communicationSocket.getInputStream().read() == -1){ return false; } 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 97ea3c1..d471ab0 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 @@ -1,12 +1,14 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; 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.EventType; import java.io.*; import java.net.*; import java.util.List; +import java.util.concurrent.BlockingQueue; public class ClientHandler implements Runnable { private Socket clientSocket; @@ -14,17 +16,17 @@ public class ClientHandler implements Runnable { public ObjectInputStream in = null; public ObjectOutputStream out = null; List clientHandlers; - GameController gameController; + BlockingQueue actionQueue; private EventType eventType; public Socket getClientSocket() { return clientSocket; } - public ClientHandler(Socket clientSocket, List clientHandlers, GameController gameController) { + public ClientHandler(Socket clientSocket, List clientHandlers, BlockingQueue actionQueue) { this.clientSocket = clientSocket; this.clientHandlers = clientHandlers; - this.gameController = gameController; + this.actionQueue = actionQueue; } @Override @@ -41,7 +43,7 @@ public class ClientHandler implements Runnable { while(true){ try{ input = (NetworkEvent) (in.readObject()); - if(input.apply(gameController)){ + if(actionQueue.add(input)){ server.broadcastUpdate(input); } } @@ -64,13 +66,26 @@ public class ClientHandler implements Runnable { synchronized(out){ try{ out = new ObjectOutputStream(clientSocket.getOutputStream()); - out.writeObject(gameController.getModel()); + out.writeObject(event); } catch(IOException e){ e.printStackTrace(); } } } + + public void notifyModel(Game game){ + synchronized(out){ + try{ + out = new ObjectOutputStream(clientSocket.getOutputStream()); + out.writeObject(game); + } + catch(IOException e){ + e.printStackTrace(); + } + } + } + } 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 f7910a3..b1ab13d 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 @@ -1,7 +1,9 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; 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; import java.io.*; import java.net.*; @@ -9,6 +11,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; + public class TCPServer { int port = -1; int ConnectedPlayers = 0; @@ -42,24 +45,32 @@ public class TCPServer { try{ clientSocket = serverTCP.accept(); - if(!gameController.addPlayer(clientSocket.getInputStream().toString()) || ConnectedPlayers > gameController.getModel().getNPlayers()){ + ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream()); + NetworkEvent event = (NetworkEvent) clientSocketObj.readObject(); + + //sincronizzazione su gameController + if(!(event instanceof AddPlayer) || (ConnectedPlayers >= gameController.getModel().getCurrentPlayerNumber() && gameController.getModel() != null)){ clientSocket.getOutputStream().write((int)(-1)); clientSocket.close(); System.out.println("Invalid parameters. Connection terminated.\n"); } else{ + actionQueue.add(event); clientSocket.getOutputStream().write((int)(1)); } // gestione di ADD_PLAYER } - catch (IOException e){ + catch(IOException e){ e.printStackTrace(); } + catch(ClassNotFoundException e){ + throw new RuntimeException(e); + } System.out.println("Accepted player: " + gameController.getModel().getPlayerByUsername(clientSocket.getInetAddress().toString())); ConnectedPlayers++; - ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, gameController); + ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, actionQueue); clientHandlers.add(clientHandler); //Sending model to clients @@ -91,4 +102,8 @@ public class TCPServer { public void broadcastUpdate(NetworkEvent event){ clientHandlers.forEach((x) -> x.notifyEvent(event)); } + + public void broadcastModel(){ + clientHandlers.forEach((x) -> x.notifyModel(gameController.getModel())); + } } From b413ff3bcff5b85fea8cf7b609b91748e875b565 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 24 Apr 2026 18:28:52 +0200 Subject: [PATCH 4/9] Add: LimitedList --- .../java/it/polimi/ingsw/gc14/LimitedList.java | 6 ++++-- .../it/polimi/ingsw/gc14/ServerLauncher.java | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/LimitedList.java b/src/main/java/it/polimi/ingsw/gc14/LimitedList.java index fb582c3..0bb1d66 100644 --- a/src/main/java/it/polimi/ingsw/gc14/LimitedList.java +++ b/src/main/java/it/polimi/ingsw/gc14/LimitedList.java @@ -2,7 +2,7 @@ package it.polimi.ingsw.gc14; import java.util.ArrayList; -public class LimitedList extends ArrayList { +public class LimitedList extends ArrayList { private int limit; private final Runnable action; @@ -12,7 +12,7 @@ public class LimitedList extends ArrayList { } @Override - public boolean add(Player element) { + public boolean add(String element) { boolean result = super.add(element); if (size() >= limit) { action.run(); @@ -23,4 +23,6 @@ public class LimitedList extends ArrayList { public void setElement(int num) { this.limit=num; } + + public int getLimit(){return limit;} } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 2761ab8..6db75a5 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -27,14 +27,13 @@ public class ServerLauncher { this.gameController = gameController; this.serverTCP = serverTCP; this.players = new LimitedList<>(5, ()->{ - synchronized (gameController) { - try { - serverRMI.notifyAll(gameController.getModel()); - } catch (RemoteException e) { - throw new RuntimeException(e); - } + try { + run(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } catch (RemoteException e) { + throw new RuntimeException(e); } - }); } @@ -70,7 +69,10 @@ public class ServerLauncher { public void run() throws InterruptedException, RemoteException { // Aggiunge il primo player e imposta il numero di giocatori voluti - + Game model = new Game(players.getLimit()); + gameController.setModel(model); + serverRMI.notifyAll(model); + //serverTCP.broadcastUpdate(model); while (true) { From 783380218776c8dfea09a369b8887ea1a18e9d67 Mon Sep 17 00:00:00 2001 From: rubenpirreram Date: Fri, 24 Apr 2026 18:31:04 +0200 Subject: [PATCH 5/9] Fixed: RMIServer and RMIClient --- .../gc14/Network/RMI/Client/RMIClient.java | 4 +-- .../gc14/Network/RMI/Common/IGameServer.java | 2 +- .../gc14/Network/RMI/Server/RMIServer.java | 29 ++++++++++++++----- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java index ac0e14b..8d0721c 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java @@ -18,7 +18,7 @@ public class RMIClient { } - public boolean connect(String username,ClientController clientController) { + public boolean connect(String username,int preferredInt,ClientController clientController) { // 1. Connettiti al registry try { Registry registry = LocateRegistry.getRegistry(host, port); @@ -29,7 +29,7 @@ public class RMIClient { // 3. Crea il callback e registralo ClientCallbackImpl callback = new ClientCallbackImpl(clientController); - if (!stub.joinGame(username, callback)) + if (!stub.joinGame(username,preferredInt, callback)) { stub = null; return false; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java index 8bcaff6..7a1ef25 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java @@ -6,7 +6,7 @@ import java.rmi.*; public interface IGameServer extends Remote { - boolean joinGame(String username, IClientCallback callback) throws RemoteException; + boolean joinGame(String username,int preferredInt, IClientCallback callback) throws RemoteException; boolean doEvent(NetworkEvent event) throws RemoteException; } 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 d81eb96..9e3d0e8 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 @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Server; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.LimitedList; import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; @@ -24,31 +25,43 @@ public class RMIServer implements IGameServer { private int nPort; private final Map clients = new ConcurrentHashMap<>(); BlockingQueue actionQueue; - + private LimitedList playerList; // Costruttore - public RMIServer(GameController controller, int nPort, BlockingQueue actionQueue) throws RemoteException { + public RMIServer(GameController controller, int nPort, BlockingQueue actionQueue,LimitedList playerList) throws RemoteException { this.controller = controller; this.nPort = nPort; this.actionQueue = actionQueue; + this.playerList = playerList; } + // Metodi esposti RMI public void setController (GameController controller) { this.controller = controller; } @Override - public boolean joinGame(String username, IClientCallback callback) { - if(controller.addPlayer(username)) - { - clients.put(username, callback); - return true; + public boolean joinGame(String username, int preferredInt,IClientCallback callback) { + synchronized (controller) { + if(playerList.size()==0 && (preferredInt<2||preferredInt>5)) + return false; + if (controller.addPlayer(username)) { + if(playerList.size()==0) + { + Game game= new Game(preferredInt); + controller.setModel(game); + playerList.setElement(preferredInt); + } + playerList.add(username); + clients.put(username, callback); + return true; + } + return false; } - return false; } @Override public boolean doEvent(NetworkEvent event) throws RemoteException { From b88b57e9d9bc068c98390878d8af3f0e0d3be810 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 24 Apr 2026 18:36:50 +0200 Subject: [PATCH 6/9] Fix: boh --- src/main/java/it/polimi/ingsw/gc14/LimitedList.java | 6 +++--- .../ingsw/gc14/Network/RMI/Server/RMIServer.java | 2 +- .../java/it/polimi/ingsw/gc14/ServerLauncher.java | 11 ++++------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/LimitedList.java b/src/main/java/it/polimi/ingsw/gc14/LimitedList.java index 0bb1d66..7ef8a47 100644 --- a/src/main/java/it/polimi/ingsw/gc14/LimitedList.java +++ b/src/main/java/it/polimi/ingsw/gc14/LimitedList.java @@ -2,7 +2,7 @@ package it.polimi.ingsw.gc14; import java.util.ArrayList; -public class LimitedList extends ArrayList { +public class LimitedList extends ArrayList { private int limit; private final Runnable action; @@ -12,7 +12,7 @@ public class LimitedList extends ArrayList { } @Override - public boolean add(String element) { + public boolean add(T element) { boolean result = super.add(element); if (size() >= limit) { action.run(); @@ -20,7 +20,7 @@ public class LimitedList extends ArrayList { return result; } - public void setElement(int num) { + public void setLimit(int num) { this.limit=num; } 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 9e3d0e8..fc74f3c 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 @@ -54,7 +54,7 @@ public class RMIServer implements IGameServer { { Game game= new Game(preferredInt); controller.setModel(game); - playerList.setElement(preferredInt); + playerList.setLimit(preferredInt); } playerList.add(username); clients.put(username, callback); diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 6db75a5..5197c13 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -17,7 +17,7 @@ public class ServerLauncher { GameController gameController; RMIServer serverRMI; TCPServer serverTCP; - LimitedList players; + static LimitedList players; @@ -59,8 +59,8 @@ public class ServerLauncher { BlockingQueue actionQueue = new LinkedBlockingQueue<>(); GameController gameController = new GameController(); - RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue); - TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue); + RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, players); + TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, players); ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); launcher.run(); @@ -68,10 +68,7 @@ public class ServerLauncher { public void run() throws InterruptedException, RemoteException { - // Aggiunge il primo player e imposta il numero di giocatori voluti - Game model = new Game(players.getLimit()); - gameController.setModel(model); - serverRMI.notifyAll(model); + serverRMI.notifyAll(gameController.getModel()); //serverTCP.broadcastUpdate(model); From ce75e470c2becf3476540d08041c677de22ce105 Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Fri, 24 Apr 2026 18:50:21 +0200 Subject: [PATCH 7/9] Fix: Fixed Player Adding Logic In TCPServer.java. --- .../gc14/Network/TCP/Server/TCPServer.java | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) 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 b1ab13d..96ac01a 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 @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14.Network.TCP.Server; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.LimitedList; import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer; @@ -18,7 +19,7 @@ public class TCPServer { ServerSocket serverTCP = null; GameController gameController; BlockingQueue actionQueue; - + private LimitedList playerList; private List clientHandlers; @@ -48,15 +49,32 @@ public class TCPServer { ObjectInputStream clientSocketObj = new ObjectInputStream(clientSocket.getInputStream()); NetworkEvent event = (NetworkEvent) clientSocketObj.readObject(); - //sincronizzazione su gameController - if(!(event instanceof AddPlayer) || (ConnectedPlayers >= gameController.getModel().getCurrentPlayerNumber() && gameController.getModel() != null)){ - clientSocket.getOutputStream().write((int)(-1)); - clientSocket.close(); - System.out.println("Invalid parameters. Connection terminated.\n"); - } - else{ - actionQueue.add(event); - clientSocket.getOutputStream().write((int)(1)); + synchronized (gameController){ + if(!(event instanceof AddPlayer) || (gameController.getModel() != null && gameController.getModel().getCurrentPlayerNumber() >= gameController.getModel().getNPlayers())){ + clientSocket.getOutputStream().write((int)(-1)); + clientSocket.close(); + System.out.println("Invalid parameters. Connection terminated.\n"); + } + else{ + synchronized (gameController) { + AddPlayer addPlayer = (AddPlayer) event; + if(playerList.isEmpty() && (addPlayer.getProposedNPlayer() < 2 || addPlayer.getProposedNPlayer() > 5)){ + clientSocket.getOutputStream().write((int)(-1)); + clientSocket.close(); + System.out.println("Invalid parameters. Connection terminated.\n"); + } + else if(gameController.addPlayer(addPlayer.getUsername())){ + if(playerList.isEmpty()){ + Game game = new Game(addPlayer.getProposedNPlayer()); + gameController.setModel(game); + playerList.setLimit(addPlayer.getProposedNPlayer()); + } + playerList.add(addPlayer.getUsername()); + clientSocket.getOutputStream().write((int)(1)); + } + } + + } } // gestione di ADD_PLAYER } @@ -93,10 +111,11 @@ public class TCPServer { } } - public TCPServer(GameController gameController, int port, BlockingQueue actionQueue){ + public TCPServer(GameController gameController, int port, BlockingQueue actionQueue, LimitedList players){ this.port = port; this.gameController = gameController; this.actionQueue = actionQueue; + this.playerList = players; } public void broadcastUpdate(NetworkEvent event){ From 152a744a8bbc771df5340fe19d493eb58f70147c Mon Sep 17 00:00:00 2001 From: GabrieleRadice <265572328+GabrieleRadice@users.noreply.github.com> Date: Fri, 24 Apr 2026 18:53:19 +0200 Subject: [PATCH 8/9] Fix: Fixed "broadcastModel" Method In TCPServer.java. --- .../it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 96ac01a..7c928c3 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 @@ -122,7 +122,7 @@ public class TCPServer { clientHandlers.forEach((x) -> x.notifyEvent(event)); } - public void broadcastModel(){ - clientHandlers.forEach((x) -> x.notifyModel(gameController.getModel())); + public void broadcastModel(Game model){ + clientHandlers.forEach((x) -> x.notifyModel(model)); } } From e184158ae4a1861522b864cd183cd19c435dbd1e Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 24 Apr 2026 19:02:04 +0200 Subject: [PATCH 9/9] Fix: ServerLauncher --- .../it/polimi/ingsw/gc14/LimitedList.java | 6 +++- .../it/polimi/ingsw/gc14/ServerLauncher.java | 28 +++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/LimitedList.java b/src/main/java/it/polimi/ingsw/gc14/LimitedList.java index 7ef8a47..6aa8cf9 100644 --- a/src/main/java/it/polimi/ingsw/gc14/LimitedList.java +++ b/src/main/java/it/polimi/ingsw/gc14/LimitedList.java @@ -4,7 +4,7 @@ import java.util.ArrayList; public class LimitedList extends ArrayList { private int limit; - private final Runnable action; + private Runnable action; public LimitedList(int limit, Runnable action) { this.limit = limit; @@ -25,4 +25,8 @@ public class LimitedList extends ArrayList { } public int getLimit(){return limit;} + + public void setAction(Runnable action) { + this.action=action; + } } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 5197c13..67d4dfd 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -13,6 +13,7 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class ServerLauncher { + BlockingQueue actionQueue; GameController gameController; RMIServer serverRMI; @@ -26,15 +27,6 @@ public class ServerLauncher { this.serverRMI = serverRMI; this.gameController = gameController; this.serverTCP = serverTCP; - this.players = new LimitedList<>(5, ()->{ - try { - run(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } catch (RemoteException e) { - throw new RuntimeException(e); - } - }); } public boolean doFirstEven() throws InterruptedException, RemoteException { @@ -57,19 +49,31 @@ 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, ()->{}); BlockingQueue actionQueue = new LinkedBlockingQueue<>(); GameController gameController = new GameController(); RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, players); TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, players); - ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); - launcher.run(); + + players.setAction(()->{ + try { + launcher.run(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } catch (RemoteException e) { + throw new RuntimeException(e); + } + }); + + serverRMI.start(); + new Thread(()->{serverTCP.start();}).start(); } public void run() throws InterruptedException, RemoteException { serverRMI.notifyAll(gameController.getModel()); - //serverTCP.broadcastUpdate(model); + serverTCP.broadcastModel(gameController.getModel()); while (true) {