This commit is contained in:
GabrieleRadice
2026-04-24 18:37:18 +02:00
6 changed files with 108 additions and 22 deletions
@@ -0,0 +1,28 @@
package it.polimi.ingsw.gc14;
import java.util.Scanner;
public class ClientLauncher {
public void main() {
Scanner scanner = new Scanner(System.in);
System.out.println("Selezionare nome utente: ");
String username = scanner.next();
System.out.println(username);
System.out.println("Selezionare numero di giocatori desiderato: ");
int proposedNumPlayers = scanner.nextInt();
System.out.println(proposedNumPlayers);
System.out.println("Selezionare RMI[0] o TCP[1]: ");
int networkType = scanner.nextInt();
System.out.println(networkType);
scanner.close(); // chiudi solo alla fine
if (networkType == 0) {
return;
} else if (networkType == 1) {
return;
}
}
}
@@ -0,0 +1,28 @@
package it.polimi.ingsw.gc14;
import java.util.ArrayList;
public class LimitedList<T> extends ArrayList<T> {
private int limit;
private final Runnable action;
public LimitedList(int limit, Runnable action) {
this.limit = limit;
this.action = action;
}
@Override
public boolean add(T element) {
boolean result = super.add(element);
if (size() >= limit) {
action.run();
}
return result;
}
public void setLimit(int num) {
this.limit=num;
}
public int getLimit(){return limit;}
}
@@ -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
try {
Registry registry = LocateRegistry.getRegistry(host, port);
@@ -29,7 +29,7 @@ public class RMIClient {
// 3. Crea il callback e registralo
ClientCallbackImpl callback = new ClientCallbackImpl(clientController);
if (!stub.joinGame(username, callback))
if (!stub.joinGame(username,preferredInt, callback))
{
stub = null;
return false;
@@ -6,7 +6,7 @@ import java.rmi.*;
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;
}
@@ -1,6 +1,7 @@
package it.polimi.ingsw.gc14.Network.RMI.Server;
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.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
@@ -24,31 +25,43 @@ public class RMIServer implements IGameServer {
private int nPort;
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
BlockingQueue<NetworkEvent> actionQueue;
private LimitedList<String> playerList;
// 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.nPort = nPort;
this.actionQueue = actionQueue;
this.playerList = playerList;
}
// 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;
public boolean joinGame(String username, int preferredInt,IClientCallback callback) {
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.setLimit(preferredInt);
}
playerList.add(username);
clients.put(username, callback);
return true;
}
return false;
}
return false;
}
@Override
public boolean doEvent(NetworkEvent event) throws RemoteException {
@@ -4,6 +4,7 @@ package it.polimi.ingsw.gc14;
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.NetworkEvents.AddPlayer;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
@@ -16,6 +17,7 @@ public class ServerLauncher {
GameController gameController;
RMIServer serverRMI;
TCPServer serverTCP;
static LimitedList<String> players;
@@ -24,9 +26,18 @@ public class ServerLauncher {
this.serverRMI = serverRMI;
this.gameController = gameController;
this.serverTCP = serverTCP;
this.players = new LimitedList<>(5, ()->{
try {
run();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
});
}
public boolean doFirstEvent() throws InterruptedException, RemoteException {
public boolean doFirstEven() throws InterruptedException, RemoteException {
NetworkEvent event = actionQueue.take();
if(event.apply(gameController)) {
serverRMI.notifyAll(event);
@@ -40,24 +51,30 @@ public class ServerLauncher {
}
public static void main() throws RemoteException {
BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
Game model = new Game();
GameController gameController = new GameController(model);
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue);
TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue);
public static void main() throws RemoteException, InterruptedException {
// TODO
// Deve avere una coda con gli eventi.
// Il server RMI e il server TCP quando ricevono un doEvent devono aggiungere l'evento alla coda condivisa
// Questa classe esegue gli eventi nella coda e chiama il notify all e notify error sia RMI che TCP
serverRMI.start();
new Thread (()->{serverTCP.start();}).start();
BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
GameController gameController = new GameController();
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, players);
TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, players);
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
launcher.run();
}
public void run() {
public void run() throws InterruptedException, RemoteException {
serverRMI.notifyAll(gameController.getModel());
//serverTCP.broadcastUpdate(model);
while (true) {
try {
this.doFirstEvent();
this.doFirstEven();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;