package be.iminds.iot.dianne.nn.runtime;

import java.util.Dictionary;

import org.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedServiceFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;

import be.iminds.iot.dianne.nn.module.factory.ModuleFactory;
import be.iminds.iot.dianne.tensor.TensorFactory;

@Component(immediate=true, property={"service.pid=be.iminds.iot.dianne.nn.module"})
public class DianneRuntime implements ManagedServiceFactory {

	private BundleContext context;
	
	// TODO support multiple factories in the future?!
	private TensorFactory tFactory;
	private ModuleFactory mFactory;
	
	@Override
	public String getName() {
		return "be.iminds.iot.dianne.nn.module";
	}

	@Activate
	public void activate(BundleContext context){
		this.context = context;
		System.out.println("ACTIVATED!");
	}
	
	@Deactivate
	public void deactivate(){
		// Tear down all modules!
	}
	
	@Reference
	public void setTensorFactory(TensorFactory factory){
		this.tFactory = factory;
	}
	
	@Reference
	public void setModuleFactory(ModuleFactory factory){
		this.mFactory = factory;
	}
	
	@Override
	public void updated(String pid, Dictionary<String, ?> properties)
			throws ConfigurationException {
		System.out.println("UPDATED "+pid);
		
		// Create and register module
		
	}

	@Override
	public void deleted(String pid) {
		System.out.println("DELETED "+pid);
		
		// Delete and unregister module
	}

}

@@ -56,60 +56,185 @@
 til.
-Dictionary;%0A%0Aimport org.osgi.framework.BundleContext
+Collections;%0Aimport java.util.Dictionary;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0Aimport org.osgi.framework.BundleContext;%0Aimport org.osgi.framework.ServiceRegistration
 ;%0Aim
@@ -557,24 +557,70 @@
 Reference;%0A%0A
+import be.iminds.iot.dianne.nn.module.Module;%0A
 import be.im
@@ -1020,16 +1020,143 @@
 tory;%0A%09%0A
+%09private Map%3CString, ServiceRegistration%3E modules = Collections.synchronizedMap(new HashMap%3CString, ServiceRegistration%3E());%0A%09%0A
 %09@Overri
@@ -1402,33 +1402,81 @@
 %7B%0A%09%09
-// Tear down all modules!
+for(ServiceRegistration reg : modules.values())%7B%0A%09%09%09reg.unregister();%0A%09%09%7D
 %0A%09%7D%0A
@@ -1861,16 +1861,349 @@
 odule%0A%09%09
+try %7B%0A%09%09%09Module module = this.mFactory.createModule(tFactory, properties);%0A%09%09%09%0A%09%09%09ServiceRegistration reg = context.registerService(Module.class.getName(), module, properties);%0A%09%09%09this.modules.put(pid, reg);%0A%09%09%09%0A%09%09%09// TODO set next/prev?%0A%09%09%7D catch(InstantiationException e)%7B%0A%09%09%09System.out.println(%22Could not instantiate module%22);%0A%09%09%7D
 %0A%09%7D%0A%0A%09@O
@@ -2292,22 +2292,76 @@
 %09%0A%09%09
-// Delete and 
+ServiceRegistration reg = modules.get(pid);%0A%09%09if(reg!=null)%7B%0A%09%09%09reg.
 unre
@@ -2366,22 +2366,22 @@
 register
- module
+();%0A%09%09%7D
 %0A%09%7D%0A%0A%7D%0A

bring module online from configuration file