From b9f576727fef502fade2370e309d8b238a6a6214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 22 Dec 2019 13:26:55 +0100 Subject: Add simple scheduler class - no request id or custom time --- .../cloudatlas/agent/timer/TimerScheduler.java | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java 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 new file mode 100644 index 0000000..8eddb21 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java @@ -0,0 +1,27 @@ +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 + */ +public class TimerScheduler { + private Timer timer; + + TimerScheduler() { + this.timer = new Timer(); + } + + public void handle(TimerTask task, long delay, long period) { + this.timer.scheduleAtFixedRate(task, delay, period); + } + + public void handle(TimerTask task, long delay) { + this.timer.schedule(task, delay); + } +} -- cgit v1.2.3 From cf8373137e283736620da5cb225907671fa83d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 22 Dec 2019 13:27:20 +0100 Subject: Add task skeleton --- .../edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java 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 new file mode 100644 index 0000000..d9bd2d2 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduledTask.java @@ -0,0 +1,11 @@ +package pl.edu.mimuw.cloudatlas.agent.timer; + +import java.util.TimerTask; + +public class TimerScheduledTask extends TimerTask { + + @Override + public void run() { + + } +} -- cgit v1.2.3 From 45aad6c3b76c294ce217883b3b3add3ccc7abc14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 22 Dec 2019 13:28:44 +0100 Subject: Add todo comment --- src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java | 2 ++ 1 file changed, 2 insertions(+) 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 index 8eddb21..6eef931 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java @@ -9,6 +9,8 @@ import java.util.TimerTask; * * 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; -- cgit v1.2.3 From 9d24163dc71c345fb2197f91abb62478e1e9e3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Sun, 22 Dec 2019 14:21:52 +0100 Subject: Add log messages --- src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java | 3 +++ 1 file changed, 3 insertions(+) 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 index 6eef931..6408a52 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/timer/TimerScheduler.java @@ -17,13 +17,16 @@ public class TimerScheduler { 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 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 --------------- .../edu/mimuw/cloudatlas/agent/EventBusTest.java | 1 + .../edu/mimuw/cloudatlas/agent/ExecutorTest.java | 1 + .../edu/mimuw/cloudatlas/agent/SchedulerTest.java | 48 ++++++++++++++++++++++ 11 files changed, 127 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 create mode 100644 src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java 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"); - } -} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java index fd7c244..178fb38 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -2,6 +2,7 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.modules.Module; import java.util.ArrayList; import java.util.HashMap; diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java index 2a6ef49..2b998f9 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.*; import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage.AgentModule; +import pl.edu.mimuw.cloudatlas.agent.modules.Module; public class ExecutorTest { public class MessageCounterModule extends Module { diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java new file mode 100644 index 0000000..45017c8 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java @@ -0,0 +1,48 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import org.junit.Test; +import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.modules.Module; +import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler; + +import java.util.ArrayList; +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; + +public class SchedulerTest { + + public HashMap initializeModule() { + HashMap modules = new HashMap(); + + modules.put(AgentMessage.AgentModule.TIMER_SCHEDULER, new TimerScheduler(AgentMessage.AgentModule.RMI)); + + return modules; + } + + @Test + void scheduleTask() { + + } + + @Test + void scheduleTwoTasks() { + HashMap modules = ini(); + HashMap executors = Agent.initializeExecutors(modules); + ArrayList executorThreads = Agent.initializeExecutorThreads(executors); + EventBus eventBus = new EventBus(executors); + Thread eventBusThread = new Thread(eventBus); + eventBusThread.start(); + + eventBus.addMessage(new AgentMessage( + "0", + AgentMessage.AgentModule.RMI, + System.currentTimeMillis() / 1000L)); + + Thread.sleep(1000); + + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + assertEquals(1, ((EventBusTest.MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter); + } +} -- 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/Agent.java | 2 +- .../pl/edu/mimuw/cloudatlas/agent/EventBus.java | 2 +- .../pl/edu/mimuw/cloudatlas/agent/Executor.java | 2 +- .../cloudatlas/agent/message/AgentMessage.java | 57 ---------------------- .../agent/message/TimerSchedulerMessage.java | 46 ----------------- .../cloudatlas/agent/messages/AgentMessage.java | 57 ++++++++++++++++++++++ .../agent/messages/TimerSchedulerMessage.java | 46 +++++++++++++++++ .../edu/mimuw/cloudatlas/agent/modules/Module.java | 2 +- .../cloudatlas/agent/modules/TimerScheduler.java | 4 +- .../edu/mimuw/cloudatlas/agent/EventBusTest.java | 2 +- .../edu/mimuw/cloudatlas/agent/ExecutorTest.java | 4 +- .../edu/mimuw/cloudatlas/agent/SchedulerTest.java | 2 +- 12 files changed, 113 insertions(+), 113 deletions(-) delete mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/message/AgentMessage.java delete mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/message/TimerSchedulerMessage.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java 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 e695773..4bd522d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Agent.java @@ -8,7 +8,7 @@ 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; 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..8cdda62 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; 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 6d29d13..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,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.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.modules.Module; /* diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/AgentMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/AgentMessage.java deleted file mode 100644 index 6e8f51c..0000000 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/AgentMessage.java +++ /dev/null @@ -1,57 +0,0 @@ -package pl.edu.mimuw.cloudatlas.agent.message; - -import pl.edu.mimuw.cloudatlas.agent.Agent; - -public class AgentMessage { - - public enum AgentModule { - TIMER_SCHEDULER, - TIMER_GTP, - RMI, - UDP, - GOSSIP_IN, - GOSSIP_OUT, - STATE, - QUERY - } - - private String messageId; - private AgentModule destinationModule; - private long timestamp; - - public AgentMessage(String messageId, AgentModule destinationModule, long timestamp) { - this.messageId = messageId; - this.destinationModule = destinationModule; - this.timestamp = timestamp; - } - - public AgentMessage(String messageId, AgentModule destinationModule) { - this.messageId = messageId; - this.destinationModule = destinationModule; - this.timestamp = System.currentTimeMillis() / 1000L; - } - - public String getMessageId() { - return messageId; - } - - public void setMessageId(String messageId) { - this.messageId = messageId; - } - - public AgentModule getDestinationModule() { - return destinationModule; - } - - public void setDestinationModule(AgentModule destinationModule) { - this.destinationModule = destinationModule; - } - - public long getTimestamp() { - return timestamp; - } - - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } -} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/TimerSchedulerMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/TimerSchedulerMessage.java deleted file mode 100644 index 9743533..0000000 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/message/TimerSchedulerMessage.java +++ /dev/null @@ -1,46 +0,0 @@ -package pl.edu.mimuw.cloudatlas.agent.message; - -import java.util.TimerTask; - -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) { - super(messageId, destinationModule, timestamp); - this.requestId = requestId; - this.delay = delay; - this.baseTime = baseTime; - this.task = task; - } - - public long getDelay() { - return delay; - } - - public void setDelay(long delay) { - this.delay = delay; - } - - public long getBaseTime() { - return baseTime; - } - - public void setBaseTime(long baseTime) { - this.baseTime = baseTime; - } - - public TimerTask getTask() { - return task; - } - - public void setTask(TimerTask task) { - this.task = task; - } - - public String getRequestId() { return requestId; } - - public void setRequestId(String requestId) { this.requestId = requestId; } -} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java new file mode 100644 index 0000000..4f5a69c --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java @@ -0,0 +1,57 @@ +package pl.edu.mimuw.cloudatlas.agent.messages; + +import pl.edu.mimuw.cloudatlas.agent.Agent; + +public class AgentMessage { + + public enum AgentModule { + TIMER_SCHEDULER, + TIMER_GTP, + RMI, + UDP, + GOSSIP_IN, + GOSSIP_OUT, + STATE, + QUERY + } + + private String messageId; + private AgentModule destinationModule; + private long timestamp; + + public AgentMessage(String messageId, AgentModule destinationModule, long timestamp) { + this.messageId = messageId; + this.destinationModule = destinationModule; + this.timestamp = timestamp; + } + + public AgentMessage(String messageId, AgentModule destinationModule) { + this.messageId = messageId; + this.destinationModule = destinationModule; + this.timestamp = System.currentTimeMillis() / 1000L; + } + + public String getMessageId() { + return messageId; + } + + public void setMessageId(String messageId) { + this.messageId = messageId; + } + + public AgentModule getDestinationModule() { + return destinationModule; + } + + public void setDestinationModule(AgentModule destinationModule) { + this.destinationModule = destinationModule; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java new file mode 100644 index 0000000..23ec700 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java @@ -0,0 +1,46 @@ +package pl.edu.mimuw.cloudatlas.agent.messages; + +import java.util.TimerTask; + +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) { + super(messageId, destinationModule, timestamp); + this.requestId = requestId; + this.delay = delay; + this.baseTime = baseTime; + this.task = task; + } + + public long getDelay() { + return delay; + } + + public void setDelay(long delay) { + this.delay = delay; + } + + public long getBaseTime() { + return baseTime; + } + + public void setBaseTime(long baseTime) { + this.baseTime = baseTime; + } + + public TimerTask getTask() { + return task; + } + + public void setTask(TimerTask task) { + this.task = task; + } + + public String getRequestId() { return requestId; } + + public void setRequestId(String requestId) { this.requestId = requestId; } +} 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 index dd6a5f8..04d002d 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java @@ -1,7 +1,7 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import pl.edu.mimuw.cloudatlas.agent.Executor; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; /* * A Module is a (potentially stateful) event handler. 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; diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java index 178fb38..e43e7ea 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -1,7 +1,7 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; -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 java.util.ArrayList; diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java index 2b998f9..1d3af92 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java @@ -3,8 +3,8 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; import static org.junit.Assert.*; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; -import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage.AgentModule; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage.AgentModule; import pl.edu.mimuw.cloudatlas.agent.modules.Module; public class ExecutorTest { diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java index 45017c8..3787355 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java @@ -1,7 +1,7 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; -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.agent.modules.TimerScheduler; -- 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/messages/AgentMessage.java | 2 -- src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduler.java | 1 - 2 files changed, 3 deletions(-) diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java index 4f5a69c..31fc6f8 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java @@ -1,7 +1,5 @@ package pl.edu.mimuw.cloudatlas.agent.messages; -import pl.edu.mimuw.cloudatlas.agent.Agent; - public class AgentMessage { public enum AgentModule { 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 2564fbadd0bb8903fd6a056b5cef7ca47e4c66e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 19:56:50 +0100 Subject: Modify gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 1b6985c..bc4eb13 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ # Ignore Gradle build output directory build + +# Ignore IntelliJ directories +.idea +scratch \ No newline at end of file -- cgit v1.2.3 From 054c14ae3d1c2f02d6012c6133fa78c5a1a15c96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Mon, 23 Dec 2019 23:22:57 +0100 Subject: Make scheduled task abstract --- src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java | 2 ++ .../java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java | 1 + .../pl/edu/mimuw/cloudatlas/agent/modules/TimerScheduledTask.java | 8 ++------ 3 files changed, 5 insertions(+), 6 deletions(-) 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 8cdda62..b84d693 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java @@ -43,11 +43,13 @@ public class EventBus implements Runnable { } public void routeMessage(AgentMessage msg) throws InterruptedException { + assert msg.correctType == msg.getDestinationModule(); System.out.println("Event bus routing message"); executors.get(msg.getDestinationModule()).addMessage(msg); } public void addMessage(AgentMessage msg) throws InterruptedException { + assert msg.correctType == msg.getDestinationModule(); this.events.put(msg); } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java index 31fc6f8..ba15a7a 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java @@ -1,6 +1,7 @@ package pl.edu.mimuw.cloudatlas.agent.messages; public class AgentMessage { + public AgentModule correctType; public enum AgentModule { TIMER_SCHEDULER, 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 db9dd26..4b933b5 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 @@ -2,10 +2,6 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import java.util.TimerTask; -public class TimerScheduledTask extends TimerTask { - - @Override - public void run() { - - } +public abstract class TimerScheduledTask extends TimerTask { + public abstract void run(); } -- 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 --- .../pl/edu/mimuw/cloudatlas/agent/EventBus.java | 4 +- .../cloudatlas/agent/messages/AgentMessage.java | 5 +- .../agent/messages/TimerSchedulerMessage.java | 5 + .../cloudatlas/agent/modules/TimerScheduler.java | 3 + .../edu/mimuw/cloudatlas/agent/EventBusTest.java | 103 +++++++++++++++++-- .../edu/mimuw/cloudatlas/agent/ExecutorTest.java | 30 +++++- .../edu/mimuw/cloudatlas/agent/SchedulerTest.java | 109 +++++++++++++++++---- 7 files changed, 227 insertions(+), 32 deletions(-) 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 b84d693..37c125a 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java @@ -43,13 +43,13 @@ public class EventBus implements Runnable { } public void routeMessage(AgentMessage msg) throws InterruptedException { - assert msg.correctType == msg.getDestinationModule(); + 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.correctType == msg.getDestinationModule(); + assert msg.getCorrectMessageType() == msg.getDestinationModule(); this.events.put(msg); } } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java index ba15a7a..b71831f 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/AgentMessage.java @@ -1,7 +1,6 @@ package pl.edu.mimuw.cloudatlas.agent.messages; -public class AgentMessage { - public AgentModule correctType; +public abstract class AgentMessage { public enum AgentModule { TIMER_SCHEDULER, @@ -30,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/messages/TimerSchedulerMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java index 23ec700..9a558b7 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java @@ -43,4 +43,9 @@ public class TimerSchedulerMessage extends AgentMessage { 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/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"); diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java index e43e7ea..f3df887 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/EventBusTest.java @@ -1,8 +1,13 @@ package pl.edu.mimuw.cloudatlas.agent; +import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; +import org.junit.runners.JUnit4; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; import pl.edu.mimuw.cloudatlas.agent.modules.Module; +import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask; import java.util.ArrayList; import java.util.HashMap; @@ -24,7 +29,13 @@ public class EventBusTest { @Override public void handle(AgentMessage event) throws InterruptedException { System.out.println("Module 1 handle called"); - sendMessage(new AgentMessage("1", AgentMessage.AgentModule.UDP)); + // TODO correct message subclass + sendMessage(new AgentMessage("1", AgentMessage.AgentModule.UDP) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); counter ++; } }); @@ -55,6 +66,8 @@ public class EventBusTest { } @Test + @Ignore + // TODO correct message subclass public void messageModule() throws InterruptedException { HashMap modules = initializeModule(); HashMap executors = Agent.initializeExecutors(modules); @@ -63,7 +76,12 @@ public class EventBusTest { Thread eventBusThread = new Thread(eventBus); eventBusThread.start(); - eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI)); + eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.RMI; + } + }); Thread.sleep(1000); eventBusThread.interrupt(); Agent.closeExecutors(executorThreads); @@ -71,6 +89,8 @@ public class EventBusTest { } @Test + @Ignore + // TODO correct message subclass public void messagingBetweenModules() throws InterruptedException { HashMap modules = initializeTwoModules(); HashMap executors = Agent.initializeExecutors(modules); @@ -79,10 +99,12 @@ public class EventBusTest { Thread eventBusThread = new Thread(eventBus); eventBusThread.start(); - eventBus.addMessage(new AgentMessage( - "0", - AgentMessage.AgentModule.RMI, - System.currentTimeMillis() / 1000L)); + eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.RMI; + } + }); Thread.sleep(1000); @@ -91,4 +113,73 @@ public class EventBusTest { assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter); assertEquals(1, ((MessageCounterModule) modules.get(AgentMessage.AgentModule.UDP)).counter); } + + @Test + public void sendWrongMessageType1() throws InterruptedException { + HashMap modules = initializeModule(); + HashMap executors = Agent.initializeExecutors(modules); + ArrayList executorThreads = Agent.initializeExecutorThreads(executors); + EventBus eventBus = new EventBus(executors); + Thread eventBusThread = new Thread(eventBus); + Boolean routingErrorCaught = false; + eventBusThread.start(); + + try { + eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.RMI, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task executed"); + } + })); + Thread.sleep(1000); + } catch (AssertionError e) { + System.out.println("Wrong timer-scheduler message type error caught"); + routingErrorCaught = true; + } finally { + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + } + + if (!routingErrorCaught) { + Assert.fail("Routing not detected as faulty"); + } + } + + @Test + public void sendWrongMessageType2() throws InterruptedException { + HashMap modules = initializeModule(); + HashMap executors = Agent.initializeExecutors(modules); + ArrayList executorThreads = Agent.initializeExecutorThreads(executors); + EventBus eventBus = new EventBus(executors); + Thread eventBusThread = new Thread(eventBus); + Boolean routingErrorCaught = false; + eventBusThread.start(); + + try { + eventBus.addMessage(new AgentMessage("0", AgentMessage.AgentModule.RMI) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.QUERY; + } + }); + Thread.sleep(1000); + } catch (AssertionError e) { + System.out.println("Wrong timer-scheduler message type error caught"); + routingErrorCaught = true; + } finally { + eventBusThread.interrupt(); + Agent.closeExecutors(executorThreads); + } + + if (!routingErrorCaught) { + Assert.fail("Routing not detected as faulty"); + } + } } diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java index 1d3af92..8653258 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/ExecutorTest.java @@ -7,6 +7,8 @@ import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage.AgentModule; import pl.edu.mimuw.cloudatlas.agent.modules.Module; +// TODO make agent messages specific subclass + public class ExecutorTest { public class MessageCounterModule extends Module { public int counter = 0; @@ -35,7 +37,12 @@ public class ExecutorTest { public void testExecutesHandlerOnce() throws Exception { MessageCounterModule module = new MessageCounterModule(AgentModule.UDP); Executor executor = new Executor(module); - executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {}); + executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); Thread thread = new Thread(executor); thread.start(); Thread.sleep(100); @@ -47,12 +54,27 @@ public class ExecutorTest { public void testExecutesHandlerMultipleTimes() throws Exception { MessageCounterModule module = new MessageCounterModule(AgentModule.UDP); Executor executor = new Executor(module); - executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {}); - executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {}); + executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); + executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); Thread thread = new Thread(executor); thread.start(); Thread.sleep(100); - executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) {}); + executor.addMessage(new AgentMessage("", AgentModule.UDP, 0) { + @Override + public AgentModule getCorrectMessageType() { + return AgentModule.UDP; + } + }); Thread.sleep(100); thread.interrupt(); assertEquals(3, module.counter); diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java index 3787355..5f0d1a7 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java @@ -2,47 +2,120 @@ package pl.edu.mimuw.cloudatlas.agent; import org.junit.Test; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; import pl.edu.mimuw.cloudatlas.agent.modules.Module; +import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask; import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler; import java.util.ArrayList; import java.util.HashMap; -import static org.junit.Assert.assertEquals; +// TODO better task tests after enabling messaging from tasks +// TODO add wrong message test with switched types public class SchedulerTest { + private HashMap modules; + private HashMap executors; + private ArrayList executorThreads; + private EventBus eventBus; + private Thread eventBusThread; + + public SchedulerTest() { + this.modules = initializeModule(); + this.executors = Agent.initializeExecutors(modules); + this.executorThreads = Agent.initializeExecutorThreads(executors); + this.eventBus = new EventBus(executors); + this.eventBusThread = new Thread(eventBus); + eventBusThread.start(); + } public HashMap initializeModule() { HashMap modules = new HashMap(); + modules.put(AgentMessage.AgentModule.TIMER_SCHEDULER, new TimerScheduler(AgentMessage.AgentModule.TIMER_SCHEDULER)); + return modules; + } - modules.put(AgentMessage.AgentModule.TIMER_SCHEDULER, new TimerScheduler(AgentMessage.AgentModule.RMI)); + @Test + public void initializeWrongModuleType() { + try { + Module timer = new TimerScheduler(AgentMessage.AgentModule.RMI); + } catch (AssertionError e) { + System.out.println("Wrong timer type during init error caught"); + } + } - return modules; + @Test + public void sendWrongMessageType() throws InterruptedException { + try { + this.eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.UDP, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task executed"); + } + })); + Thread.sleep(1000); + } catch (AssertionError e) { + System.out.println("Wrong timer-scheduler message type error caught"); + } } @Test - void scheduleTask() { + public void scheduleTask() throws InterruptedException { + this.eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task executed"); + } + })); + Thread.sleep(1000); } @Test - void scheduleTwoTasks() { - HashMap modules = ini(); - HashMap executors = Agent.initializeExecutors(modules); - ArrayList executorThreads = Agent.initializeExecutorThreads(executors); - EventBus eventBus = new EventBus(executors); - Thread eventBusThread = new Thread(eventBus); - eventBusThread.start(); + public void scheduleTwoTasks() throws InterruptedException { + this.eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task 1 executed"); + } + })); - eventBus.addMessage(new AgentMessage( + this.eventBus.addMessage(new TimerSchedulerMessage( "0", - AgentMessage.AgentModule.RMI, - System.currentTimeMillis() / 1000L)); + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 20, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task 2 executed"); + } + })); - Thread.sleep(1000); - eventBusThread.interrupt(); - Agent.closeExecutors(executorThreads); - assertEquals(1, ((EventBusTest.MessageCounterModule) modules.get(AgentMessage.AgentModule.RMI)).counter); + Thread.sleep(1000); } } -- cgit v1.2.3 From 28795098ebebd6a244b249355808a37912656f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Wed, 25 Dec 2019 12:52:48 +0100 Subject: Add timer scheduler tests with messages --- .../edu/mimuw/cloudatlas/agent/SchedulerTest.java | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java index 5f0d1a7..133b3b9 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/SchedulerTest.java @@ -10,7 +10,7 @@ import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduler; import java.util.ArrayList; import java.util.HashMap; -// TODO better task tests after enabling messaging from tasks +// TODO better task tests with counters after enabling messaging from tasks // TODO add wrong message test with switched types public class SchedulerTest { @@ -118,4 +118,43 @@ public class SchedulerTest { Thread.sleep(1000); } + + @Test + public void scheduleTwoMessagingTasks() throws InterruptedException { + TimerSchedulerMessage messageToSend = new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 20, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + System.out.println("Task 2 executed"); + } + }); + + this.eventBus.addMessage(new TimerSchedulerMessage( + "0", + AgentMessage.AgentModule.TIMER_SCHEDULER, + System.currentTimeMillis() / 1000L, + "1", + 10, + System.currentTimeMillis() / 1000L, + new TimerScheduledTask() { + @Override + public void run() { + try { + this.sendMessage(messageToSend); + } catch (InterruptedException e) { + System.out.println("Task 1 message interrupted"); + e.printStackTrace(); + } + System.out.println("Task 1 executed"); + } + })); + + Thread.sleep(1000); + } } -- cgit v1.2.3 From 26c750550b9fa6bdbebc8acbb39a34281205c140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Wed, 25 Dec 2019 12:53:39 +0100 Subject: Fix task type in timer message --- .../agent/messages/TimerSchedulerMessage.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java index 9a558b7..333b381 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/TimerSchedulerMessage.java @@ -1,14 +1,20 @@ 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,11 +38,11 @@ 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; } -- 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 --- .../cloudatlas/agent/modules/TimerScheduledTask.java | 12 ++++++++++++ .../mimuw/cloudatlas/agent/modules/TimerScheduler.java | 18 +++++++++++++++--- 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); } } -- cgit v1.2.3 From 44b899400150c0183fede3b530ab9aa850fa4fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Wed, 25 Dec 2019 12:55:28 +0100 Subject: Fix access in module members --- src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 04d002d..9744971 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java @@ -7,8 +7,8 @@ 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; public Module(AgentMessage.AgentModule moduleType) { this.moduleType = moduleType; -- cgit v1.2.3