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 --- .../cloudatlas/agent/modules/TimerScheduler.java | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java') 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"); + } +} -- cgit v1.2.3 From 219431065b335fdf250394ba66da8220d9bb868f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:54:24 +0100 Subject: Rename messages package --- .../java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java') 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 b7ce435..8f6c2f0 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 @@ -1,7 +1,7 @@ package pl.edu.mimuw.cloudatlas.agent.modules; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; -import pl.edu.mimuw.cloudatlas.agent.message.TimerSchedulerMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; import java.util.Timer; import java.util.TimerTask; -- cgit v1.2.3 From 23c86416390fdbcfc8d749e9117564d25c7ef729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:56:37 +0100 Subject: Import fixes --- src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java') 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 8f6c2f0..921f985 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 @@ -4,7 +4,6 @@ import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; import java.util.Timer; -import java.util.TimerTask; /** * Initializes a timer within a constructor during its attachment to the executor -- cgit v1.2.3 From 6bd93c4fc80624eb83e9a1eb9b93e5781a796505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 23:27:01 +0100 Subject: Fix agent message type checks --- .../java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java') 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 921f985..a08d060 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,18 +13,21 @@ 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; 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; this.timer.schedule(timerEvent.getTask(), timerEvent.getDelay()); System.out.println("Task with delay " + timerEvent.getDelay() + " scheduled"); -- cgit v1.2.3 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 --- .../mimuw/cloudatlas/agent/modules/TimerScheduler.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java') 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