diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 10:47:27 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 10:47:27 +0100 |
commit | 52088a08a9edb6d4b60e1e8923e1b3199db9f391 (patch) | |
tree | 191f3bbde90eb14b011c772c27a5f67aff28049e /jsonrpc/src/main/java/com/orbekk/same/SameState.java | |
parent | 5befe48a4cca6121cbce7f7fd5436cfc09f643f0 (diff) |
Continue SameService implementation.
- Implement ConnectionManager.
- SameService now calls notifyParticipation() when a client tries to
join.
- Add client ids – this may or may not be a good idea.
- Refactor App and Client code.
Diffstat (limited to 'jsonrpc/src/main/java/com/orbekk/same/SameState.java')
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/same/SameState.java | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameState.java b/jsonrpc/src/main/java/com/orbekk/same/SameState.java index b2518c2..d7dd3dc 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameState.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameState.java @@ -12,9 +12,12 @@ import org.slf4j.LoggerFactory; */ public class SameState extends Thread { private Logger logger = LoggerFactory.getLogger(getClass()); + private ConnectionManager connections; private List<String> participants = new LinkedList<String>(); private String currentState = ""; private String networkName; + // The client id of this participant. + private String clientId; private boolean stopped = false; /** @@ -22,14 +25,21 @@ public class SameState extends Thread { */ private List<String> pendingParticipants = new LinkedList<String>(); - public SameState(String networkName) { + public SameState(String networkName, String clientId, + ConnectionManager connections) { this.networkName = networkName; + this.clientId = clientId; + this.connections = connections; } public synchronized List<String> getParticipants() { return participants; } + public String getClientId() { + return clientId; + } + public String getNetworkName() { return networkName; } @@ -38,18 +48,24 @@ public class SameState extends Thread { return currentState; } - public synchronized void addParticipant(String url) { + public synchronized void addParticipant(String clientId, String url) { synchronized(this) { - logger.info("Add pending participant: {}", url); + logger.info("Add pending participant: {} ({})", clientId, url); pendingParticipants.add(url); notifyAll(); } } private synchronized void handleNewParticipants() { + // Adding all pending participants ensures that each of the new + // participants is informed of all participants. + // + // TODO: Does not inform old participants. + participants.addAll(pendingParticipants); for (String url : pendingParticipants) { logger.info("New participant: {}", url); - participants.add(url); + SameService remoteService = connections.getConnection(url); + remoteService.notifyParticipation(networkName, participants); } pendingParticipants.clear(); } |