summaryrefslogtreecommitdiff
path: root/same/src/main/java
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-27 16:33:01 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-27 16:33:01 +0100
commitdb6fb088c220d8c313aa8d2d3bdc1708ad7acf5a (patch)
tree9abc8d96a411c70c65eec3e8748d7db6bf2733a2 /same/src/main/java
parent145b1bd126603219dfe50b739dd3a019b43e5972 (diff)
Use DelayedOperation in ClientInterface.
Diffstat (limited to 'same/src/main/java')
-rw-r--r--same/src/main/java/com/orbekk/same/Client.java28
-rw-r--r--same/src/main/java/com/orbekk/same/ClientInterface.java4
-rw-r--r--same/src/main/java/com/orbekk/same/Variable.java4
-rw-r--r--same/src/main/java/com/orbekk/same/VariableFactory.java5
-rw-r--r--same/src/main/java/com/orbekk/same/http/StateServlet.java18
5 files changed, 31 insertions, 28 deletions
diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java
index 5b361ec..b18f523 100644
--- a/same/src/main/java/com/orbekk/same/Client.java
+++ b/same/src/main/java/com/orbekk/same/Client.java
@@ -10,6 +10,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orbekk.same.State.Component;
+import com.orbekk.util.DelayedOperation;
import com.orbekk.util.WorkQueue;
public class Client implements DiscoveryListener {
@@ -32,27 +33,28 @@ public class Client implements DiscoveryListener {
return new State(state);
}
- public void set(String name, String data, long revision)
- throws UpdateConflict {
+ // TODO: Do this asynchronously? Currently this is already achieved
+ // on Android, which makes the Java and Android versions different.
+ @Override
+ public DelayedOperation set(Component component) {
+ DelayedOperation op = new DelayedOperation();
String masterUrl = state.getDataOf(".masterUrl");
MasterService master = connections.getMaster(masterUrl);
try {
- boolean success = master.updateStateRequest(name, data,
- revision);
+ boolean success = master.updateStateRequest(
+ component.getName(), component.getData(),
+ component.getRevision());
if (!success) {
- throw new UpdateConflict("State update conflict when " +
- "updating " + name);
+ op.complete(DelayedOperation.Status
+ .createConflict("Conflict from master"));
}
} catch (Exception e) {
logger.error("Unable to contact master. Update fails.", e);
- throw new UpdateConflict("Unable to contact master. Update fails.");
+ String e_ = throwableToString(e);
+ op.complete(DelayedOperation.Status.createError(
+ "Error contacting master. Update fails: " + e_));
}
- }
-
- @Override
- public void set(Component component) throws UpdateConflict {
- set(component.getName(), component.getData(),
- component.getRevision());
+ return op;
}
@Override
diff --git a/same/src/main/java/com/orbekk/same/ClientInterface.java b/same/src/main/java/com/orbekk/same/ClientInterface.java
index c534fce..af8edfa 100644
--- a/same/src/main/java/com/orbekk/same/ClientInterface.java
+++ b/same/src/main/java/com/orbekk/same/ClientInterface.java
@@ -1,8 +1,10 @@
package com.orbekk.same;
+import com.orbekk.util.DelayedOperation;
+
public interface ClientInterface {
State getState();
- void set(State.Component component) throws UpdateConflict;
+ DelayedOperation set(State.Component component);
void addStateListener(StateChangedListener listener);
void removeStateListener(StateChangedListener listener);
} \ No newline at end of file
diff --git a/same/src/main/java/com/orbekk/same/Variable.java b/same/src/main/java/com/orbekk/same/Variable.java
index bea8664..4d42909 100644
--- a/same/src/main/java/com/orbekk/same/Variable.java
+++ b/same/src/main/java/com/orbekk/same/Variable.java
@@ -1,5 +1,7 @@
package com.orbekk.same;
+import com.orbekk.util.DelayedOperation;
+
public interface Variable<T> {
public interface OnChangeListener<T> {
/**
@@ -11,7 +13,7 @@ public interface Variable<T> {
}
T get();
- void set(T value) throws UpdateConflict;
+ DelayedOperation set(T value);
void update();
void setOnChangeListener(OnChangeListener<T> listener);
}
diff --git a/same/src/main/java/com/orbekk/same/VariableFactory.java b/same/src/main/java/com/orbekk/same/VariableFactory.java
index f1796d6..fa9eeeb 100644
--- a/same/src/main/java/com/orbekk/same/VariableFactory.java
+++ b/same/src/main/java/com/orbekk/same/VariableFactory.java
@@ -10,6 +10,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orbekk.same.State.Component;
+import com.orbekk.util.DelayedOperation;
/**
* TODO: Use WeakReference in order to make variables GC-able.
@@ -37,12 +38,12 @@ public class VariableFactory {
}
@Override
- public void set(T value) throws UpdateConflict {
+ public DelayedOperation set(T value) {
try {
String serializedValue = mapper.writeValueAsString(value);
State.Component update = new State.Component(identifier,
revision, serializedValue);
- client.set(update);
+ return client.set(update);
} catch (JsonGenerationException e) {
logger.warn("Failed to convert to JSON: {}", value);
logger.warn("Parse exception.", e);
diff --git a/same/src/main/java/com/orbekk/same/http/StateServlet.java b/same/src/main/java/com/orbekk/same/http/StateServlet.java
index 578bdfc..efc4237 100644
--- a/same/src/main/java/com/orbekk/same/http/StateServlet.java
+++ b/same/src/main/java/com/orbekk/same/http/StateServlet.java
@@ -37,18 +37,14 @@ public class StateServlet extends HttpServlet {
"Usage: action=set&key=DesiredKey&value=DesiredValue");
}
- try {
- String key = request.getParameter("key");
- String value = request.getParameter("value");
- Variable<String> variable = variableFactory.createString(key);
- variable.set(value);
+ String key = request.getParameter("key");
+ String value = request.getParameter("value");
+ Variable<String> variable = variableFactory.createString(key);
- response.getWriter().println("Updated component: " +
- key + "=" + value);
- } catch (UpdateConflict e) {
- response.getWriter().println("Update conflict: " +
- throwableToString(e));
- }
+ variable.set(value).waitFor();
+
+ response.getWriter().println("Updated component: " +
+ key + "=" + value);
}
@Override