diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 16:03:25 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 16:03:25 +0100 |
commit | 77a11fb4c2cc35a31e325efdf828f625be219207 (patch) | |
tree | ab93481ac37dd201f631edab5ddf7668ffcd29af /jsonrpc/src/main/java/com/orbekk/same/SameState.java | |
parent | ee0b57e81fce368e931a0d7282d8d84a9ecffd71 (diff) |
Add setState() operation.
- State is synchronized between all clients.
- Failure handling remains poor.
Diffstat (limited to 'jsonrpc/src/main/java/com/orbekk/same/SameState.java')
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/same/SameState.java | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameState.java b/jsonrpc/src/main/java/com/orbekk/same/SameState.java index d44cc1d..7cc8940 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameState.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameState.java @@ -16,6 +16,8 @@ public class SameState extends Thread implements UrlReceiver { private Logger logger = LoggerFactory.getLogger(getClass()); private ConnectionManager connections; private String currentState = ""; + private String _setState = null; + private String networkName; /** @@ -23,11 +25,8 @@ public class SameState extends Thread implements UrlReceiver { */ private String masterId; - /** - * TODO: Remove. - */ - public void setMasterId(String masterId) { - this.masterId = masterId; + public String getMasterId() { + return this.masterId; } /** @@ -106,6 +105,14 @@ public class SameState extends Thread implements UrlReceiver { return currentState; } + /** + * TODO: Move to a separate library. + */ + public void librarySetNewState(String newState) { + connections.getConnection(participants.get(masterId)) + .setState(newState); + } + public String getUrl() { return participants.get(clientId); } @@ -128,6 +135,12 @@ public class SameState extends Thread implements UrlReceiver { notifyAll(); } + public synchronized void setState(String newState) { + logger.info("Pending operation: _setState"); + _setState = newState; + notifyAll(); + } + private synchronized void handleSetParticipants() { if (_setParticipants != null) { if (isMaster()) { @@ -140,6 +153,20 @@ public class SameState extends Thread implements UrlReceiver { _setParticipants = null; } + public synchronized void handleSetState() { + if (_setState != null) { + if (isMaster()) { + broadcast(new ServiceOperation() { + @Override void run(SameService service) { + service.setState(_setState); + } + }); + } + currentState = _setState; + _setState = null; + } + } + private boolean isMaster() { return masterId.equals(clientId); } @@ -181,6 +208,7 @@ public class SameState extends Thread implements UrlReceiver { */ synchronized void internalRun() { handleNewParticipants(); + handleSetState(); handleSetParticipants(); } |