summaryrefslogtreecommitdiff
path: root/same
diff options
context:
space:
mode:
Diffstat (limited to 'same')
-rw-r--r--same/src/main/java/com/orbekk/same/Client.java20
-rw-r--r--same/src/test/java/com/orbekk/same/ClientTest.java9
2 files changed, 22 insertions, 7 deletions
diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java
index 4c03f31..3799041 100644
--- a/same/src/main/java/com/orbekk/same/Client.java
+++ b/same/src/main/java/com/orbekk/same/Client.java
@@ -2,6 +2,7 @@ package com.orbekk.same;
import static com.orbekk.same.StackTraceUtil.throwableToString;
+import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.type.TypeReference;
@@ -15,7 +16,8 @@ public class Client implements DiscoveryListener {
private ConnectionManager connections;
private State state;
private String myUrl;
- private StateChangedListener stateListener;
+ private List<StateChangedListener> stateListeners =
+ new ArrayList<StateChangedListener>();
private NetworkNotificationListener networkListener;
public class ClientInterface {
@@ -44,6 +46,14 @@ public class Client implements DiscoveryListener {
throw new UpdateConflict("Unable to contact master. Update fails.");
}
}
+
+ public void addStateListener(StateChangedListener listener) {
+ stateListeners.add(listener);
+ }
+
+ public void removeStateListener(StateChangedListener listener) {
+ stateListeners.remove(listener);
+ }
}
private ClientInterface clientInterface = new ClientInterface();
@@ -53,8 +63,8 @@ public class Client implements DiscoveryListener {
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(state.getComponent(component));
+ for (StateChangedListener listener : stateListeners) {
+ listener.stateChanged(state.getComponent(component));
}
} else {
logger.warn("Ignoring update: {}",
@@ -153,10 +163,6 @@ public class Client implements DiscoveryListener {
return state;
}
- public void setStateChangedListener(StateChangedListener listener) {
- this.stateListener = listener;
- }
-
public void setNetworkListener(NetworkNotificationListener listener) {
this.networkListener = listener;
}
diff --git a/same/src/test/java/com/orbekk/same/ClientTest.java b/same/src/test/java/com/orbekk/same/ClientTest.java
index a4671d4..91f9760 100644
--- a/same/src/test/java/com/orbekk/same/ClientTest.java
+++ b/same/src/test/java/com/orbekk/same/ClientTest.java
@@ -34,4 +34,13 @@ public class ClientTest {
client.discover("mockClient/ClientService.json");
verify(mockClient).notifyNetwork("ClientNetwork", "master");
}
+
+ @Test public void stateListenerReceivesUpdate() throws Exception {
+ StateChangedListener listener = mock(StateChangedListener.class);
+ client.getInterface().addStateListener(listener);
+ clientS.setState("StateListenerVariable", "100", 1);
+ State.Component component = state.getComponent("StateListenerVariable");
+ assertEquals("100", component.getData());
+ verify(listener).stateChanged(eq(component));
+ }
}