@@ -349,30 +349,39 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
349
349
350
350
// Attempt to get a non-loopback address for the local server
351
351
// and fallback to loopback if there is an error
352
- private String getLocalAddress (NsdServiceInfo serviceInfo ){
352
+ private String getHostAddress (NsdServiceInfo serviceInfo , Boolean isLocal ){
353
353
try {
354
- for (Enumeration <NetworkInterface > en = NetworkInterface .getNetworkInterfaces (); en .hasMoreElements ();) {
355
- NetworkInterface intf = en .nextElement ();
356
- for (Enumeration <InetAddress > enumIpAddr = intf .getInetAddresses (); enumIpAddr .hasMoreElements ();) {
357
- InetAddress inetAddress = enumIpAddr .nextElement ();
358
- if (!inetAddress .isLoopbackAddress () && !inetAddress .isLinkLocalAddress () && inetAddress .isSiteLocalAddress ()) {
359
- return inetAddress .getHostAddress ();
354
+ if (isLocal ) {
355
+ for (Enumeration <NetworkInterface > en = NetworkInterface .getNetworkInterfaces (); en .hasMoreElements (); ) {
356
+ NetworkInterface intf = en .nextElement ();
357
+ for (Enumeration <InetAddress > enumIpAddr = intf .getInetAddresses (); enumIpAddr .hasMoreElements (); ) {
358
+ InetAddress inetAddress = enumIpAddr .nextElement ();
359
+ if (!inetAddress .isLoopbackAddress () && !inetAddress .isLinkLocalAddress () && inetAddress .isSiteLocalAddress ()) {
360
+ return inetAddress .getHostAddress ();
361
+ }
360
362
}
361
363
}
362
364
}
363
- } catch (SocketException ex ) {
365
+ return serviceInfo .getHost ().getHostAddress ();
366
+ } catch (Exception ex ) {
364
367
Log .e (OM_SUPPLY , ex .toString ());
365
368
}
366
- return serviceInfo .getHost ().getHostAddress ();
369
+ finally {
370
+ // with no network available the getHostAddress() will
371
+ // throw an Exception - in this case default to local loopback
372
+ // as no other network is reachable!
373
+ return "127.0.0.1" ;
374
+ }
367
375
}
376
+
368
377
private JSObject serviceInfoToObject (NsdServiceInfo serviceInfo ) {
369
378
String serverHardwareId = parseAttribute (serviceInfo , discoveryConstants .HARDWARE_ID_KEY );
370
379
Boolean isLocal = serverHardwareId .equals (discoveryConstants .hardwareId );
371
380
return new JSObject ()
372
381
.put ("protocol" , parseAttribute (serviceInfo , discoveryConstants .PROTOCOL_KEY ))
373
382
.put ("clientVersion" , parseAttribute (serviceInfo , discoveryConstants .CLIENT_VERSION_KEY ))
374
383
.put ("port" , serviceInfo .getPort ())
375
- .put ("ip" , isLocal ? getLocalAddress (serviceInfo ) : serviceInfo . getHost (). getHostAddress ( ))
384
+ .put ("ip" , getHostAddress (serviceInfo , isLocal ))
376
385
.put ("hardwareId" , serverHardwareId )
377
386
.put ("isLocal" , isLocal );
378
387
@@ -499,7 +508,7 @@ public FrontEndHost(JSObject data) {
499
508
// so that we can display the local server IP for users to connect to the API
500
509
if (data .getBool ("isLocal" ) && (ip .equals ("127.0.0.1" ) || ip .equals ("localhost" ))) {
501
510
NsdServiceInfo serviceInfo = createLocalServiceInfo ();
502
- data .put ("ip" , getLocalAddress (serviceInfo ));
511
+ data .put ("ip" , getHostAddress (serviceInfo , true ));
503
512
}
504
513
this .data = data ;
505
514
}
0 commit comments