import java.net.URL;
import gw.*;

public class Ping
{
    public static double TIMEOUT = 2.0;
    private GWPort proxy;

    Ping(String gateway)
    {
        try {
            URL portURL = new URL("http://restsoap:AIIT@" + gateway + "/gw/soap");
            GWServiceLocator locator = new GWServiceLocator();
            proxy = locator.getGWPort(portURL);
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    }

    public void ping(String addr)
    {
        GW__PingNode_Result[] results;

        try {
            //results = proxy.mgmtPingNode(addr, 1, "net", "net", 
            //                             TIMEOUT, "*");
            results = proxy.mgmtPingNode(addr, TIMEOUT, "*");
            for (int i = 0; i < results.length; i++) 
                System.out.println("Response from mote " + 
                                   results[i].getLongAddr() + 
                                   " (shortAddr " + 
                                   results[i].getShortAddr() + ")");
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    }

    public static void main(String [] args)
    {
        if (args.length != 1 && args.length != 2) {
            System.out.println("Usage: java Ping gateway [mote]");
            System.exit(3);
        }
        String gateway = args[0];
        String mote;
        if (args.length == 1)
            mote = "ffffffffffffffff";
        else
            mote = args[1];
        Ping ping = new Ping(gateway);
        ping.ping(mote);
    }
}