diff options
-rw-r--r-- | same/src/main/java/com/orbekk/same/ClientServiceImpl.java | 7 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/State.java | 10 |
2 files changed, 15 insertions, 2 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java index 6675c15..36918cf 100644 --- a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java @@ -21,7 +21,11 @@ public class ClientServiceImpl implements ClientService, UrlReceiver { @Override public void setState(String component, String data, long revision) { - state.forceUpdate(component, data, revision); + boolean status = state.update(component, data, revision); + if (!status) { + logger.warn("Ignoring update: {}", + new State.Component(component, revision, data)); + } } @Override @@ -37,6 +41,7 @@ public class ClientServiceImpl implements ClientService, UrlReceiver { public void joinNetwork(String masterUrl) { if (myUrl != null) { MasterService master = connections.getMaster(masterUrl); + state.clear(); master.joinNetworkRequest(myUrl); } else { logger.error("Tried to join network at {}, but my url is unknown. " + diff --git a/same/src/main/java/com/orbekk/same/State.java b/same/src/main/java/com/orbekk/same/State.java index 4179dcb..c352990 100644 --- a/same/src/main/java/com/orbekk/same/State.java +++ b/same/src/main/java/com/orbekk/same/State.java @@ -30,6 +30,12 @@ public class State { updateFromObject(".participants", new ArrayList<String>(), 0); } + public synchronized void clear() { + logger.info("Clearing state."); + updatedComponents.clear(); + state.clear(); + } + public synchronized void forceUpdate(String componentName, String data, long revision) { Component oldComponent = state.get(componentName); @@ -48,12 +54,14 @@ public class State { component = state.get(componentName); } - if (revision == component.getRevision()) { + if (revision >= component.getRevision()) { + Component oldComponent = new Component(component); component.setName(componentName); component.setRevision(revision + 1); component.setData(data); state.put(componentName, component); updatedComponents.add(componentName); + logger.info("Updated state: {} => {}", oldComponent, component); return true; } else { return false; |