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
+1 -1
View File
@@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="semeru-25" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -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);
}
}
@@ -0,0 +1,47 @@
package it.polimi.ingsw.gc14.Network;
import java.net.*;
import java.util.Enumeration;
public class InterfaceResolver {
public static String resolveLocalInterface(String serverIp) throws Exception {
if (serverIp.equalsIgnoreCase("localhost") || serverIp.startsWith("127.") || serverIp.isEmpty()) {
return "127.0.0.1";
}
InetAddress serverAddr = InetAddress.getByName(serverIp);
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface ni = ifaces.nextElement();
if (!ni.isUp() || ni.isLoopback()) continue;
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
InetAddress localAddr = ia.getAddress();
if (!(localAddr instanceof Inet4Address)) continue;
int prefix = ia.getNetworkPrefixLength();
if (sameSubnet(localAddr, serverAddr, prefix)) {
return localAddr.getHostAddress();
}
}
}
try (DatagramSocket socket = new DatagramSocket()) {
socket.connect(serverAddr, 9);
return socket.getLocalAddress().getHostAddress();
}
}
private static boolean sameSubnet(InetAddress a, InetAddress b, int prefix) {
if (prefix < 0 || prefix > 32) return false;
int maskBits = prefix == 0 ? 0 : (0xFFFFFFFF << (32 - prefix));
int addrA = toInt(a.getAddress()) & maskBits;
int addrB = toInt(b.getAddress()) & maskBits;
return addrA == addrB;
}
private static int toInt(byte[] bytes) {
return ((bytes[0] & 0xFF) << 24) |
((bytes[1] & 0xFF) << 16) |
((bytes[2] & 0xFF) << 8) |
(bytes[3] & 0xFF);
}
}
@@ -138,10 +138,6 @@ public class GUI extends Application implements IView {
}
});
}
@Override
public void showMessage(String message) {
}
@Override
public void showError(ErrorType error,String message) {
@@ -2,6 +2,7 @@ package it.polimi.ingsw.gc14.View.GUI;
import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.ErrorType;
import it.polimi.ingsw.gc14.Network.InterfaceResolver;
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
import javafx.animation.*;
@@ -100,7 +101,7 @@ public class LoginFXMLController {
new Thread(() -> {
try {
String localInterface = resolveLocalInterface(ip);
String localInterface = InterfaceResolver.resolveLocalInterface(ip);
System.setProperty("java.rmi.server.hostname", localInterface);
connect(nome, ip, numPlayers, localInterface);
} catch (Exception ex) {
@@ -124,48 +125,8 @@ public class LoginFXMLController {
);
}
private String resolveLocalInterface(String serverIp) throws Exception {
if (serverIp.equalsIgnoreCase("localhost") || serverIp.startsWith("127.") || serverIp.isEmpty()) {
return "127.0.0.1";
}
InetAddress serverAddr = InetAddress.getByName(serverIp);
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface ni = ifaces.nextElement();
if (!ni.isUp() || ni.isLoopback()) continue;
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
InetAddress localAddr = ia.getAddress();
if (!(localAddr instanceof Inet4Address)) continue;
int prefix = ia.getNetworkPrefixLength();
if (sameSubnet(localAddr, serverAddr, prefix)) {
return localAddr.getHostAddress();
}
}
}
try (DatagramSocket socket = new DatagramSocket()) {
socket.connect(serverAddr, 9);
return socket.getLocalAddress().getHostAddress();
}
}
private boolean sameSubnet(InetAddress a, InetAddress b, int prefix) {
if (prefix < 0 || prefix > 32) return false;
int maskBits = prefix == 0 ? 0 : (0xFFFFFFFF << (32 - prefix));
int addrA = toInt(a.getAddress()) & maskBits;
int addrB = toInt(b.getAddress()) & maskBits;
return addrA == addrB;
}
private int toInt(byte[] bytes) {
return ((bytes[0] & 0xFF) << 24) |
((bytes[1] & 0xFF) << 16) |
((bytes[2] & 0xFF) << 8) |
(bytes[3] & 0xFF);
}
private void connect(String nome, String ip, int numPlayers, String localInterface) {
if (isRMI) {
@@ -9,8 +9,6 @@ public interface IView {
public void setModel(MiniModel miniModel);
public void render();
void showMessage(String message);
public void showError(ErrorType error,String message);
}
@@ -221,14 +221,6 @@ public class TUI implements IView {
System.out.println(AsciiTable.sideBySide(lines, lines2, 3));
}
/**
* Prints a plain message to standard output.
*
* @param message the message to display
*/
public void showMessage(String message) {
System.out.println(message);
}
/**
* Prints an error message to standard output.