/*
Copyright (c) 2014, Colorado State University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/

package forager.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;

import forager.events.ForagerEventMap;
import forager.events.JoinEvent;
import forager.events.TaskRequest;
import forager.events.TaskSpec;

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 {

    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);

    private Queue<String> taskList = new LinkedList<>();

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

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

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

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

    @EventHandler
    public void processTaskRequest(TaskRequest request, EventContext context) {

        System.out.println(request.numTasks + " tasks requested by "
                + context.getSource());

        if (taskList.size() == 0) {
            System.out.println("All tasks are complete!");
            System.exit(0);
        }

        System.out.println(taskList.size() + " tasks remaining.");

        for (int i = 0; i < request.numTasks; ++i) {
            String taskString = taskList.remove();
            TaskSpec spec = new TaskSpec(taskString.split("\\s+"));
            try {
                context.sendReply(spec);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

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

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

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

@@ -1386,65 +1386,14 @@
 til.
-ArrayList;%0Aimport java.util.LinkedList;%0Aimport java.util.
+Linked
 List
@@ -1733,118 +1733,8 @@
 or;%0A
-import galileo.net.GalileoMessage;%0Aimport galileo.net.MessageListener;%0Aimport galileo.net.NetworkDestination;%0A
 impo
@@ -2127,24 +2127,24 @@
 dList%3C%3E();%0A%0A
+
     public O
@@ -2163,22 +2163,8 @@
 port
-, int startNum
 ) %7B%0A
@@ -3520,17 +3520,17 @@
 ength %3C 
-2
+1
 ) %7B%0A    
@@ -3686,58 +3686,8 @@
 0%5D);
-%0A        int startNum = Integer.parseInt(args%5B1%5D);
 %0A%0A  
@@ -3733,18 +3733,8 @@
 port
-, startNum
 );%0A 

Remove starting number test parameter