Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit 8ab4c84

Browse files
committed
Add more intelligent ip address evaluation code
1 parent 0e53a0e commit 8ab4c84

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/main/java/control/Configuration.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import java.lang.reflect.InvocationTargetException;
77
import java.lang.reflect.Method;
88
import java.net.InetAddress;
9+
import java.net.NetworkInterface;
10+
import java.net.SocketException;
911
import java.util.ArrayList;
12+
import java.util.Collections;
13+
import java.util.Enumeration;
1014
import java.util.List;
1115
import java.util.Properties;
1216

@@ -58,7 +62,7 @@ public Configuration(String configName) {
5862
if (is == null) {
5963
is = new FileInputStream(configName);
6064
}
61-
65+
6266
properties.load(is);
6367
// General
6468
nodeID = new NodeID(properties.getProperty("nodeID"));
@@ -84,7 +88,7 @@ public Configuration(String configName) {
8488
namingServicePublicKey = properties.getProperty("namingServicePublicKey", "Unknown");
8589

8690
// Set IP Address of Machine
87-
machineIPAddress = InetAddress.getLocalHost().getHostAddress();
91+
machineIPAddress = getBestIPAddress();
8892
logger.info("The ip address is " + machineIPAddress);
8993

9094
checkConsistency();
@@ -95,6 +99,34 @@ public Configuration(String configName) {
9599
}
96100
}
97101

102+
/**
103+
* Gets the ip address that seems to be best.
104+
* @throws SocketException
105+
*/
106+
private static String getBestIPAddress() throws SocketException {
107+
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
108+
for (NetworkInterface netint : Collections.list(nets)) {
109+
// get en or eth adapters
110+
if (netint.getName().startsWith("e")) {
111+
logger.debug("Found network adapter: " + netint.getName());
112+
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
113+
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
114+
// only ipv4
115+
String ipString = inetAddress.getHostAddress();
116+
if (ipString.split("\\.").length == 4) {
117+
logger.debug("Evaluating address " + ipString);
118+
// make sure not a private address
119+
if (!ipString.split("\\.")[0].equals("10")) {
120+
logger.debug("Picking address " + ipString);
121+
return ipString;
122+
}
123+
}
124+
}
125+
}
126+
}
127+
throw new RuntimeException("Cannot determine node ip address");
128+
}
129+
98130
private void checkConsistency() throws IOException {
99131
Method[] methods = getClass().getMethods();
100132
for (int i = 0; i < methods.length; i++) {
@@ -134,7 +166,7 @@ public NodeConfig buildNodeConfigBasedOnData() {
134166

135167
return config;
136168
}
137-
169+
138170
public void setMachineName(String machineName) {
139171
this.machineName = machineName;
140172
}
@@ -162,7 +194,7 @@ public String getDescription() {
162194
public Connector getDatabaseConnector() {
163195
return databaseConnector;
164196
}
165-
197+
166198
public Integer getMessageHistorySize() {
167199
return messageHistorySize;
168200
}

0 commit comments

Comments
 (0)