// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/IEnvironments.java
// public interface IEnvironments {
// 
// 	public void setup(String environment) throws Exception;
// 	public void start(String environment) throws Exception;
// 	public void stop(String environment) throws Exception;
// 	public void teardown(String environment) throws Exception;
// 	
// 	
// 	public void registerConnectionFactory(String name, IConnectionFactory factory); 
// 	public void deregisterConnectionFactory(String name);
// 	public IConnection getConnection(String name) throws Exception;
// 	
// 	public void registerDeviceFactory(String name, IDeviceFactory factory); 
// 	public void deregisterDeviceFactory(String name);
// 	public IDevice getDevice(String name) throws Exception;
// 	
// 	
// 	
// }
// 
// Path: AutoSIM-UDP/src/com/automatski/autosim/udp/config/AutoSIMConnectionConfig.java
// public class AutoSIMConnectionConfig {
// 
// 	public String name;
// 	public String host;
// 	public int port;
// 	public int ttl;
// 	public String username;
// 	public String password;
// 	
// 	public String connectorClassName;
// }
// 
// Path: AutoSIM-UDP/src/com/automatski/autosim/udp/config/AutoSIMUDPConfig.java
// public class AutoSIMUDPConfig {
// 
// 	public List<AutoSIMConnectionConfig> connections = new ArrayList<AutoSIMConnectionConfig>();
// 	
// }
// 
// Path: AutoSIM-UDP/src/com/automatski/autosim/udp/utils/GsonUtils.java
// public class GsonUtils {
// 
// 	private static GsonBuilder builder = null;
// 	
// 	static {
//         builder = new GsonBuilder();
//         builder.setPrettyPrinting().serializeNulls();
// 	}
// 	
// 	public static String objectToJson(Object obj) throws Exception {
//         Gson gson = builder.create();
//         return gson.toJson(obj);
// 	}
// 	
// 	public static Object jsonToObject(String path, Class clas) throws Exception {
//         Gson gson = builder.create();
// 		String json = new String(Files.readAllBytes(Paths.get(path)));//"./albums.json")));
//         return gson.fromJson(json, clas);
// 	}
// 	
// 	
// }


import java.util.HashMap;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.automatski.autosim.environments.IEnvironments;
import com.automatski.autosim.udp.config.AutoSIMConnectionConfig;
import com.automatski.autosim.udp.config.AutoSIMUDPConfig;
import com.automatski.autosim.udp.utils.GsonUtils;
/*
 *  AutoSIM - Internet of Things Simulator
 *  Copyright (C) 2014, Aditya Yadav <aditya@automatski.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.automatski.autosim.udp;






/**
 * This class implements a simple bundle that uses the bundle
 * context to register an English language dictionary service
 * with the OSGi framework. The dictionary service interface is
 * defined in a separate class file and is implemented by an
 * inner class.
**/



public class UDPServiceActivator implements BundleActivator
{
	private IEnvironments environments = null;
	private AutoSIMUDPConfig config = null;
	private HashMap<String,UDPConnectionFactory> factories = new HashMap<String,UDPConnectionFactory>();
	
    /**
     * Implements BundleActivator.start(). Registers an
     * instance of a dictionary service using the bundle context;
     * attaches properties to the service that can be queried
     * when performing a service look-up.
     * @param context the framework context for the bundle.
    **/
    public void start(BundleContext context) throws Exception 
    {
    	
    	//System.out.println(new File(".").getAbsolutePath());
    	
    	// read the config
    	config = (AutoSIMUDPConfig) GsonUtils.jsonToObject("./conf/autosim-udp.json", AutoSIMUDPConfig.class);

    	
    	ServiceReference[] refs = context.getServiceReferences(IEnvironments.class.getName(), "(Language=*)");
    	environments = (IEnvironments) context.getService(refs[0]);

    	for (AutoSIMConnectionConfig connectionConfig : config.connections){
			UDPConnectionFactory factory = new UDPConnectionFactory(connectionConfig);
// Path: AutoSIM-Environments/src/com/automatski/autosim/environments/IEnvironments.java
// public interface IEnvironments {
// 
// 	public void setup(String environment) throws Exception;
// 	public void start(String environment) throws Exception;
// 	public void stop(String environment) throws Exception;
// 	public void teardown(String environment) throws Exception;
// 	
// 	
// 	public void registerConnectionFactory(String name, IConnectionFactory factory); 
// 	public void deregisterConnectionFactory(String name);
// 	public IConnection getConnection(String name) throws Exception;
// 	
// 	public void registerDeviceFactory(String name, IDeviceFactory factory); 
// 	public void deregisterDeviceFactory(String name);
// 	public IDevice getDevice(String name) throws Exception;
// 	
// 	
// 	
// }
// 
// Path: AutoSIM-UDP/src/com/automatski/autosim/udp/config/AutoSIMConnectionConfig.java
// public class AutoSIMConnectionConfig {
// 
// 	public String name;
// 	public String host;
// 	public int port;
// 	public int ttl;
// 	public String username;
// 	public String password;
// 	
// 	public String connectorClassName;
// }
// 
// Path: AutoSIM-UDP/src/com/automatski/autosim/udp/config/AutoSIMUDPConfig.java
// public class AutoSIMUDPConfig {
// 
// 	public List<AutoSIMConnectionConfig> connections = new ArrayList<AutoSIMConnectionConfig>();
// 	
// }
// 
// Path: AutoSIM-UDP/src/com/automatski/autosim/udp/utils/GsonUtils.java
// public class GsonUtils {
// 
// 	private static GsonBuilder builder = null;
// 	
// 	static {
//         builder = new GsonBuilder();
//         builder.setPrettyPrinting().serializeNulls();
// 	}
// 	
// 	public static String objectToJson(Object obj) throws Exception {
//         Gson gson = builder.create();
//         return gson.toJson(obj);
// 	}
// 	
// 	public static Object jsonToObject(String path, Class clas) throws Exception {
//         Gson gson = builder.create();
// 		String json = new String(Files.readAllBytes(Paths.get(path)));//"./albums.json")));
//         return gson.fromJson(json, clas);
// 	}
// 	
// 	
// }


// Path: AutoSIM-UDP/src/com/automatski/autosim/udp/UDPServiceActivator.java
import java.util.HashMap;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import com.automatski.autosim.environments.IEnvironments;
import com.automatski.autosim.udp.config.AutoSIMConnectionConfig;
import com.automatski.autosim.udp.config.AutoSIMUDPConfig;
import com.automatski.autosim.udp.utils.GsonUtils;

/*
 *  AutoSIM - Internet of Things Simulator
 *  Copyright (C) 2014, Aditya Yadav <aditya@automatski.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.automatski.autosim.udp;






/**
 * This class implements a simple bundle that uses the bundle
 * context to register an English language dictionary service
 * with the OSGi framework. The dictionary service interface is
 * defined in a separate class file and is implemented by an
 * inner class.
**/



public class UDPServiceActivator implements BundleActivator
{
	private IEnvironments environments = null;
	private AutoSIMUDPConfig config = null;
	private HashMap<String,UDPConnectionFactory> factories = new HashMap<String,UDPConnectionFactory>();
	
    /**
     * Implements BundleActivator.start(). Registers an
     * instance of a dictionary service using the bundle context;
     * attaches properties to the service that can be queried
     * when performing a service look-up.
     * @param context the framework context for the bundle.
    **/
    public void start(BundleContext context) throws Exception 
    {
    	
    	//System.out.println(new File(".").getAbsolutePath());
    	
    	// read the config
    	config = (AutoSIMUDPConfig) GsonUtils.jsonToObject("./conf/autosim-udp.json", AutoSIMUDPConfig.class);

    	
    	ServiceReference[] refs = context.getServiceReferences(IEnvironments.class.getName(), "(Language=*)");
    	environments = (IEnvironments) context.getService(refs[0]);

    	for (AutoSIMConnectionConfig connectionConfig : config.connections){
			UDPConnectionFactory factory = new UDPConnectionFactory(connectionConfig);
			environments.registerConnectionFactory(connectionConfig.name, factory);