
package forager.client;

import java.io.IOException;

import galileo.net.ClientMessageRouter;
import galileo.net.GalileoMessage;
import galileo.net.MessageListener;
import galileo.net.NetworkDestination;

public class Forager implements MessageListener {

    private NetworkDestination overlord;
    private ClientMessageRouter messageRouter;
    private boolean once = false;

    public Forager(NetworkDestination overlord) {
        this.overlord = overlord;
    }

    public void start()
    throws IOException {
        messageRouter = new ClientMessageRouter();
        messageRouter.addListener(this);
        System.out.println("conn");
        messageRouter.connectTo(overlord);
        System.out.println("done");
        try {
        Thread.sleep(2000);
        } catch (InterruptedException e) { }
        System.out.println("sending1");
        messageRouter.sendMessage(overlord, new GalileoMessage("hello".getBytes()));
        try {
        Thread.sleep(2000);
        } catch (InterruptedException e) { }
        System.out.println("sending2");
        messageRouter.sendMessage(overlord, new GalileoMessage("hello!".getBytes()));
    }

    @Override
    public void onConnect(NetworkDestination endpoint) {

    }

    @Override
    public void onDisconnect(NetworkDestination endpoint) {
        System.out.println("x");
        if (once == false) {
            once = true;

            System.out.println("ooo");
            try {
                Thread.sleep(1000);
                //messageRouter = new ClientMessageRouter();
                messageRouter.connectTo(overlord);
                System.out.println("y");
        messageRouter.sendMessage(overlord, new GalileoMessage("hello".getBytes()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onMessage(GalileoMessage message) {

    }

    public static void main(String[] args)
    throws IOException {
        if (args.length < 2) {
            System.out.println("Usage: forager.client.Forager <host> <port>");
            System.exit(1);
        }

        String host = args[0];
        int port = Integer.parseInt(args[1]);
        NetworkDestination overlord = new NetworkDestination(host, port);

        Forager forager = new Forager(overlord);
        forager.start();
        System.out.println("done");
    }
}

@@ -617,1215 +617,265 @@
     
-System.out.println(%22conn%22);%0A        messageRouter.connectTo(overlord);%0A        System.out.println(%22done%22);%0A        try %7B%0A        Thread.sleep(2000);%0A        %7D catch (InterruptedException e) %7B %7D%0A        System.out.println(%22sending1%22);%0A        messageRouter.sendMessage(overlord, new GalileoMessage(%22hello%22.getBytes()));%0A        try %7B%0A        Thread.sleep(2000);%0A        %7D catch (InterruptedException e) %7B %7D%0A        System.out.println(%22sending2%22);%0A        messageRouter.sendMessage(overlord, new GalileoMessage(%22hello!%22.getBytes()));%0A    %7D%0A%0A    @Override%0A    public void onConnect(NetworkDestination endpoint) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onDisconnect(NetworkDestination endpoint) %7B%0A        System.out.println(%22x%22);%0A        if (once == false) %7B%0A            once = true;%0A%0A            System.out.println(%22ooo%22);%0A            try %7B%0A                Thread.sleep(1000);%0A                //messageRouter = new ClientMessageRouter();%0A                messageRouter.connectTo(overlord);%0A                System.out.println(%22y%22);%0A        messageRouter.sendMessage(overlord, new GalileoMessage(%22hello%22.getBytes()));%0A            %7D catch (Exception e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D
+messageRouter.connectTo(overlord);%0A%0A        /* Join the network */%0A    %7D%0A%0A    @Override%0A    public void onConnect(NetworkDestination endpoint) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onDisconnect(NetworkDestination endpoint) %7B%0A        //TODO: auto reconnect
 %0A   
@@ -1399,44 +1399,8 @@
 ();%0A
-        System.out.println(%22done%22);%0A
     

Clean up messy test code