From 8b076ed54b692381a3c1410b704bdad33ad5ca0b Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 26 Dec 2019 15:27:44 +0100 Subject: Refactor handle (#75) * Refactor passing messages to handler * Add RMIMessage skeleton * Handle RMI message in RMI module --- .../edu/mimuw/cloudatlas/agent/modules/Module.java | 24 +++++++++++++++++++--- .../mimuw/cloudatlas/agent/modules/ModuleType.java | 12 +++++++++++ .../pl/edu/mimuw/cloudatlas/agent/modules/RMI.java | 8 +++----- .../cloudatlas/agent/modules/TimerScheduler.java | 9 ++++---- 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/ModuleType.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules') 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 9744971..a50a95f 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 @@ -2,19 +2,37 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import pl.edu.mimuw.cloudatlas.agent.Executor; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.RMIMessage; /* * A Module is a (potentially stateful) event handler. */ public abstract class Module { - protected AgentMessage.AgentModule moduleType; + public class InvalidMessageType extends Exception { + public InvalidMessageType(String message) { + super(message); + } + } + + protected ModuleType moduleType; protected Executor executor; - public Module(AgentMessage.AgentModule moduleType) { + public Module(ModuleType moduleType) { this.moduleType = moduleType; } - public abstract void handle(AgentMessage event) throws InterruptedException ; + public void handle(AgentMessage event) throws InterruptedException, InvalidMessageType { + event.callMe(this); + } + + public void handleTyped(TimerSchedulerMessage message) throws InterruptedException, InvalidMessageType { + throw new InvalidMessageType("Got a TimerSchedulerMessage in module " + moduleType.toString()); + } + + public void handleTyped(RMIMessage message) throws InterruptedException, InvalidMessageType { + throw new InvalidMessageType("Got an RMIMessage in module " + moduleType.toString()); + } public void setExecutor(Executor executor) { this.executor = executor; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/ModuleType.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/ModuleType.java new file mode 100644 index 0000000..ff4a92e --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/ModuleType.java @@ -0,0 +1,12 @@ +package pl.edu.mimuw.cloudatlas.agent.modules; + +public enum ModuleType { + TIMER_SCHEDULER, + TIMER_GTP, + RMI, + UDP, + GOSSIP_IN, + GOSSIP_OUT, + STATE, + QUERY +} diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/RMI.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/RMI.java index 452b214..1a86fc7 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/RMI.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/RMI.java @@ -2,7 +2,7 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import pl.edu.mimuw.cloudatlas.agent.ApiImplementation; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; -import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.RMIMessage; import pl.edu.mimuw.cloudatlas.api.Api; import pl.edu.mimuw.cloudatlas.interpreter.Main; import pl.edu.mimuw.cloudatlas.model.ZMI; @@ -12,7 +12,7 @@ import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; public class RMI extends Module { - public RMI(AgentMessage.AgentModule moduleType) { + public RMI(ModuleType moduleType) { super(moduleType); runRegistry(); } @@ -32,8 +32,6 @@ public class RMI extends Module { } } - @Override - public void handle(AgentMessage event) throws InterruptedException { - assert event.getDestinationModule() == event.getCorrectMessageType(); + public void handleTyped(RMIMessage event) throws InterruptedException { } } 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 9390940..68a9326 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 @@ -2,6 +2,7 @@ package pl.edu.mimuw.cloudatlas.agent.modules; import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage; import pl.edu.mimuw.cloudatlas.agent.messages.TimerSchedulerMessage; +import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType; import java.util.Timer; @@ -17,17 +18,15 @@ import java.util.Timer; public class TimerScheduler extends Module { private Timer timer; - public TimerScheduler(AgentMessage.AgentModule moduleType) { + public TimerScheduler(ModuleType moduleType) { super(moduleType); - assert moduleType == AgentMessage.AgentModule.TIMER_SCHEDULER; + assert moduleType == ModuleType.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; + public void handleTyped(TimerSchedulerMessage timerEvent) throws InterruptedException { addTask(timerEvent); } -- cgit v1.2.3