m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMagdalena Grodzińska <mag.grodzinska@gmail.com>2019-12-25 12:54:37 +0100
committerMagdalena Grodzińska <mag.grodzinska@gmail.com>2019-12-25 12:54:37 +0100
commit8e8a49a7477042d5424ecf8ddc432d9297f3e005 (patch)
tree3c02230095469fc1599312ddb870687a2342398f /src
parent26c750550b9fa6bdbebc8acbb39a34281205c140 (diff)
Add messaging capability to tasks
Diffstat (limited to 'src')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java12
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java18
2 files changed, 27 insertions, 3 deletions
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
index 4b933b5..7a0da70 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java
@@ -1,7 +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
index a08d060..9390940 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java
@@ -13,7 +13,6 @@ import java.util.Timer;
* Tasks declared as inherited from TimerTask
*
* TODO: add request id and custom time
- * TODO: enable messaging from scheduled tasks
*/
public class TimerScheduler extends Module {
private Timer timer;
@@ -29,7 +28,20 @@ public class TimerScheduler extends Module {
public void handle(AgentMessage event) throws InterruptedException {
assert event.getDestinationModule() == event.getCorrectMessageType();
TimerSchedulerMessage timerEvent = (TimerSchedulerMessage) event;
- this.timer.schedule(timerEvent.getTask(), timerEvent.getDelay());
- System.out.println("Task with delay " + timerEvent.getDelay() + " scheduled");
+ 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);
}
}