diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncher.java deleted file mode 100644 index fd8a3f8..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncher.java +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java new file mode 100644 index 0000000..bb89ac2 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -0,0 +1,54 @@ +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("CLIENT CONNESSO DAJE"); + } else { + System.out.println("NON CONNESSO D:"); + } + + while(true) { + System.out.flush(); + if (controller.localModel!=null) { + System.out.println("MODEL SETTATO"); + } + Thread.sleep(500); + } + + + + + + + + } else if (networkType == 1) { + return; + } + } +} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java index d206d22..1da7933 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java @@ -6,9 +6,9 @@ import it.polimi.ingsw.gc14.View.IView; public class ClientController { - private Game localModel; + public Game localModel; public GameController localController; - public final IView view; + public IView view=null; public ClientController(IView view,Game localModel) { this.view = view; @@ -16,12 +16,18 @@ public class ClientController { this.localController = new GameController(localModel); } + public ClientController() { + this.localController = new GameController(localModel); + } + public void setModel(Game model) { this.localModel = 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) { view.showError(message); } diff --git a/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/Board.java b/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/Board.java index 55a17ee..4fda653 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/Board.java +++ b/src/main/java/it/polimi/ingsw/gc14/Model/GamePackage/Board.java @@ -111,9 +111,9 @@ public class Board implements Serializable { ArrayList buildingDeck = new ArrayList<>(DecksCreator.loadBuildingDeckByEra(1)); Collections.shuffle(buildingDeck); if(nTotem==2) - upperListBuilding= buildingDeck.subList(0,1); + upperListBuilding = new ArrayList<>(buildingDeck.subList(0,1)); else - upperListBuilding= buildingDeck.subList(0,2); + upperListBuilding = new ArrayList<>(buildingDeck.subList(0,2)); } /** diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java index 928fa52..5b16b76 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java @@ -5,6 +5,7 @@ import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; +import java.io.Serializable; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; @@ -13,7 +14,7 @@ import java.rmi.server.UnicastRemoteObject; * RMI client callback implementation of {@link IClientCallback}. * 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 */ private final ClientController clientController; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java index 84d797f..19bd574 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java @@ -7,7 +7,6 @@ import it.polimi.ingsw.gc14.Controller.ClientController; import it.polimi.ingsw.gc14.Network.NetworkEvent; 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.Server.RMIServer; /** * Client RMI. Uses the methods exposed by the server RMI. diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java index 0292fcc..c7fa94d 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java @@ -3,9 +3,10 @@ package it.polimi.ingsw.gc14.Network.RMI.Common; import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.NetworkEvent; +import java.io.Serializable; import java.rmi.*; -public interface IClientCallback extends Remote { +public interface IClientCallback extends Remote, Serializable { void onGameInit(Game model) throws RemoteException; void onAction(NetworkEvent action) throws RemoteException; } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java index 05152c1..6fadf03 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java @@ -21,7 +21,7 @@ import java.rmi.*; /** * 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 */ private GameController controller; @@ -88,12 +88,13 @@ public class RMIServer implements IGameServer { playerList.setLimit(preferredInt); } if (controller.addPlayer(username)) { - playerList.add(username); clients.put(username, callback); + playerList.add(username); return true; } return false; } + } diff --git a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java index df3c776..d1650d9 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java +++ b/src/main/java/it/polimi/ingsw/gc14/ServerLauncher.java @@ -34,7 +34,7 @@ import java.util.concurrent.LinkedBlockingQueue; 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. */ BlockingQueue actionQueue; @@ -109,13 +109,15 @@ public class ServerLauncher { ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); playerList.setAction(()->{ - try { - launcher.run(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } catch (RemoteException e) { - throw new RuntimeException(e); - } + new Thread(()->{ + try { + launcher.run(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } catch (RemoteException e) { + throw new RuntimeException(e); + } + }).start(); }); serverRMI.start(); @@ -132,6 +134,7 @@ public class ServerLauncher { */ public void run() throws InterruptedException, RemoteException { // Game creation + System.out.println("NOTIFICO MODEL"); serverRMI.notifyAll(gameController.getModel()); serverTCP.notifyAll(gameController.getModel()); diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 732a715..e178647 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -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.Model to com.google.gson; + opens it.polimi.ingsw.gc14.Model.GamePackage to com.google.gson; 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; } \ No newline at end of file