Java PingPolling

PingPolling is a Java program that pictures in a GUI a network with nodes. We ping every node after a specific number of seconds to check if the node is alive and responding. Each graphic representation of a node has an editable icon and info labels. Nodes objects can be moved in the canvas and linked one another with lines that represent network connections. Java has no real inbuilt Ping function. So you are going to call the OS ping command instead, with the function.

  public String runPingCommand(String host) {

      String stringOutput = "";
      String cmd = "";

      if (System.getProperty("os.name").startsWith("Windows")) {
          //For Windows: creates process conhost.exe (1.316 KB) and PING.EXE (1.172 KB)
          //cmd = "ping -n 2 -w 1 " + host;  // -w timeout after one single request
          cmd = "ping -n 2 -w 1 " + host;
      } else {
          //For Linux and OSX
          cmd = "ping -c 2 -t 3 " + host; // -t timeout general
      }

      try {
          Process p = Runtime.getRuntime().exec(cmd);
          BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));

          // reading output stream of the command
          while (inputStream.readLine() != null) { 
              stringOutput += inputStream.readLine();
          }

      } catch (Exception e) {
          e.printStackTrace();
      }

      pingResultString = stringOutput;
      return stringOutput;
  }

this function will return a String representing the output of the command as if it was run on terminal or Winows cmd. By analyzing the output String you determine if the ping succeeded or not.

Pinger Class

Everything can be wrapped into a class extending Thread so the ping command can be processed async. Pinger.class

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

public class Pinger extends Thread {

    private String IPAddress; 
    private String pingResultString;
    private boolean pingResultBoolean;

    public void run() {
        runBooleanPingCommand(IPAddress);
    }

    public Pinger(String host) {
        IPAddress = host;
    }

    public String getPingResultString() {
        return pingResultString;
    }

    public boolean getPingResultBoolean() {
        return pingResultBoolean;
    }

    public String runPingCommand(String host) {

        String stringOutput = "";
        String cmd = "";

        if (System.getProperty("os.name").startsWith("Windows")) {
            //For Windows: crea i processi conhost.exe (1.316 KB) e PING.EXE (1.172 KB)
            //cmd = "ping -n 2 -w 1 " + host;  // -w timeout after one single request
            cmd = "ping -n 2 -w 1 " + host;
        } else {
            //For Linux and OSX
            cmd = "ping -c 2 -t 3 " + host; // -t timeout general
        }


        try {
            Process p = Runtime.getRuntime().exec(cmd);
            BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));

            // reading output stream of the command
            while (inputStream.readLine() != null) { 
                stringOutput += inputStream.readLine();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        pingResultString = stringOutput;
        return stringOutput;
    }


    public boolean runBooleanPingCommand(String host) {
        String pingOutputString = runPingCommand(host);

        // ** TODO parmetrizzare i valori di errore
        String[] errorWords = new String[] {
            "scaduta",
            "errore",
            "timeout"
        };

        for (String s: errorWords) {
            if (pingOutputString.toLowerCase().contains(s.toLowerCase())) {
                pingResultBoolean = false;
                return pingResultBoolean;
            }
        }
        pingResultBoolean = true;
        return pingResultBoolean;
    }


}

…class usage:

String IPAddress = "8.8.8.8"; // for example
Pinger pinger = new Pinger(IPAddress);                                       
pinger.start();

Note that Pinger class (in the version above) do not include a call back system to notify the calling object when ping command has finished; you should implement your own.

Draggable connected object in Canvas

To achieve the draggable nodes and line drawing features, there are good hints from this link to stackoverflow.com