
package forager.client;

import java.io.IOException;

import forager.events.ForagerEventMap;
import forager.events.JoinEvent;

import galileo.event.BasicEventWrapper;
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;

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

    public void start()
    throws IOException {
        messageRouter = new ClientMessageRouter();
        messageRouter.addListener(this);

        /* Join the network */
        JoinEvent join = new JoinEvent();
        BasicEventWrapper wrap = new BasicEventWrapper(new ForagerEventMap());
        messageRouter.sendMessage(overlord, wrap.wrap(join));
    }

    @Override
    public void onConnect(NetworkDestination endpoint) {

    }

    @Override
    public void onDisconnect(NetworkDestination endpoint) {
    }

    @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();
    }
}

@@ -35,22 +35,78 @@
 ava.
-io.IOException
+util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors
 ;%0A%0Ai
@@ -143,16 +143,43 @@
 entMap;%0A
+import forager.events.Job;%0A
 import f
@@ -225,31 +225,26 @@
 o.event.
-BasicEventWrapp
+EventHandl
 er;%0Aimpo
@@ -258,100 +258,64 @@
 leo.
-net.ClientMessageRouter;%0Aimport galileo.net.GalileoMessage;%0Aimport galileo.net.
+event.EventReactor;%0Aimport galileo.net.Client
 Message
-Listen
+Rout
 er;%0A
@@ -379,35 +379,8 @@
 ger 
-implements MessageListener 
 %7B%0A%0A 
@@ -409,24 +409,22 @@
 ination 
-overlord
+server
 ;%0A    pr
@@ -474,85 +474,415 @@
    p
-ublic Forager(NetworkDestination overlord) %7B%0A        this.overlord = overlord
+rivate ForagerEventMap eventMap = new ForagerEventMap();%0A    private EventReactor eventReactor = new EventReactor(this, eventMap);%0A%0A    private ExecutorService threadPool;%0A%0A    public Forager(NetworkDestination server) %7B%0A        this(server, 4);%0A    %7D%0A%0A    public Forager(NetworkDestination server, int threads) %7B%0A        this.server = server;%0A        this.threadPool = Executors.newFixedThreadPool(threads)
 ;%0A  
@@ -913,34 +913,32 @@
 rt()%0A    throws 
-IO
 Exception %7B%0A    
@@ -1018,20 +1018,28 @@
 istener(
-this
+eventReactor
 );%0A%0A    
@@ -1119,132 +1119,64 @@
     
-BasicEventWrapper wrap = new BasicEventWrapper(new ForagerEventMap());%0A        messageRouter.sendMessage(overlord, wrap.wrap
+messageRouter.sendMessage(server, eventReactor.wrapEvent
 (joi
@@ -1184,86 +1184,86 @@
 ));%0A
+%0A
     
-%7D%0A%0A
     
-@Override%0A    public void onConnect(NetworkDestination endpoint) %7B%0A
+while (true) %7B%0A            eventReactor.processNextEvent();%0A        %7D
 %0A   
@@ -1275,81 +1275,62 @@
    @
-Override%0A    public void onDisconnect(NetworkDestination endpoint
+EventHandler%0A    private void startJob(Job job
 ) %7B%0A    
 %7D%0A%0A 
@@ -1329,77 +1329,86 @@
     
-%7D%0A%0A
     
-@Override%0A    public void onMessage(GalileoMessage message) %7B%0A
+System.out.println(%22Starting job: %22 + job);%0A        //threadPool.submit();
 %0A   
@@ -1469,10 +1469,8 @@
 ows 
-IO
 Exce

Use EventReactor framework