diff options
Diffstat (limited to 'jsonrpc')
3 files changed, 64 insertions, 6 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java b/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java index a946c61..27579b5 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java @@ -55,6 +55,6 @@ public class SameServiceImpl implements SameService { @Override public void setState(String newState) { - logger.error("setState not implemented."); + sameState.setState(newState); } } 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(); } diff --git a/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java b/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java index 7e21e7a..4c2a11d 100644 --- a/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java +++ b/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java @@ -43,6 +43,22 @@ public class SameStateTest { connections.connections.put(state3.getUrl(), service3); } + public void joinNetwork() { + connections.getConnection(state1.getUrl()). + participateNetwork("Network1", state2.getClientId(), + state2.getUrl()); + connections.getConnection(state1.getUrl()). + participateNetwork("Network1", state3.getClientId(), + state3.getUrl()); + state1.internalRun(); + state2.internalRun(); + state3.internalRun(); + + assertTrue(state1.getParticipants().size() == 3); + assertTrue(state2.getParticipants().size() == 3); + assertTrue(state3.getParticipants().size() == 3); + } + @Test public void testJoinNetwork() { connections.getConnection(state1.getUrl()). participateNetwork("Network1", state2.getClientId(), @@ -69,5 +85,19 @@ public class SameStateTest { assertTrue(state3.getParticipants().size() == 3); assertEquals(state1.getNetworkName(), state2.getNetworkName()); assertEquals(state2.getNetworkName(), state3.getNetworkName()); + + assertEquals("Client1", state1.getMasterId()); + assertEquals("Client1", state2.getMasterId()); + assertEquals("Client1", state3.getMasterId()); + } + + @Test public void setState() { + joinNetwork(); + state1.librarySetNewState("New state1"); + state1.internalRun(); + state2.internalRun(); + state3.internalRun(); + assertEquals(state1.getCurrentState(), state2.getCurrentState()); + assertEquals(state2.getCurrentState(), state3.getCurrentState()); } } |