Add: ClientLauncher

Fix: changed management of addPlayer in ServerLauncher
This commit is contained in:
2026-04-24 16:56:53 +02:00
parent f065ad109f
commit 2fe430900d
2 changed files with 59 additions and 9 deletions
@@ -4,6 +4,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.NetworkEvents.AddPlayer;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
@@ -26,7 +27,7 @@ public class ServerLauncher {
this.serverTCP = serverTCP;
}
public boolean doFirstEvent() throws InterruptedException, RemoteException {
public boolean doFirstEven() throws InterruptedException, RemoteException {
NetworkEvent event = actionQueue.take();
if(event.apply(gameController)) {
serverRMI.notifyAll(event);
@@ -40,24 +41,45 @@ public class ServerLauncher {
}
public static void main() throws RemoteException {
public static void main() throws RemoteException, InterruptedException {
// 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<>();
Game model = new Game();
GameController gameController = new GameController(model);
GameController gameController = new GameController();
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue);
TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue);
serverRMI.start();
new Thread (()->{serverTCP.start();}).start();
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
launcher.run();
}
public void run() {
public void run() throws InterruptedException, RemoteException {
// Aggiunge il primo player e imposta il numero di giocatori voluti
AddPlayer player1 = (AddPlayer) actionQueue.take();
Game model = new Game(player1.getProposedNPlayer());
gameController.setModel(model);
player1.apply(gameController);
for(int i=1; i<model.getNPlayers(); i++) {
AddPlayer player = (AddPlayer) actionQueue.take();
if(!player.apply(gameController)) {
serverRMI.notifyError(player.getUsername(), "fottiti");
//serverTCP
i--;
}
}
//serverRMI.sendModel(model);
//serverTCP.sendModel(model);
while (true) {
try {
this.doFirstEvent();
this.doFirstEven();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;