From 4c728b0ba906799afd30f0a337b540cf167a0cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:50:46 +0100 Subject: Make timer inherit from module, move to modules package --- .../java/pl/edu/mimuw/cloudatlas/agent/Agent.java | 2 +- .../pl/edu/mimuw/cloudatlas/agent/Executor.java | 1 + .../java/pl/edu/mimuw/cloudatlas/agent/Module.java | 30 -------------------- .../edu/mimuw/cloudatlas/agent/modules/Module.java | 31 ++++++++++++++++++++ .../agent/modules/TimerScheduledTask.java | 11 ++++++++ .../cloudatlas/agent/modules/TimerScheduler.java | 33 ++++++++++++++++++++++ .../cloudatlas/agent/timer/TimerScheduledTask.java | 11 -------- .../cloudatlas/agent/timer/TimerScheduler.java | 32 --------------------- 8 files changed, 77 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java delete mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java delete mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java (limited to 'src/main') 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/Module.java deleted file mode 100644 index 167f3b7..0000000 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java +++ /dev/null @@ -1,30 +0,0 @@ -package pl.edu.mimuw.cloudatlas.agent; - -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; - -/* - * A Module is a (potentially stateful) event handler. - */ -public abstract class Module { - private AgentMessage.AgentModule moduleType; - private Executor executor; - - 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/Module.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java new file mode 100644 index 0000000..dd6a5f8 --- /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.message.AgentMessage; + +/* + * A Module is a (potentially stateful) event handler. + */ +public abstract class Module { + private AgentMessage.AgentModule moduleType; + private 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..db9dd26 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java @@ -0,0 +1,11 @@ +package pl.edu.mimuw.cloudatlas.agent.modules; + +import java.util.TimerTask; + +public class TimerScheduledTask extends TimerTask { + + @Override + public void run() { + + } +} 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/TimerScheduledTask.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java deleted file mode 100644 index d9bd2d2..0000000 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java +++ /dev/null @@ -1,11 +0,0 @@ -package pl.edu.mimuw.cloudatlas.agent.timer; - -import java.util.TimerTask; - -public class TimerScheduledTask extends TimerTask { - - @Override - public void run() { - - } -} 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"); - } -} -- cgit v1.2.3