Fix: ServerLauncher

This commit is contained in:
2026-04-24 19:02:04 +02:00
parent 152a744a8b
commit e184158ae4
2 changed files with 21 additions and 13 deletions
@@ -4,7 +4,7 @@ import java.util.ArrayList;
public class LimitedList<T> extends ArrayList<T> { public class LimitedList<T> extends ArrayList<T> {
private int limit; private int limit;
private final Runnable action; private Runnable action;
public LimitedList(int limit, Runnable action) { public LimitedList(int limit, Runnable action) {
this.limit = limit; this.limit = limit;
@@ -25,4 +25,8 @@ public class LimitedList<T> extends ArrayList<T> {
} }
public int getLimit(){return limit;} public int getLimit(){return limit;}
public void setAction(Runnable action) {
this.action=action;
}
} }
@@ -13,6 +13,7 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
public class ServerLauncher { public class ServerLauncher {
BlockingQueue<NetworkEvent> actionQueue; BlockingQueue<NetworkEvent> actionQueue;
GameController gameController; GameController gameController;
RMIServer serverRMI; RMIServer serverRMI;
@@ -26,15 +27,6 @@ public class ServerLauncher {
this.serverRMI = serverRMI; this.serverRMI = serverRMI;
this.gameController = gameController; this.gameController = gameController;
this.serverTCP = serverTCP; 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 { 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 // 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 // Questa classe esegue gli eventi nella coda e chiama il notify all e notify error sia RMI che TCP
players = new LimitedList<>(5, ()->{});
BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>(); BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
GameController gameController = new GameController(); GameController gameController = new GameController();
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, players); RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, players);
TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, players); TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, players);
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
players.setAction(()->{
try {
launcher.run(); 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 { public void run() throws InterruptedException, RemoteException {
serverRMI.notifyAll(gameController.getModel()); serverRMI.notifyAll(gameController.getModel());
//serverTCP.broadcastUpdate(model); serverTCP.broadcastModel(gameController.getModel());
while (true) { while (true) {