Merge pull request #38 from rubenpirreram/RMI

RMI
This commit is contained in:
rubenpirreram
2026-04-19 19:52:03 +02:00
committed by GitHub
10 changed files with 164 additions and 35 deletions
@@ -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 onGameInit(Game model) throws RemoteException;
void onAction(NetworkEvent action) throws RemoteException; void onAction(NetworkEvent action) throws RemoteException;
void onError(String message) 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.registry.Registry;
import java.rmi.server.UnicastRemoteObject; 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 { 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; boolean doEvent(NetworkEvent event) throws RemoteException;
} }
@@ -3,7 +3,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Server;
import java.rmi.RemoteException; import java.rmi.RemoteException;
public interface IRMIServer { public interface IRMIServer {
void connect(VirtualView client) throws RemoteException;
void add(Integer number) throws RemoteException; void add(Integer number) throws RemoteException;
void reset() throws RemoteException; void reset() throws RemoteException;
} }
@@ -1,6 +1,7 @@
package it.polimi.ingsw.gc14.Network.RMI.Server; package it.polimi.ingsw.gc14.Network.RMI.Server;
import it.polimi.ingsw.gc14.Controller.GameController; 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.RMI.Client.IClientCallback;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.DrawUpperBuildingCard; import it.polimi.ingsw.gc14.Network.NetworkEvents.DrawUpperBuildingCard;
@@ -19,21 +20,23 @@ public class RMIGameController implements IGameServer {
} }
@Override @Override
public String joinGame(String username, IClientCallback callback) throws RemoteException { public boolean joinGame(String username, IClientCallback callback) {
controller.addPlayer(username); if(controller.addPlayer(username))
clients.put(username, callback); {
callback.onGameInit(controller.getModel()); clients.put(username, callback);
return username; return true;
}
return false;
} }
@Override @Override
public void doEvent(NetworkEvent event) throws RemoteException { public boolean doEvent(NetworkEvent event) throws RemoteException {
boolean result = event.apply(controller); boolean result = event.apply(controller);
if (!result) { if (!result) {
notifyError("","Mossa non valida"); notifyError(event.getUsername(),"Mossa non valida");
} else { } else {
notifyAll(new DrawUpperBuildingCard(username, pos)); notifyAll(event);
} }
return result; return result;
} }
@@ -43,6 +46,11 @@ public class RMIGameController implements IGameServer {
cb.onAction(action); 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 { private void notifyError(String username, String message) throws RemoteException {
IClientCallback cb = clients.get(username); IClientCallback cb = clients.get(username);
@@ -11,22 +11,30 @@ public class RMIServer {
private final RMIGameController controller; private final RMIGameController controller;
private Registry registry; private Registry registry;
private int nPort;
public RMIServer(RMIGameController controller) { public RMIServer(RMIGameController controller, int nPort) {
this.controller = controller; this.controller = controller;
this.nPort = nPort;
} }
@Override
public void start() throws Exception { public boolean start() throws Exception {
IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0); try {
registry = LocateRegistry.createRegistry(1099); IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0);
registry.rebind("GameServer", stub); registry = LocateRegistry.createRegistry(nPort);
System.out.println("RMI Server avviato sulla porta 1099"); 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 { public void stop() throws Exception {
registry.unbind("GameServer"); registry.unbind("RMIGameServer");
UnicastRemoteObject.unexportObject(controller, true); UnicastRemoteObject.unexportObject(controller, true);
System.out.println("RMI Server fermato"); System.out.println("RMI Server fermato");
} }
@@ -1,19 +1,12 @@
package it.polimi.ingsw.gc14.View; package it.polimi.ingsw.gc14.View;
public interface IView { import it.polimi.ingsw.gc14.Model.Game;
// --- Setup --- import it.polimi.ingsw.gc14.Network.NetworkEvent;
void showWelcome();
String askPlayerName();
// --- Rendering --- public interface IView {
void render(GameState state);
void render(Game model);
void showMessage(String message); void showMessage(String message);
void showError(String message); void showError(String message);
void showWinner(String winner);
// --- Input giocatore ---
NetworkEvent askMove(GameState state);
// --- Lifecycle ---
void close();
} }