summaryrefslogtreecommitdiff
path: root/same
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-13 15:23:05 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-13 15:23:05 +0100
commitf41eca9bee17f45981e41c273c3a50f5d04ff426 (patch)
tree05d70ee720cdebaa86f3af571d745a63c4e06dcb /same
parentb256d125695da6b2a8eedd6f59ad07e94bd2b0bc (diff)
Clients can become new masters.
Diffstat (limited to 'same')
-rw-r--r--same/src/main/java/com/orbekk/same/Master.java22
-rw-r--r--same/src/test/java/com/orbekk/same/MasterTest.java3
2 files changed, 22 insertions, 3 deletions
diff --git a/same/src/main/java/com/orbekk/same/Master.java b/same/src/main/java/com/orbekk/same/Master.java
index 6e08c48..4402487 100644
--- a/same/src/main/java/com/orbekk/same/Master.java
+++ b/same/src/main/java/com/orbekk/same/Master.java
@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory;
public class Master {
private Logger logger = LoggerFactory.getLogger(getClass());
private final ConnectionManager connections;
+ private String myUrl;
State state;
private Broadcaster broadcaster;
@@ -17,14 +18,15 @@ public class Master {
Broadcaster broadcaster, String myUrl, String networkName) {
State state = new State(networkName);
state.update(".masterUrl", myUrl, 1);
- return new Master(state, connections, broadcaster);
+ return new Master(state, connections, broadcaster, myUrl);
}
Master(State initialState, ConnectionManager connections,
- Broadcaster broadcaster) {
+ Broadcaster broadcaster, String myUrl) {
this.state = initialState;
this.connections = connections;
this.broadcaster = broadcaster;
+ this.myUrl = myUrl;
}
private MasterService serviceImpl = new MasterService() {
@@ -135,5 +137,21 @@ public class Master {
/** This master should take over from an earlier master. */
public void resumeFrom(State lastKnownState) {
+ state = lastKnownState;
+ broadcaster.broadcast(state.getList(".participants"),
+ new ServiceOperation() {
+ @Override
+ public void run(String url) {
+ ClientService client = connections.getClient(url);
+ try {
+ client.masterTakeover(myUrl,
+ state.getDataOf(".networkName"), 0);
+ } catch (Exception e) {
+ logger.info("Client {} failed to acknowledge new master. " +
+ "Removing {}", url);
+ removeParticipant(url);
+ }
+ }
+ });
}
}
diff --git a/same/src/test/java/com/orbekk/same/MasterTest.java b/same/src/test/java/com/orbekk/same/MasterTest.java
index 77f024a..ec0a56c 100644
--- a/same/src/test/java/com/orbekk/same/MasterTest.java
+++ b/same/src/test/java/com/orbekk/same/MasterTest.java
@@ -43,7 +43,8 @@ public class MasterTest {
@Before
public void setUp() {
state.update(".masterUrl", "http://master/MasterService.json", 1);
- master = new Master(state, connections, broadcaster);
+ master = new Master(state, connections, broadcaster,
+ "http://master/MasterService.json");
masterS = master.getService();
connections.masterMap.put("http://master/MasterService.json",
masterS);