diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 13:15:28 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 13:15:28 +0100 |
commit | df3a22794ea14ce702dbf17b2a57d6bcf0a8c9a8 (patch) | |
tree | 9086de128921155ee2127ecc51b98b0c42df71e7 /jsonrpc/src/main/java/com/orbekk/same/SameState.java | |
parent | 2b1a0ad7d0e2a7766c6c4e4f5f31a08fb8d89d63 (diff) |
Improve participant handling.
- Send correct list of participants in notifyParticipation().
- Add self to list of participants.
Diffstat (limited to 'jsonrpc/src/main/java/com/orbekk/same/SameState.java')
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/same/SameState.java | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameState.java b/jsonrpc/src/main/java/com/orbekk/same/SameState.java index c097a0d..cd1c84a 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameState.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameState.java @@ -2,6 +2,8 @@ package com.orbekk.same; import java.util.List; import java.util.LinkedList; +import java.util.Map; +import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,42 +15,43 @@ import org.slf4j.LoggerFactory; public class SameState extends Thread implements UrlReceiver { 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. + * The participants of this network. + * + * Maps clientId to url. */ - private String clientId; + private Map<String, String> participants = new HashMap<String, String>(); /** - * The URL of this participant. - * - * Important note: Our URL is unknown initially. Url is null until we - * receive our first request. The URL is then taken to be the target URL - * from the request. + * The client id of this participant. */ - private String url = null; + private String clientId; /** * Stopping condition for this thread. */ private boolean stopped = false; - /** - * Queue for pending participants. - */ - private List<String> pendingParticipants = new LinkedList<String>(); + private Map<String, String> pendingParticipants = + new HashMap<String, String>(); public SameState(String networkName, String clientId, ConnectionManager connections) { this.networkName = networkName; this.clientId = clientId; this.connections = connections; + participants.put(clientId, null); } - public synchronized List<String> getParticipants() { + /** + * Get participants as a list. + * + * List format: ["clientId1,url1", "clientId2,url2", ...] + */ + public synchronized Map<String, String> getParticipants() { return participants; } @@ -65,18 +68,18 @@ public class SameState extends Thread implements UrlReceiver { } public String getUrl() { - return url; + return participants.get(clientId); } @Override public void setUrl(String url) { logger.info("My URL is {}", url); - this.url = url; + participants.put(clientId, url); } public synchronized void addParticipant(String clientId, String url) { logger.info("PendingParticipant.add: {} ({})", clientId, url); - pendingParticipants.add(url); + pendingParticipants.put(clientId, url); notifyAll(); } @@ -85,9 +88,11 @@ public class SameState extends Thread implements UrlReceiver { // 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.putAll(pendingParticipants); + for (Map.Entry<String, String> e : pendingParticipants.entrySet()) { + String clientId = e.getKey(); + String url = e.getValue(); + logger.info("New participant: {} URL({})", clientId, url); SameService remoteService = connections.getConnection(url); remoteService.notifyParticipation(networkName, participants); } |