Merge pull request #80 from rubenpirreram/FIX_RMI

Fix rmi
This commit is contained in:
rubenpirreram
2026-05-06 14:49:27 +02:00
committed by GitHub
8 changed files with 532 additions and 38 deletions
@@ -5,7 +5,11 @@ import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient; import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
import it.polimi.ingsw.gc14.View.TUI.TUI; import it.polimi.ingsw.gc14.View.TUI.TUI;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
@@ -65,7 +69,13 @@ public class ClientLauncherTUI {
// RMI // RMI
if (networkType == 0) { if (networkType == 0) {
// Connect // Connect
RMIClient client = new RMIClient(controller, IP, 1099); String myIP;
try {
myIP = chooseNetworkInterface(scanner);
} catch (Exception e) {
throw new RuntimeException(e);
}
RMIClient client = new RMIClient(controller, IP, 1099, myIP);
if (client.connect(username, proposedNumPlayers)) { if (client.connect(username, proposedNumPlayers)) {
System.out.println("Succesfully connected to RMI server\n\n"); System.out.println("Succesfully connected to RMI server\n\n");
} else { } else {
@@ -159,4 +169,37 @@ public class ClientLauncherTUI {
view.showError("ERROR: Invalid input(action not valid)"); view.showError("ERROR: Invalid input(action not valid)");
} }
} }
public static String chooseNetworkInterface(Scanner scanner) throws Exception {
List<String> ips = new ArrayList<>();
// Lista tutte le interfacce attive con IP reale
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
// Salta loopback, interfacce spente o virtuali
if (!ni.isUp() || ni.isLoopback() || ni.isVirtual()) continue;
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
// Solo IPv4
if (addr instanceof Inet4Address) {
System.out.println("[" + ips.size() + "] " + ni.getDisplayName() + " -> " + addr.getHostAddress());
ips.add(addr.getHostAddress());
}
}
}
if (ips.isEmpty()) throw new Exception("Nessuna interfaccia disponibile");
if (ips.size() == 1) {
System.out.println("Una sola interfaccia trovata, uso: " + ips.get(0));
return ips.get(0);
}
System.out.print("Scegli interfaccia: ");
int choice = scanner.nextInt();
return ips.get(choice);
}
} }
@@ -5,6 +5,7 @@ import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvents.*; import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
import it.polimi.ingsw.gc14.View.IView; import it.polimi.ingsw.gc14.View.IView;
import java.rmi.RemoteException;
import java.util.Objects; import java.util.Objects;
/** /**
@@ -77,7 +78,11 @@ public class ClientController {
} }
else else
{ {
client.doEvent(new DrawUpperTribeCard(playerUsername,pos)); try {
client.drawUpperTribeCard(playerUsername, pos);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
} }
} }
@@ -92,7 +97,11 @@ public class ClientController {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else
client.doEvent(new DrawLowerTribeCard(playerUsername,pos)); try {
client.drawLowerTribeCard(playerUsername, pos);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
} }
@@ -105,8 +114,13 @@ public class ClientController {
public void drawUpperBuildingCard(String playerUsername,int pos) { public void drawUpperBuildingCard(String playerUsername,int pos) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else {
client.doEvent(new DrawUpperBuildingCard(playerUsername,pos)); try {
client.drawUpperBuildingCard(playerUsername,pos);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
} }
@@ -120,8 +134,13 @@ public class ClientController {
public void drawLowerBuildingCard(String playerUsername,int pos) { public void drawLowerBuildingCard(String playerUsername,int pos) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else {
client.doEvent(new DrawLowerBuildingCard(playerUsername,pos)); try {
client.drawLowerBuildingCard(playerUsername,pos);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
} }
@@ -133,8 +152,13 @@ public class ClientController {
public void skipUpper(String playerUsername) { public void skipUpper(String playerUsername) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else {
client.doEvent(new SkipUpper(playerUsername)); try {
client.skipUpper(playerUsername);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
} }
@@ -146,8 +170,14 @@ public class ClientController {
public void skipLower(String playerUsername) { public void skipLower(String playerUsername) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else {
client.doEvent(new SkipLower(playerUsername));} try {
client.skipLower(playerUsername);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
}
/** /**
@@ -159,8 +189,13 @@ public class ClientController {
public void pickOptionalTribeCard(String playerUsername,int pos) { public void pickOptionalTribeCard(String playerUsername,int pos) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else {
client.doEvent(new PickOptionalTribeCard(playerUsername,pos)); try {
client.pickOptionalTribeCard(playerUsername,pos);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
} }
@@ -173,8 +208,13 @@ public class ClientController {
public void pickOptionalBuildingCard(String playerUsername,int pos) { public void pickOptionalBuildingCard(String playerUsername,int pos) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else {
client.doEvent(new PickOptionalBuildingCard(playerUsername,pos)); try {
client.pickOptionalBuildingCard(playerUsername,pos);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
} }
@@ -186,8 +226,13 @@ public class ClientController {
public void noOptionalCard(String playerUsername) { public void noOptionalCard(String playerUsername) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else {
client.doEvent(new NoOptionalCard(playerUsername)); try {
client.noOptionalCard(playerUsername);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
} }
@@ -199,8 +244,13 @@ public class ClientController {
public void slotChoice(String playerUsername,int pos) { public void slotChoice(String playerUsername,int pos) {
if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName())) if(!Objects.equals(playerUsername, localController.getModel().getCurrentState().getCurrentPlayer().getUserName()))
view.showError("It's not your turn!"); view.showError("It's not your turn!");
else else {
client.doEvent(new SlotChoice(playerUsername,pos)); try {
client.slotChoice(playerUsername,pos);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
} }
} }
@@ -1,8 +1,33 @@
package it.polimi.ingsw.gc14.Network; package it.polimi.ingsw.gc14.Network;
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.Objects;
public interface IClient { public interface IClient {
public boolean connect(String username,int preferredInt); public boolean connect(String username,int preferredInt);
public void doEvent(NetworkEvent event) ;
public void drawUpperTribeCard(String playerUsername, int pos) throws RemoteException;
public void drawLowerTribeCard(String playerUsername,int pos) throws RemoteException;
public void drawUpperBuildingCard(String playerUsername,int pos) throws RemoteException;
public void drawLowerBuildingCard(String playerUsername,int pos) throws RemoteException;
public void skipUpper(String playerUsername) throws RemoteException;
public void skipLower(String playerUsername) throws RemoteException;
public void pickOptionalTribeCard(String playerUsername,int pos) throws RemoteException;
public void pickOptionalBuildingCard(String playerUsername,int pos) throws RemoteException;
public void noOptionalCard(String playerUsername) throws RemoteException;
public void slotChoice(String playerUsername,int pos) throws RemoteException;
} }
@@ -1,11 +1,14 @@
package it.polimi.ingsw.gc14.Network.RMI.Client; package it.polimi.ingsw.gc14.Network.RMI.Client;
import java.net.InetAddress;
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.util.Objects;
import it.polimi.ingsw.gc14.Controller.ClientController; import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Network.IClient; import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer; import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer; import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
@@ -27,6 +30,7 @@ public class RMIClient implements IClient {
/** Client game's controller */ /** Client game's controller */
ClientController controller; ClientController controller;
private String myIP;
/** /**
* Class constructor. * Class constructor.
@@ -34,10 +38,11 @@ public class RMIClient implements IClient {
* @param host the host address of the RMI server * @param host the host address of the RMI server
* @param port the port of the RMI server * @param port the port of the RMI server
*/ */
public RMIClient(ClientController controller, String host, int port) { public RMIClient(ClientController controller, String host, int port, String myIP) {
this.controller=controller; this.controller=controller;
this.host = host; this.host = host;
this.port = port; this.port = port;
this.myIP = myIP;
} }
@@ -51,6 +56,7 @@ public class RMIClient implements IClient {
*/ */
public boolean connect(String username,int preferredInt) { public boolean connect(String username,int preferredInt) {
try { try {
System.setProperty("java.rmi.server.hostname", this.myIP);
Registry registry = LocateRegistry.getRegistry(host, port); Registry registry = LocateRegistry.getRegistry(host, port);
this.stub = (IGameServer) registry.lookup("RMIGameServer"); this.stub = (IGameServer) registry.lookup("RMIGameServer");
ClientCallbackImpl callback = new ClientCallbackImpl(controller); ClientCallbackImpl callback = new ClientCallbackImpl(controller);
@@ -64,17 +70,111 @@ public class RMIClient implements IClient {
} }
/**
* Sends a {@link NetworkEvent} to the server.
* @param event the event to send
* @throws RemoteException if any RMI error occurs
*/
public void doEvent(NetworkEvent event) {
try {
stub.doEvent(event);
}catch (Exception e) {
/**
* Requests to draw a tribe card from the upper list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawUpperTribeCard(String playerUsername, int pos) throws RemoteException {
stub.drawUpperTribeCard(playerUsername,pos);
} }
/**
* Requests to draw a tribe card from the lower list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawLowerTribeCard(String playerUsername,int pos) throws RemoteException {
stub.drawLowerTribeCard(playerUsername,pos);
} }
/**
* Requests to draw a building card from the upper list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawUpperBuildingCard(String playerUsername,int pos) throws RemoteException {
stub.drawUpperBuildingCard(playerUsername,pos);
}
/**
* Requests to draw a building card from the lower list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawLowerBuildingCard(String playerUsername,int pos) throws RemoteException {
stub.drawLowerBuildingCard(playerUsername,pos);
}
/**
* Requests to skip drawing from the upper list.
* This action is available only when the upper list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipUpper(String playerUsername) throws RemoteException {
stub.skipUpper(playerUsername);
}
/**
* Requests to skip drawing from the lower list.
* This action is available only when the lower list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipLower(String playerUsername) throws RemoteException {
stub.skipLower(playerUsername);
}
/**
* Used to draw a tribe card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalTribeCard(String playerUsername,int pos) throws RemoteException {
stub.pickOptionalTribeCard(playerUsername,pos);
}
/**
* Used to draw a building card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalBuildingCard(String playerUsername,int pos) throws RemoteException {
stub.pickOptionalBuildingCard(playerUsername,pos);
}
/**
* Used to skip the action of drawing a card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
*/
public void noOptionalCard(String playerUsername) throws RemoteException {
stub.noOptionalCard(playerUsername);
}
/**
* Used to perform the slot choice action for the specified player at the specified position.
* @param playerUsername the name of the player performing the action
* @param pos the index of the selected slot
*/
public void slotChoice(String playerUsername,int pos) throws RemoteException {
stub.slotChoice(playerUsername,pos);
}
} }
@@ -8,5 +8,27 @@ public interface IGameServer extends Remote {
boolean joinGame(String username,int preferredInt, 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;
void drawUpperTribeCard(String playerUsername, int pos) throws RemoteException;
void drawLowerTribeCard(String playerUsername,int pos) throws RemoteException;
void drawUpperBuildingCard(String playerUsername,int pos) throws RemoteException;
void drawLowerBuildingCard(String playerUsername,int pos) throws RemoteException;
void skipUpper(String playerUsername) throws RemoteException;
void skipLower(String playerUsername) throws RemoteException;
void pickOptionalTribeCard(String playerUsername,int pos) throws RemoteException;
void pickOptionalBuildingCard(String playerUsername,int pos) throws RemoteException;
void noOptionalCard(String playerUsername) throws RemoteException;
void slotChoice(String playerUsername,int pos) throws RemoteException;
} }
@@ -4,14 +4,18 @@ import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.LimitedList; 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.NetworkEvents.*;
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback; import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer; import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
import java.net.InetAddress;
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.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@@ -22,7 +26,7 @@ import java.rmi.*;
* Server RMI. Exposes a method to join the game and one to execute an event. * Server RMI. Exposes a method to join the game and one to execute an event.
*/ */
public class RMIServer extends UnicastRemoteObject implements IGameServer { public class RMIServer extends UnicastRemoteObject implements IGameServer {
private String host;
/** Server game's controller */ /** Server game's controller */
private GameController controller; private GameController controller;
@@ -56,11 +60,12 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* @param playerList The player's usernames list * @param playerList The player's usernames list
* @throws RemoteException if an RMI error occurs * @throws RemoteException if an RMI error occurs
*/ */
public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue,LimitedList<String> playerList) throws RemoteException { public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue,LimitedList<String> playerList,String host) throws RemoteException {
this.controller = controller; this.controller = controller;
this.nPort = nPort; this.nPort = nPort;
this.actionQueue = actionQueue; this.actionQueue = actionQueue;
this.playerList = playerList; this.playerList = playerList;
this.host = host;
} }
@@ -76,7 +81,6 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* @param callback The client's callback interface * @param callback The client's callback interface
* @return true if the player successfully joined the game, false otherwise * @return true if the player successfully joined the game, false otherwise
*/ */
@Override
public boolean joinGame(String username, int preferredInt, IClientCallback callback) { public boolean joinGame(String username, int preferredInt, IClientCallback callback) {
if (preferredInt<2 || preferredInt>5) { if (preferredInt<2 || preferredInt>5) {
return false; return false;
@@ -104,11 +108,114 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* @param action The desired actio * @param action The desired actio
* @return true if the action was successfully added, false otherwise * @return true if the action was successfully added, false otherwise
*/ */
@Override
public boolean doEvent(NetworkEvent action) { public boolean doEvent(NetworkEvent action) {
return actionQueue.offer(action); return actionQueue.offer(action);
} }
/**
* Requests to draw a tribe card from the upper list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawUpperTribeCard(String playerUsername, int pos) {
actionQueue.offer(new DrawUpperTribeCard(playerUsername,pos));
}
/**
* Requests to draw a tribe card from the lower list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawLowerTribeCard(String playerUsername,int pos) {
actionQueue.offer(new DrawLowerTribeCard(playerUsername,pos));
}
/**
* Requests to draw a building card from the upper list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawUpperBuildingCard(String playerUsername,int pos) {
actionQueue.offer(new DrawUpperBuildingCard(playerUsername,pos));
}
/**
* Requests to draw a building card from the lower list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawLowerBuildingCard(String playerUsername,int pos) {
actionQueue.offer(new DrawLowerBuildingCard(playerUsername,pos));
}
/**
* Requests to skip drawing from the upper list.
* This action is available only when the upper list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipUpper(String playerUsername) {
actionQueue.offer(new SkipUpper(playerUsername));
}
/**
* Requests to skip drawing from the lower list.
* This action is available only when the lower list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipLower(String playerUsername) {
actionQueue.offer(new SkipLower(playerUsername));
}
/**
* Used to draw a tribe card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalTribeCard(String playerUsername,int pos) {
actionQueue.offer(new PickOptionalTribeCard(playerUsername,pos));
}
/**
* Used to draw a building card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalBuildingCard(String playerUsername,int pos) {
actionQueue.offer(new PickOptionalBuildingCard(playerUsername,pos));
}
/**
* Used to skip the action of drawing a card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
*/
public void noOptionalCard(String playerUsername) {
actionQueue.offer(new NoOptionalCard(playerUsername));
}
/**
* Used to perform the slot choice action for the specified player at the specified position.
* @param playerUsername the name of the player performing the action
* @param pos the index of the selected slot
*/
public void slotChoice(String playerUsername,int pos) {
actionQueue.offer(new SlotChoice(playerUsername,pos));
}
// RMI's internal methods // RMI's internal methods
@@ -143,6 +250,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
*/ */
public boolean start() { public boolean start() {
try { try {
System.setProperty("java.rmi.server.hostname", host); // o il tuo IP/hostname
registry = LocateRegistry.createRegistry(nPort); registry = LocateRegistry.createRegistry(nPort);
registry.rebind("RMIGameServer", this); registry.rebind("RMIGameServer", this);
System.out.println("RMI Server started on port: "+nPort); System.out.println("RMI Server started on port: "+nPort);
@@ -4,7 +4,7 @@ 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.IClient; import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer; import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
@@ -113,12 +113,117 @@ public class TCPClient implements IClient {
} }
} }
/**
* Requests to draw a tribe card from the upper list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawUpperTribeCard(String playerUsername, int pos) {
doEvent(new DrawUpperTribeCard(playerUsername,pos));
}
/**
* Requests to draw a tribe card from the lower list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawLowerTribeCard(String playerUsername,int pos) {
doEvent(new DrawLowerTribeCard(playerUsername,pos));
}
/**
* Requests to draw a building card from the upper list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawUpperBuildingCard(String playerUsername,int pos) {
doEvent(new DrawUpperBuildingCard(playerUsername,pos));
}
/**
* Requests to draw a building card from the lower list.
* Creates a NetworkEvent and sends it through the network client.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void drawLowerBuildingCard(String playerUsername,int pos) {
doEvent(new DrawLowerBuildingCard(playerUsername,pos));
}
/**
* Requests to skip drawing from the upper list.
* This action is available only when the upper list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipUpper(String playerUsername) {
doEvent(new SkipUpper(playerUsername));
}
/**
* Requests to skip drawing from the lower list.
* This action is available only when the lower list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipLower(String playerUsername) {
doEvent(new SkipLower(playerUsername));
}
/**
* Used to draw a tribe card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalTribeCard(String playerUsername,int pos) {
doEvent(new PickOptionalTribeCard(playerUsername,pos));
}
/**
* Used to draw a building card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalBuildingCard(String playerUsername,int pos) {
doEvent(new PickOptionalBuildingCard(playerUsername,pos));
}
/**
* Used to skip the action of drawing a card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
*/
public void noOptionalCard(String playerUsername) {
doEvent(new NoOptionalCard(playerUsername));
}
/**
* Used to perform the slot choice action for the specified player at the specified position.
* @param playerUsername the name of the player performing the action
* @param pos the index of the selected slot
*/
public void slotChoice(String playerUsername,int pos) {
doEvent(new SlotChoice(playerUsername,pos));
}
/** /**
* Sends a {@link NetworkEvent} to the server. * Sends a {@link NetworkEvent} to the server.
* @param event The NetworkEvent to send. * @param event The NetworkEvent to send.
*/ */
public void doEvent(NetworkEvent event) { private void doEvent(NetworkEvent event) {
try { try {
socketSend.writeObject(event); socketSend.writeObject(event);
} catch (IOException e) { } catch (IOException e) {
@@ -10,8 +10,10 @@ import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
import it.polimi.ingsw.gc14.View.TUI.TUI; import it.polimi.ingsw.gc14.View.TUI.TUI;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.*;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.net.*;
/** /**
@@ -107,7 +109,14 @@ public class ServerLauncher {
playerList = new LimitedList<>(5, ()->{}); playerList = new LimitedList<>(5, ()->{});
BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>(); BlockingQueue<NetworkEvent> actionQueue = new LinkedBlockingQueue<>();
GameController gameController = new GameController(); GameController gameController = new GameController();
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, playerList); String IP;
try {
IP=chooseNetworkInterface(new Scanner(System.in));
} catch (Exception e) {
throw new RuntimeException(e);
}
RMIServer serverRMI = new RMIServer(gameController, 1099, actionQueue, playerList,IP);
TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, playerList); TCPServer serverTCP = new TCPServer(gameController, 8080, actionQueue, playerList);
ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP); ServerLauncher launcher = new ServerLauncher(actionQueue, gameController, serverRMI, serverTCP);
@@ -156,4 +165,36 @@ public class ServerLauncher {
} }
} }
} }
public static String chooseNetworkInterface(Scanner scanner) throws Exception {
List<String> ips = new ArrayList<>();
// Lista tutte le interfacce attive con IP reale
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
// Salta loopback, interfacce spente o virtuali
if (!ni.isUp() || ni.isLoopback() || ni.isVirtual()) continue;
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
// Solo IPv4
if (addr instanceof Inet4Address) {
System.out.println("[" + ips.size() + "] " + ni.getDisplayName() + " -> " + addr.getHostAddress());
ips.add(addr.getHostAddress());
}
}
}
if (ips.isEmpty()) throw new Exception("Nessuna interfaccia disponibile");
if (ips.size() == 1) {
System.out.println("Una sola interfaccia trovata, uso: " + ips.get(0));
return ips.get(0);
}
System.out.print("Scegli interfaccia: ");
int choice = Integer.parseInt(scanner.nextLine().trim());
return ips.get(choice);
}
} }