
package forager.server;

import java.io.IOException;

import java.util.logging.Level;
import java.util.logging.Logger;

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

import galileo.event.EventContext;
import galileo.event.EventHandler;
import galileo.event.EventReactor;
import galileo.net.GalileoMessage;
import galileo.net.MessageListener;
import galileo.net.NetworkDestination;
import galileo.net.ServerMessageRouter;

public class Overlord implements MessageListener {

    private static final Logger logger = Logger.getLogger("forager");

    private int port;
    private ServerMessageRouter messageRouter;

    private ForagerEventMap eventMap = new ForagerEventMap();
    private EventReactor eventReactor = new EventReactor(this, eventMap);

    public Overlord(int port) {
        this.port = port;
    }

    public void start()
    throws IOException, Exception {
        messageRouter = new ServerMessageRouter();
        messageRouter.addListener(this);
        messageRouter.addListener(eventReactor);
        messageRouter.listen(this.port);

        while (true) {
            eventReactor.processNextEvent();
        }
    }

    @Override
    public void onConnect(NetworkDestination endpoint) {

    }

    @Override
    public void onDisconnect(NetworkDestination endpoint) {

    }

    @Override
    public void onMessage(GalileoMessage message) { }

    @EventHandler
    public void processJoinEvent(JoinEvent join, EventContext context) {
        logger.log(Level.INFO, "Received join request: {0}",
                context.getSource());
    }

    public static void main(String[] args)
    throws Exception {
        if (args.length < 1) {
            System.out.println("Usage: forager.server.Overlord <port>");
            System.exit(1);
        }

        int port = Integer.parseInt(args[0]);

        Overlord overlord = new Overlord(port);
        overlord.start();
    }
}

@@ -469,35 +469,8 @@
 ord 
-implements MessageListener 
 %7B%0A%0A 
@@ -926,49 +926,8 @@
 ();%0A
-        messageRouter.addListener(this);%0A
     
@@ -1099,32 +1099,36 @@
     %7D%0A%0A    @
-Override
+EventHandler
 %0A    public 
@@ -1136,204 +1136,167 @@
 oid 
-onConnect(NetworkDestination endpoin
+processJoinEvent(JoinEvent join, EventContext contex
 t) %7B%0A
-%0A
     
-%7D%0A%0A
     
-@Override%0A    public void onDisconnect(NetworkDestination endpoint) %7B%0A
+logger.log(Level.INFO, %22Received join request: %7B0%7D%22,
 %0A    
-%7D%0A%0A
+  
     
-@Override%0A    public void onMessage(GalileoMessage message) %7B
+      context.getSource());%0A   
  %7D%0A%0A
@@ -1340,32 +1340,39 @@
 cess
-JoinEvent(JoinEvent join
+TaskRequest(TaskRequest request
 , Ev
@@ -1389,32 +1389,33 @@
  context) %7B%0A
+%0A
         
 logger.log(L
@@ -1406,99 +1406,574 @@
     
-logger.log(Level.INFO, %22Received join request: %7B0%7D%22,%0A                context.getSour
+System.out.println(request.numTasks + %22 tasks requested by %22%0A                + context.getSource());%0A%0A        if (taskList.size() == 0) %7B%0A            System.out.println(%22All tasks are complete!%22);%0A            System.exit(0);%0A        %7D%0A%0A        for (int i = 0; i %3C request.numTasks; ++i) %7B%0A            String taskString = taskList.remove();%0A            TaskSpec spec = new TaskSpec(taskString.split(%22%5C%5Cs+%22));%0A            try %7B%0A                context.sendReply(spec);%0A            %7D catch (IOException e) %7B%0A                e.printStackTra
 ce()
-)
 ;%0A
+            %7D%0A        %7D%0A
     

Remove MessageListener implementation