Refactor: RMI

Fixed: NetworkEvents
This commit is contained in:
2026-04-23 17:39:23 +02:00
parent 0b8077d8eb
commit 1243fd98d6
17 changed files with 104 additions and 129 deletions
@@ -1,6 +1,7 @@
package it.polimi.ingsw.gc14.Network; package it.polimi.ingsw.gc14.Network;
import it.polimi.ingsw.gc14.Controller.GameController; import it.polimi.ingsw.gc14.Controller.GameController;
import javafx.event.Event;
import java.io.Serializable; import java.io.Serializable;
@@ -9,9 +10,11 @@ public abstract class NetworkEvent implements Serializable {
public String getUsername() { public String getUsername() {
return username; 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) { public NetworkEvent(String username) {
this.username = username; this.username = username;
@@ -8,10 +8,8 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable; import java.io.Serializable;
public class AddPlayer extends NetworkEvent implements Serializable { public class AddPlayer extends NetworkEvent implements Serializable {
private EventType eventType;
public AddPlayer(String username) { public AddPlayer(String username) {
this.username = username; super(username, EventType.ADD_PLAYER);
this.eventType = EventType.ADD_PLAYER;
} }
@Override @Override
public boolean apply(GameController gameController) public boolean apply(GameController gameController)
@@ -8,13 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable; import java.io.Serializable;
public class DrawLowerBuildingCard extends NetworkEvent implements Serializable{ public class DrawLowerBuildingCard extends NetworkEvent implements Serializable{
private EventType eventType;
private int pos; private int pos;
public DrawLowerBuildingCard(String username, int pos){ public DrawLowerBuildingCard(String username, int pos){
this.username = username; super(username, EventType.DRAW_LOWER_BUILD);
this.eventType = EventType.DRAW_LOWER_BUILD;
this.pos = pos; this.pos = pos;
} }
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable; import java.io.Serializable;
public class DrawLowerTribeCard extends NetworkEvent implements Serializable{ public class DrawLowerTribeCard extends NetworkEvent implements Serializable{
private EventType eventType;
private int pos; private int pos;
public DrawLowerTribeCard(String username, int pos){ public DrawLowerTribeCard(String username, int pos){
this.username = username; super(username, EventType.DRAW_LOWER_TRIBE);
this.eventType = EventType.DRAW_LOWER_TRIBE;
this.pos = pos; this.pos = pos;
} }
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable; import java.io.Serializable;
public class DrawUpperBuildingCard extends NetworkEvent implements Serializable{ public class DrawUpperBuildingCard extends NetworkEvent implements Serializable{
private EventType eventType;
private int pos; private int pos;
public DrawUpperBuildingCard(String username, int pos){ public DrawUpperBuildingCard(String username, int pos){
this.username = username; super(username, EventType.DRAW_UPPER_BUILD);
this.eventType = EventType.DRAW_UPPER_BUILD;
this.pos = pos; this.pos = pos;
} }
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable; import java.io.Serializable;
public class DrawUpperTribeCard extends NetworkEvent implements Serializable{ public class DrawUpperTribeCard extends NetworkEvent implements Serializable{
private EventType eventType;
private int pos; private int pos;
public DrawUpperTribeCard(String username, int pos){ public DrawUpperTribeCard(String username, int pos){
this.username = username; super(username, EventType.DRAW_UPPER_TRIBE);
this.eventType = EventType.DRAW_UPPER_TRIBE;
this.pos = pos; this.pos = pos;
} }
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable; import java.io.Serializable;
public class PickOptionalBuildingCard extends NetworkEvent implements Serializable{ public class PickOptionalBuildingCard extends NetworkEvent implements Serializable{
private EventType eventType;
private int pos; private int pos;
public PickOptionalBuildingCard(String username, int pos){ public PickOptionalBuildingCard(String username, int pos){
this.username = username; super(username, EventType.PICK_OPTIONAL_BUILD);
this.eventType = EventType.PICK_OPTIONAL_BUILD;
this.pos = pos; this.pos = pos;
} }
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable; import java.io.Serializable;
public class PickOptionalTribeCard extends NetworkEvent implements Serializable{ public class PickOptionalTribeCard extends NetworkEvent implements Serializable{
private EventType eventType;
private int pos; private int pos;
public PickOptionalTribeCard(String username, int pos){ public PickOptionalTribeCard(String username, int pos){
this.username = username; super(username, EventType.PICK_OPTIONAL_TRIBE);
this.eventType = EventType.PICK_OPTIONAL_TRIBE;
this.pos = pos; this.pos = pos;
} }
@@ -8,12 +8,10 @@ import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable; import java.io.Serializable;
public class SlotChoice extends NetworkEvent implements Serializable { public class SlotChoice extends NetworkEvent implements Serializable {
private EventType eventType;
private int pos; private int pos;
public SlotChoice(String username, int pos) { public SlotChoice(String username, int pos) {
this.username = username; super(username, EventType.SLOT_CHOICE);
this.eventType = EventType.SLOT_CHOICE;
this.pos = pos; this.pos = pos;
} }
@@ -3,6 +3,7 @@ package it.polimi.ingsw.gc14.Network.RMI.Client;
import it.polimi.ingsw.gc14.Controller.ClientController; import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
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;
@@ -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;
}
@@ -1,15 +1,12 @@
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 java.rmi.server.UnicastRemoteObject;
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.Server.*; import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
import it.polimi.ingsw.gc14.View.IView;
public class RMIClient implements GameClient { public class RMIClient {
private final String host; private final String host;
private final int port; private final int port;
@@ -20,7 +17,7 @@ public class RMIClient implements GameClient {
this.port = port; this.port = port;
} }
@Override
public boolean connect(String username,ClientController clientController) { public boolean connect(String username,ClientController clientController) {
// 1. Connettiti al registry // 1. Connettiti al registry
try { try {
@@ -49,7 +46,7 @@ public class RMIClient implements GameClient {
} }
@Override
public void doEvent(NetworkEvent event) throws Exception { public void doEvent(NetworkEvent event) throws Exception {
stub.doEvent(event); stub.doEvent(event);
} }
@@ -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.Model.Game;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
@@ -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 it.polimi.ingsw.gc14.Network.NetworkEvent;
import java.rmi.*; import java.rmi.*;
@@ -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;
}
@@ -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<String, IClientCallback> 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);
}
}
@@ -1,28 +1,92 @@
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.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.RemoteException;
import java.rmi.registry.LocateRegistry; 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;
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 Registry registry;
private int nPort; private int nPort;
public RMIServer(RMIGameController controller, int nPort) { private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
// Costruttore
public RMIServer(GameController controller, int nPort) throws RemoteException {
this.controller = controller; this.controller = controller;
this.nPort = nPort; 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 { try {
IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0);
registry = LocateRegistry.createRegistry(nPort); registry = LocateRegistry.createRegistry(nPort);
registry.rebind("RMIGameServer", stub); registry.rebind("RMIGameServer", this);
System.out.println("RMI Server avviato sulla porta "+nPort); System.out.println("RMI Server avviato sulla porta "+nPort);
return true; return true;
} }
@@ -31,11 +95,17 @@ public class RMIServer {
return false; return false;
} }
} }
public boolean stop() {
try {
public void stop() throws Exception {
registry.unbind("RMIGameServer"); registry.unbind("RMIGameServer");
UnicastRemoteObject.unexportObject(controller, true); UnicastRemoteObject.unexportObject(this, true);
System.out.println("RMI Server fermato"); System.out.println("RMI Server fermato");
return true;
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
return false;
} }
} }
}