m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java
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/NewApiImplementation.java
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/NewApiImplementation.java')
-rw-r--r--src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java143
1 files changed, 143 insertions, 0 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java
new file mode 100644
index 0000000..def1d72
--- /dev/null
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/agent/NewApiImplementation.java
@@ -0,0 +1,143 @@
+package pl.edu.mimuw.cloudatlas.agent;
+
+import java.io.PrintStream;
+
+import java.rmi.RemoteException;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.List;
+import java.util.AbstractMap.SimpleImmutableEntry;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+import pl.edu.mimuw.cloudatlas.agent.messages.RequestStateMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.SetAttributeMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage;
+import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage;
+import pl.edu.mimuw.cloudatlas.interpreter.Interpreter;
+import pl.edu.mimuw.cloudatlas.interpreter.InterpreterException;
+import pl.edu.mimuw.cloudatlas.interpreter.Main;
+import pl.edu.mimuw.cloudatlas.interpreter.QueryResult;
+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.ValueContact;
+import pl.edu.mimuw.cloudatlas.model.Value;
+import pl.edu.mimuw.cloudatlas.model.ValueNull;
+import pl.edu.mimuw.cloudatlas.model.ValueQuery;
+import pl.edu.mimuw.cloudatlas.model.ValueSet;
+import pl.edu.mimuw.cloudatlas.model.ValueTime;
+import pl.edu.mimuw.cloudatlas.model.Type;
+import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
+import pl.edu.mimuw.cloudatlas.model.ZMI;
+import pl.edu.mimuw.cloudatlas.api.Api;
+
+public class NewApiImplementation implements Api {
+ private EventBus eventBus;
+
+ public NewApiImplementation(EventBus eventBus) {
+ this.eventBus = eventBus;
+ }
+
+ public Set<String> getZoneSet() throws RemoteException {
+ CompletableFuture<ResponseMessage> responseFuture = new CompletableFuture();
+ RequestStateMessage message = new RequestStateMessage("", 0, responseFuture);
+ try {
+ eventBus.addMessage(message);
+ ResponseMessage response = responseFuture.get();
+
+ if (response.getType() == ResponseMessage.Type.STATE) {
+ StateMessage stateMessage = (StateMessage) response;
+ Set<String> zones = new HashSet<String>();
+ collectZoneNames(stateMessage.getZMI(), zones);
+ return zones;
+ } else {
+ System.out.println("ERROR: getZoneSet didn't receive a StateMessage");
+ throw new Exception("Failed to retrieve zone set");
+ }
+ } catch (Exception e) {
+ System.out.println("ERROR: exception caught in getZoneSet");
+ throw new RemoteException(e.getMessage());
+ }
+ }
+
+ private void collectZoneNames(ZMI zone, Set<String> names) {
+ names.add(zone.getPathName().toString());
+ for (ZMI son : zone.getSons()) {
+ collectZoneNames(son, names);
+ }
+ }
+
+ public AttributesMap getZoneAttributeValues(String zoneName) throws RemoteException {
+ CompletableFuture<ResponseMessage> responseFuture = new CompletableFuture();
+ RequestStateMessage message = new RequestStateMessage("", 0, responseFuture);
+ try {
+ eventBus.addMessage(message);
+ ResponseMessage response = responseFuture.get();
+
+ if (response.getType() == ResponseMessage.Type.STATE) {
+ StateMessage stateMessage = (StateMessage) response;
+ return stateMessage.getZMI().findDescendant(zoneName).getAttributes();
+ } else {
+ System.out.println("ERROR: getZoneSet didn't receive a StateMessage");
+ throw new Exception("Failed to retrieve zone set");
+ }
+ } catch (Exception e) {
+ System.out.println("ERROR: exception caught in getZoneSet");
+ throw new RemoteException(e.getMessage());
+ }
+ }
+
+ public void installQuery(String name, String queryCode) throws RemoteException {
+ Pattern queryNamePattern = Pattern.compile("&[a-zA-Z][\\w_]*");
+ Matcher matcher = queryNamePattern.matcher(name);
+ if (!matcher.matches()) {
+ throw new RemoteException("Invalid query identifier");
+ }
+ try {
+ ValueQuery query = new ValueQuery(queryCode);
+ Attribute attributeName = new Attribute(name);
+ ValueTime timestamp = new ValueTime(System.currentTimeMillis());
+ Map<Attribute, Entry<ValueQuery, ValueTime>> queries = new HashMap();
+ queries.put(attributeName, new SimpleImmutableEntry(query, timestamp));
+ UpdateQueriesMessage message = new UpdateQueriesMessage("", 0, queries);
+ eventBus.addMessage(message);
+ } catch (Exception e) {
+ throw new RemoteException("Failed to install query", e);
+ }
+ }
+
+ public void uninstallQuery(String queryName) throws RemoteException {
+ try {
+ Attribute attributeName = new Attribute(queryName);
+ ValueTime timestamp = new ValueTime(System.currentTimeMillis());
+ Map<Attribute, Entry<ValueQuery, ValueTime>> queries = new HashMap();
+ queries.put(attributeName, new SimpleImmutableEntry(null, timestamp));
+ UpdateQueriesMessage message = new UpdateQueriesMessage("", 0, queries);
+ eventBus.addMessage(message);
+ } catch (Exception e) {
+ System.out.println("ERROR: failed to remove query");
+ throw new RemoteException("Failed to uninstall query", e);
+ }
+ }
+
+ public void setAttributeValue(String zoneName, String attributeName, Value value) throws RemoteException {
+ try {
+ SetAttributeMessage message = new SetAttributeMessage("", 0, zoneName, new Attribute(attributeName), value, new ValueTime(System.currentTimeMillis()));
+ eventBus.addMessage(message);
+ } catch (Exception e) {
+ System.out.println("ERROR: failed to set attribute");
+ throw new RemoteException("Failed to set attribute", e);
+ }
+ }
+
+ public void setFallbackContacts(Set<ValueContact> contacts) throws RemoteException {
+ // this.contacts = contacts;
+ }
+}