Add: network interface selection GUI
This commit is contained in:
@@ -32,7 +32,7 @@ public class ClientLauncherGUI extends Application {
|
||||
// Connessione in un thread separato — non bloccare il JavaFX thread!
|
||||
new Thread(() -> {
|
||||
if (networkType == 0) {
|
||||
RMIClient client = new RMIClient(controller, ip, 1099, "localhost");
|
||||
RMIClient client = new RMIClient(controller, ip, 1099, loginView.getInterfaccia());
|
||||
if (client.connect(nome, numPlayer)) {
|
||||
controller.setClient(client);
|
||||
// Da qui in poi il server chiamerà onGameInit → render()
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
package it.polimi.ingsw.gc14.View.GUI;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
public class LoginFXMLController {
|
||||
|
||||
@FXML private TextField campoNome;
|
||||
@@ -12,14 +20,38 @@ public class LoginFXMLController {
|
||||
@FXML private RadioButton btnTCP;
|
||||
@FXML private Button btnAccedi;
|
||||
@FXML private Label labelErrore;
|
||||
@FXML private ComboBox<String> comboInterfaccia;
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
// I RadioButton vanno collegati a un ToggleGroup a mano
|
||||
// perché FXML non lo fa automaticamente
|
||||
ToggleGroup group = new ToggleGroup();
|
||||
btnRMI.setToggleGroup(group);
|
||||
btnTCP.setToggleGroup(group);
|
||||
|
||||
// Carica le interfacce in background
|
||||
new Thread(() -> {
|
||||
try {
|
||||
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) {
|
||||
ips.add(ni.getDisplayName() + " -> " + addr.getHostAddress());
|
||||
}
|
||||
}
|
||||
}
|
||||
Platform.runLater(() -> {
|
||||
comboInterfaccia.getItems().addAll(ips);
|
||||
if (!ips.isEmpty()) comboInterfaccia.getSelectionModel().selectFirst();
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Platform.runLater(() -> setErrore("Errore nel rilevare le interfacce."));
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
// --- Getter esposti a LoginView ---
|
||||
@@ -40,5 +72,11 @@ public class LoginFXMLController {
|
||||
|
||||
public String getIP() { return campoIP.getText().trim(); }
|
||||
|
||||
public String getInterfaccia() {
|
||||
String selected = comboInterfaccia.getValue();
|
||||
if (selected == null) return "";
|
||||
return selected.contains("->") ? selected.split("->")[1].trim() : selected;
|
||||
}
|
||||
|
||||
public void setErrore(String messaggio) { labelErrore.setText(messaggio); }
|
||||
}
|
||||
@@ -31,5 +31,6 @@ public class LoginView {
|
||||
public int getNumPlayers() { return fxmlController.getNumPlayers(); }
|
||||
public int getNetworkType() { return fxmlController.getNetworkType(); }
|
||||
public String getIP() { return fxmlController.getIP(); }
|
||||
public String getInterfaccia() {return fxmlController.getInterfaccia();}
|
||||
public void setErrore(String messaggio) { fxmlController.setErrore(messaggio); }
|
||||
}
|
||||
@@ -22,6 +22,9 @@
|
||||
<Label text="IP server:"/>
|
||||
<TextField fx:id="campoIP" promptText="IP server"/>
|
||||
|
||||
<Label text="Interfaccia di rete:"/>
|
||||
<ComboBox fx:id="comboInterfaccia" maxWidth="Infinity"/>
|
||||
|
||||
<Label fx:id="labelErrore" style="-fx-text-fill: red;"/>
|
||||
<Button fx:id="btnAccedi" text="Connetti"/>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user