Fixed: RMI Implementation
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user