From 1243fd98d659c655f2068e545cf2e64803bc4ff2 Mon Sep 17 00:00:00 2001 From: AleandroPagani Date: Thu, 23 Apr 2026 17:39:23 +0200 Subject: [PATCH] Refactor: RMI Fixed: NetworkEvents --- .../ingsw/gc14/Network/NetworkEvent.java | 9 +- .../gc14/Network/NetworkEvents/AddPlayer.java | 4 +- .../NetworkEvents/DrawLowerBuildingCard.java | 5 +- .../NetworkEvents/DrawLowerTribeCard.java | 4 +- .../NetworkEvents/DrawUpperBuildingCard.java | 4 +- .../NetworkEvents/DrawUpperTribeCard.java | 4 +- .../PickOptionalBuildingCard.java | 4 +- .../NetworkEvents/PickOptionalTribeCard.java | 4 +- .../Network/NetworkEvents/SlotChoice.java | 4 +- .../RMI/Client/ClientCallbackImpl.java | 1 + .../gc14/Network/RMI/Client/GameClient.java | 10 -- .../gc14/Network/RMI/Client/RMIClient.java | 13 +-- .../{Client => Common}/IClientCallback.java | 2 +- .../RMI/{Server => Common}/IGameServer.java | 3 +- .../gc14/Network/RMI/Server/IRMIServer.java | 9 -- .../Network/RMI/Server/RMIGameController.java | 59 ------------ .../gc14/Network/RMI/Server/RMIServer.java | 94 ++++++++++++++++--- 17 files changed, 104 insertions(+), 129 deletions(-) delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java rename src/main/java/it/polimi/ingsw/gc14/Network/RMI/{Client => Common}/IClientCallback.java (87%) rename src/main/java/it/polimi/ingsw/gc14/Network/RMI/{Server => Common}/IGameServer.java (70%) delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java delete mode 100644 src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java index 6e0f683..5e3d45b 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvent.java @@ -1,6 +1,7 @@ package it.polimi.ingsw.gc14.Network; import it.polimi.ingsw.gc14.Controller.GameController; +import javafx.event.Event; import java.io.Serializable; @@ -9,9 +10,11 @@ public abstract class NetworkEvent implements Serializable { public String getUsername() { return username; } - public NetworkEvent() - { - + protected EventType eventType; + public EventType getEventType() {return eventType;} + protected NetworkEvent(String username, EventType eventType) { + this.username = username; + this.eventType = eventType; } public NetworkEvent(String username) { this.username = username; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java index 3d3a9c5..602a231 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/AddPlayer.java @@ -8,10 +8,8 @@ import it.polimi.ingsw.gc14.View.IView; import java.io.Serializable; public class AddPlayer extends NetworkEvent implements Serializable { - private EventType eventType; public AddPlayer(String username) { - this.username = username; - this.eventType = EventType.ADD_PLAYER; + super(username, EventType.ADD_PLAYER); } @Override public boolean apply(GameController gameController) diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java index a4910dd..2456a6a 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerBuildingCard.java @@ -8,13 +8,10 @@ import it.polimi.ingsw.gc14.View.IView; import java.io.Serializable; public class DrawLowerBuildingCard extends NetworkEvent implements Serializable{ - - private EventType eventType; private int pos; public DrawLowerBuildingCard(String username, int pos){ - this.username = username; - this.eventType = EventType.DRAW_LOWER_BUILD; + super(username, EventType.DRAW_LOWER_BUILD); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java index 85f4598..64b0a17 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawLowerTribeCard.java @@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView; import java.io.Serializable; public class DrawLowerTribeCard extends NetworkEvent implements Serializable{ - private EventType eventType; private int pos; public DrawLowerTribeCard(String username, int pos){ - this.username = username; - this.eventType = EventType.DRAW_LOWER_TRIBE; + super(username, EventType.DRAW_LOWER_TRIBE); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java index 77f7c60..5f0a985 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperBuildingCard.java @@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView; import java.io.Serializable; public class DrawUpperBuildingCard extends NetworkEvent implements Serializable{ - private EventType eventType; private int pos; public DrawUpperBuildingCard(String username, int pos){ - this.username = username; - this.eventType = EventType.DRAW_UPPER_BUILD; + super(username, EventType.DRAW_UPPER_BUILD); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java index 0f6d04d..69ab281 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/DrawUpperTribeCard.java @@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView; import java.io.Serializable; public class DrawUpperTribeCard extends NetworkEvent implements Serializable{ - private EventType eventType; private int pos; public DrawUpperTribeCard(String username, int pos){ - this.username = username; - this.eventType = EventType.DRAW_UPPER_TRIBE; + super(username, EventType.DRAW_UPPER_TRIBE); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java index dcf3822..a1fde73 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalBuildingCard.java @@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView; import java.io.Serializable; public class PickOptionalBuildingCard extends NetworkEvent implements Serializable{ - private EventType eventType; private int pos; public PickOptionalBuildingCard(String username, int pos){ - this.username = username; - this.eventType = EventType.PICK_OPTIONAL_BUILD; + super(username, EventType.PICK_OPTIONAL_BUILD); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java index 46cc783..d739e3c 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/PickOptionalTribeCard.java @@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView; import java.io.Serializable; public class PickOptionalTribeCard extends NetworkEvent implements Serializable{ - private EventType eventType; private int pos; public PickOptionalTribeCard(String username, int pos){ - this.username = username; - this.eventType = EventType.PICK_OPTIONAL_TRIBE; + super(username, EventType.PICK_OPTIONAL_TRIBE); this.pos = pos; } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java index 700335a..97000d9 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/NetworkEvents/SlotChoice.java @@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView; import java.io.Serializable; public class SlotChoice extends NetworkEvent implements Serializable { - private EventType eventType; private int pos; public SlotChoice(String username, int pos) { - this.username = username; - this.eventType = EventType.SLOT_CHOICE; + super(username, EventType.SLOT_CHOICE); this.pos = pos; } 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 d79d357..64c53e9 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 @@ -3,6 +3,7 @@ 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 it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java deleted file mode 100644 index ffb42ff..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/GameClient.java +++ /dev/null @@ -1,10 +0,0 @@ -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; -} 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 39ada7c..ac0e14b 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,15 +1,12 @@ package it.polimi.ingsw.gc14.Network.RMI.Client; -import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; -import java.rmi.server.UnicastRemoteObject; 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; +import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer; -public class RMIClient implements GameClient { +public class RMIClient { private final String host; private final int port; @@ -20,7 +17,7 @@ public class RMIClient implements GameClient { this.port = port; } - @Override + public boolean connect(String username,ClientController clientController) { // 1. Connettiti al registry try { @@ -49,8 +46,8 @@ public class RMIClient implements GameClient { } - @Override - public void doEvent( NetworkEvent event) throws Exception { + + public void doEvent(NetworkEvent event) throws Exception { stub.doEvent(event); } diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java similarity index 87% rename from src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java rename to src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java index 0d3772f..5ff2b0b 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/IClientCallback.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IClientCallback.java @@ -1,4 +1,4 @@ -package it.polimi.ingsw.gc14.Network.RMI.Client; +package it.polimi.ingsw.gc14.Network.RMI.Common; import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Network.NetworkEvent; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java similarity index 70% rename from src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java rename to src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java index 17398b1..8bcaff6 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IGameServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Common/IGameServer.java @@ -1,6 +1,5 @@ -package it.polimi.ingsw.gc14.Network.RMI.Server; +package it.polimi.ingsw.gc14.Network.RMI.Common; -import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback; import it.polimi.ingsw.gc14.Network.NetworkEvent; import java.rmi.*; diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java deleted file mode 100644 index 70d7813..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/IRMIServer.java +++ /dev/null @@ -1,9 +0,0 @@ -package it.polimi.ingsw.gc14.Network.RMI.Server; - -import java.rmi.RemoteException; - -public interface IRMIServer { - - void add(Integer number) throws RemoteException; - void reset() throws RemoteException; -} diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java deleted file mode 100644 index 2f7cf82..0000000 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIGameController.java +++ /dev/null @@ -1,59 +0,0 @@ -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; - -import java.rmi.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.*; - -public class RMIGameController implements IGameServer { - - private final GameController controller; - private final Map clients = new ConcurrentHashMap<>(); - - public RMIGameController(GameController controller) { - this.controller = controller; - } - - @Override - public boolean joinGame(String username, IClientCallback callback) { - if(controller.addPlayer(username)) - { - clients.put(username, callback); - return true; - } - return false; - } - - - @Override - public boolean doEvent(NetworkEvent event) throws RemoteException { - boolean result = event.apply(controller); - if (!result) { - notifyError(event.getUsername(),"Mossa non valida"); - } else { - notifyAll(event); - } - return result; - } - - private void notifyAll(NetworkEvent action) throws RemoteException { - for (IClientCallback cb : clients.values()) { - 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); - if (cb != null) cb.onError(message); - } -} \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java index e07a1da..9a82dec 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Server/RMIServer.java @@ -1,28 +1,92 @@ 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.NetworkEvent; +import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; +import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; -public class RMIServer { - private final RMIGameController controller; +import java.rmi.*; + +public class RMIServer implements IGameServer { + + private GameController controller; private Registry registry; private int nPort; - public RMIServer(RMIGameController controller, int nPort) { + private final Map clients = new ConcurrentHashMap<>(); + + + // Costruttore + public RMIServer(GameController controller, int nPort) throws RemoteException { this.controller = controller; this.nPort = nPort; } - public boolean start() throws Exception { + + + + // Metodi esposti RMI + public void setController (GameController controller) { + this.controller = controller; + } + @Override + public boolean joinGame(String username, IClientCallback callback) { + if(controller.addPlayer(username)) + { + clients.put(username, callback); + return true; + } + return false; + } + @Override + public boolean doEvent(NetworkEvent event) throws RemoteException { + boolean result = event.apply(controller); + if (!result) { + notifyError(event.getUsername(),"Mossa non valida"); + } else { + notifyAll(event); + } + return result; + } + + + + + + // Metodi interni del server + private void notifyAll(NetworkEvent action) throws RemoteException { + for (IClientCallback cb : clients.values()) { + 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); + if (cb != null) cb.onError(message); + } + + + + + + // Metodi per avviare server RMI + public boolean start() { try { - IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0); registry = LocateRegistry.createRegistry(nPort); - registry.rebind("RMIGameServer", stub); + registry.rebind("RMIGameServer", this); System.out.println("RMI Server avviato sulla porta "+nPort); return true; } @@ -31,11 +95,17 @@ public class RMIServer { return false; } } - - - public void stop() throws Exception { - registry.unbind("RMIGameServer"); - UnicastRemoteObject.unexportObject(controller, true); - System.out.println("RMI Server fermato"); + public boolean stop() { + try { + registry.unbind("RMIGameServer"); + UnicastRemoteObject.unexportObject(this, true); + System.out.println("RMI Server fermato"); + return true; + } catch (RemoteException | NotBoundException e) { + e.printStackTrace(); + return false; + } } + + }