summaryrefslogtreecommitdiff
path: root/same/src/main/java/com
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 /same/src/main/java/com
parent411fbcf6bceb858daec6192cd920f2b13226a169 (diff)
Refactor client.
Hide ClientService inside ClientServiceImpl.
Diffstat (limited to 'same/src/main/java/com')
-rw-r--r--same/src/main/java/com/orbekk/same/ClientServiceImpl.java70
1 files changed, 43 insertions, 27 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;
}
}