diff --git a/.idea/misc.xml b/.idea/misc.xml index 9facaf9..0c04b52 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java index 1227f18..fb3a65f 100644 --- a/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/ClientLauncherTUI.java @@ -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. - * - *

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 ips = new ArrayList<>(); - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - while (interfaces.hasMoreElements()) { - NetworkInterface ni = interfaces.nextElement(); - - if (!ni.isUp() || ni.isLoopback() || ni.isVirtual()) continue; - - Enumeration 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); - } } \ No newline at end of file diff --git a/src/main/java/it/polimi/ingsw/gc14/Network/InterfaceResolver.java b/src/main/java/it/polimi/ingsw/gc14/Network/InterfaceResolver.java new file mode 100644 index 0000000..f8f8226 --- /dev/null +++ b/src/main/java/it/polimi/ingsw/gc14/Network/InterfaceResolver.java @@ -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 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); + } +} diff --git a/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java b/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java index ac421ff..096d286 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/GUI/GUI.java @@ -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) { diff --git a/src/main/java/it/polimi/ingsw/gc14/View/GUI/LoginFXMLController.java b/src/main/java/it/polimi/ingsw/gc14/View/GUI/LoginFXMLController.java index 77b35b3..f398d12 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/GUI/LoginFXMLController.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/GUI/LoginFXMLController.java @@ -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 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) { diff --git a/src/main/java/it/polimi/ingsw/gc14/View/IView.java b/src/main/java/it/polimi/ingsw/gc14/View/IView.java index d6bb9b0..41ccd2b 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/IView.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/IView.java @@ -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); } diff --git a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java index 1b49090..3bc0d73 100644 --- a/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java +++ b/src/main/java/it/polimi/ingsw/gc14/View/TUI/TUI.java @@ -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.