package javasnack.snacks;

import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

/**
 * Original source code:
 * - http://itpro.nikkeibp.co.jp/article/COLUMN/20070801/278834/
 * - http://d.hatena.ne.jp/nowokay/20120803
 * - https://gist.github.com/takashi209/3057659
 * 
 * @author "Masahiko Sakamoto"(msakamoto-sf, sakamoto.gsyc.3s@gmail.com)
 */
public class NetworkInterface1 implements Runnable {
    @Override
    public void run() {
        try {
            Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
            while (nics.hasMoreElements()) {
                dumpNetworkInterfaceInformation(nics.nextElement());
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    /**
     * copied from:
     * - http://itpro.nikkeibp.co.jp/article/COLUMN/20070801/278834/
     * 
     *      (adding some changes for sysout.) thanx!
     */
    static void dumpNetworkInterfaceInformation(NetworkInterface nic) throws SocketException {
        System.out.println("-----------------------");
        System.out.println("Name: " + nic.getName());
        System.out.println("DisplayName: " + nic.getDisplayName());

        List<InterfaceAddress> iaddresses = nic.getInterfaceAddresses();
        for (InterfaceAddress iaddr : iaddresses) {
            System.out.println("  ==========================");
            System.out.println("  Address: " + iaddr.getAddress());
            System.out.println("  Broadcast: " + iaddr.getBroadcast());
            System.out.println("  Prefix Length: " + iaddr.getNetworkPrefixLength());
        }
        System.out.println("  ==========================");

        Enumeration<NetworkInterface> subInterfaces = nic.getSubInterfaces();
        while (subInterfaces.hasMoreElements()) {
            System.out.println("Sub Interface: " + subInterfaces.nextElement().getDisplayName());
        }

        NetworkInterface parent = nic.getParent();
        if (null != parent) {
            System.out.println("Parent Interface: " + parent.getDisplayName());
        }

        System.out.println("Status : " + (nic.isUp() ? "UP" : "DOWN"));
        System.out.println(nic.isLoopback() ? "Loopback" : "NO Loopback");
        System.out.println(nic.isPointToPoint() ? "PPP" : "NO PPP");
        System.out.println(nic.supportsMulticast() ? "MULTICAST Supported" : "MULTICAST NOT Supported");

        byte[] hwAddress = nic.getHardwareAddress();
        if (null != hwAddress) {
            System.out.print("MAC Address: ");
            for (byte segment : hwAddress) {
                System.out.printf("%02x ", segment);
            }
            System.out.println();
        }
        System.out.println("MTU: " + nic.getMTU());
        System.out.println(nic.isVirtual() ? "Virtual Interface" : "Physical Interface");
    }
}

@@ -20,16 +20,45 @@
 nacks;%0A%0A
+import java.net.InetAddress;%0A
 import j
@@ -146,24 +146,62 @@
 tException;%0A
+import java.net.UnknownHostException;%0A
 import java.
@@ -614,16 +614,432 @@
   try %7B%0A
+            final InetAddress loopbackAddress = InetAddress.getLoopbackAddress();%0A            System.out.println(%22=============== InetAddress.getLoopbackAddress()%22);%0A            dumpInetAddress(loopbackAddress);%0A%0A            final InetAddress localhostAddress = InetAddress.getLocalHost();%0A            System.out.println(%22=============== InetAddress.getLocalHost()%22);%0A            dumpInetAddress(localhostAddress);%0A%0A
         
@@ -1281,16 +1281,39 @@
 ception 
+%7C UnknownHostException 
 e) %7B%0A   
@@ -1355,24 +1355,1034 @@
    %7D%0A    %7D%0A%0A
+    static void dumpInetAddress(final InetAddress ia) %7B%0A        System.out.println(%22getHostAddress()=%22 + ia.getHostAddress());%0A        System.out.println(%22getHostName()=%22 + ia.getHostName());%0A        System.out.println(%22getCanonicalHostName()=%22 + ia.getCanonicalHostName());%0A        System.out.println(%22isAnyLocalAddress()=%22 + ia.isAnyLocalAddress());%0A        System.out.println(%22isLinkLocalAddress()=%22 + ia.isLinkLocalAddress());%0A        System.out.println(%22isLoopbackAddress()=%22 + ia.isLoopbackAddress());%0A        System.out.println(%22isMCGlobal()=%22 + ia.isMCGlobal());%0A        System.out.println(%22isMCLinkLocal()=%22 + ia.isMCLinkLocal());%0A        System.out.println(%22isMCNodeLocal()=%22 + ia.isMCNodeLocal());%0A        System.out.println(%22isMCOrgLocal()=%22 + ia.isMCOrgLocal());%0A        System.out.println(%22isMCSiteLocal()=%22 + ia.isMCSiteLocal());%0A        System.out.println(%22isMulticastAddress()=%22 + ia.isMulticastAddress());%0A        System.out.println(%22isSiteLocalAddress()=%22 + ia.isSiteLocalAddress());%0A    %7D%0A%0A
     /**%0A    

add dump InetAddress.getLoopbackAddress()/getLocalHost() to NetworkInterface1