diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-18 12:43:18 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-18 12:43:18 +0100 |
commit | e048c5caf68df2b162c8f8f3d186f79fc5f59b57 (patch) | |
tree | f545ef7e911534e91f5d820b716c514bc398fde8 /same/src/main/java/com | |
parent | eae8848dd1484da88bbe09b7e16877dfdf7f78bb (diff) |
Fix race condition.
When new client joins: Add new participant to participants list before
broadcasting new state.
Diffstat (limited to 'same/src/main/java/com')
-rw-r--r-- | same/src/main/java/com/orbekk/same/MasterServiceImpl.java | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java index ced0dba..dc5eed3 100644 --- a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java @@ -24,24 +24,22 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable { } @Override - public void joinNetworkRequest(String clientUrl) { + public synchronized void joinNetworkRequest(String clientUrl) { logger.info("JoinNetworkRequest({})", clientUrl); List<String> participants = participants(); if (!participants.contains(clientUrl)) { participants.add(clientUrl); _fullStateReceivers.add(clientUrl); - synchronized(this) { - state.updateFromObject(".participants", participants, - state.getRevision(".participants") + 1); - notifyAll(); - } + state.updateFromObject(".participants", participants, + state.getRevision(".participants") + 1); + notifyAll(); } else { logger.warn("Client {} already part of network. " + "Ignoring participation request", clientUrl); } } - public boolean _sendUpdatedComponents() { + boolean _sendUpdatedComponents() { boolean worked = false; for (final Component component : state.getAndClearUpdatedComponents()) { logger.info("Broadcasting new component {}", component); @@ -57,11 +55,12 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable { return list; } - public synchronized boolean _sendFullState() { + synchronized boolean _sendFullState() { boolean hasWork = _fullStateReceivers.size() != 0; if (hasWork) { + logger.info("Sending full state to new participants."); final List<State.Component> components = state.getComponents(); - broadcastNewComponents(participants(), components); + broadcastNewComponents(_fullStateReceivers, components); _fullStateReceivers.clear(); } return hasWork; |