/**
 * 
 */
package com.github.phantomthief.failover.impl.checker;

import static org.slf4j.LoggerFactory.getLogger;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import com.google.common.net.HostAndPort;

/**
 * @author w.vela
 */
public class SimplePortChecker {

    private static final int DEFAULT_CONNECTION_TIMEOUT = 5000;

    private static org.slf4j.Logger logger = getLogger(SimplePortChecker.class);

    public static boolean check(String host, int port) {
        return check(host, port, DEFAULT_CONNECTION_TIMEOUT);
    }

    public static boolean check(HostAndPort hostAndPort) {
        return check(hostAndPort.getHostText(), hostAndPort.getPort(), DEFAULT_CONNECTION_TIMEOUT);
    }

    public static boolean check(String host, int port, int connectionTimeoutInMs) {
        try (Socket socket = new Socket()) {
            SocketAddress sockaddr = new InetSocketAddress(host, port);
            socket.connect(sockaddr, connectionTimeoutInMs);
            logger.info("[{}:{}] is reachable.", host, port);
            return true;
        } catch (Throwable e) {
            logger.warn("[{}:{}] is NOT reachable.", host, port);
            return false;
        }
    }
}

@@ -534,136 +534,417 @@
 eck(
-host, port, DEFAULT_CONNECTION_TIMEOUT);%0A    %7D%0A%0A    public static boolean check(HostAndPort hostAndPort) %7B%0A        return check(
+null, host, port);%0A    %7D%0A%0A    public static boolean check(String friendlyName, String host, int port) %7B%0A        return check(friendlyName, host, port, DEFAULT_CONNECTION_TIMEOUT);%0A    %7D%0A%0A    public static boolean check(HostAndPort hostAndPort) %7B%0A        return check(null, hostAndPort);%0A    %7D%0A%0A    public static boolean check(String friendlyName, HostAndPort hostAndPort) %7B%0A        return check(friendlyName, 
 host
@@ -988,16 +988,32 @@
 tPort(),
+%0A               
  DEFAULT
@@ -1121,24 +1121,290 @@
 eoutInMs) %7B%0A
+        return check(null, host, port, connectionTimeoutInMs);%0A    %7D%0A%0A    public static boolean check(String friendlyName, String host, int port,%0A            int connectionTimeoutInMs) %7B%0A        String logName = friendlyName == null ? %22%22 : %22%5B%22 + friendlyName + %22%5D%22;%0A
         try 
@@ -1594,16 +1594,18 @@
 r.info(%22
+%7B%7D
 %5B%7B%7D:%7B%7D%5D 
@@ -1611,32 +1611,41 @@
  is reachable.%22,
+ logName,
  host, port);%0A  
@@ -1724,16 +1724,18 @@
 r.warn(%22
+%7B%7D
 %5B%7B%7D:%7B%7D%5D 
@@ -1753,16 +1753,25 @@
 hable.%22,
+ logName,
  host, p

add friendly name for simple port check.