m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu
diff options
context:
space:
mode:
authorMagdalena Grodzińska <mag.grodzinska@gmail.com>2019-12-23 19:06:41 +0100
committerMagdalena Grodzińska <mag.grodzinska@gmail.com>2019-12-23 19:06:41 +0100
commitcb173ac60b8307247efd83aaed5a0bb44a107766 (patch)
tree035dfe9ee0b1ce87e0ef60ca4a2dd6407566c5f6 /src/main/java/pl/edu
parent651c2b76ff3db39cefd1542d6f4c19d8686dfb85 (diff)
Add sending messages from modules
Diffstat (limited to 'src/main/java/pl/edu')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java12
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java22
2 files changed, 33 insertions, 1 deletions
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 0651a9b..f3cf2ea 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java
@@ -11,16 +11,20 @@ import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
public class Executor implements Runnable {
private Module module;
private LinkedBlockingQueue<AgentMessage> events;
+ private EventBus eventBus;
public Executor(Module module) {
this.module = module;
+ this.module.setExecutor(this);
this.events = new LinkedBlockingQueue<AgentMessage>();
}
public void run() {
+ System.out.println("Executor " + this.module.toString() + " running");
while (!Thread.currentThread().interrupted()) {
try {
AgentMessage event = events.take();
+ System.out.println("Executor " + this.module.toString() + " passed message to handle");
module.handle(event);
} catch (InterruptedException e) {
System.out.println("Executor interrupted. Exiting loop.");
@@ -32,4 +36,12 @@ public class Executor implements Runnable {
public void addMessage(AgentMessage event) throws InterruptedException {
events.put(event);
}
+
+ public void passMessage(AgentMessage event) throws InterruptedException {
+ eventBus.addMessage(event);
+ }
+
+ public void setEventBus(EventBus eventBus) {
+ this.eventBus = eventBus;
+ }
}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java
index 46df31b..167f3b7 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java
@@ -6,5 +6,25 @@ import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage;
* A Module is a (potentially stateful) event handler.
*/
public abstract class Module {
- public abstract void handle(AgentMessage event);
+ 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();
+ }
}