6
6
import java .lang .reflect .InvocationTargetException ;
7
7
import java .lang .reflect .Method ;
8
8
import java .net .InetAddress ;
9
+ import java .net .NetworkInterface ;
10
+ import java .net .SocketException ;
9
11
import java .util .ArrayList ;
12
+ import java .util .Collections ;
13
+ import java .util .Enumeration ;
10
14
import java .util .List ;
11
15
import java .util .Properties ;
12
16
@@ -58,7 +62,7 @@ public Configuration(String configName) {
58
62
if (is == null ) {
59
63
is = new FileInputStream (configName );
60
64
}
61
-
65
+
62
66
properties .load (is );
63
67
// General
64
68
nodeID = new NodeID (properties .getProperty ("nodeID" ));
@@ -84,7 +88,7 @@ public Configuration(String configName) {
84
88
namingServicePublicKey = properties .getProperty ("namingServicePublicKey" , "Unknown" );
85
89
86
90
// Set IP Address of Machine
87
- machineIPAddress = InetAddress . getLocalHost (). getHostAddress ();
91
+ machineIPAddress = getBestIPAddress ();
88
92
logger .info ("The ip address is " + machineIPAddress );
89
93
90
94
checkConsistency ();
@@ -95,6 +99,34 @@ public Configuration(String configName) {
95
99
}
96
100
}
97
101
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
+
98
130
private void checkConsistency () throws IOException {
99
131
Method [] methods = getClass ().getMethods ();
100
132
for (int i = 0 ; i < methods .length ; i ++) {
@@ -134,7 +166,7 @@ public NodeConfig buildNodeConfigBasedOnData() {
134
166
135
167
return config ;
136
168
}
137
-
169
+
138
170
public void setMachineName (String machineName ) {
139
171
this .machineName = machineName ;
140
172
}
@@ -162,7 +194,7 @@ public String getDescription() {
162
194
public Connector getDatabaseConnector () {
163
195
return databaseConnector ;
164
196
}
165
-
197
+
166
198
public Integer getMessageHistorySize () {
167
199
return messageHistorySize ;
168
200
}
0 commit comments