From e184158ae4a1861522b864cd183cd19c435dbd1e Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Fri, 24 Apr 2026 19:02:04 +0200 Subject: [PATCH] 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) {