diff options
Diffstat (limited to 'same/src/main/java')
3 files changed, 33 insertions, 19 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) |