diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index 8ff7bf9..f6f70c1 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -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.View.TUI.TUI; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; import java.util.ArrayList; +import java.util.Enumeration; import java.util.List; import java.util.Scanner; @@ -65,7 +69,13 @@ public class ClientLauncherTUI { // RMI if (networkType == 0) { // 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)) { System.out.println("Succesfully connected to RMI server\n\n"); } else { @@ -159,4 +169,37 @@ public class ClientLauncherTUI { view.showError("ERROR: Invalid input(action not valid)"); } } + + public static String chooseNetworkInterface(Scanner scanner) throws Exception { + List ips = new ArrayList<>(); + + // Lista tutte le interfacce attive con IP reale + Enumeration 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 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); + } } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java index 4bc8a21..141e01e 100644 --- a/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java +++ b/src/main/java/it/polimi/ingsw/gc14/Network/RMI/Client/RMIClient.java @@ -30,6 +30,7 @@ public class RMIClient implements IClient { /** Client game's controller */ ClientController controller; + private String myIP; /** * Class constructor. @@ -37,10 +38,11 @@ public class RMIClient implements IClient { * @param host the host address 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.host = host; this.port = port; + this.myIP = myIP; } @@ -54,8 +56,7 @@ public class RMIClient implements IClient { */ public boolean connect(String username,int preferredInt) { try { - String ip = InetAddress.getLocalHost().getHostAddress(); - System.setProperty("java.rmi.server.hostname", ip); + System.setProperty("java.rmi.server.hostname", this.myIP); Registry registry = LocateRegistry.getRegistry(host, port); this.stub = (IGameServer) registry.lookup("RMIGameServer"); ClientCallbackImpl callback = new ClientCallbackImpl(controller);