Merge pull request #48 from rubenpirreram/RMI

RMI
This commit is contained in:
rubenpirreram
2026-04-25 19:25:54 +02:00
committed by GitHub
3 changed files with 145 additions and 46 deletions
@@ -8,25 +8,52 @@ import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject; 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 {
/** The client controller used to apply events and update the model */
private final ClientController clientController; private final ClientController clientController;
/**
* Class constructor.
* @param clientController the client controller
* @throws RemoteException if any RMI error occurs
*/
public ClientCallbackImpl(ClientController clientController) throws RemoteException { public ClientCallbackImpl(ClientController clientController) throws RemoteException {
this.clientController = clientController; this.clientController = clientController;
} }
/**
* Called by the server when the game is initialized.
* Sets the client game model in the {@link ClientController}.
* @param model the initialized {@link Game} model
* @throws RemoteException if any RMI error occurs
*/
@Override @Override
public void onGameInit(Game model) throws RemoteException { public void onGameInit(Game model) throws RemoteException {
clientController.setModel(model); // setta il model clientController.setModel(model);
} }
/**
* Called by the server when an action has been accepted.
* If the event contains an error, it is printed to the console.
* Otherwise, the event is applied to the local model.
* @param event the {@link NetworkEvent} sent by the server
* @throws RemoteException if any RMI error occurs
*/
@Override @Override
public void onAction(NetworkEvent event) throws RemoteException { public void onAction(NetworkEvent event) throws RemoteException {
if(event.getIsError()) { if(event.getIsError()) {
System.out.println(event.toString()); System.out.println(event.toString());
} else { } else {
event.apply(clientController.localController); // delega tutto al controller event.apply(clientController.localController);
//clientController.view.update(); TODO //clientController.view.update(); TODO
} }
} }
@@ -1,43 +1,56 @@
package it.polimi.ingsw.gc14.Network.RMI.Client; package it.polimi.ingsw.gc14.Network.RMI.Client;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry; import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry; import java.rmi.registry.Registry;
import it.polimi.ingsw.gc14.Controller.ClientController; 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.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.
*/
public class RMIClient { public class RMIClient {
/** The host address of the RMI server */
private final String host; private final String host;
/** The port of the RMI server */
private final int port; private final int port;
/** The remote stub used to call methods on the server */
private IGameServer stub; private IGameServer stub;
/**
* Class constructor.
* @param host the host address of the RMI server
* @param port the port of the RMI server
*/
public RMIClient(String host, int port) { public RMIClient(String host, int port) {
this.host = host; this.host = host;
this.port = port; this.port = port;
} }
/**
* Connects to the RMI server and attempts to join the game.
* Looks up the RMI registry to retrieve the {@link IGameServer} stub.
* Then, creates a {@link ClientCallbackImpl} and calls {@link RMIServer#joinGame(String, int, IClientCallback)}.
* @param username the player's username
* @param preferredInt the desired number of players
* @param clientController the client controller used to create the callback
* @return true if the player successfully joined the game, false otherwise
*/
public boolean connect(String username,int preferredInt, ClientController clientController) { public boolean connect(String username,int preferredInt, ClientController clientController) {
// 1. Connettiti al registry
try { try {
Registry registry = LocateRegistry.getRegistry(host, port); Registry registry = LocateRegistry.getRegistry(host, port);
// 2. Prendi lo stub del server
this.stub = (IGameServer) registry.lookup("RMIGameServer"); this.stub = (IGameServer) registry.lookup("RMIGameServer");
// 3. Crea il callback e registralo
ClientCallbackImpl callback = new ClientCallbackImpl(clientController); ClientCallbackImpl callback = new ClientCallbackImpl(clientController);
if (!stub.joinGame(username,preferredInt, callback)) return stub.joinGame(username, preferredInt, callback);
{
stub = null;
return false;
}
else
{
return true;
}
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@@ -46,8 +59,12 @@ public class RMIClient {
} }
/**
public void doEvent(NetworkEvent event) throws Exception { * Sends a {@link NetworkEvent} to the server.
* @param event the event to send
* @throws RemoteException if any RMI error occurs
*/
public void doEvent(NetworkEvent event) throws RemoteException {
stub.doEvent(event); stub.doEvent(event);
} }
@@ -15,19 +15,47 @@ import java.util.Map;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.rmi.*; 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 implements IGameServer {
/** Server game's controller */
private GameController controller; private GameController controller;
/** Server game's model */
private Game model;
/** RMI registry */
private Registry registry; private Registry registry;
/** RMI port */
private int nPort; private int nPort;
/** Map containing the associations between a player's username and its callback */
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>(); private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
/** Queue containing the events to be applied to the game model */
BlockingQueue<NetworkEvent> actionQueue; BlockingQueue<NetworkEvent> actionQueue;
/**
* List containing the usernames of joined players.
* {@link LimitedList}'s limit defines at which size the list calls its action. The limit can be set using {@link LimitedList#setLimit(int)}.
*/
private LimitedList<String> playerList; private LimitedList<String> playerList;
// Costruttore
/**
* Class constructor that initializes the attributes.
* @param controller The game controller
* @param nPort The RMI port
* @param actionQueue The action queue
* @param playerList The player's usernames list
* @throws RemoteException if an RMI error occurs
*/
public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue,LimitedList<String> playerList) throws RemoteException { public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue,LimitedList<String> playerList) throws RemoteException {
this.controller = controller; this.controller = controller;
this.nPort = nPort; this.nPort = nPort;
@@ -37,25 +65,29 @@ public class RMIServer implements IGameServer {
// RMI's exposed methods
/**
* Allows a player to join the game.
// Metodi esposti RMI * If the desired number of player is invalid, the request is rejected.
public void setController (GameController controller) { * If this is the first player, a new game model is created and passed to the controller. Additionally, the playerList's limit is set.
this.controller = controller; * Then, if the controller successfully adds the player, the username is added to {@link #playerList} and {@link #clients}.
} * @param username The player's name
* @param preferredInt The desired number of players
* @param callback The client's callback interface
* @return true if the player successfully joined the game, false otherwise
*/
@Override @Override
public boolean joinGame(String username, int preferredInt, IClientCallback callback) { public boolean joinGame(String username, int preferredInt, IClientCallback callback) {
synchronized (controller) { if (preferredInt<2 || preferredInt>5) {
if(playerList.size()==0 && (preferredInt<2||preferredInt>5))
return false; return false;
if (controller.addPlayer(username)) { }
if(playerList.size()==0) synchronized (controller) {
{ if(playerList.isEmpty()){
Game game = new Game(preferredInt); model = new Game(preferredInt);
controller.setModel(game); controller.setModel(model);
playerList.setLimit(preferredInt); playerList.setLimit(preferredInt);
} }
if (controller.addPlayer(username)) {
playerList.add(username); playerList.add(username);
clients.put(username, callback); clients.put(username, callback);
return true; return true;
@@ -63,21 +95,36 @@ public class RMIServer implements IGameServer {
return false; return false;
} }
} }
/**
* Push an action in actionQueue.
* @param action The desired actio
* @return true if the action was successfully added, false otherwise
*/
@Override @Override
public boolean doEvent(NetworkEvent event) throws RemoteException { public boolean doEvent(NetworkEvent action) {
return actionQueue.offer(event); return actionQueue.offer(action);
} }
// RMI's internal methods
/**
// Metodi interni del server * Sends an action to all RMI clients.
* @param action The desired action
*/
public void notifyAll(NetworkEvent action) throws RemoteException { public void notifyAll(NetworkEvent action) throws RemoteException {
for (IClientCallback cb : clients.values()) { for (IClientCallback cb : clients.values()) {
cb.onAction(action); cb.onAction(action);
} }
} }
/**
* Sends a model game to all RMI clients.
* @param model The desired model
*/
public void notifyAll(Game model) throws RemoteException { public void notifyAll(Game model) throws RemoteException {
for (IClientCallback cb : clients.values()) { for (IClientCallback cb : clients.values()) {
cb.onGameInit(model); cb.onGameInit(model);
@@ -86,14 +133,16 @@ public class RMIServer implements IGameServer {
// Metodi per avviare server RMI // Metodi per avviare server RMI
/**
* Starts the RMI server.
* @return true if the server starts successfully, false otherwise
*/
public boolean start() { public boolean start() {
try { try {
registry = LocateRegistry.createRegistry(nPort); registry = LocateRegistry.createRegistry(nPort);
registry.rebind("RMIGameServer", this); registry.rebind("RMIGameServer", this);
System.out.println("RMI Server avviato sulla porta "+nPort); System.out.println("RMI Server started on port: "+nPort);
return true; return true;
} }
catch (Exception e) { catch (Exception e) {
@@ -101,6 +150,12 @@ public class RMIServer implements IGameServer {
return false; return false;
} }
} }
/**
* Stops the RMI server.
* @return true if the server stops successfully, false otherwise
*/
public boolean stop() { public boolean stop() {
try { try {
registry.unbind("RMIGameServer"); registry.unbind("RMIGameServer");