diff --git a/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java new file mode 100644 index 0000000..e330a90 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Controller/ClientController.java @@ -0,0 +1,29 @@ +package it.polimi.ingsw.gc14.Controller; + +import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.Observer; +import it.polimi.ingsw.gc14.View.IView; + +public class ClientController { + + private Game localModel; + public GameController localController; + private final IView view; + + public ClientController(IView view,Game localModel) { + this.view = view; + this.localModel = localModel; + 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 + } + + public void onError(String message) { + view.showError(message); + } + +} 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 new file mode 100644 index 0000000..d79d357 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/ClientCallbackImpl.java @@ -0,0 +1,34 @@ +package it.polimi.ingsw.gc14.Network.RMI.Client; + +import it.polimi.ingsw.gc14.Controller.ClientController; +import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.NetworkEvent; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; + +public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCallback { + + private final ClientController clientController; + + public ClientCallbackImpl(ClientController clientController) throws RemoteException { + this.clientController = clientController; + } + + @Override + public void onGameInit(Game model) throws RemoteException { + clientController.setModel(model); // setta il model + } + + @Override + public void onAction(NetworkEvent event) throws RemoteException { + event.apply(clientController.localController); // delega tutto al controller + } + + + @Override + public void onError(String message) throws RemoteException { + clientController.onError(message); + } + +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java new file mode 100644 index 0000000..ffb42ff --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java @@ -0,0 +1,10 @@ +package it.polimi.ingsw.gc14.Network.RMI.Client; + +import it.polimi.ingsw.gc14.Controller.ClientController; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.View.IView; + +public interface GameClient { + boolean connect( String username,ClientController clientController ); + void doEvent(NetworkEvent event) throws Exception; +} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java index d70ddcc..0d3772f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java @@ -9,5 +9,4 @@ public interface IClientCallback extends Remote { void onGameInit(Game model) throws RemoteException; void onAction(NetworkEvent action) throws RemoteException; void onError(String message) throws RemoteException; - void onGameOver(String winner) throws RemoteException; } \ No newline at end of file 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 e31222e..39ada7c 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 @@ -4,6 +4,54 @@ import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; -public class RMIClient { +import it.polimi.ingsw.gc14.Controller.ClientController; +import it.polimi.ingsw.gc14.Network.NetworkEvent; +import it.polimi.ingsw.gc14.Network.RMI.Server.*; +import it.polimi.ingsw.gc14.View.IView; -} +public class RMIClient implements GameClient { + + private final String host; + private final int port; + private IGameServer stub; + + public RMIClient(String host, int port) { + this.host = host; + this.port = port; + } + + @Override + public boolean connect(String username,ClientController clientController) { + // 1. Connettiti al registry + try { + Registry registry = LocateRegistry.getRegistry(host, port); + + // 2. Prendi lo stub del server + this.stub = (IGameServer) registry.lookup("RMIGameServer"); + + // 3. Crea il callback e registralo + ClientCallbackImpl callback = new ClientCallbackImpl(clientController); + + if (!stub.joinGame(username, callback)) + { + stub = null; + return false; + } + else + { + return true; + } + } + catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + @Override + public void doEvent( NetworkEvent event) throws Exception { + stub.doEvent(event); + } + +} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java index e966683..17398b1 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java @@ -7,7 +7,7 @@ import java.rmi.*; public interface IGameServer extends Remote { - String joinGame(String username, IClientCallback callback) throws RemoteException; + boolean joinGame(String username, IClientCallback callback) throws RemoteException; boolean doEvent(NetworkEvent event) throws RemoteException; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java index 2fddf40..70d7813 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java @@ -3,7 +3,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Server; import java.rmi.RemoteException; public interface IRMIServer { - void connect(VirtualView client) throws RemoteException; + void add(Integer number) throws RemoteException; void reset() throws RemoteException; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java index b32bc2f..2f7cf82 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Server; import it.polimi.ingsw.gc14.Controller.GameController; +import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback; import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvents.DrawUpperBuildingCard; @@ -19,21 +20,23 @@ public class RMIGameController implements IGameServer { } @Override - public String joinGame(String username, IClientCallback callback) throws RemoteException { - controller.addPlayer(username); - clients.put(username, callback); - callback.onGameInit(controller.getModel()); - return username; + public boolean joinGame(String username, IClientCallback callback) { + if(controller.addPlayer(username)) + { + clients.put(username, callback); + return true; + } + return false; } @Override - public void doEvent(NetworkEvent event) throws RemoteException { + public boolean doEvent(NetworkEvent event) throws RemoteException { boolean result = event.apply(controller); if (!result) { - notifyError("","Mossa non valida"); + notifyError(event.getUsername(),"Mossa non valida"); } else { - notifyAll(new DrawUpperBuildingCard(username, pos)); + notifyAll(event); } return result; } @@ -43,6 +46,11 @@ public class RMIGameController implements IGameServer { cb.onAction(action); } } + private void notifyAll(Game model) throws RemoteException { + for (IClientCallback cb : clients.values()) { + cb.onGameInit(model); + } + } private void notifyError(String username, String message) throws RemoteException { IClientCallback cb = clients.get(username); 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 ecc802a..e07a1da 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 @@ -11,22 +11,30 @@ public class RMIServer { private final RMIGameController controller; private Registry registry; - - public RMIServer(RMIGameController controller) { + private int nPort; + public RMIServer(RMIGameController controller, int nPort) { this.controller = controller; + this.nPort = nPort; } - @Override - public void start() throws Exception { - IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0); - registry = LocateRegistry.createRegistry(1099); - registry.rebind("GameServer", stub); - System.out.println("RMI Server avviato sulla porta 1099"); + + public boolean start() throws Exception { + try { + IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0); + registry = LocateRegistry.createRegistry(nPort); + registry.rebind("RMIGameServer", stub); + System.out.println("RMI Server avviato sulla porta "+nPort); + return true; + } + catch (Exception e) { + e.printStackTrace(); + return false; + } } - @Override + public void stop() throws Exception { - registry.unbind("GameServer"); + registry.unbind("RMIGameServer"); UnicastRemoteObject.unexportObject(controller, true); System.out.println("RMI Server fermato"); } diff --git a/src/main/java/it/polimi/ingsw/gc14/View/IView.java b/src/main/java/it/polimi/ingsw/gc14/View/IView.java index ac13003..80c7616 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/IView.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/IView.java @@ -1,19 +1,12 @@ package it.polimi.ingsw.gc14.View; -public interface IView { - // --- Setup --- - void showWelcome(); - String askPlayerName(); +import it.polimi.ingsw.gc14.Model.Game; +import it.polimi.ingsw.gc14.Network.NetworkEvent; - // --- Rendering --- - void render(GameState state); +public interface IView { + + void render(Game model); void showMessage(String message); void showError(String message); - void showWinner(String winner); - // --- Input giocatore --- - NetworkEvent askMove(GameState state); - - // --- Lifecycle --- - void close(); }