summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-20 16:30:44 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-20 16:30:44 +0100
commit7fb62a6d592646df7b3a728399f830e7b4e68dec (patch)
tree8908a16de7ec407917b11afd57fa6444a40bbb22
parent2e0c6dd5ca704b85b15921e9e6707f6c3fcb3c9f (diff)
Implement interface for notification of state changes.
-rw-r--r--same/src/main/java/com/orbekk/same/ClientServiceImpl.java13
-rw-r--r--same/src/main/java/com/orbekk/same/Same.java32
-rw-r--r--same/src/main/java/com/orbekk/same/SameInterface.java3
-rw-r--r--same/src/main/java/com/orbekk/same/StateChangedListener.java5
4 files changed, 50 insertions, 3 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java
index 75df9cc..7865167 100644
--- a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java
+++ b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java
@@ -9,7 +9,8 @@ public class ClientServiceImpl implements ClientService, UrlReceiver {
private ConnectionManager connections;
private State state;
private String myUrl = null;
-
+ private StateChangedListener listener;
+
public ClientServiceImpl(State state, ConnectionManager connections) {
this.state = state;
this.connections = connections;
@@ -23,7 +24,11 @@ public class ClientServiceImpl implements ClientService, UrlReceiver {
@Override
public void setState(String component, String data, long revision) {
boolean status = state.update(component, data, revision);
- if (!status) {
+ if (status) {
+ if (listener != null) {
+ listener.stateChanged(component, data);
+ }
+ } else {
logger.warn("Ignoring update: {}",
new State.Component(component, revision, data));
}
@@ -86,4 +91,8 @@ public class ClientServiceImpl implements ClientService, UrlReceiver {
State testGetState() {
return state;
}
+
+ public void setStateChangedListener(StateChangedListener listener) {
+ this.listener = listener;
+ }
}
diff --git a/same/src/main/java/com/orbekk/same/Same.java b/same/src/main/java/com/orbekk/same/Same.java
index b70c7aa..d69061f 100644
--- a/same/src/main/java/com/orbekk/same/Same.java
+++ b/same/src/main/java/com/orbekk/same/Same.java
@@ -1,5 +1,6 @@
package com.orbekk.same;
+import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.type.TypeReference;
@@ -9,8 +10,27 @@ import org.slf4j.LoggerFactory;
public class Same implements SameInterface {
private Logger logger = LoggerFactory.getLogger(getClass());
private ClientServiceImpl client;
+ private StateChangedProxy stateChangedProxy = new StateChangedProxy();
- public Same(ClientServiceImpl client) {
+ private class StateChangedProxy implements StateChangedListener {
+ public List<StateChangedListener> listeners =
+ new ArrayList<StateChangedListener>();
+
+ @Override
+ public void stateChanged(String id, String data) {
+ for (StateChangedListener listener : listeners) {
+ listener.stateChanged(id, data);
+ }
+ }
+ }
+
+ public static Same createSame(ClientServiceImpl client) {
+ Same same = new Same(client);
+ client.setStateChangedListener(same.stateChangedProxy);
+ return same;
+ }
+
+ Same(ClientServiceImpl client) {
this.client = client;
}
@@ -39,4 +59,14 @@ public class Same implements SameInterface {
throw new RuntimeException("Not implemented.");
}
+ @Override
+ public void addStateChangedListener(StateChangedListener listener) {
+ stateChangedProxy.listeners.add(listener);
+ }
+
+ @Override
+ public void removeStateChangedListener(StateChangedListener listener) {
+ stateChangedProxy.listeners.remove(listener);
+ }
+
}
diff --git a/same/src/main/java/com/orbekk/same/SameInterface.java b/same/src/main/java/com/orbekk/same/SameInterface.java
index 7541636..280bd9d 100644
--- a/same/src/main/java/com/orbekk/same/SameInterface.java
+++ b/same/src/main/java/com/orbekk/same/SameInterface.java
@@ -32,4 +32,7 @@ public interface SameInterface {
* Set from an object: Pass it, e.g., a List<String>.
*/
void setObject(String id, Object data);
+
+ void addStateChangedListener(StateChangedListener listener);
+ void removeStateChangedListener(StateChangedListener listener);
}
diff --git a/same/src/main/java/com/orbekk/same/StateChangedListener.java b/same/src/main/java/com/orbekk/same/StateChangedListener.java
new file mode 100644
index 0000000..deb9369
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/StateChangedListener.java
@@ -0,0 +1,5 @@
+package com.orbekk.same;
+
+public interface StateChangedListener {
+ void stateChanged(String id, String data);
+}