m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules
diff options
context:
space:
mode:
authorMartin <marcin.j.chrzanowski@gmail.com>2020-01-03 18:29:55 +0100
committerGitHub <noreply@github.com>2020-01-03 18:29:55 +0100
commit9be545d6f2609d5fe8143a1964d44f6eee588ea2 (patch)
treef0bb8d159249e9ddf328e92d7fb5eda10122ab3b /src/main/java/pl/edu/mimuw/cloudatlas/agent/modules
parentfda20e7aa496926d4f4d78921925025040414d9b (diff)
parentec2da92e9636e219f5ad7caa73c9334dae12e5b1 (diff)
Merge pull request #83 from m-chrzan/remik
Use new modular architecture when implementing the RMI API
Diffstat (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/agent/modules')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Module.java6
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java14
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/RMI.java37
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Remik.java54
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java25
5 files changed, 90 insertions, 46 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 ba5e1d1..0a934cb 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
@@ -4,8 +4,8 @@ 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.QurnikMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.RemikMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage;
-import pl.edu.mimuw.cloudatlas.agent.messages.RMIMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage;
/*
@@ -37,8 +37,8 @@ public abstract class Module {
throw new InvalidMessageType("Got a QurnikMessage in module " + moduleType.toString());
}
- public void handleTyped(RMIMessage message) throws InterruptedException, InvalidMessageType {
- throw new InvalidMessageType("Got an RMIMessage in module " + moduleType.toString());
+ public void handleTyped(RemikMessage message) throws InterruptedException, InvalidMessageType {
+ throw new InvalidMessageType("Got a RemikMessage in module " + moduleType.toString());
}
public void handleTyped(StanikMessage message) throws InterruptedException, InvalidMessageType {
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java
index 9b7268a..1ab8a16 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Qurnik.java
@@ -80,13 +80,15 @@ public class Qurnik extends Module {
Interpreter interpreter = new Interpreter(zmi);
AttributesMap newAttributes = new AttributesMap();
for (ValueQuery query : queries) {
- try {
- List<QueryResult> result = interpreter.interpretProgram(query.getQuery());
- for(QueryResult r : result) {
- newAttributes.addOrChange(r.getName(), r.getValue());
+ if (query != null) {
+ try {
+ List<QueryResult> result = interpreter.interpretProgram(query.getQuery());
+ for(QueryResult r : result) {
+ newAttributes.addOrChange(r.getName(), r.getValue());
+ }
+ } catch(InterpreterException exception) {
+ System.out.println("ERROR: thrown while running interpreter: " + exception.getMessage());
}
- } catch(InterpreterException exception) {
- System.out.println("ERROR: thrown while running interpreter: " + exception.getMessage());
}
}
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
deleted file mode 100644
index 1a86fc7..0000000
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/RMI.java
+++ /dev/null
@@ -1,37 +0,0 @@
-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.RMIMessage;
-import pl.edu.mimuw.cloudatlas.api.Api;
-import pl.edu.mimuw.cloudatlas.interpreter.Main;
-import pl.edu.mimuw.cloudatlas.model.ZMI;
-
-import java.rmi.registry.LocateRegistry;
-import java.rmi.registry.Registry;
-import java.rmi.server.UnicastRemoteObject;
-
-public class RMI extends Module {
- public RMI(ModuleType moduleType) {
- super(moduleType);
- runRegistry();
- }
-
- public void runRegistry() {
- try {
- ZMI root = Main.createTestHierarchy2();
- ApiImplementation api = new ApiImplementation(root);
- Api apiStub =
- (Api) UnicastRemoteObject.exportObject(api, 0);
- Registry registry = LocateRegistry.getRegistry();
- registry.rebind("Api", apiStub);
- System.out.println("Agent: api bound");
- } catch (Exception e) {
- System.err.println("Agent registry initialization exception:");
- e.printStackTrace();
- }
- }
-
- public void handleTyped(RMIMessage event) throws InterruptedException {
- }
-}
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<Long, CompletableFuture<ResponseMessage>> 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<ResponseMessage> 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);
+ }
+}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
index b030901..3e5b790 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java
@@ -6,6 +6,7 @@ import java.util.Map.Entry;
import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.RemoveZMIMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.SetAttributeMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage;
@@ -49,6 +50,9 @@ public class Stanik extends Module {
case REMOVE_ZMI:
handleRemoveZMI((RemoveZMIMessage) message);
break;
+ case SET_ATTRIBUTE:
+ handleSetAttribte((SetAttributeMessage) message);
+ break;
case UPDATE_ATTRIBUTES:
handleUpdateAttributes((UpdateAttributesMessage) message);
break;
@@ -85,6 +89,27 @@ public class Stanik extends Module {
}
}
+ /*
+ * Always adds the new attribute.
+ * The zone must already exist.
+ * The zone's timestamp will be the maximum of its current timestamp or the
+ * timestamp provided with the new value.
+ */
+ public void handleSetAttribte(SetAttributeMessage message) {
+ try {
+ ZMI zmi = hierarchy.findDescendant(new PathName(message.getPathName()));
+ ValueTime updateTimestamp = message.getUpdateTimestamp();
+ ValueTime currentTimestamp = (ValueTime) zmi.getAttributes().getOrNull("timestamp");
+ if (ValueUtils.valueLower(currentTimestamp, updateTimestamp)) {
+ zmi.getAttributes().addOrChange("timestamp", updateTimestamp);
+ }
+
+ zmi.getAttributes().addOrChange(message.getAttribute(), message.getValue());
+ } catch (ZMI.NoSuchZoneException e) {
+ System.out.println("DEBUG: trying to set attribute in zone that doesn't exist");
+ }
+ }
+
public void handleUpdateAttributes(UpdateAttributesMessage message) {
try {
validateUpdateAttributesMessage(message);