package be.iminds.ilabt.jfed.connectivity_tester;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * User: twalcari
 * Date: 12/23/13
 * Time: 10:13 AM
 */
public class PingUtils {
    private static final Logger LOG = LoggerFactory.getLogger(PingUtils.class);

    private static String cachedPing6Command = null;
    private static String getUnixPing6Command() throws IOException {
        if (cachedPing6Command != null)
            return cachedPing6Command;

        try {
            //quick and dirty...
            Process cmdProc = Runtime.getRuntime().exec("which ping6");

            String out = "";

            BufferedReader stdoutReader = new BufferedReader(
                    new InputStreamReader(cmdProc.getInputStream()));
            String line;
            while ((line = stdoutReader.readLine()) != null)
                out += line;
            stdoutReader.close();

            BufferedReader stderrReader = new BufferedReader(
                    new InputStreamReader(cmdProc.getErrorStream()));
            while ((line = stderrReader.readLine()) != null)
                out += line;
            stderrReader.close();

            try {
                cmdProc.waitFor();
            } catch (InterruptedException e) {
                LOG.warn("InterruptedException in getLinuxPing6Command", e);
            }

            int retValue = cmdProc.exitValue();
            if (retValue != 0) {
                cachedPing6Command = "ping -6";
            } else {
                cachedPing6Command = "ping6";
            }
        } catch (Exception e) {
            LOG.error("Failed to get ping6 command. Will ignore by defaulting to \"ping6\".", e);
            cachedPing6Command = "ping6";
        }

        LOG.debug("getUnixPing6Command() found ping6 command and is returning \""+cachedPing6Command+"\"");

        return cachedPing6Command;
    }


    public static boolean isReachableByPing(String host) {
        try{
            String cmd = "";
            if(System.getProperty("os.name").startsWith("Windows")) {
                // For Windows
                cmd = "ping -n 1 " + host;
            } else {
                // For Linux and OSX
                cmd = "ping -c 1 " + host;
            }

            Process myProcess = Runtime.getRuntime().exec(cmd);
            myProcess.waitFor();

            if(myProcess.exitValue() == 0) {

                return true;
            } else {

                return false;
            }

        } catch( Exception e ) {

            e.printStackTrace();
            return false;
        }
    }

    public static boolean isReachableByPing6(String host) {
        try{
            String cmd = "";
            if(System.getProperty("os.name").startsWith("Windows")) {
                // For Windows
                cmd = "ping -6 -n 1 " + host;
            } else {
                // For Linux and OSX
                cmd = getUnixPing6Command()+" -c 1 " + host;
            }

            Process myProcess = Runtime.getRuntime().exec(cmd);
            myProcess.waitFor();

            if(myProcess.exitValue() == 0) {
                return true;
            } else {
                return false;
            }

        } catch( Exception e ) {

            e.printStackTrace();
            return false;
        }
    }
}
