
package forager.server;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import galileo.event.Event;
import galileo.event.EventHandler;
import galileo.event.EventTypeMap;

public class EventMapper {

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

    private Class<?> handlerClass;
    private Object handlerObject;
    private Map<Class<?>, Method> classToMethod = new HashMap<>();

    public EventMapper(Object handlerObject) {
        this.handlerClass = handlerObject.getClass();
        this.handlerObject = handlerObject;
    }

    public <T extends Event> void map(EventTypeMap typeMap, Class<T> type) {

    }

    public void linkEventHandlers() {
        for (Method m : handlerClass.getMethods()) {
            for (Annotation a : m.getAnnotations()) {
                if (a.annotationType().equals(EventHandler.class)) {
                    /* This method is an event handler */
                    Class<?>[] params = m.getParameterTypes();
                    Class<?> eventClass = extractEventClass(params);
                    logger.log(Level.INFO,
                            "Linking handler method [{0}] to class [{1}]",
                            new Object[] { m.getName(), eventClass.getName() });
                    classToMethod.put(eventClass, m);
                    break;
                }
            }
        }
    }

    private Class<?> extractEventClass(Class<?>[] parameters) {
        if (parameters.length <= 0) {
            //TODO throw new exception
        }

        List<Class<?>> interfaces
            = Arrays.asList(parameters[0].getInterfaces());
        if (interfaces.contains(Event.class) == false) {
            //TODO throw error
        }

        return parameters[0];
        //classToMethod.put(parameters[0], m);
    }
}

@@ -15,24 +15,90 @@
 er.server;%0A%0A
+import java.io.ByteArrayInputStream;%0A%0Aimport java.io.IOException;%0A
 import java.
@@ -158,16 +158,55 @@
 Method;%0A
+import java.nio.channels.SocketChannel;
 %0Aimport 
@@ -356,24 +356,65 @@
 ng.Logger;%0A%0A
+import forager.events.ForagerEventType;%0A%0A
 import galil
@@ -503,31 +503,223 @@
 ap;%0A
-%0Apublic class EventMapp
+import galileo.net.GalileoMessage;%0Aimport galileo.net.MessageListener;%0Aimport galileo.net.NetworkDestination;%0Aimport galileo.serialization.SerializationInputStream;%0A%0Apublic class EventMapper implements MessageListen
 er %7B
@@ -856,24 +856,57 @@
 dlerObject;%0A
+%0A    private EventMap eventMap;%0A%0A
     private 
@@ -1004,16 +1004,35 @@
 erObject
+, EventMap eventMap
 ) %7B%0A    
@@ -1125,16 +1125,100 @@
 Object;%0A
+        this.eventMap = eventMap;%0A        System.out.println(EventMap.getClass(1));%0A
     %7D%0A%0A 
@@ -2462,10 +2462,680 @@
 ;%0A    %7D%0A
+%0A%0A    @Override%0A    public void onConnect(NetworkDestination endpoint) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onDisconnect(NetworkDestination endpoint) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onMessage(GalileoMessage message) %7B%0A        SerializationInputStream in = new SerializationInputStream(%0A                new ByteArrayInputStream(message.getPayload()));%0A        int type = 0;%0A        try %7B%0A        type = in.readInt();%0A        %7D catch (IOException e) %7B %7D%0A        System.out.println(ForagerEventType.fromInt(type));%0A        System.out.println(%0A                ((SocketChannel) message.getSelectionKey().channel()).socket().getInetAddress().getHostName());%0A    %7D%0A%0A
 %7D%0A

Implement MessageListener