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 | |
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')
4 files changed, 52 insertions, 12 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/ConnectionManagerImpl.java b/jsonrpc/src/main/java/com/orbekk/same/ConnectionManagerImpl.java index e3a6928..841d5fa 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/ConnectionManagerImpl.java +++ b/jsonrpc/src/main/java/com/orbekk/same/ConnectionManagerImpl.java @@ -1,16 +1,31 @@ package com.orbekk.same; +import com.googlecode.jsonrpc4j.JsonRpcHttpClient; +import com.googlecode.jsonrpc4j.ProxyUtil; +import java.net.MalformedURLException; +import java.net.URL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ConnectionManagerImpl { +public class ConnectionManagerImpl implements ConnectionManager { + private Logger logger = LoggerFactory.getLogger(getClass()); public ConnectionManagerImpl() { } - SameService getConnection(String url) { - // TODO: Implement this class. - return null; + @Override + public SameService getConnection(String url) { + SameService service = null; + try { + JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(url)); + service = ProxyUtil.createProxy( + this.getClass().getClassLoader(), + SameService.class, + client); + } catch (MalformedURLException e) { + logger.warn("Unable to create client for {}, {}", url, e); + } + return service; } } diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameService.java b/jsonrpc/src/main/java/com/orbekk/same/SameService.java index d18cb9d..0a261a8 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameService.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameService.java @@ -13,8 +13,14 @@ public interface SameService { /** * A request from the callee to participate in 'networkName'. + * + * A client may not know its URL. If the url parameter is empty, + * use info from the HttpServletRequest. + * + * TODO: Always pass a valid URL and get rid of the port parameter. */ - void participateNetwork(String networkName, int remotePort); + void participateNetwork(String networkName, String clientId, + String url, int remotePort); /** * Notification of participation in network. diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java b/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java index 0052de3..e7484c1 100644 --- a/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java +++ b/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java @@ -26,13 +26,16 @@ public class SameServiceImpl implements SameService, CallerInfoListener { } @Override - public void participateNetwork(String networkName, int remotePort) { + public void participateNetwork(String networkName, String clientId, + String url, int remotePort) { if (!networkName.equals(sameState.getNetworkName())) { logger.warn("Client tried to join {}, but network name is {}.", networkName, sameState.getNetworkName()); } - String url = "http://" + currentCallerIp + ":" + remotePort; - sameState.addParticipant(url); + if (url.equals("")) { + url = "http://" + currentCallerIp + ":" + remotePort; + } + sameState.addParticipant(clientId, url); } @Override 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(); } |