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 | |
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')
3 files changed, 35 insertions, 28 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameService.java b/jsonrpc/src/main/java/com/orbekk/same/SameService.java index 76e9035..0db9698 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameService.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameService.java @@ -1,6 +1,6 @@ package com.orbekk.same; -import java.util.List; +import java.util.Map; public interface SameService { /** @@ -20,5 +20,6 @@ public interface SameService { /** * Notification of participation in network. */ - void notifyParticipation(String networkName, List<String> participants); + void notifyParticipation(String networkName, + Map<String, String> participants); } diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java b/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java index 533a055..7e61a2d 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java @@ -1,7 +1,6 @@ package com.orbekk.same; -import java.util.List; -import java.util.LinkedList; +import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,12 +36,14 @@ public class SameServiceImpl implements SameService { @Override public void notifyParticipation(String networkName, - List<String> participants) { + Map<String, String> participants) { logger.info("Joining network {}.", networkName); int i = 1; - for (String participant : participants) { - logger.info(" {} participant {}: {}", - new Object[]{networkName, i, participant}); + for (Map.Entry<String, String> e : participants.entrySet()) { + String clientId = e.getKey(); + String url = e.getValue(); + logger.info(" {} participant {}: {}, {}", + new Object[]{networkName, i, clientId, url}); i++; } logger.warn("Joining not implemented."); 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); } |