m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagdalena Grodzińska <mag.grodzinska@gmail.com>2019-12-23 19:50:46 +0100
committerMagdalena Grodzińska <mag.grodzinska@gmail.com>2019-12-23 19:53:22 +0100
commit4c728b0ba906799afd30f0a337b540cf167a0cfd (patch)
treed9d453d9402a8af5de8ee76a0d5b7141952822ea
parent9d24163dc71c345fb2197f91abb62478e1e9e3ee (diff)
Make timer inherit from module, move to modules package
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java2
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java1
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java (renamed from src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java)5
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java (renamed from src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java)2
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java33
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java32
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java1
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java1
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java48
9 files changed, 89 insertions, 36 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java
index 0cfdfd6..e695773 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java
@@ -1,6 +1,5 @@
package pl.edu.mimuw.cloudatlas.agent;
-import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
@@ -10,6 +9,7 @@ import java.util.Iterator;
import java.util.Map;
import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
+import pl.edu.mimuw.cloudatlas.agent.modules.Module;
import pl.edu.mimuw.cloudatlas.api.Api;
import pl.edu.mimuw.cloudatlas.interpreter.Main;
import pl.edu.mimuw.cloudatlas.model.ZMI;
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java
index f3cf2ea..6d29d13 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java
@@ -3,6 +3,7 @@ package pl.edu.mimuw.cloudatlas.agent;
import java.util.concurrent.LinkedBlockingQueue;
import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
+import pl.edu.mimuw.cloudatlas.agent.modules.Module;
/*
* Queues messages sent to a particular module and ensures they are eventually
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java
index 167f3b7..dd6a5f8 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java
@@ -1,5 +1,6 @@
-package pl.edu.mimuw.cloudatlas.agent;
+package pl.edu.mimuw.cloudatlas.agent.modules;
+import pl.edu.mimuw.cloudatlas.agent.Executor;
import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
/*
@@ -9,7 +10,7 @@ public abstract class Module {
private AgentMessage.AgentModule moduleType;
private Executor executor;
- Module(AgentMessage.AgentModule moduleType) {
+ public Module(AgentMessage.AgentModule moduleType) {
this.moduleType = moduleType;
}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java
index d9bd2d2..db9dd26 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java
@@ -1,4 +1,4 @@
-package pl.edu.mimuw.cloudatlas.agent.timer;
+package pl.edu.mimuw.cloudatlas.agent.modules;
import java.util.TimerTask;
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java
new file mode 100644
index 0000000..b7ce435
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java
@@ -0,0 +1,33 @@
+package pl.edu.mimuw.cloudatlas.agent.modules;
+
+import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
+import pl.edu.mimuw.cloudatlas.agent.message.TimerSchedulerMessage;
+
+import java.util.Timer;
+import java.util.TimerTask;
+
+/**
+ * Initializes a timer within a constructor during its attachment to the executor
+ * Runs in a thread separate from executor - maybe refactor so that it's attached to executor's thread
+ *
+ * Handle used to attach tasks to schedule
+ * Tasks declared as inherited from TimerTask
+ *
+ * TODO: add request id and custom time
+ */
+public class TimerScheduler extends Module {
+ private Timer timer;
+
+ public TimerScheduler(AgentMessage.AgentModule moduleType) {
+ super(moduleType);
+ this.timer = new Timer();
+ System.out.println("TimerScheduler instance initialized");
+ }
+
+ @Override
+ public void handle(AgentMessage event) throws InterruptedException {
+ TimerSchedulerMessage timerEvent = (TimerSchedulerMessage) event;
+ this.timer.schedule(timerEvent.getTask(), timerEvent.getDelay());
+ System.out.println("Task with delay " + timerEvent.getDelay() + " scheduled");
+ }
+}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java
deleted file mode 100644
index 6408a52..0000000
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package pl.edu.mimuw.cloudatlas.agent.timer;
-
-import java.util.Timer;
-import java.util.TimerTask;
-
-/**
- * Initializes a timer within a constructor during its attachment to the executor
- * Runs in a thread separate from executor - maybe refactor so that it's attached to executor's thread
- *
- * Handle used to attach tasks to schedule
- * Tasks declared as inherited from TimerTask
- *
- * TODO: add request id and custom time
- */
-public class TimerScheduler {
- private Timer timer;
-
- TimerScheduler() {
- this.timer = new Timer();
- System.out.println("TimerScheduler instance initialized");
- }
-
- public void handle(TimerTask task, long delay, long period) {
- this.timer.scheduleAtFixedRate(task, delay, period);
- System.out.println("Task with delay " + delay + " and period " + period + " scheduled");
- }
-
- public void handle(TimerTask task, long delay) {
- this.timer.schedule(task, delay);
- System.out.println("Task with delay " + delay + " scheduled");
- }
-}
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java
index fd7c244..178fb38 100644
--- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java
@@ -2,6 +2,7 @@ package pl.edu.mimuw.cloudatlas.agent;
import org.junit.Test;
import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
+import pl.edu.mimuw.cloudatlas.agent.modules.Module;
import java.util.ArrayList;
import java.util.HashMap;
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java
index 2a6ef49..2b998f9 100644
--- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java
@@ -5,6 +5,7 @@ import static org.junit.Assert.*;
import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage.AgentModule;
+import pl.edu.mimuw.cloudatlas.agent.modules.Module;
public class ExecutorTest {
public class MessageCounterModule extends Module {
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java
new file mode 100644
index 0000000..45017c8
--- /dev/null
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java
@@ -0,0 +1,48 @@
+package pl.edu.mimuw.cloudatlas.agent;
+
+import org.junit.Test;
+import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
+import pl.edu.mimuw.cloudatlas.agent.modules.Module;
+import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import static org.junit.Assert.assertEquals;
+
+public class SchedulerTest {
+
+ public HashMap<AgentMessage.AgentModule, Module> initializeModule() {
+ HashMap<AgentMessage.AgentModule, Module> modules = new HashMap<AgentMessage.AgentModule, Module>();
+
+ modules.put(AgentMessage.AgentModule.TIMER_SCHEDULER, new TimerScheduler(AgentMessage.AgentModule.RMI));
+
+ return modules;
+ }
+
+ @Test
+ void scheduleTask() {
+
+ }
+
+ @Test
+ void scheduleTwoTasks() {
+ HashMap<AgentMessage.AgentModule, Module> modules = ini();
+ HashMap<AgentMessage.AgentModule, Executor> executors = Agent.initializeExecutors(modules);
+ ArrayList<Thread> executorThreads = Agent.initializeExecutorThreads(executors);
+ EventBus eventBus = new EventBus(executors);
+ Thread eventBusThread = new Thread(eventBus);
+ eventBusThread.start();
+
+ eventBus.addMessage(new AgentMessage(
+ "0",
+ AgentMessage.AgentModule.RMI,
+ System.currentTimeMillis() / 1000L));
+
+ Thread.sleep(1000);
+
+ eventBusThread.interrupt();
+ Agent.closeExecutors(executorThreads);
+ assertEquals(1, ((EventBusTest.MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter);
+ }
+}