package pl.edu.mimuw.cloudatlas.agent; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import pl.edu.mimuw.cloudatlas.agent.modules.Module; import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType; import pl.edu.mimuw.cloudatlas.agent.modules.RMI; import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler; public class Agent { public static HashMap initializeModules() { HashMap modules = new HashMap(); modules.put(ModuleType.TIMER_SCHEDULER, new TimerScheduler(ModuleType.TIMER_SCHEDULER)); modules.put(ModuleType.RMI, new RMI(ModuleType.RMI)); // TODO add modules as we implement them return modules; } public static HashMap initializeExecutors( HashMap modules) { HashMap executors = new HashMap(); Iterator it = modules.entrySet().iterator(); while (it.hasNext()) { Map.Entry moduleEntry = (Map.Entry) it.next(); Module module = moduleEntry.getValue(); Executor executor = new Executor(module); executors.put(moduleEntry.getKey(), executor); } return executors; } public static ArrayList initializeExecutorThreads(HashMap executors) { ArrayList executorThreads = new ArrayList(); Iterator it = executors.entrySet().iterator(); while (it.hasNext()) { Map.Entry executorEntry = (Map.Entry) it.next(); Thread thread = new Thread(executorEntry.getValue()); thread.setDaemon(true); System.out.println("Initializing executor " + executorEntry.getKey()); thread.start(); executorThreads.add(thread); } return executorThreads; } public static void closeExecutors(ArrayList executorThreads) { for (Thread executorThread : executorThreads) { executorThread.interrupt(); } } public static void runModulesAsThreads() { HashMap modules = initializeModules(); HashMap executors = initializeExecutors(modules); ArrayList executorThreads = initializeExecutorThreads(executors); Thread eventBusThread = new Thread(new EventBus(executors)); System.out.println("Initializing event bus"); eventBusThread.start(); System.out.println("Closing executors"); closeExecutors(executorThreads); } public static void main(String[] args) { runModulesAsThreads(); } }