From 2f6a20b9f8a7d458a445a068ebbe9f245c88158b Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Sat, 25 Apr 2026 19:23:26 +0200 Subject: [PATCH] Add: Complete JavaDOC of RMIClient and ClientCallbackImpl --- .../RMI/Client/ClientCallbackImpl.java | 31 ++++++++++- .../gc14/Network/RMI/Client/RMIClient.java | 53 ++++++++++++------- 2 files changed, 64 insertions(+), 20 deletions(-) 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 index 871b653..928fa52 100644 --- 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 @@ -8,25 +8,52 @@ import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import java.rmi.RemoteException; 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 { + /** The client controller used to apply events and update the model */ private final ClientController clientController; + + /** + * Class constructor. + * @param clientController the client controller + * @throws RemoteException if any RMI error occurs + */ public ClientCallbackImpl(ClientController clientController) throws RemoteException { 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 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 public void onAction(NetworkEvent event) throws RemoteException { if(event.getIsError()) { System.out.println(event.toString()); } else { - event.apply(clientController.localController); // delega tutto al controller + event.apply(clientController.localController); //clientController.view.update(); TODO } } 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 8d0721c..84d797f 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 @@ -1,43 +1,56 @@ package it.polimi.ingsw.gc14.Network.RMI.Client; +import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import it.polimi.ingsw.gc14.Controller.ClientController; 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.Server.RMIServer; -public class RMIClient { +/** + * Client RMI. Uses the methods exposed by the server RMI. + */ +public class RMIClient { + /** The host address of the RMI server */ private final String host; + + /** The port of the RMI server */ private final int port; + + /** The remote stub used to call methods on the server */ 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) { this.host = host; this.port = port; } - public boolean connect(String username,int preferredInt,ClientController clientController) { - // 1. Connettiti al registry + /** + * 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) { 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,preferredInt, callback)) - { - stub = null; - return false; - } - else - { - return true; - } + return stub.joinGame(username, preferredInt, callback); } catch (Exception e) { 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); }