From 8e8a49a7477042d5424ecf8ddc432d9297f3e005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Wed, 25 Dec 2019 12:54:37 +0100 Subject: Add messaging capability to tasks --- .../cloudatlas/agent/modules/TimerScheduledTask.java | 12 ++++++++++++ .../mimuw/cloudatlas/agent/modules/TimerScheduler.java | 18 +++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) (limited to 'src/main/java/pl/edu/mimuw') 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); } } -- cgit v1.2.3