summaryrefslogtreecommitdiff
path: root/same/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'same/src/main/java')
-rw-r--r--same/src/main/java/com/orbekk/same/Client.java17
-rw-r--r--same/src/main/java/com/orbekk/same/http/StateServlet.java26
2 files changed, 43 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java
index 2a5f11e..60faf1b 100644
--- a/same/src/main/java/com/orbekk/same/Client.java
+++ b/same/src/main/java/com/orbekk/same/Client.java
@@ -28,6 +28,23 @@ public class Client implements DiscoveryListener {
public State getState() {
return new State(state);
}
+
+ public void set(String name, String data) throws UpdateConflict {
+ String masterUrl = state.getDataOf(".masterUrl");
+ long revision = state.getRevision(name) + 1;
+ MasterService master = connections.getMaster(masterUrl);
+ try {
+ boolean success = master.updateStateRequest(name, data,
+ revision);
+ if (!success) {
+ throw new UpdateConflict("State update conflict when " +
+ "updating " + name);
+ }
+ } catch (Exception e) {
+ logger.error("Unable to contact master. Update fails.", e);
+ throw new UpdateConflict("Unable to contact master. Update fails.");
+ }
+ }
}
private ClientInterface clientInterface = new ClientInterface();
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 0ccf062..0ca1682 100644
--- a/same/src/main/java/com/orbekk/same/http/StateServlet.java
+++ b/same/src/main/java/com/orbekk/same/http/StateServlet.java
@@ -1,5 +1,7 @@
package com.orbekk.same.http;
+import static com.orbekk.same.StackTraceUtil.throwableToString;
+
import java.io.IOException;
import javax.servlet.http.HttpServlet;
@@ -10,6 +12,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orbekk.same.Client;
+import com.orbekk.same.UpdateConflict;
public class StateServlet extends HttpServlet {
private Logger logger = LoggerFactory.getLogger(getClass());
@@ -19,9 +22,32 @@ public class StateServlet extends HttpServlet {
this.client = client;
}
+ private void handleSetState(HttpServletRequest request,
+ HttpServletResponse response) throws IOException {
+ if (request.getParameter("key") == null ||
+ request.getParameter("value") == null) {
+ response.getWriter().println(
+ "Usage: action=set&key=DesiredKey&value=DesiredValue");
+ }
+
+ try {
+ client.set(request.getParameter("key"), request.getParameter("value"));
+ response.getWriter().println("Updated component: " +
+ request.getParameter("key") + "=" +
+ request.getParameter("value"));
+ } catch (UpdateConflict e) {
+ response.getWriter().println("Update conflict: " +
+ throwableToString(e));
+ }
+ }
+
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
+ if ("set".equals(request.getParameter("action"))) {
+ handleSetState(request, response);
+ }
+
response.setContentType("text/plain; charset=utf8");
response.getWriter().println(client.getState());
response.setStatus(HttpServletResponse.SC_OK);