Microfix: SevrerLauncher

This commit is contained in:
2026-04-24 15:40:43 +02:00
parent d6a87210b5
commit 05fbfd070b
@@ -26,35 +26,30 @@ public class ServerLauncher {
this.serverTCP = serverTCP; this.serverTCP = serverTCP;
} }
public boolean doFirstEven() throws InterruptedException, RemoteException { public boolean doFirstEvent() throws InterruptedException, RemoteException {
if(!actionQueue.isEmpty()) { NetworkEvent event = actionQueue.take();
NetworkEvent event = actionQueue.take(); if(event.apply(gameController)) {
if(event.apply(gameController)) { serverRMI.notifyAll(event);
serverRMI.notifyAll(event); serverTCP.broadcastUpdate(event);
serverTCP.broadcastUpdate(event); return true;
return true; } else {
} else { 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
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)
//serverTCP // TODO non esiste un notify error (guarda sopra) return false;
return false;
}
} }
return false;
} }
public static void main() 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<NetworkEvent> actionQueue = new LinkedBlockingQueue<>(); BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
Game model = new Game(); Game model = new Game();
GameController gameController = new GameController(model); GameController gameController = new GameController(model);
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue); RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue);
TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue); TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue);
serverRMI.start();
new Thread (()->{serverTCP.start();}).start();
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
launcher.run(); launcher.run();
} }
@@ -62,7 +57,7 @@ public class ServerLauncher {
public void run() { public void run() {
while (true) { while (true) {
try { try {
this.doFirstEven(); this.doFirstEvent();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
break; break;