diff options
5 files changed, 65 insertions, 13 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientApp.java b/same/src/main/java/com/orbekk/same/ClientApp.java index 970f16d..3095a59 100644 --- a/same/src/main/java/com/orbekk/same/ClientApp.java +++ b/same/src/main/java/com/orbekk/same/ClientApp.java @@ -12,7 +12,7 @@ public class ClientApp { private Server server; private static final int timeout = 1000; - public ClientServiceImpl getClient(int port, String networkName, + public SameInterface getClient(int port, String networkName, String masterUrl) { logger.info("Starting client with port:{}, networkName:{}, masterUrl:{}", new Object[]{port, networkName, masterUrl}); @@ -46,7 +46,7 @@ public class ClientApp { } client.joinNetwork(masterUrl + "MasterService.json"); - return client; + return new Same(client); } public void run(int port, String networkName, diff --git a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java index 46cdd86..75df9cc 100644 --- a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java @@ -1,5 +1,6 @@ package com.orbekk.same; +import org.codehaus.jackson.type.TypeReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,16 +53,29 @@ public class ClientServiceImpl implements ClientService, UrlReceiver { "Run discovery service.", masterUrl); } } + + String lib_get(String name) { + return state.getDataOf(name); + } + + <T> T lib_get(String name, TypeReference<T> type) { + return state.getParsedData(name, type); + } - public boolean sendStateUpdate(String componentName, String data, - long revision) { + void lib_set(String name, String data) throws UpdateConflict { String masterUrl = state.getDataOf(".masterUrl"); + long revision = state.getRevision(name) + 1; MasterService master = connections.getMaster(masterUrl); try { - return master.updateStateRequest(componentName, data, revision); + 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."); - return false; + throw new UpdateConflict("Unable to contact master. Update fails."); } } diff --git a/same/src/main/java/com/orbekk/same/Same.java b/same/src/main/java/com/orbekk/same/Same.java new file mode 100644 index 0000000..b70c7aa --- /dev/null +++ b/same/src/main/java/com/orbekk/same/Same.java @@ -0,0 +1,42 @@ +package com.orbekk.same; + +import java.util.List; + +import org.codehaus.jackson.type.TypeReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Same implements SameInterface { + private Logger logger = LoggerFactory.getLogger(getClass()); + private ClientServiceImpl client; + + public Same(ClientServiceImpl client) { + this.client = client; + } + + @Override + public String get(String id) { + return client.lib_get(id); + } + + @Override + public <T> T get(String id, TypeReference<T> type) { + return client.lib_get(id, type); + } + + @Override + public List<String> getList(String id) { + return client.lib_get(id, new TypeReference<List<String>>() { }); + } + + @Override + public void set(String id, String data) throws UpdateConflict { + client.lib_set(id, data); + } + + @Override + public void setObject(String id, Object data) { + throw new RuntimeException("Not implemented."); + } + +} diff --git a/same/src/main/java/com/orbekk/same/SameInterface.java b/same/src/main/java/com/orbekk/same/SameInterface.java index a778f39..7541636 100644 --- a/same/src/main/java/com/orbekk/same/SameInterface.java +++ b/same/src/main/java/com/orbekk/same/SameInterface.java @@ -29,13 +29,6 @@ public interface SameInterface { void set(String id, String data) throws UpdateConflict; /** - * Set the state. - * - * Retry until there is no conflict. - */ - void forceSet(String id, String data); - - /** * Set from an object: Pass it, e.g., a List<String>. */ void setObject(String id, Object data); diff --git a/same/src/main/java/com/orbekk/same/UpdateConflict.java b/same/src/main/java/com/orbekk/same/UpdateConflict.java index 35fbb01..4ea5887 100644 --- a/same/src/main/java/com/orbekk/same/UpdateConflict.java +++ b/same/src/main/java/com/orbekk/same/UpdateConflict.java @@ -1,4 +1,7 @@ package com.orbekk.same; public class UpdateConflict extends Exception { + public UpdateConflict(String string) { + super(string); + } } |