m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-12-29 14:12:31 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-12-29 14:12:31 +0100
commitaf6f6f37403d0e23a09a6ba4476271c2a0eaf623 (patch)
tree1b28c18d8dc4adffc24ea8baa762adad9e0b9d57
parent8e0aed0711a26fec5c0497cee9777744f1d48663 (diff)
Include timestamped queries in state
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/GetStateMessage.java (renamed from src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/GetHierarchyMessage.java)6
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/HierarchyMessage.java17
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/ResponseMessage.java2
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java2
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StateMessage.java29
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/modules/Stanik.java22
-rw-r--r--src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java41
7 files changed, 72 insertions, 47 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/GetHierarchyMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/GetStateMessage.java
index d3c749d..63392e8 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/GetHierarchyMessage.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/GetStateMessage.java
@@ -2,12 +2,12 @@ package pl.edu.mimuw.cloudatlas.agent.messages;
import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType;
-public class GetHierarchyMessage extends StanikMessage {
+public class GetStateMessage extends StanikMessage {
private ModuleType requestingModule;
private long requestId;
- public GetHierarchyMessage(String messageId, long timestamp, ModuleType requestingModule, long requestId) {
- super(messageId, timestamp, Type.GET_HIERARCHY);
+ public GetStateMessage(String messageId, long timestamp, ModuleType requestingModule, long requestId) {
+ super(messageId, timestamp, Type.GET_STATE);
this.requestingModule = requestingModule;
this.requestId = requestId;
}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/HierarchyMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/HierarchyMessage.java
deleted file mode 100644
index 9f7ca70..0000000
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/HierarchyMessage.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package pl.edu.mimuw.cloudatlas.agent.messages;
-
-import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType;
-import pl.edu.mimuw.cloudatlas.model.ZMI;
-
-public class HierarchyMessage extends ResponseMessage {
- private ZMI zmi;
-
- public HierarchyMessage(String messageId, ModuleType destinationModule, long timestamp, long requestId, ZMI zmi) {
- super(messageId, destinationModule, timestamp, Type.HIERARCHY, requestId);
- this.zmi = zmi;
- }
-
- public ZMI getZMI() {
- return zmi;
- }
-}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/ResponseMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/ResponseMessage.java
index 171cf07..02b3337 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/ResponseMessage.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/ResponseMessage.java
@@ -5,7 +5,7 @@ import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType;
public abstract class ResponseMessage extends AgentMessage {
public enum Type {
- HIERARCHY
+ STATE
}
Type type;
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java
index 8661c90..da35f2d 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StanikMessage.java
@@ -5,7 +5,7 @@ import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType;
public abstract class StanikMessage extends AgentMessage {
public enum Type {
- GET_HIERARCHY,
+ GET_STATE,
UPDATE_ATTRIBUTES
}
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StateMessage.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StateMessage.java
new file mode 100644
index 0000000..f7f490b
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/messages/StateMessage.java
@@ -0,0 +1,29 @@
+package pl.edu.mimuw.cloudatlas.agent.messages;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import pl.edu.mimuw.cloudatlas.agent.modules.ModuleType;
+import pl.edu.mimuw.cloudatlas.model.Attribute;
+import pl.edu.mimuw.cloudatlas.model.ValueQuery;
+import pl.edu.mimuw.cloudatlas.model.ValueTime;
+import pl.edu.mimuw.cloudatlas.model.ZMI;
+
+public class StateMessage extends ResponseMessage {
+ private ZMI zmi;
+ private Map<Attribute, Entry<ValueQuery, ValueTime>> queries;
+
+ public StateMessage(String messageId, ModuleType destinationModule, long timestamp, long requestId, ZMI zmi, Map<Attribute, Entry<ValueQuery, ValueTime>> queries) {
+ super(messageId, destinationModule, timestamp, Type.STATE, requestId);
+ this.zmi = zmi;
+ this.queries = queries;
+ }
+
+ public ZMI getZMI() {
+ return zmi;
+ }
+
+ public Map<Attribute, Entry<ValueQuery, ValueTime>> getQueries() {
+ return queries;
+ }
+}
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 76ca0b6..a31f265 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
@@ -1,20 +1,22 @@
package pl.edu.mimuw.cloudatlas.agent.modules;
import java.util.Iterator;
+import java.util.HashMap;
import java.util.Map.Entry;
import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
-import pl.edu.mimuw.cloudatlas.agent.messages.GetHierarchyMessage;
-import pl.edu.mimuw.cloudatlas.agent.messages.HierarchyMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StanikMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage;
-import pl.edu.mimuw.cloudatlas.model.AttributesMap;
import pl.edu.mimuw.cloudatlas.model.Attribute;
+import pl.edu.mimuw.cloudatlas.model.AttributesMap;
import pl.edu.mimuw.cloudatlas.model.PathName;
import pl.edu.mimuw.cloudatlas.model.Type;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.model.ValueBoolean;
+import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueString;
import pl.edu.mimuw.cloudatlas.model.ValueTime;
import pl.edu.mimuw.cloudatlas.model.ZMI;
@@ -27,17 +29,19 @@ public class Stanik extends Module {
}
private ZMI hierarchy;
+ private HashMap<Attribute, Entry<ValueQuery, ValueTime>> queries;
public Stanik() {
super(ModuleType.STATE);
hierarchy = new ZMI();
+ queries = new HashMap<Attribute, Entry<ValueQuery, ValueTime>>();
hierarchy.getAttributes().add("timestamp", new ValueTime(0l));
}
public void handleTyped(StanikMessage message) throws InterruptedException, InvalidMessageType {
switch(message.getType()) {
- case GET_HIERARCHY:
- handleGetHierarchy((GetHierarchyMessage) message);
+ case GET_STATE:
+ handleGetState((GetStateMessage) message);
break;
case UPDATE_ATTRIBUTES:
handleUpdateAttributes((UpdateAttributesMessage) message);
@@ -47,8 +51,8 @@ public class Stanik extends Module {
}
}
- public void handleGetHierarchy(GetHierarchyMessage message) throws InterruptedException {
- HierarchyMessage response = new HierarchyMessage("", message.getRequestingModule(), 0, message.getRequestId(), hierarchy.clone());
+ public void handleGetState(GetStateMessage message) throws InterruptedException {
+ StateMessage response = new StateMessage("", message.getRequestingModule(), 0, message.getRequestId(), hierarchy.clone(), (HashMap<Attribute, Entry<ValueQuery, ValueTime>>) queries.clone());
sendMessage(response);
}
@@ -141,4 +145,8 @@ public class Stanik extends Module {
public ZMI getHierarchy() {
return hierarchy;
}
+
+ public HashMap<Attribute, Entry<ValueQuery, ValueTime>> getQueries() {
+ return queries;
+ }
}
diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
index 12fa531..dd44e8f 100644
--- a/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
+++ b/src/test/java/pl/edu/mimuw/cloudatlas/agent/modules/StanikTest.java
@@ -1,11 +1,13 @@
package pl.edu.mimuw.cloudatlas.agent.modules;
+import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.Map.Entry;
import pl.edu.mimuw.cloudatlas.agent.messages.AgentMessage;
-import pl.edu.mimuw.cloudatlas.agent.messages.GetHierarchyMessage;
-import pl.edu.mimuw.cloudatlas.agent.messages.HierarchyMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage;
import pl.edu.mimuw.cloudatlas.agent.MockExecutor;
@@ -13,6 +15,7 @@ import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.model.ValueInt;
+import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueString;
import pl.edu.mimuw.cloudatlas.model.ValueTime;
import pl.edu.mimuw.cloudatlas.model.ZMI;
@@ -32,34 +35,36 @@ public class StanikTest {
}
@Test
- public void getEmptyHierarchy() throws Exception {
- GetHierarchyMessage message = new GetHierarchyMessage("test_msg", 0, ModuleType.TEST, 42);
+ public void getEmptyState() throws Exception {
+ GetStateMessage message = new GetStateMessage("test_msg", 0, ModuleType.TEST, 42);
stanik.handleTyped(message);
assertEquals(1, executor.messagesToPass.size());
ResponseMessage receivedMessage = (ResponseMessage) executor.messagesToPass.take();
assertEquals(ModuleType.TEST, receivedMessage.getDestinationModule());
- assertEquals(ResponseMessage.Type.HIERARCHY, receivedMessage.getType());
+ assertEquals(ResponseMessage.Type.STATE, receivedMessage.getType());
assertEquals(42, receivedMessage.getRequestId());
- HierarchyMessage hierarchyMessage = (HierarchyMessage) receivedMessage;
- ZMI zmi = hierarchyMessage.getZMI();
+ StateMessage stateMessage = (StateMessage) receivedMessage;
+ ZMI zmi = stateMessage.getZMI();
assertNull(zmi.getFather());
assertTrue(zmi.getSons().isEmpty());
- assertEquals(1, countAttributes(zmi.getAttributes()));
+ assertEquals(1, iterableSize(zmi.getAttributes()));
+ Map<Attribute, Entry<ValueQuery, ValueTime>> queries = stateMessage.getQueries();
+ assertEquals(0, iterableSize(queries.keySet()));
}
@Test
public void hierarchyIsDeepCopy() throws Exception {
- GetHierarchyMessage message = new GetHierarchyMessage("test_msg", 0, ModuleType.TEST, 42);
+ GetStateMessage message = new GetStateMessage("test_msg", 0, ModuleType.TEST, 42);
stanik.handleTyped(message);
- HierarchyMessage receivedMessage = (HierarchyMessage) executor.messagesToPass.poll();
+ StateMessage receivedMessage = (StateMessage) executor.messagesToPass.poll();
assertNotNull(receivedMessage);
AttributesMap attributes = receivedMessage.getZMI().getAttributes();
assertNull(attributes.getOrNull("foo"));
attributes.add("foo", new ValueInt(1337l));
- GetHierarchyMessage newMessage = new GetHierarchyMessage("test_msg2", 123, ModuleType.TEST, 43);
+ GetStateMessage newMessage = new GetStateMessage("test_msg2", 123, ModuleType.TEST, 43);
stanik.handleTyped(newMessage);
- HierarchyMessage newReceivedMessage = (HierarchyMessage) executor.messagesToPass.poll();
+ StateMessage newReceivedMessage = (StateMessage) executor.messagesToPass.poll();
AttributesMap newAttributes = newReceivedMessage.getZMI().getAttributes();
assertNull(newAttributes.getOrNull("foo"));
}
@@ -73,7 +78,7 @@ public class StanikTest {
UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/", attributes);
stanik.handleTyped(message);
AttributesMap actualAttributes = stanik.getHierarchy().getAttributes();
- assertEquals(3, countAttributes(actualAttributes));
+ assertEquals(3, iterableSize(actualAttributes));
assertEquals(new ValueInt(1337l), actualAttributes.get("foo"));
assertEquals(new ValueString("baz"), actualAttributes.get("bar"));
assertEquals(new ValueTime("2012/12/21 04:20:00.000"), actualAttributes.getOrNull("timestamp"));
@@ -89,7 +94,7 @@ public class StanikTest {
UpdateAttributesMessage message = new UpdateAttributesMessage("test_msg", 0, "/new", attributes);
stanik.handleTyped(message);
AttributesMap actualAttributes = stanik.getHierarchy().findDescendant("/new").getAttributes();
- assertEquals(4, countAttributes(actualAttributes));
+ assertEquals(4, iterableSize(actualAttributes));
assertEquals(new ValueInt(1337l), actualAttributes.getOrNull("foo"));
assertEquals(new ValueString("baz"), actualAttributes.getOrNull("bar"));
assertEquals(new ValueString("new"), actualAttributes.getOrNull("name"));
@@ -112,7 +117,7 @@ public class StanikTest {
stanik.handleTyped(newMessage);
AttributesMap actualAttributes = stanik.getHierarchy().getAttributes();
- assertEquals(2, countAttributes(actualAttributes));
+ assertEquals(2, iterableSize(actualAttributes));
assertEquals(new ValueInt(1338l), actualAttributes.getOrNull("foo"));
assertEquals(new ValueTime("2012/12/21 04:20:42.000"), actualAttributes.getOrNull("timestamp"));
}
@@ -132,14 +137,14 @@ public class StanikTest {
stanik.handleTyped(newMessage);
AttributesMap actualAttributes = stanik.getHierarchy().getAttributes();
- assertEquals(2, countAttributes(actualAttributes));
+ assertEquals(2, iterableSize(actualAttributes));
assertEquals(new ValueInt(1337l), actualAttributes.getOrNull("foo"));
assertEquals(new ValueTime("2012/12/21 04:20:00.000"), actualAttributes.getOrNull("timestamp"));
}
- public int countAttributes(AttributesMap attributes) {
+ public <T> int iterableSize(Iterable<T> iterable) {
int count = 0;
- for (Entry<Attribute, Value> attribute : attributes) {
+ for (T attribute : iterable) {
count++;
}