From 6f58c3a3ce15289e559360330896cfb0cc22ae4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Sun, 15 Jan 2012 18:47:27 +0100 Subject: Move old SameService files out of the way. --- same/old/App.java | 43 ++++++++ same/old/Client.java | 62 +++++++++++ same/old/SameService.java | 40 ++++++++ same/old/SameServiceImpl.java | 60 +++++++++++ same/old/SameState.java | 233 ++++++++++++++++++++++++++++++++++++++++++ same/old/SameStateTest.java | 105 +++++++++++++++++++ 6 files changed, 543 insertions(+) create mode 100644 same/old/App.java create mode 100644 same/old/Client.java create mode 100644 same/old/SameService.java create mode 100644 same/old/SameServiceImpl.java create mode 100644 same/old/SameState.java create mode 100644 same/old/SameStateTest.java (limited to 'same/old') diff --git a/same/old/App.java b/same/old/App.java new file mode 100644 index 0000000..5d94678 --- /dev/null +++ b/same/old/App.java @@ -0,0 +1,43 @@ +package com.orbekk.same; + +import com.googlecode.jsonrpc4j.JsonRpcServer; +import org.eclipse.jetty.server.Server; + +public class App { + public static void main(String[] args) { + if (args.length < 3) { + System.err.println("Arguments: port networkName clientId"); + System.exit(1); + } + int port = Integer.parseInt(args[0]); + String networkName = args[1]; + String clientId = args[2]; + + ConnectionManagerImpl connections = new ConnectionManagerImpl(); + + SameState sameState = new SameState(networkName, clientId, + connections); + sameState.start(); + + SameServiceImpl service = new SameServiceImpl(sameState); + JsonRpcServer jsonServer = new JsonRpcServer(service, + SameService.class); + + Server server = new Server(port); + RpcHandler rpcHandler = new RpcHandler(jsonServer, sameState); + server.setHandler(rpcHandler); + + try { + server.start(); + } catch (Exception e) { + System.out.println("Could not start jetty server."); + e.printStackTrace(); + } + + try { + server.join(); + } catch (InterruptedException e) { + System.out.println("Interrupt"); + } + } +} diff --git a/same/old/Client.java b/same/old/Client.java new file mode 100644 index 0000000..11a7449 --- /dev/null +++ b/same/old/Client.java @@ -0,0 +1,62 @@ +package com.orbekk.same; + +import com.googlecode.jsonrpc4j.JsonRpcServer; +import com.orbekk.net.HttpUtil; +import org.eclipse.jetty.server.Server; + +public class Client { + + public static void main(String[] args) { + if (args.length < 4) { + System.err.println("Arguments: port clientId thisNetworkName " + + "remoteNetworkAddr"); + System.exit(1); + } + int port = Integer.parseInt(args[0]); + String clientId = args[1]; + String networkName = args[2]; + String remoteAddr = args[3]; + + ConnectionManagerImpl connections = new ConnectionManagerImpl(); + + SameState sameState = new SameState(networkName, clientId, + connections); + sameState.start(); + + SameServiceImpl service = new SameServiceImpl(sameState); + JsonRpcServer jsonServer = new JsonRpcServer(service, + SameService.class); + + Server server = new Server(port); + RpcHandler rpcHandler = new RpcHandler(jsonServer, sameState); + server.setHandler(rpcHandler); + + try { + server.start(); + } catch (Exception e) { + System.out.println("Could not start jetty server."); + e.printStackTrace(); + } + + while (sameState.getUrl() == null) { + HttpUtil.sendHttpRequest(remoteAddr + "ping?port=" + port); + try { + Thread.sleep(500); + } catch (InterruptedException e) { + // Ignore interrupt in wait loop. + } + } + + SameService remoteService = connections.getConnection(remoteAddr); + remoteService.notifyNetwork("NoNetwork"); + remoteService.participateNetwork("FirstNetwork", + sameState.getClientId(), sameState.getUrl()); + + try { + server.join(); + } catch (InterruptedException e) { + System.out.println("Interrupt"); + } + + } +} diff --git a/same/old/SameService.java b/same/old/SameService.java new file mode 100644 index 0000000..8f239da --- /dev/null +++ b/same/old/SameService.java @@ -0,0 +1,40 @@ +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 participants); +} diff --git a/same/old/SameServiceImpl.java b/same/old/SameServiceImpl.java new file mode 100644 index 0000000..27579b5 --- /dev/null +++ b/same/old/SameServiceImpl.java @@ -0,0 +1,60 @@ +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 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 participants) { + sameState.setParticipants(participants); + } + + @Override + public void setState(String newState) { + sameState.setState(newState); + } +} diff --git a/same/old/SameState.java b/same/old/SameState.java new file mode 100644 index 0000000..369c5d7 --- /dev/null +++ b/same/old/SameState.java @@ -0,0 +1,233 @@ +// 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 _setParticipants = null; +// +// private Map pendingParticipants = +// new HashMap(); +// +// 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 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 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 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 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 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/same/old/SameStateTest.java b/same/old/SameStateTest.java new file mode 100644 index 0000000..13378a4 --- /dev/null +++ b/same/old/SameStateTest.java @@ -0,0 +1,105 @@ +// package com.orbekk.same; +// +// import static org.junit.Assert.*; +// +// import java.util.Map; +// import java.util.HashMap; +// import org.junit.Test; +// import org.junit.Before; +// +// public class SameStateTest { +// private MockConnectionManager connections; +// private SameState state1, state2, state3; +// private SameService service1, service2, service3; +// +// public static class MockConnectionManager implements ConnectionManager { +// public Map connections = +// new HashMap(); +// +// @Override +// public SameService getConnection(String url) { +// return connections.get(url); +// } +// } +// +// public SameStateTest() { +// } +// +// @Before public void setUp() { +// connections = new MockConnectionManager(); +// +// state1 = new SameState("Network1", "Client1", connections); +// state1.setUrl("test://client1"); +// service1 = new SameServiceImpl(state1); +// state2 = new SameState("Network2", "Client2", connections); +// state2.setUrl("test://client2"); +// service2 = new SameServiceImpl(state2); +// state3 = new SameState("Network3", "Client3", connections); +// state3.setUrl("test://client3"); +// service3 = new SameServiceImpl(state3); +// +// connections.connections.put(state1.getUrl(), service1); +// connections.connections.put(state2.getUrl(), service2); +// connections.connections.put(state3.getUrl(), service3); +// } +// +// public void joinNetwork() { +// connections.getConnection(state1.getUrl()). +// participateNetwork("Network1", state2.getClientId(), +// state2.getUrl()); +// connections.getConnection(state1.getUrl()). +// participateNetwork("Network1", state3.getClientId(), +// state3.getUrl()); +// state1.internalRun(); +// state2.internalRun(); +// state3.internalRun(); +// +// assertTrue(state1.getParticipants().size() == 3); +// assertTrue(state2.getParticipants().size() == 3); +// assertTrue(state3.getParticipants().size() == 3); +// } +// +// @Test public void testJoinNetwork() { +// connections.getConnection(state1.getUrl()). +// participateNetwork("Network1", state2.getClientId(), +// state2.getUrl()); +// assertTrue(state1.getParticipants().size() == 1); +// assertTrue(state2.getParticipants().size() == 1); +// +// state1.internalRun(); +// state2.internalRun(); +// +// assertTrue(state1.getParticipants().size() == 2); +// assertTrue(state2.getParticipants().size() == 2); +// assertEquals(state1.getNetworkName(), state2.getNetworkName()); +// +// connections.getConnection(state2.getUrl()). +// participateNetwork("Network1", state3.getClientId(), +// state3.getUrl()); +// state2.internalRun(); +// state1.internalRun(); +// state3.internalRun(); +// state2.internalRun(); +// +// assertTrue(state1.getParticipants().size() == 3); +// assertTrue(state2.getParticipants().size() == 3); +// assertTrue(state3.getParticipants().size() == 3); +// assertEquals(state1.getNetworkName(), state2.getNetworkName()); +// assertEquals(state2.getNetworkName(), state3.getNetworkName()); +// +// assertEquals("Client1", state1.getMasterId()); +// assertEquals("Client1", state2.getMasterId()); +// assertEquals("Client1", state3.getMasterId()); +// } +// +// @Test public void setState() { +// joinNetwork(); +// state1.librarySetNewState("New state1"); +// state1.internalRun(); +// state2.internalRun(); +// state3.internalRun(); +// assertEquals("New state1", state1.getCurrentState()); +// assertEquals("New state1", state2.getCurrentState()); +// assertEquals("New state1", state2.getCurrentState()); +// } +// } -- cgit v1.2.3