From 1df1cbf93448ed7d446e3f9e192f86f7dd99708b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sat, 11 Jan 2020 19:36:42 +0100 Subject: Split agent to config classes --- .../pl/edu/mimuw/cloudatlas/agent/AgentConfig.java | 113 +++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java new file mode 100644 index 0000000..b49525e --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java @@ -0,0 +1,113 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import pl.edu.mimuw.cloudatlas.agent.modules.*; +import pl.edu.mimuw.cloudatlas.agent.modules.Module; +import pl.edu.mimuw.cloudatlas.api.Api; + +import java.net.InetAddress; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class AgentConfig { + private EventBus eventBus; + + AgentConfig() {} + + public EventBus getEventBus() { + return eventBus; + } + + public void runRegistry() { + try { + NewApiImplementation api = new NewApiImplementation(eventBus); + Api apiStub = + (Api) UnicastRemoteObject.exportObject(api, 0); + Registry registry = LocateRegistry.getRegistry(); + registry.rebind("Api", apiStub); + System.out.println("Agent: api bound"); + } catch (Exception e) { + System.err.println("Agent registry initialization exception:"); + e.printStackTrace(); + } + } + + private HashMap initializeModules() throws UnknownHostException, SocketException, NullPointerException { + HashMap modules = new HashMap(); + modules.put(ModuleType.TIMER_SCHEDULER, new TimerScheduler(ModuleType.TIMER_SCHEDULER)); + modules.put(ModuleType.RMI, new Remik()); + Long freshnessPeriod = Long.getLong("freshness_period"); + modules.put(ModuleType.STATE, new Stanik(freshnessPeriod)); + modules.put(ModuleType.QUERY, new Qurnik()); + modules.put(ModuleType.GOSSIP, new GossipGirl()); + + Integer port = Integer.getInteger("UDUPServer.port"); + Integer timeout = Integer.getInteger("UDUPServer.timeout"); + Integer bufsize = Integer.getInteger("UDUPServer.bufsize"); + InetAddress serverAddr = InetAddress.getByName(System.getProperty("UDUPServer.hostname")); + UDUPServer server = new UDUPServer(serverAddr, port, bufsize); + modules.put(ModuleType.UDP, new UDUP(port, timeout, bufsize, server)); + return modules; + } + + private HashMap initializeExecutors( + HashMap modules) { + HashMap executors = new HashMap(); + + for (Map.Entry moduleEntry : modules.entrySet()) { + Module module = moduleEntry.getValue(); + Executor executor = new Executor(module); + executors.put(moduleEntry.getKey(), executor); + } + + return executors; + } + + private ArrayList initializeExecutorThreads(HashMap executors) { + ArrayList executorThreads = new ArrayList(); + + for (Map.Entry executorEntry : executors.entrySet()) { + Thread thread = new Thread(executorEntry.getValue()); + thread.setDaemon(true); + System.out.println("Initializing executor " + executorEntry.getKey()); + thread.start(); + executorThreads.add(thread); + } + + return executorThreads; + } + + public void closeExecutors(ArrayList executorThreads) { + for (Thread executorThread : executorThreads) { + executorThread.interrupt(); + } + } + + public void runModulesAsThreads() { + HashMap modules = null; + + try { + modules = initializeModules(); + } catch (UnknownHostException | SocketException e) { + System.out.println("Module initialization failed"); + e.printStackTrace(); + return; + } + + HashMap executors = initializeExecutors(modules); + ArrayList executorThreads = initializeExecutorThreads(executors); + eventBus = new EventBus(executors); + Thread UDUPServerThread = new Thread(((UDUP) modules.get(ModuleType.UDP)).getServer()); + Thread eventBusThread = new Thread(eventBus); + System.out.println("Initializing event bus"); + eventBusThread.start(); + UDUPServerThread.start(); + } + + +} -- cgit v1.2.3 From 22ec12d762db07b6ecc65462b1e7cb38c8ca1578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sat, 11 Jan 2020 20:06:14 +0100 Subject: Refactor agent configs nicely --- .../pl/edu/mimuw/cloudatlas/agent/AgentConfig.java | 33 +++++++++++----------- 1 file changed, 16 insertions(+), 17 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java index b49525e..16bd99a 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java @@ -15,15 +15,14 @@ import java.util.HashMap; import java.util.Map; public class AgentConfig { - private EventBus eventBus; + private HashMap executors; + HashMap modules; - AgentConfig() {} - - public EventBus getEventBus() { - return eventBus; + public HashMap getExecutors() { + return executors; } - public void runRegistry() { + public void runRegistry(EventBus eventBus) { try { NewApiImplementation api = new NewApiImplementation(eventBus); Api apiStub = @@ -38,18 +37,20 @@ public class AgentConfig { } private HashMap initializeModules() throws UnknownHostException, SocketException, NullPointerException { + // TODO config setup + Long freshnessPeriod = Long.getLong("freshness_period"); + Integer port = Integer.getInteger("UDUPServer.port"); + Integer timeout = Integer.getInteger("UDUPServer.timeout"); + Integer bufsize = Integer.getInteger("UDUPServer.bufsize"); + InetAddress serverAddr = InetAddress.getByName(System.getProperty("UDUPServer.hostname")); + HashMap modules = new HashMap(); modules.put(ModuleType.TIMER_SCHEDULER, new TimerScheduler(ModuleType.TIMER_SCHEDULER)); modules.put(ModuleType.RMI, new Remik()); - Long freshnessPeriod = Long.getLong("freshness_period"); modules.put(ModuleType.STATE, new Stanik(freshnessPeriod)); modules.put(ModuleType.QUERY, new Qurnik()); modules.put(ModuleType.GOSSIP, new GossipGirl()); - Integer port = Integer.getInteger("UDUPServer.port"); - Integer timeout = Integer.getInteger("UDUPServer.timeout"); - Integer bufsize = Integer.getInteger("UDUPServer.bufsize"); - InetAddress serverAddr = InetAddress.getByName(System.getProperty("UDUPServer.hostname")); UDUPServer server = new UDUPServer(serverAddr, port, bufsize); modules.put(ModuleType.UDP, new UDUP(port, timeout, bufsize, server)); return modules; @@ -89,8 +90,6 @@ public class AgentConfig { } public void runModulesAsThreads() { - HashMap modules = null; - try { modules = initializeModules(); } catch (UnknownHostException | SocketException e) { @@ -99,15 +98,15 @@ public class AgentConfig { return; } - HashMap executors = initializeExecutors(modules); + executors = initializeExecutors(modules); ArrayList executorThreads = initializeExecutorThreads(executors); - eventBus = new EventBus(executors); + } + + void startNonModuleThreads(EventBus eventBus) { Thread UDUPServerThread = new Thread(((UDUP) modules.get(ModuleType.UDP)).getServer()); Thread eventBusThread = new Thread(eventBus); System.out.println("Initializing event bus"); eventBusThread.start(); UDUPServerThread.start(); } - - } -- cgit v1.2.3 From 3831d80df5dcd5d75d95308c8906dd9e3c054334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sat, 11 Jan 2020 20:12:03 +0100 Subject: Fix tests --- src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java index 16bd99a..38d764a 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/AgentConfig.java @@ -56,7 +56,7 @@ public class AgentConfig { return modules; } - private HashMap initializeExecutors( + public static HashMap initializeExecutors( HashMap modules) { HashMap executors = new HashMap(); @@ -69,7 +69,7 @@ public class AgentConfig { return executors; } - private ArrayList initializeExecutorThreads(HashMap executors) { + public static ArrayList initializeExecutorThreads(HashMap executors) { ArrayList executorThreads = new ArrayList(); for (Map.Entry executorEntry : executors.entrySet()) { -- cgit v1.2.3