From e2b51fd914af48c5f22d3665298a684095a71382 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 24 Apr 2026 11:30:43 +0200 Subject: [PATCH 1/3] Add: first implementation of ServerLauncher --- .../gc14/Network/RMI/Server/RMIServer.java | 6 +- .../gc14/Network/TCP/Server/TCPServer.java | 2 +- .../it/polimi/ingsw/gc14/ServerLauncher.java | 66 ++++++++++++++++++- 3 files changed, 69 insertions(+), 5 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 9a82dec..0c4fd8d 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 @@ -63,17 +63,17 @@ public class RMIServer implements IGameServer { // Metodi interni del server - private void notifyAll(NetworkEvent action) throws RemoteException { + public void notifyAll(NetworkEvent action) throws RemoteException { for (IClientCallback cb : clients.values()) { cb.onAction(action); } } - private void notifyAll(Game model) throws RemoteException { + public void notifyAll(Game model) throws RemoteException { for (IClientCallback cb : clients.values()) { cb.onGameInit(model); } } - private void notifyError(String username, String message) throws RemoteException { + 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/Server/TCPServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/TCP/Server/TCPServer.java index da62d1d..970d032 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 @@ -80,7 +80,7 @@ public class TCPServer { } } - private TCPServer(GameController gameController, int port){ + public TCPServer(GameController gameController, int port){ this.port = port; this.gameController = gameController; } diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index a1bd487..14d4c4d 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -1,11 +1,75 @@ package it.polimi.ingsw.gc14; +import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer; +import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer; + +import java.rmi.RemoteException; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + public class ServerLauncher { - public static void main(String[] args){ + BlockingQueue actionQueue; + GameController gameController; + RMIServer serverRMI; + TCPServer serverTCP; + + + + public ServerLauncher(GameController gameController, RMIServer serverRMI, TCPServer serverTCP) { + this.actionQueue = new LinkedBlockingQueue<>(); + this.serverRMI = serverRMI; + this.gameController = gameController; + this.serverTCP = serverTCP; + } + + public boolean doFirstEven() throws InterruptedException, RemoteException { + if(!actionQueue.isEmpty()) { + NetworkEvent event = actionQueue.take(); + if(event.apply(gameController)) { + serverRMI.notifyAll(event); + serverTCP.broadcastUpdate(event); + return true; + } else { + //serverRMI.notifyError(); // TODO difficile trovare username. 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 + return false; + } + } + return false; + } + + public boolean addEvent(NetworkEvent event) { + return actionQueue.offer(event); + } + + + public static void main(String[] args) throws RemoteException { // 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 + + GameController gameController = new GameController(); + RMIServer serverRMI = new RMIServer(gameController, 1099); + TCPServer serverTCP = new TCPServer(gameController, 8080); + + ServerLauncher launcher = new ServerLauncher(gameController, serverRMI, serverTCP); + launcher.run(); + } + + public void run() { + while (true) { + try { + this.doFirstEven(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } catch (RemoteException e) { + throw new RuntimeException(e); + } + } } } From 39457df5f0fceb13e3a4d5795504e27d9782756a Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 24 Apr 2026 11:55:06 +0200 Subject: [PATCH 2/3] Add: shared actionQueue between ServerLauncher, ServerRMI and ServerTCP --- .../gc14/Network/RMI/Server/RMIServer.java | 13 +++++------- .../gc14/Network/TCP/Server/TCPServer.java | 5 ++++- .../it/polimi/ingsw/gc14/ServerLauncher.java | 21 ++++++++----------- 3 files changed, 18 insertions(+), 21 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 0c4fd8d..d81eb96 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 @@ -11,6 +11,7 @@ import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; import java.util.Map; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; @@ -22,12 +23,14 @@ public class RMIServer implements IGameServer { private Registry registry; private int nPort; private final Map clients = new ConcurrentHashMap<>(); + BlockingQueue actionQueue; // Costruttore - public RMIServer(GameController controller, int nPort) throws RemoteException { + public RMIServer(GameController controller, int nPort, BlockingQueue actionQueue) throws RemoteException { this.controller = controller; this.nPort = nPort; + this.actionQueue = actionQueue; } @@ -49,13 +52,7 @@ public class RMIServer implements IGameServer { } @Override public boolean doEvent(NetworkEvent event) throws RemoteException { - boolean result = event.apply(controller); - if (!result) { - notifyError(event.getUsername(),"Mossa non valida"); - } else { - notifyAll(event); - } - return result; + return actionQueue.offer(event); } 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 970d032..8421d0e 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 @@ -7,12 +7,14 @@ import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.BlockingQueue; public class TCPServer { int port = -1; int ConnectedPlayers = 0; ServerSocket serverTCP = null; GameController gameController; + BlockingQueue actionQueue; private List clientHandlers; @@ -80,9 +82,10 @@ public class TCPServer { } } - public TCPServer(GameController gameController, int port){ + public TCPServer(GameController gameController, int port, BlockingQueue actionQueue){ this.port = port; this.gameController = gameController; + this.actionQueue = actionQueue; } public void broadcastUpdate(NetworkEvent event){ diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 14d4c4d..8658dde 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -18,8 +18,8 @@ public class ServerLauncher { - public ServerLauncher(GameController gameController, RMIServer serverRMI, TCPServer serverTCP) { - this.actionQueue = new LinkedBlockingQueue<>(); + public ServerLauncher(BlockingQueue actionQueue, GameController gameController, RMIServer serverRMI, TCPServer serverTCP) { + this.actionQueue = actionQueue; this.serverRMI = serverRMI; this.gameController = gameController; this.serverTCP = serverTCP; @@ -33,30 +33,27 @@ public class ServerLauncher { serverTCP.broadcastUpdate(event); return true; } else { - //serverRMI.notifyError(); // TODO difficile trovare username. 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 + 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; } } return false; } - public boolean addEvent(NetworkEvent event) { - return actionQueue.offer(event); - } - - public static void main(String[] args) throws RemoteException { + public static void main() throws RemoteException { // 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<>(); GameController gameController = new GameController(); - RMIServer serverRMI = new RMIServer(gameController, 1099); - TCPServer serverTCP = new TCPServer(gameController, 8080); + RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue); + TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue); - ServerLauncher launcher = new ServerLauncher(gameController, serverRMI, serverTCP); + ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); launcher.run(); } From ad7a096778499e0ac5402dc852d345aeabea350d Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 24 Apr 2026 12:00:26 +0200 Subject: [PATCH 3/3] Add: Game model created and used in Game controller --- src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index 8658dde..4ab23d5 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -2,6 +2,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.RMI.Server.RMIServer; import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer; @@ -49,7 +50,8 @@ public class ServerLauncher { // Questa classe esegue gli eventi nella coda e chiama il notify all e notify error sia RMI che TCP BlockingQueue actionQueue = new LinkedBlockingQueue<>(); - GameController gameController = new GameController(); + Game model = new Game(); + GameController gameController = new GameController(model); RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue); TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue);