Merge branch 'RMI' into main
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Client;
|
||||
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||
|
||||
import java.rmi.*;
|
||||
|
||||
public interface IClientCallback extends Remote {
|
||||
void onGameInit(Game model) throws RemoteException;
|
||||
void onAction(NetworkEvent action) throws RemoteException;
|
||||
void onError(String message) throws RemoteException;
|
||||
void onGameOver(String winner) throws RemoteException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
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;
|
||||
|
||||
public class RMIClient {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback;
|
||||
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||
|
||||
import java.rmi.*;
|
||||
|
||||
public interface IGameServer extends Remote {
|
||||
// Connessione
|
||||
String joinGame(String username, IClientCallback callback) throws RemoteException;
|
||||
|
||||
// Azioni di gioco — specchio dei tuoi metodi GameController
|
||||
boolean drawUpperTribeCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean drawLowerTribeCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean drawUpperBuildingCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean drawLowerBuildingCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean pickOptionalTribeCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean pickOptionalBuildingCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean slotChoice(String playerUsername, int pos) throws RemoteException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
public interface IRMIServer {
|
||||
void connect(VirtualView client) throws RemoteException;
|
||||
void add(Integer number) throws RemoteException;
|
||||
void reset() throws RemoteException;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback;
|
||||
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||
|
||||
import java.rmi.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.*;
|
||||
|
||||
public class RMIGameController implements IGameServer {
|
||||
|
||||
private final GameController controller; // delega tutto qui
|
||||
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
||||
|
||||
public RMIGameController(GameController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String joinGame(String username, IClientCallback callback) throws RemoteException {
|
||||
controller.addPlayer(username);
|
||||
clients.put(username, callback);
|
||||
callback.onGameInit(controller.getModel());
|
||||
return username;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean drawUpperTribeCard(String username, int pos) throws RemoteException {
|
||||
boolean result = controller.drawUpperTribeCard(username, pos);
|
||||
if (!result) {
|
||||
notifyError(username, "Mossa non valida");
|
||||
} else {
|
||||
notifyAll(new NetworkEvent(Action.ActionType.DRAW_UPPER_TRIBE, username, pos));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void notifyAll(Action action) throws RemoteException {
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
cb.onAction(action);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyError(String username, String message) throws RemoteException {
|
||||
IClientCallback cb = clients.get(username);
|
||||
if (cb != null) cb.onError(message);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
public class RMIServer {
|
||||
|
||||
private final RMIGameController controller;
|
||||
private Registry registry;
|
||||
|
||||
public RMIServer(RMIGameController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0);
|
||||
registry = LocateRegistry.createRegistry(1099);
|
||||
registry.rebind("GameServer", stub);
|
||||
System.out.println("RMI Server avviato sulla porta 1099");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
registry.unbind("GameServer");
|
||||
UnicastRemoteObject.unexportObject(controller, true);
|
||||
System.out.println("RMI Server fermato");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user