Fixed: RMIServer and RMIClient

This commit is contained in:
rubenpirreram
2026-04-24 18:31:04 +02:00
parent b413ff3bcf
commit 7833802187
3 changed files with 24 additions and 11 deletions
@@ -18,7 +18,7 @@ public class RMIClient {
} }
public boolean connect(String username,ClientController clientController) { public boolean connect(String username,int preferredInt,ClientController clientController) {
// 1. Connettiti al registry // 1. Connettiti al registry
try { try {
Registry registry = LocateRegistry.getRegistry(host, port); Registry registry = LocateRegistry.getRegistry(host, port);
@@ -29,7 +29,7 @@ public class RMIClient {
// 3. Crea il callback e registralo // 3. Crea il callback e registralo
ClientCallbackImpl callback = new ClientCallbackImpl(clientController); ClientCallbackImpl callback = new ClientCallbackImpl(clientController);
if (!stub.joinGame(username, callback)) if (!stub.joinGame(username,preferredInt, callback))
{ {
stub = null; stub = null;
return false; return false;
@@ -6,7 +6,7 @@ import java.rmi.*;
public interface IGameServer extends Remote { public interface IGameServer extends Remote {
boolean joinGame(String username, IClientCallback callback) throws RemoteException; boolean joinGame(String username,int preferredInt, IClientCallback callback) throws RemoteException;
boolean doEvent(NetworkEvent event) throws RemoteException; boolean doEvent(NetworkEvent event) throws RemoteException;
} }
@@ -1,6 +1,7 @@
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.LimitedList;
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 it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
@@ -24,32 +25,44 @@ public class RMIServer implements IGameServer {
private int nPort; private int nPort;
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>(); private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
BlockingQueue<NetworkEvent> actionQueue; BlockingQueue<NetworkEvent> actionQueue;
private LimitedList<String> playerList;
// Costruttore // Costruttore
public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue) 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;
this.actionQueue = actionQueue; this.actionQueue = actionQueue;
this.playerList = playerList;
} }
// Metodi esposti RMI // Metodi esposti RMI
public void setController (GameController controller) { public void setController (GameController controller) {
this.controller = controller; this.controller = controller;
} }
@Override @Override
public boolean joinGame(String username, IClientCallback callback) { public boolean joinGame(String username, int preferredInt,IClientCallback callback) {
if(controller.addPlayer(username)) synchronized (controller) {
if(playerList.size()==0 && (preferredInt<2||preferredInt>5))
return false;
if (controller.addPlayer(username)) {
if(playerList.size()==0)
{ {
Game game= new Game(preferredInt);
controller.setModel(game);
playerList.setElement(preferredInt);
}
playerList.add(username);
clients.put(username, callback); clients.put(username, callback);
return true; return true;
} }
return false; return false;
} }
}
@Override @Override
public boolean doEvent(NetworkEvent event) throws RemoteException { public boolean doEvent(NetworkEvent event) throws RemoteException {
return actionQueue.offer(event); return actionQueue.offer(event);