diff options
5 files changed, 49 insertions, 45 deletions
diff --git a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java index a870372..0854458 100644 --- a/same/src/main/java/com/orbekk/same/ClientServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/ClientServiceImpl.java @@ -15,7 +15,7 @@ public class ClientServiceImpl implements DiscoveryListener { private Logger logger = LoggerFactory.getLogger(getClass()); private ConnectionManager connections; private State state; - private String myUrl = null; + private String myUrl; private StateChangedListener stateListener; private NetworkNotificationListener networkListener; @@ -57,9 +57,11 @@ public class ClientServiceImpl implements DiscoveryListener { } }; - public ClientServiceImpl(State state, ConnectionManager connections) { + public ClientServiceImpl(State state, ConnectionManager connections, + String myUrl) { this.state = state; this.connections = connections; + this.myUrl = myUrl; } public void start() { @@ -75,18 +77,13 @@ public class ClientServiceImpl implements DiscoveryListener { } public void joinNetwork(String masterUrl) { - if (myUrl != null) { - MasterService master = connections.getMaster(masterUrl); - state.clear(); - try { - master.joinNetworkRequest(myUrl); - } catch (Exception e) { - logger.error("Unable to connect to master.", e); - } - } else { - logger.error("Tried to join network at {}, but my url is unknown. " + - "Run discovery service.", masterUrl); - } + MasterService master = connections.getMaster(masterUrl); + state.clear(); + try { + master.joinNetworkRequest(myUrl); + } catch (Exception e) { + logger.error("Unable to connect to master.", e); + } } String lib_get(String name) { diff --git a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java index 4a842ab..32a8781 100644 --- a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java +++ b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java @@ -17,9 +17,17 @@ public class MasterServiceImpl implements MasterService, Runnable { private List<String> _fullStateReceivers = new ArrayList<String>(); private Thread workerThread = null; - public MasterServiceImpl(State initialState, ConnectionManager connections, + public static MasterServiceImpl create(ConnectionManager connections, + Broadcaster broadcaster, String myUrl) { + State state = new State("DefaultMaster"); + state.update(".masterUrl", myUrl, 1); + return new MasterServiceImpl(state, connections, broadcaster); + } + + /** Constructor for internal use. + */ + private MasterServiceImpl(State initialState, ConnectionManager connections, Broadcaster broadcaster) { - state = initialState; this.connections = connections; this.broadcaster = broadcaster; } @@ -140,6 +148,7 @@ public class MasterServiceImpl implements MasterService, Runnable { if (workerThread == null) { workerThread = new Thread(this); workerThread.start(); + logger.info("Master thread started. {}", state); } } diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java index 2faf669..d898c1d 100644 --- a/same/src/main/java/com/orbekk/same/SameController.java +++ b/same/src/main/java/com/orbekk/same/SameController.java @@ -31,9 +31,17 @@ public class SameController { State state = new State("Default"); Broadcaster broadcaster = BroadcasterImpl.getDefaultBroadcastRunner(); - MasterServiceImpl master = new MasterServiceImpl(state, connections, - broadcaster); - ClientServiceImpl client = new ClientServiceImpl(state, connections); + String baseUrl = String.format("http://%s:%s/", + configuration.get("localIp"), configuration.getInt("port")); + + String masterUrl = baseUrl + "MasterService.json"; + String clientUrl = baseUrl + "ClientService.json"; + + MasterServiceImpl master = MasterServiceImpl.create( + connections, broadcaster, masterUrl); + + ClientServiceImpl client = new ClientServiceImpl(state, connections, + clientUrl); PaxosServiceImpl paxos = new PaxosServiceImpl(""); ServerContainer server = new ServerBuilder(port) diff --git a/same/src/test/java/com/orbekk/same/ClientServiceImplTest.java b/same/src/test/java/com/orbekk/same/ClientServiceImplTest.java index 2cae247..2c5604a 100644 --- a/same/src/test/java/com/orbekk/same/ClientServiceImplTest.java +++ b/same/src/test/java/com/orbekk/same/ClientServiceImplTest.java @@ -8,8 +8,8 @@ import static org.mockito.Mockito.*; public class ClientServiceImplTest { private State state = new State("ClientNetwork"); private TestConnectionManager connections = new TestConnectionManager(); - private TestBroadcaster broadcaster = new TestBroadcaster(); - private ClientServiceImpl client = new ClientServiceImpl(state, connections); + private ClientServiceImpl client = new ClientServiceImpl(state, connections, + "http://client/ClientService.json"); private ClientService clientS = client.getService(); @Test public void testSetState() throws Exception { diff --git a/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java b/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java index daa55a2..d5126b6 100644 --- a/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java +++ b/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java @@ -12,9 +12,9 @@ public class MasterServiceImplTest { private State state = new State("TestNetwork"); private TestConnectionManager connections = new TestConnectionManager(); private TestBroadcaster broadcaster = new TestBroadcaster(); - private MasterServiceImpl master = new MasterServiceImpl(state, - connections, broadcaster); - + private MasterServiceImpl master = MasterServiceImpl.create( + connections, broadcaster, "http://master/MasterService.json"); + public static class UnreachableClient implements ClientService { @Override public void notifyNetwork(String networkName, String masterUrl) @@ -36,14 +36,7 @@ public class MasterServiceImplTest { @Before public void setUp() { - connections.masterMap.put("http://master", master); - } - - @Test - public void setsMasterUrl() { - master.setUrl("http://10.0.0.54:10050/"); - assertEquals("http://10.0.0.54:10050/MasterService.json", - state.getDataOf(".masterUrl")); + connections.masterMap.put("http://master/MasterService.json", master); } @Test @@ -72,11 +65,10 @@ public class MasterServiceImplTest { @Test public void clientJoin() { - master.setUrl("http://master/"); ClientServiceImpl client = new ClientServiceImpl( - new State("ClientNetwork"), connections); + new State("ClientNetwork"), connections, + "http://client/ClientService.json"); ClientService clientS = client.getService(); - client.setUrl("http://client/"); connections.clientMap.put("http://client/ClientService.json", clientS); client.joinNetwork("http://master"); assertTrue(master._performWork()); @@ -86,20 +78,19 @@ public class MasterServiceImplTest { @Test public void validStateRequest() { - master.setUrl("http://master/"); ClientServiceImpl client1 = new ClientServiceImpl( - new State("ClientNetwork"), connections); + new State("ClientNetwork"), connections, + "http://client/ClientService.json"); ClientService client1S = client1.getService(); - client1.setUrl("http://client/"); connections.clientMap.put("http://client/ClientService.json", client1S); ClientServiceImpl client2 = new ClientServiceImpl( - new State("ClientNetwork"), connections); + new State("ClientNetwork"), connections, + "http://client2/ClientService.json"); ClientService client2S = client2.getService(); - client2.setUrl("http://client2/"); connections.clientMap.put("http://client2/ClientService.json", client2S); - client1.joinNetwork("http://master"); - client2.joinNetwork("http://master"); + client1.joinNetwork("http://master/MasterService.json"); + client2.joinNetwork("http://master/MasterService.json"); assertTrue(master._performWork()); assertTrue(state.getList(".participants").contains("http://client/ClientService.json")); @@ -122,11 +113,10 @@ public class MasterServiceImplTest { @Test public void masterRemovesParticipant() { - master.setUrl("http://master/"); ClientServiceImpl client = new ClientServiceImpl( - new State("ClientNetwork"), connections); + new State("ClientNetwork"), connections, + "http://client/ClientService.json"); ClientService clientS = client.getService(); - client.setUrl("http://client/"); connections.clientMap.put("http://client/ClientService.json", clientS); client.joinNetwork("http://master"); assertTrue(master._performWork()); |