package org.graphstream.gama.extension;

import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.graphstream.gama.extension.sender.GSSender;

import org.graphstream.stream.sync.SinkTime;
import org.graphstream.stream.sync.SourceTime;

/**
 * This is the main extension class.
 *
 * It maintains a common sync mechanism for all senders and receivers, as well
 * as methods to access senders and receivers by their id, to add and to remove
 * them.
 *
 * @author Stefan Balev
 *
 */
public class GSManager {
	private static String sourceId;
	private static SourceTime sourceTime;
	private static SinkTime sinkTime;

	private static Map<String, GSSender> senders;
//	private static Map<String, GSReceiver> receivers;

	static {
		sourceId = "gs-gama@"
				+ ManagementFactory.getRuntimeMXBean().getName();
		sourceTime = new SourceTime(sourceId);
		sinkTime = new SinkTime();
		sourceTime.setSinkTime(sinkTime);
		senders = new HashMap<String, GSSender>();
//		receivers = new HashMap<String, GSReceiver>();
	}


	public static GSSender getSender(String senderId)  {
		GSSender sender = senders.get(senderId);
		if (sender == null)
			System.out.println("Sender \"" + senderId
					+ "\" does not exist");
		return sender;
	}

	public static void addSender(String senderId, String host, int port) {
		GSSender sender = senders.get(senderId);
		if (sender != null)
			System.out.println("Sender \"" + senderId
					+ "\" already exists");
		sender = new GSSender(sourceId, sourceTime, host, port);
		senders.put(senderId, sender);
	}

	public static void clearSenders() {
		for (GSSender sender : senders.values())
			sender.close();
		senders.clear();
	}

	/*public static GSReceiver getReceiver(String receiverId)
			throws ExtensionException {
		GSReceiver receiver = receivers.get(receiverId);
		if (receiver == null)
			throw new ExtensionException("Receiver \"" + receiverId
					+ "\" does not exist");
		return receiver;
	}

	public static void addReceiver(String receiverId, String host, int port,
			Set<String> attributeFilter) throws ExtensionException {
		GSReceiver receiver = receivers.get(receiverId);
		if (receiver != null)
			throw new ExtensionException("Receiver \"" + receiverId
					+ "\" already exists");
		receiver = new GSReceiver(sinkTime, host, port, attributeFilter);
		receivers.put(receiverId, receiver);
	}

	public static void clearReceivers() {
		for (GSReceiver receiver : receivers.values())
			receiver.close();
		receivers.clear();
	}*/
}
@@ -152,16 +152,133 @@
 l.Set;%0A%0A
+import msi.gama.runtime.exceptions.GamaRuntimeException;%0A%0Aimport org.graphstream.gama.extension.receiver.GSReceiver;%0A
 import o
@@ -636,32 +636,151 @@
 .%0A *
-%0A * @author Stefan Balev
+ %0A * This class is almost a copy of the one from gs-netlogo but updated to run with gama%0A * %0A * @author Stefan Balev, modified by Thibaut Dmare
 %0A *%0A
@@ -963,18 +963,16 @@
 enders;%0A
-//
 %09private
@@ -1258,10 +1258,8 @@
 ();%0A
-//
 %09%09re
@@ -1359,16 +1359,43 @@
 nderId) 
+throws GamaRuntimeException
  %7B%0A%09%09GSS
@@ -1457,33 +1457,41 @@
 ull)%0A%09%09%09
-System.out.printl
+throw new RuntimeExceptio
 n(%22Sende
@@ -1626,16 +1626,44 @@
 nt port)
+ throws GamaRuntimeException
  %7B%0A%09%09GSS
@@ -1729,25 +1729,33 @@
 %0A%09%09%09
-System.out.printl
+throw new RuntimeExceptio
 n(%22S
@@ -2023,18 +2023,16 @@
 );%0A%09%7D%0A%0A%09
-/*
 public s
@@ -2085,33 +2085,35 @@
 )%0A%09%09%09throws 
-Extension
+GamaRuntime
 Exception %7B%0A
@@ -2103,32 +2103,33 @@
 ntimeException %7B
+ 
 %0A%09%09GSReceiver re
@@ -2197,33 +2197,31 @@
 %09%09throw new 
-Extension
+Runtime
 Exception(%22R
@@ -2410,25 +2410,27 @@
  throws 
-Extension
+GamaRuntime
 Exceptio
@@ -2525,17 +2525,15 @@
 new 
-Extension
+Runtime
 Exce
@@ -2837,12 +2837,10 @@
 ar();%0A%09%7D
-*/
 %0A%7D

Add some new stuff about receiver and a best management of exceptions