summaryrefslogtreecommitdiff
path: root/same/src/main
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-15 18:56:40 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-15 18:56:40 +0100
commit171341543525b950a1db1692bab10d251a2245dc (patch)
tree1fee5d834b752ba026a7120aadaa0aab9c5bda2f /same/src/main
parent6f58c3a3ce15289e559360330896cfb0cc22ae4d (diff)
Make Broadcaster testable.
Diffstat (limited to 'same/src/main')
-rw-r--r--same/src/main/java/com/orbekk/same/Broadcaster.java11
-rw-r--r--same/src/main/java/com/orbekk/same/BroadcasterImpl.java (renamed from same/src/main/java/com/orbekk/same/BroadcastRunner.java)13
-rw-r--r--same/src/main/java/com/orbekk/same/MasterServiceImpl.java6
3 files changed, 22 insertions, 8 deletions
diff --git a/same/src/main/java/com/orbekk/same/Broadcaster.java b/same/src/main/java/com/orbekk/same/Broadcaster.java
new file mode 100644
index 0000000..ce1ad4f
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/Broadcaster.java
@@ -0,0 +1,11 @@
+package com.orbekk.same;
+
+import java.util.List;
+
+/**
+ * An interface for broadcasting a message to all clients.
+ */
+public interface Broadcaster {
+ public void broadcast(final List<String> targets,
+ final ServiceOperation operation);
+}
diff --git a/same/src/main/java/com/orbekk/same/BroadcastRunner.java b/same/src/main/java/com/orbekk/same/BroadcasterImpl.java
index 45935b6..688a388 100644
--- a/same/src/main/java/com/orbekk/same/BroadcastRunner.java
+++ b/same/src/main/java/com/orbekk/same/BroadcasterImpl.java
@@ -4,25 +4,28 @@ import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
-public class BroadcastRunner {
+public class BroadcasterImpl {
private ConnectionManager connections;
private Executor executor;
/**
* Get a BroadcastRunner for ClientService using a thread pool of size 20.
*/
- public static BroadcastRunner getDefaultBroadcastRunner() {
- return new BroadcastRunner(Executors.newFixedThreadPool(20));
+ public static BroadcasterImpl getDefaultBroadcastRunner() {
+ return new BroadcasterImpl(Executors.newFixedThreadPool(20),
+ new ConnectionManagerImpl());
}
- public BroadcastRunner(Executor executor, ConnectionManager connections) {
+ public BroadcasterImpl(Executor executor,
+ ConnectionManager connections) {
+ this.connections = connections;
this.executor = executor;
}
public synchronized void broadcast(final List<String> targets,
final ServiceOperation operation) {
for (String t : targets) {
- final ClientService client = connections.getConnection(t);
+ final ClientService client = connections.getClient(t);
executor.execute(new Runnable() {
@Override public void run() {
operation.run(client);
diff --git a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java
index dcebb80..6093fa4 100644
--- a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java
+++ b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java
@@ -10,10 +10,10 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable {
private ConnectionManager connections;
private State state;
private boolean stopped = false;
- private BroadcastRunner broadcaster;
+ private Broadcaster broadcaster;
public MasterServiceImpl(State initialState, ConnectionManager connections,
- BroadcastRunner broadcaster) {
+ Broadcaster broadcaster) {
state = initialState;
this.broadcaster = broadcaster;
}
@@ -45,7 +45,7 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable {
logger.info("Broadcasting new component {}", state.show(component));
broadcaster.broadcast(participants(), new ServiceOperation() {
- @Override void run(ClientService client) {
+ @Override public void run(ClientService client) {
client.setState(component, state.getDataOf(component),
state.getRevision(component));
}