Fix: ClientLauncher local IP select
This commit is contained in:
@@ -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<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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user