Fixed: Resolver IP fixed, deleted show error

This commit is contained in:
rubenpirreram
2026-06-09 16:05:41 +02:00
parent 839aacb0a1
commit 129747c9c5
7 changed files with 55 additions and 101 deletions
@@ -2,14 +2,13 @@ package it.polimi.ingsw.gc14;
import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character;
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
import it.polimi.ingsw.gc14.Network.InterfaceResolver;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
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.net.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
@@ -74,7 +73,7 @@ public class ClientLauncherTUI {
// Connect
String myIP;
try {
myIP = chooseNetworkInterface(scanner);
myIP = InterfaceResolver.resolveLocalInterface(IP);
} catch (Exception e) {
throw new RuntimeException(e);
}
@@ -175,7 +174,7 @@ public class ClientLauncherTUI {
System.out.println("Insert the required position:");
pos = scanner.nextInt();
} catch (Exception e) {
view.showMessage("ERROR: Invalid input(expected number)");
view.showError(ErrorType.GENERIC_ERROR,"Invalid input(expected number)");
return;
}
}
@@ -202,48 +201,9 @@ public class ClientLauncherTUI {
}
else
{
view.showMessage("ERROR: Invalid input(action not valid)");
view.showError(ErrorType.GENERIC_ERROR,"Invalid input(expected number)");
}
}
/**
* Lets the user choose one of the available active IPv4 network interfaces.
*
* <p>The method lists all active, non-loopback and non-virtual network
* interfaces with an IPv4 address. If only one interface is available, it is
* selected automatically.
*
* @param scanner the scanner used to read the user's choice.
* @return the selected IPv4 address as a string.
* @throws Exception if no valid network interface is available.
*/
public static String chooseNetworkInterface(Scanner scanner) throws Exception {
List<String> ips = new ArrayList<>();
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
if (!ni.isUp() || ni.isLoopback() || ni.isVirtual()) continue;
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if (addr instanceof Inet4Address) {
System.out.println("[" + ips.size() + "] " + ni.getDisplayName() + " -> " + addr.getHostAddress());
ips.add(addr.getHostAddress());
}
}
}
if (ips.isEmpty()) throw new Exception("No interface available");
if (ips.size() == 1) {
System.out.println("Only one interface found, used: " + ips.get(0));
return ips.get(0);
}
System.out.print("Choose the interface: ");
int choice = scanner.nextInt();
return ips.get(choice);
}
}