Example usage for java.net NetworkInterface toString

List of usage examples for java.net NetworkInterface toString

Introduction

In this page you can find the example usage for java.net NetworkInterface toString.

Prototype

public String toString() 

Source Link

Usage

From source file:org.opendaylight.lispflowmapping.implementation.lisp.MapServer.java

private static InetAddress getLocalAddress() {
    try {/* w ww  .j a  v a  2s.  co  m*/
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface current = interfaces.nextElement();
            LOG.debug("Interface " + current.toString());
            if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
                continue;
            }
            Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress current_addr = addresses.nextElement();
                // Skip loopback and link local addresses
                if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress()) {
                    continue;
                }
                LOG.debug(current_addr.getHostAddress());
                return current_addr;
            }
        }
    } catch (SocketException se) {
        LOG.debug("Caught socket exceptio", se);
    }
    return null;
}

From source file:org.ihsp.data.common.ObjectId.java

private static int createMachineIdentifier() {
    // build a 2-byte machine piece based on NICs info
    int machinePiece;
    try {/*w ww.  j  a  va2  s .  c o m*/
        StringBuilder sb = new StringBuilder();
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface ni = e.nextElement();
            sb.append(ni.toString());
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                ByteBuffer bb = ByteBuffer.wrap(mac);
                try {
                    sb.append(bb.getChar());
                    sb.append(bb.getChar());
                    sb.append(bb.getChar());
                } catch (BufferUnderflowException shortHardwareAddressException) { //NOPMD
                    // mac with less than 6 bytes. continue
                }
            }
        }
        machinePiece = sb.toString().hashCode();
    } catch (Throwable t) {
        // exception sometimes happens with IBM JVM, use random
        machinePiece = (new SecureRandom().nextInt());
        log.info("Failed to get machine identifier from network interface, using random number instead", t);
    }
    machinePiece = machinePiece & LOW_ORDER_THREE_BYTES;
    return machinePiece;
}

From source file:com.googlecode.networklog.NetworkLog.java

public static void getLocalIpAddresses() {
    MyLog.d("getLocalIpAddresses");
    localIpAddrs = new ArrayList<String>();

    try {/*from   ww w .  ja va  2 s.c o  m*/
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            MyLog.d("Network interface found: " + intf.toString());

            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                MyLog.d("InetAddress: " + inetAddress.toString());

                if (!inetAddress.isLoopbackAddress()) {
                    MyLog.d("Adding local IP address: [" + inetAddress.getHostAddress().toString() + "]");
                    localIpAddrs.add(inetAddress.getHostAddress().toString());
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("NetworkLog", ex.toString());
    }
}

From source file:org.trafodion.rest.util.NetworkConfiguration.java

public InetAddress getInetAddress(NetworkInterface ni) throws Exception {
    InetAddress inet = null;/*  w  w  w .  j a v  a 2  s. c  o m*/
    Enumeration<InetAddress> rawAdrs = ni.getInetAddresses();
    while (rawAdrs.hasMoreElements()) {
        inet = rawAdrs.nextElement();
        LOG.info("Match Found interface [" + ni.toString() + "," + ni.getDisplayName() + ","
                + inet.getCanonicalHostName() + "," + inet.getHostAddress() + "]");
    }
    matchedInterface = true;
    return inet;
}

From source file:org.psit.transwatcher.TransWatcher.java

private String getBroadcastIP() {
    String myIP = null;/*from w  ww. ja  va 2s . c  om*/

    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements() && myIP == null) {
            NetworkInterface current = interfaces.nextElement();
            notifyMessage(current.toString());
            notifyMessage("Name: " + current.getName());
            if (!current.isUp() || current.isLoopback() || current.isVirtual()
                    || !current.getName().startsWith("wl"))
                continue;
            Enumeration<InetAddress> addresses = current.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress current_addr = addresses.nextElement();
                if (current_addr.isLoopbackAddress())
                    continue;
                if (current_addr instanceof Inet4Address) {
                    myIP = current_addr.getHostAddress();
                    break;
                }
            }
        }
    } catch (Exception exc) {
        notifyMessage("Error determining network interfaces:\n");
    }

    if (myIP != null) {
        // broadcast for IPv4
        StringTokenizer st = new StringTokenizer(myIP, ".");
        StringBuffer broadcastIP = new StringBuffer();
        // hate that archaic string puzzle
        broadcastIP.append(st.nextElement());
        broadcastIP.append(".");
        broadcastIP.append(st.nextElement());
        broadcastIP.append(".");
        broadcastIP.append(st.nextElement());
        broadcastIP.append(".");
        broadcastIP.append("255");
        return broadcastIP.toString();
    }

    return null;
}