/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.orchestration.internal.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * 获取真实本机网络的服务.
 * 
 * @author caohao
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class IpUtils {
    
    private static volatile String cachedIpAddress;
    
    /**
     * 获取本机IP地址.
     * 
     * <p>
     * 有限获取外网IP地址.
     * 也有可能是链接着路由器的最终IP地址.
     * 如果发生异常返回UnknownIP.
     * </p>
     * 
     * @return 本机IP地址
     */
    public static String getIp() {
        if (null != cachedIpAddress) {
            return cachedIpAddress;
        }
        Enumeration<NetworkInterface> netInterfaces;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();
        } catch (final SocketException ex) {
            return "UnknownIP";
        }
        String localIpAddress = null;
        while (netInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = netInterfaces.nextElement();
            Enumeration<InetAddress> ipAddresses = netInterface.getInetAddresses();
            while (ipAddresses.hasMoreElements()) {
                InetAddress ipAddress = ipAddresses.nextElement();
                if (isPublicIpAddress(ipAddress)) {
                    String publicIpAddress = ipAddress.getHostAddress();
                    cachedIpAddress = publicIpAddress;
                    return publicIpAddress;
                }
                if (isLocalIpAddress(ipAddress)) {
                    localIpAddress = ipAddress.getHostAddress();
                }
            }
        }
        cachedIpAddress = localIpAddress;
        return localIpAddress;
    }
    
    private static boolean isPublicIpAddress(final InetAddress ipAddress) {
        return !ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && !isV6IpAddress(ipAddress);
    }
    
    private static boolean isLocalIpAddress(final InetAddress ipAddress) {
        return ipAddress.isSiteLocalAddress() && !ipAddress.isLoopbackAddress() && !isV6IpAddress(ipAddress);
    }
    
    private static boolean isV6IpAddress(final InetAddress ipAddress) {
        return ipAddress.getHostAddress().contains(":");
    }
}

@@ -868,19 +868,20 @@
 %0A * 
-%E8%8E%B7%E5%8F%96%E7%9C%9F%E5%AE%9E%E6%9C%AC%E6%9C%BA%E7%BD%91%E7%BB%9C%E7%9A%84%E6%9C%8D%E5%8A%A1
+IP utilities
 .%0A *
@@ -1063,16 +1063,32 @@
   * 
-%E8%8E%B7%E5%8F%96%E6%9C%AC%E6%9C%BAIP%E5%9C%B0%E5%9D%80
+Get localhost IP address
 .%0A  
@@ -1115,72 +1115,88 @@
   * 
-%E6%9C%89%E9%99%90%E8%8E%B7%E5%8F%96%E5%A4%96%E7%BD%91IP%E5%9C%B0%E5%9D%80.%0A     * %E4%B9%9F%E6%9C%89%E5%8F%AF%E8%83%BD%E6%98%AF%E9%93%BE%E6%8E%A5%E7%9D%80%E8%B7%AF%E7%94%B1%E5%99%A8%E7%9A%84%E6%9C%80%E7%BB%88IP%E5%9C%B0%E5%9D%80.%0A     * %E5%A6%82%E6%9E%9C%E5%8F%91%E7%94%9F%E5%BC%82%E5%B8%B8%E8%BF%94%E5%9B%9EU
+It maybe get IP address of router.%0A     * Return u
 nknown
+ 
 IP
-.
+ if exception occur. 
 %0A   
@@ -1231,14 +1231,32 @@
 urn 
-%E6%9C%AC%E6%9C%BAIP%E5%9C%B0%E5%9D%80
+IP address of localhost 
 %0A   

comment to en for IPUtils