summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-31 13:02:35 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-31 13:02:35 +0100
commit66bbea9deadc0cf559e2c84d248f1b86a7e59467 (patch)
tree2bee7a917b4125644b0ef1d2843ab49664d11934
parent411fbcf6bceb858daec6192cd920f2b13226a169 (diff)
Refactor client.
Hide ClientService inside ClientServiceImpl.
-rw-r--r--same/src/main/java/com/orbekk/same/ClientServiceImpl.java70
-rw-r--r--same/src/test/java/com/orbekk/same/ClientServiceImplTest.java15
-rw-r--r--same/src/test/java/com/orbekk/same/MasterServiceImplTest.java12
3 files changed, 59 insertions, 38 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java
index f19d272..658bf37 100644
--- a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java
+++ b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java
@@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory;
import com.orbekk.util.WorkQueue;
-public class ClientServiceImpl implements ClientService, UrlReceiver,
+public class ClientServiceImpl implements UrlReceiver,
DiscoveryListener {
private Logger logger = LoggerFactory.getLogger(getClass());
private ConnectionManager connections;
@@ -20,6 +20,35 @@ public class ClientServiceImpl implements ClientService, UrlReceiver,
private StateChangedListener stateListener;
private NetworkNotificationListener networkListener;
+ private ClientService serviceImpl = new ClientService() {
+ @Override
+ public void setState(String component, String data, long revision) throws Exception {
+ boolean status = state.update(component, data, revision);
+ if (status) {
+ if (stateListener != null) {
+ stateListener.stateChanged(component, data);
+ }
+ } else {
+ logger.warn("Ignoring update: {}",
+ new State.Component(component, revision, data));
+ }
+ }
+
+ @Override
+ public void notifyNetwork(String networkName, String masterUrl) throws Exception {
+ logger.info("NotifyNetwork(networkName={}, masterUrl={})",
+ networkName, masterUrl);
+ if (networkListener != null) {
+ networkListener.notifyNetwork(networkName, masterUrl);
+ }
+ }
+
+ @Override
+ public void discoveryRequest(String remoteUrl) {
+ discoveryThread.add(remoteUrl);
+ }
+ };
+
private WorkQueue<String> discoveryThread = new WorkQueue<String>() {
@Override protected void onChange() {
List<String> pending = getAndClear();
@@ -41,28 +70,6 @@ public class ClientServiceImpl implements ClientService, UrlReceiver,
public void interrupt() {
discoveryThread.interrupt();
}
-
- @Override
- public void notifyNetwork(String networkName, String masterUrl) {
- logger.info("NotifyNetwork(networkName={}, masterUrl={})",
- networkName, masterUrl);
- if (networkListener != null) {
- networkListener.notifyNetwork(networkName, masterUrl);
- }
- }
-
- @Override
- public void setState(String component, String data, long revision) {
- boolean status = state.update(component, data, revision);
- if (status) {
- if (stateListener != null) {
- stateListener.stateChanged(component, data);
- }
- } else {
- logger.warn("Ignoring update: {}",
- new State.Component(component, revision, data));
- }
- }
@Override
public void setUrl(String myUrl) {
@@ -130,6 +137,16 @@ public class ClientServiceImpl implements ClientService, UrlReceiver,
this.networkListener = listener;
}
+ public void sendDiscoveryRequest(String url) {
+ try {
+ connections.getClient(url)
+ .discoveryRequest(myUrl);
+ } catch (Exception e) {
+ logger.warn("Failed to send discovery request: {}",
+ throwableToString(e));
+ }
+ }
+
@Override
public void discover(String url) {
String clientUrl = url + "ClientService.json";
@@ -144,9 +161,8 @@ public class ClientServiceImpl implements ClientService, UrlReceiver,
}
}
}
-
- @Override
- public void discoveryRequest(String remoteUrl) {
- discoveryThread.add(remoteUrl);
+
+ public ClientService getService() {
+ return serviceImpl;
}
}
diff --git a/same/src/test/java/com/orbekk/same/ClientServiceImplTest.java b/same/src/test/java/com/orbekk/same/ClientServiceImplTest.java
index 4523fe1..5f04485 100644
--- a/same/src/test/java/com/orbekk/same/ClientServiceImplTest.java
+++ b/same/src/test/java/com/orbekk/same/ClientServiceImplTest.java
@@ -10,24 +10,25 @@ public class ClientServiceImplTest {
private TestConnectionManager connections = new TestConnectionManager();
private TestBroadcaster broadcaster = new TestBroadcaster();
private ClientServiceImpl client = new ClientServiceImpl(state, connections);
+ private ClientService clientS = client.getService();
- @Test public void testSetState() {
- client.setState("TestState", "Test data", 100);
+ @Test public void testSetState() throws Exception {
+ clientS.setState("TestState", "Test data", 100);
assertEquals(100, state.getRevision("TestState"));
assertEquals("Test data", state.getDataOf("TestState"));
}
- @Test public void testNetworkListener() {
+ @Test public void testNetworkListener() throws Exception {
NetworkNotificationListener listener =
mock(NetworkNotificationListener.class);
client.setNetworkListener(listener);
- client.notifyNetwork("MyNetwork", "MasterUrl");
+ clientS.notifyNetwork("MyNetwork", "MasterUrl");
verify(listener).notifyNetwork("MyNetwork", "MasterUrl");
}
- @Test public void discover() {
- client.setState(".masterUrl", "master", 1);
- ClientServiceImpl mockClient = mock(ClientServiceImpl.class);
+ @Test public void discover() throws Exception {
+ clientS.setState(".masterUrl", "master", 1);
+ ClientService mockClient = mock(ClientService.class);
connections.clientMap.put("mockClient/ClientService.json",
mockClient);
client.discover("mockClient/");
diff --git a/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java b/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java
index a309e38..daa55a2 100644
--- a/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java
+++ b/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java
@@ -75,8 +75,9 @@ public class MasterServiceImplTest {
master.setUrl("http://master/");
ClientServiceImpl client = new ClientServiceImpl(
new State("ClientNetwork"), connections);
+ ClientService clientS = client.getService();
client.setUrl("http://client/");
- connections.clientMap.put("http://client/ClientService.json", client);
+ connections.clientMap.put("http://client/ClientService.json", clientS);
client.joinNetwork("http://master");
assertTrue(master._performWork());
assertTrue(state.getList(".participants").contains("http://client/ClientService.json"));
@@ -88,12 +89,14 @@ public class MasterServiceImplTest {
master.setUrl("http://master/");
ClientServiceImpl client1 = new ClientServiceImpl(
new State("ClientNetwork"), connections);
+ ClientService client1S = client1.getService();
client1.setUrl("http://client/");
- connections.clientMap.put("http://client/ClientService.json", client1);
+ connections.clientMap.put("http://client/ClientService.json", client1S);
ClientServiceImpl client2 = new ClientServiceImpl(
new State("ClientNetwork"), connections);
+ ClientService client2S = client2.getService();
client2.setUrl("http://client2/");
- connections.clientMap.put("http://client2/ClientService.json", client2);
+ connections.clientMap.put("http://client2/ClientService.json", client2S);
client1.joinNetwork("http://master");
client2.joinNetwork("http://master");
@@ -122,8 +125,9 @@ public class MasterServiceImplTest {
master.setUrl("http://master/");
ClientServiceImpl client = new ClientServiceImpl(
new State("ClientNetwork"), connections);
+ ClientService clientS = client.getService();
client.setUrl("http://client/");
- connections.clientMap.put("http://client/ClientService.json", client);
+ connections.clientMap.put("http://client/ClientService.json", clientS);
client.joinNetwork("http://master");
assertTrue(master._performWork());
assertTrue(state.getList(".participants").contains("http://client/ClientService.json"));