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.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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -30,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.
|
||||||
@@ -37,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -54,8 +56,7 @@ public class RMIClient implements IClient {
|
|||||||
*/
|
*/
|
||||||
public boolean connect(String username,int preferredInt) {
|
public boolean connect(String username,int preferredInt) {
|
||||||
try {
|
try {
|
||||||
String ip = InetAddress.getLocalHost().getHostAddress();
|
System.setProperty("java.rmi.server.hostname", this.myIP);
|
||||||
System.setProperty("java.rmi.server.hostname", ip);
|
|
||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user