From ca2f1ab4a37f0d590d5c116cd1fa7c341c77fad3 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Mon, 30 Dec 2019 22:16:46 +0100 Subject: Implement modular getZoneSet --- .../edu/mimuw/cloudatlas/agent/modules/Remik.java | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Remik.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Remik.java') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Remik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Remik.java new file mode 100644 index 0000000..9645d71 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Remik.java @@ -0,0 +1,54 @@ +package pl.edu.mimuw.cloudatlas.agent.modules; + +import java.util.concurrent.CompletableFuture; +import java.util.HashMap; +import java.util.Map; + + +import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.RemikMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.RequestStateMessage; +import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage; + +/* + * Remik is a cute little module that allows RMI functions to interface with + * agent's asynchronous modules. + */ +public class Remik extends Module { + private Map> awaitingRequests; + private long nextRequestId = 0; + + public Remik() { + super(ModuleType.RMI); + awaitingRequests = new HashMap(); + } + + public void handleTyped(RemikMessage message) throws InvalidMessageType, InterruptedException { + switch (message.getType()) { + case REQUEST_STATE: + handleRequestState((RequestStateMessage) message); + break; + default: + throw new InvalidMessageType("This type of message cannot be handled by Remik"); + } + } + + public void handleTyped(ResponseMessage message) { + CompletableFuture responseFuture = awaitingRequests.get(message.getRequestId()); + + if (responseFuture == null) { + System.out.println("ERROR: Remik got response for nonexistent/finished request"); + } else { + responseFuture.complete(message); + } + } + + private void handleRequestState(RequestStateMessage message) throws InterruptedException { + awaitingRequests.put(nextRequestId, message.getFuture()); + + GetStateMessage getStateMessage = new GetStateMessage("", 0, ModuleType.RMI, nextRequestId); + nextRequestId++; + + sendMessage(getStateMessage); + } +} -- cgit v1.2.3