static Endpoint produceLocalEndpoint ( String serviceName ) { Endpoint . Builder builder = Endpoint . newBuilder ( ) . serviceName ( serviceName ) ; try { Enumeration < NetworkInterface > nics = NetworkInterface . getNetworkInterfaces ( ) ; if ( nics == null ) { return builder . build ( ) ; } while ( nics . hasMoreElements ( ) ) { NetworkInterface nic = nics . nextElement ( ) ; Enumeration < InetAddress > addresses = nic . getInetAddresses ( ) ; while ( addresses . hasMoreElements ( ) ) { InetAddress address = addresses . nextElement ( ) ; if ( address . isSiteLocalAddress ( ) ) { builder . ip ( address ) ; break ; } } } } catch ( Exception e ) { // don't crash the caller if there was a problem reading nics. if ( logger . isLoggable ( Level . FINE ) ) { logger . log ( Level . FINE , "error reading nics" , e ) ; } } return builder . build ( ) ; }
Logic borrowed from brave . internal . Platform . produceLocalEndpoint