diff options
author | Martin <marcin.j.chrzanowski@gmail.com> | 2019-12-22 17:29:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-22 17:29:25 +0100 |
commit | 651c2b76ff3db39cefd1542d6f4c19d8686dfb85 (patch) | |
tree | 4184366307a88f149e68b6d4ab79f253e004982e /src/main/java/pl/edu/mimuw/cloudatlas | |
parent | 3fff4cef0bf33c7fa88dba92c81be7e402ec2bc9 (diff) |
Implement executor (#65)
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas')
3 files changed, 52 insertions, 0 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 new file mode 100644 index 0000000..47cb1ff --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/EventBus.java @@ -0,0 +1,7 @@ +package pl.edu.mimuw.cloudatlas.agent; + +/* + * The EventBus routes messages sent between Modules. + */ +public class EventBus { +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java new file mode 100644 index 0000000..0651a9b --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Executor.java @@ -0,0 +1,35 @@ +package pl.edu.mimuw.cloudatlas.agent; + +import java.util.concurrent.LinkedBlockingQueue; + +import pl.edu.mimuw.cloudatlas.agent.message.AgentMessage; + +/* + * Queues messages sent to a particular module and ensures they are eventually + * handled. + */ +public class Executor implements Runnable { + private Module module; + private LinkedBlockingQueue<AgentMessage> events; + + public Executor(Module module) { + this.module = module; + this.events = new LinkedBlockingQueue<AgentMessage>(); + } + + public void run() { + while (!Thread.currentThread().interrupted()) { + try { + AgentMessage event = events.take(); + module.handle(event); + } catch (InterruptedException e) { + System.out.println("Executor interrupted. Exiting loop."); + break; + } + } + } + + public void addMessage(AgentMessage event) throws InterruptedException { + events.put(event); + } +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java new file mode 100644 index 0000000..46df31b --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/Module.java @@ -0,0 +1,10 @@ +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 { + public abstract void handle(AgentMessage event); +} |