diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-16 21:16:34 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-16 21:16:34 +0100 |
commit | 489cd721117cdc5b66bbe3bc180bdfab7aaae301 (patch) | |
tree | d996bf9c7349490f906370cb576d04564d595640 | |
parent | 7a05213bc091688d5330212e9c785005ccb147b5 (diff) |
Fix network joining.
Prevent infinite loop in master thread.
-rw-r--r-- | same/src/main/java/com/orbekk/same/ClientApp.java | 2 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/ClientServiceImpl.java | 10 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/MasterApp.java | 4 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/MasterServiceImpl.java | 24 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/TestBroadcaster.java (renamed from same/src/test/java/com/orbekk/same/TestBroadcaster.java) | 0 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/TestConnectionManager.java (renamed from same/src/test/java/com/orbekk/same/TestConnectionManager.java) | 0 | ||||
-rw-r--r-- | same/src/test/java/com/orbekk/same/MasterServiceImplTest.java | 8 |
7 files changed, 34 insertions, 14 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientApp.java b/same/src/main/java/com/orbekk/same/ClientApp.java index 69997ca..bf391dc 100644 --- a/same/src/main/java/com/orbekk/same/ClientApp.java +++ b/same/src/main/java/com/orbekk/same/ClientApp.java @@ -42,6 +42,8 @@ public class ClientApp { } } + client.joinNetwork(masterUrl + "MasterService.json"); + try { server.join(); } catch (InterruptedException e) { diff --git a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java index c648abd..6675c15 100644 --- a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java @@ -34,6 +34,16 @@ public class ClientServiceImpl implements ClientService, UrlReceiver { return myUrl; } + public void joinNetwork(String masterUrl) { + if (myUrl != null) { + MasterService master = connections.getMaster(masterUrl); + master.joinNetworkRequest(myUrl); + } else { + logger.error("Tried to join network at {}, but my url is unknown. " + + "Run discovery service.", masterUrl); + } + } + State testGetState() { return state; } diff --git a/same/src/main/java/com/orbekk/same/MasterApp.java b/same/src/main/java/com/orbekk/same/MasterApp.java index c4154da..3e22563 100644 --- a/same/src/main/java/com/orbekk/same/MasterApp.java +++ b/same/src/main/java/com/orbekk/same/MasterApp.java @@ -22,6 +22,9 @@ public class MasterApp { RpcHandler rpcHandler = new RpcHandler(jsonServer, master); server.setHandler(rpcHandler); + Thread masterThread = new Thread(master); + masterThread.start(); + try { server.start(); } catch (Exception e) { @@ -30,6 +33,7 @@ public class MasterApp { try { server.join(); + masterThread.join(); } catch (InterruptedException e) { logger.info("Received exception. Exiting. {}", e); } diff --git a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java index 410def8..729c0ba 100644 --- a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java @@ -24,6 +24,7 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable { @Override public void joinNetworkRequest(String clientUrl) { + logger.info("JoinNetworkRequest({})", clientUrl); List<String> participants = participants(); if (!participants.contains(clientUrl)) { participants.add(clientUrl); @@ -53,17 +54,20 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable { return worked; } - public boolean _sendFullState() { - boolean worked = _fullStateReceivers.size() != 0; - final List<State.Component> components = state.getComponents(); - broadcaster.broadcast(participants(), new ServiceOperation() { - @Override public void run(ClientService client) { - for (Component c : components) { - client.setState(c.getName(), c.getData(), c.getRevision()); + public synchronized boolean _sendFullState() { + boolean hasWork = _fullStateReceivers.size() != 0; + if (hasWork) { + final List<State.Component> components = state.getComponents(); + broadcaster.broadcast(participants(), new ServiceOperation() { + @Override public void run(ClientService client) { + for (Component c : components) { + client.setState(c.getName(), c.getData(), c.getRevision()); + } } - } - }); - return worked; + }); + _fullStateReceivers.clear(); + } + return hasWork; } private List<String> participants() { diff --git a/same/src/test/java/com/orbekk/same/TestBroadcaster.java b/same/src/main/java/com/orbekk/same/TestBroadcaster.java index 434b107..434b107 100644 --- a/same/src/test/java/com/orbekk/same/TestBroadcaster.java +++ b/same/src/main/java/com/orbekk/same/TestBroadcaster.java diff --git a/same/src/test/java/com/orbekk/same/TestConnectionManager.java b/same/src/main/java/com/orbekk/same/TestConnectionManager.java index a41ec48..a41ec48 100644 --- a/same/src/test/java/com/orbekk/same/TestConnectionManager.java +++ b/same/src/main/java/com/orbekk/same/TestConnectionManager.java diff --git a/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java b/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java index da45e38..583e464 100644 --- a/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java +++ b/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java @@ -34,7 +34,7 @@ public class MasterServiceImplTest { @Test public void joinNetworkAddsClient() { - master.joinNetworkRequest("TestNetwork", "http://clientUrl"); + master.joinNetworkRequest("http://clientUrl"); List<String> participants = state.getList(".participants"); assertTrue(participants.contains("http://clientUrl")); } @@ -53,7 +53,7 @@ public class MasterServiceImplTest { new State("ClientNetwork"), connections); client.setUrl("http://client/"); connections.clientMap.put("http://client/ClientService.json", client); - master.joinNetworkRequest("TestNetwork", "http://client/ClientService.json"); + master.joinNetworkRequest("http://client/ClientService.json"); assertTrue(master._performWork()); assertTrue(state.getList(".participants").contains("http://client/ClientService.json")); assertEquals(state, client.testGetState()); @@ -71,8 +71,8 @@ public class MasterServiceImplTest { client1.setUrl("http://client2/"); connections.clientMap.put("http://client2/ClientService.json", client2); - master.joinNetworkRequest("TestNetwork", "http://client/ClientService.json"); - master.joinNetworkRequest("TestNetwork", "http://client2/ClientService.json"); + master.joinNetworkRequest("http://client/ClientService.json"); + master.joinNetworkRequest("http://client2/ClientService.json"); assertTrue(master._performWork()); assertTrue(state.getList(".participants").contains("http://client/ClientService.json")); |