diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-03-13 15:23:05 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-03-13 15:23:05 +0100 |
commit | f41eca9bee17f45981e41c273c3a50f5d04ff426 (patch) | |
tree | 05d70ee720cdebaa86f3af571d745a63c4e06dcb /same/src/main | |
parent | b256d125695da6b2a8eedd6f59ad07e94bd2b0bc (diff) |
Clients can become new masters.
Diffstat (limited to 'same/src/main')
-rw-r--r-- | same/src/main/java/com/orbekk/same/Master.java | 22 |
1 files changed, 20 insertions, 2 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); + } + } + }); } } |