Add: Complete JavaDOC of RMIClient and ClientCallbackImpl

This commit is contained in:
2026-04-25 19:23:26 +02:00
parent 3f727335a9
commit 2f6a20b9f8
2 changed files with 64 additions and 20 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);
} }