Merge pull request #53 from rubenpirreram/Launcher

Launcher
This commit is contained in:
rubenpirreram
2026-04-29 17:49:06 +02:00
committed by GitHub
10 changed files with 90 additions and 47 deletions
@@ -1,28 +0,0 @@
package it.polimi.ingsw.gc14;
import java.util.Scanner;
public class ClientLauncher {
public void main() {
Scanner scanner = new Scanner(System.in);
System.out.println("Selezionare nome utente: ");
String username = scanner.next();
System.out.println(username);
System.out.println("Selezionare numero di giocatori desiderato: ");
int proposedNumPlayers = scanner.nextInt();
System.out.println(proposedNumPlayers);
System.out.println("Selezionare RMI[0] o TCP[1]: ");
int networkType = scanner.nextInt();
System.out.println(networkType);
scanner.close(); // chiudi solo alla fine
if (networkType == 0) {
return;
} else if (networkType == 1) {
return;
}
}
}
@@ -0,0 +1,53 @@
package it.polimi.ingsw.gc14;
import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
import it.polimi.ingsw.gc14.View.IView;
import java.util.Scanner;
public class ClientLauncherTUI {
public void main() throws InterruptedException {
ClientController controller = new ClientController();
Scanner scanner = new Scanner(System.in);
System.out.println("Selezionare nome utente: ");
String username = scanner.next();
System.out.println(username);
System.out.println("Selezionare numero di giocatori desiderato: ");
int proposedNumPlayers = scanner.nextInt();
System.out.println(proposedNumPlayers);
System.out.println("Selezionare RMI[0] o TCP[1]: ");
int networkType = scanner.nextInt();
System.out.println(networkType);
scanner.close();
if (networkType == 0) {
RMIClient client = new RMIClient("localhost", 1099);
if (client.connect(username, proposedNumPlayers, controller)) {
System.out.println("Succesfully connected to RMI server\n\n");
} else {
System.out.println("RMI connection refused\n\n");
}
while(true) {
System.out.flush();
if (controller.localModel!=null) {
break;
}
Thread.sleep(500);
}
System.out.println("Model set\n\n");
} else if (networkType == 1) {
return;
}
}
}
@@ -6,9 +6,9 @@ import it.polimi.ingsw.gc14.View.IView;
public class ClientController { public class ClientController {
private Game localModel; public Game localModel;
public GameController localController; public GameController localController;
public final IView view; public IView view=null;
public ClientController(IView view,Game localModel) { public ClientController(IView view,Game localModel) {
this.view = view; this.view = view;
@@ -16,12 +16,18 @@ public class ClientController {
this.localController = new GameController(localModel); this.localController = new GameController(localModel);
} }
public ClientController() {
this.localController = new GameController(localModel);
}
public void setModel(Game model) { public void setModel(Game model) {
this.localModel = model; this.localModel = model;
localController.setModel(model); localController.setModel(model);
localModel.addObserver((Observer) view); // registra la view come observer // localModel.addObserver((Observer) view); // registra la view come observer
} }
public void onError(String message) { public void onError(String message) {
view.showError(message); view.showError(message);
} }
@@ -111,9 +111,9 @@ public class Board implements Serializable {
ArrayList<BuildingCard> buildingDeck = new ArrayList<>(DecksCreator.loadBuildingDeckByEra(1)); ArrayList<BuildingCard> buildingDeck = new ArrayList<>(DecksCreator.loadBuildingDeckByEra(1));
Collections.shuffle(buildingDeck); Collections.shuffle(buildingDeck);
if(nTotem==2) if(nTotem==2)
upperListBuilding= buildingDeck.subList(0,1); upperListBuilding = new ArrayList<>(buildingDeck.subList(0,1));
else else
upperListBuilding= buildingDeck.subList(0,2); upperListBuilding = new ArrayList<>(buildingDeck.subList(0,2));
} }
/** /**
@@ -5,6 +5,7 @@ import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import java.io.Serializable;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject; import java.rmi.server.UnicastRemoteObject;
@@ -13,7 +14,7 @@ import java.rmi.server.UnicastRemoteObject;
* RMI client callback implementation of {@link IClientCallback}. * RMI client callback implementation of {@link IClientCallback}.
* Receives notifications from the server and updates the client game model. * Receives notifications from the server and updates the client game model.
*/ */
public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCallback { public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCallback, Serializable {
/** The client controller used to apply events and update the model */ /** The client controller used to apply events and update the model */
private final ClientController clientController; private final ClientController clientController;
@@ -7,7 +7,6 @@ import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer; import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
/** /**
* Client RMI. Uses the methods exposed by the server RMI. * Client RMI. Uses the methods exposed by the server RMI.
@@ -3,9 +3,10 @@ package it.polimi.ingsw.gc14.Network.RMI.Common;
import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import java.io.Serializable;
import java.rmi.*; import java.rmi.*;
public interface IClientCallback extends Remote { public interface IClientCallback extends Remote, Serializable {
void onGameInit(Game model) throws RemoteException; void onGameInit(Game model) throws RemoteException;
void onAction(NetworkEvent action) throws RemoteException; void onAction(NetworkEvent action) throws RemoteException;
} }
@@ -21,7 +21,7 @@ import java.rmi.*;
/** /**
* Server RMI. Exposes a method to join the game and one to execute an event. * Server RMI. Exposes a method to join the game and one to execute an event.
*/ */
public class RMIServer implements IGameServer { public class RMIServer extends UnicastRemoteObject implements IGameServer {
/** Server game's controller */ /** Server game's controller */
private GameController controller; private GameController controller;
@@ -88,12 +88,13 @@ public class RMIServer implements IGameServer {
playerList.setLimit(preferredInt); playerList.setLimit(preferredInt);
} }
if (controller.addPlayer(username)) { if (controller.addPlayer(username)) {
playerList.add(username);
clients.put(username, callback); clients.put(username, callback);
playerList.add(username);
return true; return true;
} }
return false; return false;
} }
} }
@@ -34,7 +34,7 @@ import java.util.concurrent.LinkedBlockingQueue;
public class ServerLauncher { public class ServerLauncher {
/** /**
* Queue containing the events to be applied to the game model. * Queue containinetworkTypeng the events to be applied to the game model.
* Thread safe by design. * Thread safe by design.
*/ */
BlockingQueue<NetworkEvent> actionQueue; BlockingQueue<NetworkEvent> actionQueue;
@@ -109,13 +109,15 @@ public class ServerLauncher {
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
playerList.setAction(()->{ playerList.setAction(()->{
try { new Thread(()->{
launcher.run(); try {
} catch (InterruptedException e) { launcher.run();
throw new RuntimeException(e); } catch (InterruptedException e) {
} catch (RemoteException e) { throw new RuntimeException(e);
throw new RuntimeException(e); } catch (RemoteException e) {
} throw new RuntimeException(e);
}
}).start();
}); });
serverRMI.start(); serverRMI.start();
@@ -132,6 +134,7 @@ public class ServerLauncher {
*/ */
public void run() throws InterruptedException, RemoteException { public void run() throws InterruptedException, RemoteException {
// Game creation // Game creation
System.out.println("\n\nNotifying model");
serverRMI.notifyAll(gameController.getModel()); serverRMI.notifyAll(gameController.getModel());
serverTCP.notifyAll(gameController.getModel()); serverTCP.notifyAll(gameController.getModel());
+8 -1
View File
@@ -8,7 +8,14 @@ module it.polimi.ingsw.gc14 {
opens it.polimi.ingsw.gc14 to javafx.fxml, com.google.gson; opens it.polimi.ingsw.gc14 to javafx.fxml, com.google.gson;
opens it.polimi.ingsw.gc14.Model to com.google.gson; opens it.polimi.ingsw.gc14.Model to com.google.gson;
opens it.polimi.ingsw.gc14.Model.GamePackage to com.google.gson;
exports it.polimi.ingsw.gc14; exports it.polimi.ingsw.gc14;
opens it.polimi.ingsw.gc14.Model.GamePackage to com.google.gson;
// RMI
exports it.polimi.ingsw.gc14.Network.RMI.Common to java.rmi;
exports it.polimi.ingsw.gc14.Network.RMI.Server to java.rmi;
exports it.polimi.ingsw.gc14.Network.RMI.Client to java.rmi;
exports it.polimi.ingsw.gc14.Network to java.rmi;
exports it.polimi.ingsw.gc14.Model to java.rmi, com.google.gson;
} }