public static InetAddress getCurrentIp()

in tchannel-core/src/main/java/com/uber/tchannel/utils/TChannelUtilities.java [67:98]


    public static InetAddress getCurrentIp() {
        InetAddress bestAddr = null;

        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

            int bestScore = -1;
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface iface = networkInterfaces.nextElement();

                Enumeration<InetAddress> addrs = iface.getInetAddresses();

                while (addrs.hasMoreElements()) {
                    InetAddress addr = addrs.nextElement();

                    int score = scoreAddr(iface, addr);

                    // Prefer the latest match, instead of the earliest like
                    // the Go implementation. This is because the ordering of
                    // this output is reversed from Go.
                    if (score >= bestScore) {
                        bestScore = score;
                        bestAddr = addr;
                    }
                }
            }
        } catch (IOException e) {
            logger.error("Problem getting local IP", e);
        }

        return bestAddr;
    }