package it.integry.integrywmsnative.core.utility;

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

import it.integry.integrywmsnative.core.REST.consumers.ISingleValueOperationCallback;

public class UtilityServer {

    public static void isHostReachable(final String serverAddress, final int serverTCPport, final ISingleValueOperationCallback<Boolean> callback){
        new Thread(() -> {

            boolean connected = false;
            Socket socket;
            try {
                socket = new Socket();
                SocketAddress socketAddress = new InetSocketAddress(serverAddress, serverTCPport);
                socket.connect(socketAddress, 10000);
                if (socket.isConnected()) {
                    connected = true;
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                socket = null;
            }

            callback.onResult(connected);

        }).start();


    }

}
