diff options
Diffstat (limited to 'jsonrpc/src/main/java/com/orbekk/same')
7 files changed, 0 insertions, 462 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/ConnectionManager.java b/jsonrpc/src/main/java/com/orbekk/same/ConnectionManager.java deleted file mode 100644 index 985f6f0..0000000 --- a/jsonrpc/src/main/java/com/orbekk/same/ConnectionManager.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.orbekk.same; - -/** - * An interface that returns a connection for a participant. - * - * When testing, this interface can be mocked to use local participants only. - */ -public interface ConnectionManager { - SameService getConnection(String url); -} diff --git a/jsonrpc/src/main/java/com/orbekk/same/ConnectionManagerImpl.java b/jsonrpc/src/main/java/com/orbekk/same/ConnectionManagerImpl.java deleted file mode 100644 index 841d5fa..0000000 --- a/jsonrpc/src/main/java/com/orbekk/same/ConnectionManagerImpl.java +++ /dev/null @@ -1,31 +0,0 @@ -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 implements ConnectionManager { - - private Logger logger = LoggerFactory.getLogger(getClass()); - - public ConnectionManagerImpl() { - } - - @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 deleted file mode 100644 index 8f239da..0000000 --- a/jsonrpc/src/main/java/com/orbekk/same/SameService.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.orbekk.same; - -import java.util.Map; - -public interface SameService { - /** - * A notification that 'networkName' exists. - * - * This is called by any participant of a network after a broadcast - * has been performed. - */ - void notifyNetwork(String networkName); - - /** - * A request from the callee to participate in 'networkName'. - */ - void participateNetwork(String networkName, String clientId, String url); - - /** - * Notification of participation in network. - */ - void notifyParticipation(String networkName, String masterId); - - /** - * New state. - * - * When sent to a non-master from the master, use 'newState' as the - * current state. - * - * When sent to a master, broadcast the new state to all clients. - */ - void setState(String newState); - - /** - * Notify all nodes of network participants. - * - * Only sent from master to non-master. - */ - void setParticipants(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 deleted file mode 100644 index 27579b5..0000000 --- a/jsonrpc/src/main/java/com/orbekk/same/SameServiceImpl.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.orbekk.same; - -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SameServiceImpl implements SameService { - private Logger logger = LoggerFactory.getLogger(getClass()); - private SameState sameState; - - public SameServiceImpl(SameState sameState) { - this.sameState = sameState; - } - - @Override - public void notifyNetwork(String networkName) { - logger.info("Notification from network " + networkName); - } - - @Override - public void participateNetwork(String networkName, String clientId, - String url) { - if (!networkName.equals(sameState.getNetworkName())) { - logger.warn("Client tried to join {}, but network name is {}.", - networkName, sameState.getNetworkName()); - return; - } - if (clientId.equals("") || url.equals("")) { - logger.warn("Missing client info: ClientId({}), URL({}).", - clientId, url); - return; - } - sameState.addParticipant(clientId, url); - } - - @Override - public void notifyParticipation(String networkName, String masterId) { - logger.info("Joining network {}. Master is {}", networkName, masterId); - // int i = 1; - // 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++; - // } - sameState.joinNetwork(networkName, masterId); - } - - @Override - public void setParticipants(Map<String, String> participants) { - sameState.setParticipants(participants); - } - - @Override - public void setState(String newState) { - sameState.setState(newState); - } -} diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameState.java b/jsonrpc/src/main/java/com/orbekk/same/SameState.java deleted file mode 100644 index c9cd216..0000000 --- a/jsonrpc/src/main/java/com/orbekk/same/SameState.java +++ /dev/null @@ -1,233 +0,0 @@ -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; - -/** - * The implementation of a 'Same' state. - * - * This class manages the current state of the Same protocol. - */ -public class SameState extends Thread implements UrlReceiver { - private Logger logger = LoggerFactory.getLogger(getClass()); - private ConnectionManager connections; - - // TODO: Change the name of State. - private com.orbekk.same.State state = - new com.orbekk.same.State(); - - /** - * The client id of this participant. - */ - private String clientId; - - /** - * Stopping condition for this thread. - */ - private boolean stopped = false; - - private String _setState = null; - private Map<String, String> _setParticipants = null; - - private Map<String, String> pendingParticipants = - new HashMap<String, String>(); - - public SameState(String networkName, String clientId, - ConnectionManager connections) { - state.setNetworkName(networkName); - this.clientId = clientId; - this.connections = connections; - state.setMasterId(clientId); - state.getParticipants().put(clientId, null); - } - - public String getMasterId() { - return state.getMasterId(); - } - - public synchronized Map<String, String> getParticipants() { - return state.getParticipants(); - } - - /** - * Reset this SameService to an initial state. - * - * TODO: Implement fully. - */ - private synchronized void resetState() { - state = new com.orbekk.same.State(); - pendingParticipants.clear(); - } - - public synchronized void joinNetwork(String networkName, String masterId) { - resetState(); - state.setNetworkName(networkName); - state.setMasterId(masterId); - logger.info("Joined network {}.", networkName); - } - - public String getClientId() { - return clientId; - } - - public String getNetworkName() { - return state.getNetworkName(); - } - - public String getCurrentState() { - return state.getData(); - } - - /** - * TODO: Move to a separate library. - */ - public void librarySetNewState(String newState) { - connections.getConnection( - state.getParticipants().get(state.getMasterId())) - .setState(newState); - } - - public String getUrl() { - return state.getParticipants().get(clientId); - } - - @Override - public void setUrl(String url) { - logger.info("My URL is {}", url); - state.getParticipants().put(clientId, url); - } - - public synchronized void addParticipant(String clientId, String url) { - logger.info("PendingParticipant.add: {} ({})", clientId, url); - pendingParticipants.put(clientId, url); - notifyAll(); - } - - public synchronized void setParticipants(Map<String, String> participants) { - logger.info("Pending operation: _setParticipants"); - _setParticipants = participants; - notifyAll(); - } - - public synchronized void setState(String newState) { - logger.info("Pending operation: _setState"); - _setState = newState; - notifyAll(); - } - - private synchronized void handleSetParticipants() { - if (_setParticipants != null) { - if (isMaster()) { - logger.error("{}: Master received setParticipants.", clientId); - } else { - logger.info("{}: New participants committed.", clientId); - state.getParticipants().clear(); - state.getParticipants().putAll(_setParticipants); - } - } - _setParticipants = null; - } - - public synchronized void handleSetState() { - if (_setState != null) { - if (isMaster()) { - broadcast(new ServiceOperation() { - @Override void run(SameService service) { - service.setState(_setState); - } - }); - } - state.setData(_setState); - _setState = null; - } - } - - private boolean isMaster() { - return state.getMasterId().equals(clientId); - } - - private synchronized void handleNewParticipants() { - if (!isMaster()) { - for (Map.Entry<String, String> e : pendingParticipants.entrySet()) { - SameService master = connections.getConnection( - state.getParticipants().get(state.getMasterId())); - logger.info("Redirecting participant request to {}", - state.getMasterId()); - String clientId = e.getKey(); - String url = e.getValue(); - master.participateNetwork(state.getNetworkName(), clientId, - url); - } - } else { - state.getParticipants().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(state.getNetworkName(), - state.getMasterId()); - broadcast(new ServiceOperation(){ - @Override void run(SameService service) { - service.setParticipants(state.getParticipants()); - } - }); - } - } - pendingParticipants.clear(); - } - - /** - * This method runs the pending commands to SameState. - * - * It should be called by the worker thread, but can be called directly - * for testing purposes to avoid threading in unit tests. - */ - synchronized void internalRun() { - handleNewParticipants(); - handleSetState(); - handleSetParticipants(); - } - - public synchronized void run() { - while (!stopped) { - internalRun(); - try { - wait(1000); - } catch (InterruptedException e) { - // Ignore interrupt in wait loop. - } - } - } - - public synchronized void stopSame() { - try { - stopped = true; - notifyAll(); - this.join(); - } catch (InterruptedException e) { - logger.warn("Got InterruptedException while waiting for SameState " + - "to finish. Ignoring."); - } - } - - public abstract static class ServiceOperation { - abstract void run(SameService service); - } - - public synchronized void broadcast(ServiceOperation operation) { - for (Map.Entry<String, String> e : - state.getParticipants().entrySet()) { - String clientId = e.getKey(); - String url = e.getValue(); - if (!clientId.equals(this.clientId)) { - operation.run(connections.getConnection(url)); - } - } - } -} diff --git a/jsonrpc/src/main/java/com/orbekk/same/State.java b/jsonrpc/src/main/java/com/orbekk/same/State.java deleted file mode 100644 index 70ffa83..0000000 --- a/jsonrpc/src/main/java/com/orbekk/same/State.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.orbekk.same; - -import java.util.Map; -import java.util.HashMap; - -public class State { - private long stateIteration = 0; - private Map<String, String> participants = new HashMap<String, String>(); - private String networkName = ""; - private String masterId = ""; - private String data = ""; - - public long getStateIteration() { - return stateIteration; - } - - public void setStateIteration(long stateIteration) { - this.stateIteration = stateIteration; - } - - public Map<String, String> getParticipants() { - return participants; - } - - public String getNetworkName() { - return networkName; - } - - public void setNetworkName(String networkName) { - this.networkName = networkName; - } - - public String getMasterId() { - return masterId; - } - - public void setMasterId(String masterId) { - this.masterId = masterId; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - @Override - public String toString() { - StringBuilder participantsString = new StringBuilder(); - participantsString.append("["); - boolean first = true; - for (Map.Entry<String, String> e : participants.entrySet()) { - if (!first) { - participantsString.append(", "); - } - first = false; - participantsString.append(e.getKey()) - .append("(") - .append(e.getValue()) - .append(")"); - String clientId = e.getKey(); - String url = e.getValue(); - } - participantsString.append("]"); - - return String.format( - "State( \n" + - " stateIteration = %d,\n" + - " networkName = %s,\n" + - " masterId = %s,\n" + - " data = %s,\n" + - " participants = %s\n" + - ")", stateIteration, networkName, masterId, data, - participantsString); - } -} diff --git a/jsonrpc/src/main/java/com/orbekk/same/UrlReceiver.java b/jsonrpc/src/main/java/com/orbekk/same/UrlReceiver.java deleted file mode 100644 index 31a0276..0000000 --- a/jsonrpc/src/main/java/com/orbekk/same/UrlReceiver.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.orbekk.same; - -/** - * An interface to get notified of the URL to this computer. - * - * This interface is used to reliably obtain the URL of this host. - */ -public interface UrlReceiver { - void setUrl(String url); -} |