Add: first implementation of ServerLauncher

This commit is contained in:
2026-04-24 11:30:43 +02:00
parent ce6f656b17
commit e2b51fd914
3 changed files with 69 additions and 5 deletions
@@ -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<NetworkEvent> 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);
}
}
}
}