diff options
author | Magdalena GrodziĆska <mag.grodzinska@gmail.com> | 2019-12-25 12:58:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-25 12:58:08 +0100 |
commit | 08bd4342ae7ac18b21843a1c61c0023455d94d65 (patch) | |
tree | a401de66ab5ed9d5fc2d0b88ce447db7ff1dc457 /src/main/java/pl/edu/mimuw/cloudatlas/agent | |
parent | 0949c9d56c595c0e253e6a35604fb2b9af46f82a (diff) | |
parent | 44b899400150c0183fede3b530ab9aa850fa4fa6 (diff) |
Merge pull request #69 from m-chrzan/timer_scheduler
Timer scheduler
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent')
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java | 4 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java | 4 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java | 3 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java (renamed from src/main/java/pl/edu/mimuw/cloudatlas/agent/message/AgentMessage.java) | 8 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java (renamed from src/main/java/pl/edu/mimuw/cloudatlas/agent/message/TimerSchedulerMessage.java) | 25 | ||||
-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) | 11 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java | 19 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java | 47 |
8 files changed, 101 insertions, 20 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..4bd522d 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; @@ -9,7 +8,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.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/EventBus.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java index afb4e27..37c125a 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java @@ -1,6 +1,6 @@ package pl.edu.mimuw.cloudatlas.agent; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import java.util.HashMap; import java.util.Iterator; @@ -43,11 +43,13 @@ public class EventBus implements Runnable { } public void routeMessage(AgentMessage msg) throws InterruptedException { + assert msg.getCorrectMessageType() == msg.getDestinationModule(); System.out.println("Event bus routing message"); executors.get(msg.getDestinationModule()).addMessage(msg); } public void addMessage(AgentMessage msg) throws InterruptedException { + assert msg.getCorrectMessageType() == msg.getDestinationModule(); this.events.put(msg); } } 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..6ebf6a6 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java @@ -2,7 +2,8 @@ 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.messages.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/message/AgentMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java index 6e8f51c..b71831f 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/AgentMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java @@ -1,8 +1,6 @@ -package pl.edu.mimuw.cloudatlas.agent.message; +package pl.edu.mimuw.cloudatlas.agent.messages; -import pl.edu.mimuw.cloudatlas.agent.Agent; - -public class AgentMessage { +public abstract class AgentMessage { public enum AgentModule { TIMER_SCHEDULER, @@ -31,6 +29,8 @@ public class AgentMessage { this.timestamp = System.currentTimeMillis() / 1000L; } + public abstract AgentModule getCorrectMessageType(); + public String getMessageId() { return messageId; } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/TimerSchedulerMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java index 9743533..333b381 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/TimerSchedulerMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java @@ -1,14 +1,20 @@ -package pl.edu.mimuw.cloudatlas.agent.message; +package pl.edu.mimuw.cloudatlas.agent.messages; -import java.util.TimerTask; +import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask; public class TimerSchedulerMessage extends AgentMessage { private String requestId; private long delay; private long baseTime; - private TimerTask task; - - public TimerSchedulerMessage(String messageId, AgentModule destinationModule, long timestamp, String requestId, long delay, long baseTime, TimerTask task) { + private TimerScheduledTask task; + + public TimerSchedulerMessage(String messageId, + AgentModule destinationModule, + long timestamp, + String requestId, + long delay, + long baseTime, + TimerScheduledTask task) { super(messageId, destinationModule, timestamp); this.requestId = requestId; this.delay = delay; @@ -32,15 +38,20 @@ public class TimerSchedulerMessage extends AgentMessage { this.baseTime = baseTime; } - public TimerTask getTask() { + public TimerScheduledTask getTask() { return task; } - public void setTask(TimerTask task) { + public void setTask(TimerScheduledTask task) { this.task = task; } public String getRequestId() { return requestId; } public void setRequestId(String requestId) { this.requestId = requestId; } + + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.TIMER_SCHEDULER; + } } 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..9744971 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,15 +1,16 @@ -package pl.edu.mimuw.cloudatlas.agent; +package pl.edu.mimuw.cloudatlas.agent.modules; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +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 { - private AgentMessage.AgentModule moduleType; - private Executor executor; + protected AgentMessage.AgentModule moduleType; + protected 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/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); + } +} |