m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules
diff options
context:
space:
mode:
authorMagdalena GrodziƄska <mag.grodzinska@gmail.com>2019-12-25 12:58:08 +0100
committerGitHub <noreply@github.com>2019-12-25 12:58:08 +0100
commit08bd4342ae7ac18b21843a1c61c0023455d94d65 (patch)
treea401de66ab5ed9d5fc2d0b88ce447db7ff1dc457 /src/main/java/pl/edu/mimuw/cloudatlas/agent/modules
parent0949c9d56c595c0e253e6a35604fb2b9af46f82a (diff)
parent44b899400150c0183fede3b530ab9aa850fa4fa6 (diff)
Merge pull request #69 from m-chrzan/timer_scheduler
Timer scheduler
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java31
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java19
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java47
3 files changed, 97 insertions, 0 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java
new file mode 100644
index 0000000..9744971
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java
@@ -0,0 +1,31 @@
+package pl.edu.mimuw.cloudatlas.agent.modules;
+
+import pl.edu.mimuw.cloudatlas.agent.Executor;
+import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
+
+/*
+ * A Module is a (potentially stateful) event handler.
+ */
+public abstract class Module {
+ protected AgentMessage.AgentModule moduleType;
+ protected Executor executor;
+
+ public Module(AgentMessage.AgentModule moduleType) {
+ this.moduleType = moduleType;
+ }
+
+ public abstract void handle(AgentMessage event) throws InterruptedException ;
+
+ public void setExecutor(Executor executor) {
+ this.executor = executor;
+ }
+
+ public void sendMessage(AgentMessage event) throws InterruptedException {
+ this.executor.passMessage(event);
+ }
+
+ @Override
+ public String toString() {
+ return moduleType.toString();
+ }
+}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java
new file mode 100644
index 0000000..7a0da70
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java
@@ -0,0 +1,19 @@
+package pl.edu.mimuw.cloudatlas.agent.modules;
+
+import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
+
+import java.util.TimerTask;
+
+public abstract class TimerScheduledTask extends TimerTask {
+ private TimerScheduler scheduler;
+
+ public abstract void run();
+
+ public void setScheduler(TimerScheduler scheduler) {
+ this.scheduler = scheduler;
+ }
+
+ protected void sendMessage(AgentMessage msg) throws InterruptedException {
+ this.scheduler.passMessageFromTask(msg);
+ }
+}
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..9390940
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java
@@ -0,0 +1,47 @@
+package pl.edu.mimuw.cloudatlas.agent.modules;
+
+import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage;
+
+import java.util.Timer;
+
+/**
+ * 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);
+ assert moduleType == AgentMessage.AgentModule.TIMER_SCHEDULER;
+ this.timer = new Timer();
+ System.out.println("TimerScheduler instance initialized");
+ }
+
+ @Override
+ public void handle(AgentMessage event) throws InterruptedException {
+ assert event.getDestinationModule() == event.getCorrectMessageType();
+ TimerSchedulerMessage timerEvent = (TimerSchedulerMessage) event;
+ addTask(timerEvent);
+ }
+
+ public void addTask(TimerSchedulerMessage msg) {
+ TimerScheduledTask task = msg.getTask();
+ task.setScheduler(this);
+ this.timer.schedule(task, msg.getDelay());
+ System.out.println("Task with delay " + msg.getDelay() + " scheduled");
+ }
+
+ // TODO
+ public void removeTask(String requestId) {}
+
+ public void passMessageFromTask(AgentMessage msg) throws InterruptedException {
+ this.executor.passMessage(msg);
+ }
+}