Skip to content
Snippets Groups Projects
Unverified Commit 8b79cd4c authored by David Bohn's avatar David Bohn
Browse files

Made network interface for remote selectable

parent 961b2e54
No related branches found
No related tags found
1 merge request!3Preliminary result of the software
...@@ -3,22 +3,28 @@ package fucoin; ...@@ -3,22 +3,28 @@ package fucoin;
import akka.actor.ActorRef; import akka.actor.ActorRef;
import akka.actor.ActorSelection; import akka.actor.ActorSelection;
import akka.actor.ActorSystem; import akka.actor.ActorSystem;
import akka.japi.Pair;
import akka.util.Timeout; import akka.util.Timeout;
import com.typesafe.config.Config; import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory; import com.typesafe.config.ConfigFactory;
import fucoin.setup.SelectableNetworkInterface;
import fucoin.wallet.WalletImpl; import fucoin.wallet.WalletImpl;
import scala.concurrent.Await; import scala.concurrent.Await;
import scala.concurrent.duration.FiniteDuration;
import javax.swing.*; import javax.swing.*;
import java.awt.*;
import java.io.File; import java.io.File;
import java.net.InetAddress; import java.net.*;
import java.net.UnknownHostException; import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class MainRemote { public class MainRemote {
private static JTextField walletNameField = new JTextField(5); private static JTextField walletNameField = new JTextField(10);
private static JTextField pathField = new JTextField(5); private static JTextField pathField = new JTextField(10);
private static JComboBox<SelectableNetworkInterface> hostnameField = new JComboBox<>();
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
...@@ -43,7 +49,7 @@ public class MainRemote { ...@@ -43,7 +49,7 @@ public class MainRemote {
//Init System Actor System //Init System Actor System
ActorSystem system = ActorSystem.create("Remote", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config)); ActorSystem system = ActorSystem.create("Remote", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config));
JPanel dialogPanel = createDialogPanel(); JPanel dialogPanel = createDialogPanel(hostname);
Timeout timeout = new Timeout(5, TimeUnit.SECONDS); Timeout timeout = new Timeout(5, TimeUnit.SECONDS);
ActorRef preknownNeighbour = null; ActorRef preknownNeighbour = null;
...@@ -55,20 +61,35 @@ public class MainRemote { ...@@ -55,20 +61,35 @@ public class MainRemote {
int result = JOptionPane.showConfirmDialog(null, dialogPanel, "Connect to wallet network", JOptionPane.OK_CANCEL_OPTION); int result = JOptionPane.showConfirmDialog(null, dialogPanel, "Connect to wallet network", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) { // terminate if user clicked cancel
walletName = walletNameField.getText(); if (result == JOptionPane.CANCEL_OPTION) {
path = pathField.getText(); if (system != null) {
system.terminate();
}
return;
} }
// check input SelectableNetworkInterface selectedHostname = (SelectableNetworkInterface) hostnameField.getSelectedItem();
if(result == JOptionPane.OK_OPTION && (path.equals("") || walletName.equals(""))){
continue; if (!selectedHostname.getHostName().equals(hostname)) {
if (system != null) {
try {
Await.result(system.terminate(), new FiniteDuration(5, TimeUnit.SECONDS));
} catch (Exception e) {
e.printStackTrace();
}
}
hostname = selectedHostname.getHostName();
system = ActorSystem.create("Remote", ConfigFactory.parseString("akka.remote.netty.tcp.hostname=" + hostname).withFallback(config));
} }
// terminate if user clicked cancel walletName = walletNameField.getText();
if (result == JOptionPane.CANCEL_OPTION) { path = pathField.getText();
system.terminate();
return; // check input
if (path.equals("") || walletName.equals("")) {
continue;
} }
// resolve the given address // resolve the given address
...@@ -86,13 +107,46 @@ public class MainRemote { ...@@ -86,13 +107,46 @@ public class MainRemote {
} }
private static JPanel createDialogPanel() { private static JPanel createDialogPanel(String defaultHostname) {
JPanel dialogPanel = new JPanel(); JPanel dialogPanel = new JPanel();
dialogPanel.setLayout(new BoxLayout(dialogPanel, BoxLayout.PAGE_AXIS)); dialogPanel.setLayout(new GridLayout(3, 1));
dialogPanel.add(new JLabel("Pick your wallet name: ")); JLabel walletLabel = new JLabel("Pick your wallet name: ", SwingConstants.LEFT);
dialogPanel.add(walletLabel);
dialogPanel.add(walletNameField); dialogPanel.add(walletNameField);
dialogPanel.add(new JLabel("Enter a neighbour node address: ")); dialogPanel.add(new JLabel("Enter a neighbour node address: ", SwingConstants.LEFT));
dialogPanel.add(pathField); dialogPanel.add(pathField);
dialogPanel.add(new JLabel("Select your reachable IP address: ", SwingConstants.LEFT));
List<SelectableNetworkInterface> interfaces = fetchNetworkInterfaces();
for (SelectableNetworkInterface netint : interfaces) {
hostnameField.addItem(netint);
}
hostnameField.setSelectedItem(new SelectableNetworkInterface(defaultHostname, "default"));
dialogPanel.add(hostnameField);
return dialogPanel; return dialogPanel;
} }
private static List<SelectableNetworkInterface> fetchNetworkInterfaces() {
List<SelectableNetworkInterface> map = new ArrayList<>();
try {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface networkInterface : Collections.list(nets)) {
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
// To ease the setup, currently only IPv4 addresses are supported
Optional<InetAddress> picked = Collections.list(inetAddresses).stream().filter(inetAddress -> !(inetAddress instanceof Inet6Address)).findFirst();
if (picked.isPresent()) {
String hostAddress = picked.get().getHostAddress();
map.add(new SelectableNetworkInterface(hostAddress, networkInterface.getDisplayName()));
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return map;
}
} }
package fucoin.setup;
/**
* @author davidbohn
*/
public class SelectableNetworkInterface {
String hostName;
String interfaceName;
public SelectableNetworkInterface(String hostName, String interfaceName) {
this.hostName = hostName;
this.interfaceName = interfaceName;
}
public String getHostName() {
return hostName;
}
public String getInterfaceName() {
return interfaceName;
}
@Override
public String toString() {
return getInterfaceName() + " (" + getHostName() + ")";
}
@Override
public boolean equals(Object obj) {
if (obj instanceof SelectableNetworkInterface) {
return ((SelectableNetworkInterface) obj).getHostName().equals(this.getHostName());
}
return super.equals(obj);
}
}
...@@ -2,6 +2,7 @@ akka { ...@@ -2,6 +2,7 @@ akka {
actor { actor {
provider = "akka.remote.RemoteActorRefProvider" provider = "akka.remote.RemoteActorRefProvider"
warn-about-java-serializer-usage = false
} }
remote { remote {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment