From df3a22794ea14ce702dbf17b2a57d6bcf0a8c9a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Wed, 11 Jan 2012 13:15:28 +0100 Subject: Improve participant handling. - Send correct list of participants in notifyParticipation(). - Add self to list of participants. --- .../src/main/java/com/orbekk/same/SameState.java | 45 ++++++++++++---------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'jsonrpc/src/main/java/com/orbekk/same/SameState.java') 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 participants = new LinkedList(); 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 participants = new HashMap(); /** - * 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 pendingParticipants = new LinkedList(); + private Map pendingParticipants = + new HashMap(); public SameState(String networkName, String clientId, ConnectionManager connections) { this.networkName = networkName; this.clientId = clientId; this.connections = connections; + participants.put(clientId, null); } - public synchronized List getParticipants() { + /** + * Get participants as a list. + * + * List format: ["clientId1,url1", "clientId2,url2", ...] + */ + public synchronized Map 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 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); } -- cgit v1.2.3