package org.mwc.asset.netasset2.core;

import java.io.IOException;
import java.util.Vector;

import org.mwc.asset.netasset2.common.Network;
import org.mwc.asset.netasset2.common.Network.GetScenarios;
import org.mwc.asset.netasset2.common.Network.LightScenario;
import org.mwc.asset.netasset2.common.Network.ScenarioList;

import ASSET.Scenario.MultiScenarioLister;

import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.minlog.Log;

public class AServer
{
	private MultiScenarioLister _dataProvider;
	private SModel _model;

	public static class SModel
	{
		private Server _server;

		public SModel() throws IOException
		{
			_server = new Server();
			Network.register(_server);
			_server.start();
			_server.bind(Network.TCP_PORT, Network.UDP_PORT);
		}

		public void addListener(Listener listener)
		{
			_server.addListener(listener);
		}

		public void stop()
		{
			_server.stop();
		}

	}

	public AServer() throws IOException
	{
		_model = new SModel();

		_model.addListener(new Listener()
		{
			public void received(Connection connection, Object object)
			{
				if (object instanceof GetScenarios)
				{
					System.err.println("getS received");
					Log.info("GetScenarios received");
					ScenarioList res = new ScenarioList();
					res.list = new Vector<LightScenario>();
					res.list.add(new LightScenario("zaa"));
					res.list.add(new LightScenario("bbb"));
					res.list.add(new LightScenario("ccc"));

					// res.list = _dataProvider.getScenarios();
					System.err.println("about to send list");
					connection.sendTCP(res);
					System.err.println("sent list");
				}
			}
		});

	}

	// public static void main(String[] args) throws IOException
	// {
	// AServer server = new AServer();
	//
	// System.out.println("pausing");
	// System.in.read();
	//
	// server.stop();
	// }

	public void setDataProvider(MultiScenarioLister lister)
	{
		_dataProvider = lister;
	}

	public void stop()
	{
		_model.stop();
	}
}

@@ -56,24 +56,50 @@
 OException;%0A
+import java.util.HashMap;%0A
 import java.
@@ -587,16 +587,45 @@
 erver%0A%7B%0A
+%09@SuppressWarnings(%22unused%22)%0A
 %09private
@@ -741,16 +741,66 @@
 _server;
+%0A%09%09private HashMap%3CClass%3C?%3E, Listener%3E _listeners;
 %0A%0A%09%09publ
@@ -962,25 +962,510 @@
 DP_PORT);%0A%09%09
-%7D
+%09%0A%09%09%09_listeners = new HashMap%3CClass%3C?%3E, Listener%3E();%0A%0A%09%09%09// sort out our handler%0A%09%09%09_server.addListener(new Listener()%0A%09%09%09%7B%0A%0A%09%09%09%09@Override%0A%09%09%09%09public void received(Connection connection, Object object)%0A%09%09%09%09%7B%0A%09%09%09%09%09// ok, see if we have a handler%0A%09%09%09%09%09Listener match = _listeners.get(object.getClass());%0A%09%09%09%09%09if (match != null)%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09match.received(connection, object);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09else%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09System.err.println(%22HANDLER NOT FOUND FOR:%22 + object);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A%09%09%7D%0A%09%09
 %0A%0A%09%09public v
@@ -1480,16 +1480,37 @@
 istener(
+Class%3C?%3E objectType, 
 Listener
@@ -1532,35 +1532,141 @@
 %09%09%09_
-server.addListener(listener
+listeners.put(objectType, listener);%0A%09%09%7D%0A%0A%09%09public void removeListener(final Class%3C?%3E objectType)%0A%09%09%7B%0A%09%09%09_listeners.remove(objectType
 );%0A%09
@@ -1794,27 +1794,24 @@
 %0A%0A%09%09
-_model.addListener(
+Listener getS = 
 new 
@@ -1896,54 +1896,8 @@
 %09%09%7B%0A
-%09%09%09%09if (object instanceof GetScenarios)%0A%09%09%09%09%7B%0A
 %09%09%09%09
@@ -2367,22 +2367,16 @@
 %22);%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
 );%0A%0A
@@ -2375,208 +2375,71 @@
 %0A%09%09%7D
-)
 ;%0A
-%0A%09%7D%0A%0A%09// public static void main(String%5B%5D args) throws IOException%0A%09// %7B%0A%09// AServer server = new AServer();%0A%09//%0A%09// System.out.println(%22pausing%22);%0A%09// System.in.read();%0A%09//%0A%09// server.stop(
+%09%09_model.addListener(new GetScenarios().getClass(), getS
 );%0A
+%0A
 %09
-// 
 %7D%0A%0A%09

introduce handler list