summaryrefslogtreecommitdiff
path: root/jsonrpc/src
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-11 20:49:49 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-11 20:49:49 +0100
commit38c2e737408a73c85aa926dd20d34ff9045fd01a (patch)
tree4ae7da986469816f7cbcfb01ae1c2e44cc3869fa /jsonrpc/src
parent03833cd58c9ac42bf8414cb73258ddfec6b20447 (diff)
Use State class to hold the state.
Diffstat (limited to 'jsonrpc/src')
-rw-r--r--jsonrpc/src/main/java/com/orbekk/same/SameState.java93
-rw-r--r--jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java1
2 files changed, 39 insertions, 55 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/same/SameState.java b/jsonrpc/src/main/java/com/orbekk/same/SameState.java
index 7cc8940..c9cd216 100644
--- a/jsonrpc/src/main/java/com/orbekk/same/SameState.java
+++ b/jsonrpc/src/main/java/com/orbekk/same/SameState.java
@@ -15,33 +15,10 @@ import org.slf4j.LoggerFactory;
public class SameState extends Thread implements UrlReceiver {
private Logger logger = LoggerFactory.getLogger(getClass());
private ConnectionManager connections;
- private String currentState = "";
- private String _setState = null;
-
- private String networkName;
- /**
- * The master participant id.
- */
- private String masterId;
-
- public String getMasterId() {
- return this.masterId;
- }
-
- /**
- * The participants of this network.
- *
- * Maps clientId to url.
- */
- private Map<String, String> participants = new HashMap<String, String>();
-
- /**
- * New participants.
- *
- * New participants map to replace participants.
- */
- private Map<String, String> _setParticipants = null;
+ // TODO: Change the name of State.
+ private com.orbekk.same.State state =
+ new com.orbekk.same.State();
/**
* The client id of this participant.
@@ -53,25 +30,27 @@ public class SameState extends Thread implements UrlReceiver {
*/
private boolean stopped = false;
+ private String _setState = null;
+ private Map<String, String> _setParticipants = null;
+
private Map<String, String> pendingParticipants =
new HashMap<String, String>();
public SameState(String networkName, String clientId,
ConnectionManager connections) {
- this.networkName = networkName;
+ state.setNetworkName(networkName);
this.clientId = clientId;
this.connections = connections;
- this.masterId = clientId;
- participants.put(clientId, null);
+ state.setMasterId(clientId);
+ state.getParticipants().put(clientId, null);
+ }
+
+ public String getMasterId() {
+ return state.getMasterId();
}
- /**
- * Get participants as a list.
- *
- * List format: ["clientId1,url1", "clientId2,url2", ...]
- */
public synchronized Map<String, String> getParticipants() {
- return participants;
+ return state.getParticipants();
}
/**
@@ -80,16 +59,14 @@ public class SameState extends Thread implements UrlReceiver {
* TODO: Implement fully.
*/
private synchronized void resetState() {
- networkName = "";
- masterId = "";
+ state = new com.orbekk.same.State();
pendingParticipants.clear();
- participants.clear();
}
public synchronized void joinNetwork(String networkName, String masterId) {
resetState();
- this.networkName = networkName;
- this.masterId = masterId;
+ state.setNetworkName(networkName);
+ state.setMasterId(masterId);
logger.info("Joined network {}.", networkName);
}
@@ -98,29 +75,30 @@ public class SameState extends Thread implements UrlReceiver {
}
public String getNetworkName() {
- return networkName;
+ return state.getNetworkName();
}
public String getCurrentState() {
- return currentState;
+ return state.getData();
}
/**
* TODO: Move to a separate library.
*/
public void librarySetNewState(String newState) {
- connections.getConnection(participants.get(masterId))
+ connections.getConnection(
+ state.getParticipants().get(state.getMasterId()))
.setState(newState);
}
public String getUrl() {
- return participants.get(clientId);
+ return state.getParticipants().get(clientId);
}
@Override
public void setUrl(String url) {
logger.info("My URL is {}", url);
- participants.put(clientId, url);
+ state.getParticipants().put(clientId, url);
}
public synchronized void addParticipant(String clientId, String url) {
@@ -147,7 +125,8 @@ public class SameState extends Thread implements UrlReceiver {
logger.error("{}: Master received setParticipants.", clientId);
} else {
logger.info("{}: New participants committed.", clientId);
- participants = _setParticipants;
+ state.getParticipants().clear();
+ state.getParticipants().putAll(_setParticipants);
}
}
_setParticipants = null;
@@ -162,37 +141,40 @@ public class SameState extends Thread implements UrlReceiver {
}
});
}
- currentState = _setState;
+ state.setData(_setState);
_setState = null;
}
}
private boolean isMaster() {
- return masterId.equals(clientId);
+ return state.getMasterId().equals(clientId);
}
private synchronized void handleNewParticipants() {
if (!isMaster()) {
for (Map.Entry<String, String> e : pendingParticipants.entrySet()) {
SameService master = connections.getConnection(
- participants.get(masterId));
- logger.info("Redirecting participant request to {}", masterId);
+ state.getParticipants().get(state.getMasterId()));
+ logger.info("Redirecting participant request to {}",
+ state.getMasterId());
String clientId = e.getKey();
String url = e.getValue();
- master.participateNetwork(networkName, clientId, url);
+ master.participateNetwork(state.getNetworkName(), clientId,
+ url);
}
} else {
- participants.putAll(pendingParticipants);
+ state.getParticipants().putAll(pendingParticipants);
for (Map.Entry<String, String> 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(networkName, masterId);
+ remoteService.notifyParticipation(state.getNetworkName(),
+ state.getMasterId());
broadcast(new ServiceOperation(){
@Override void run(SameService service) {
- service.setParticipants(participants);
+ service.setParticipants(state.getParticipants());
}
});
}
@@ -239,7 +221,8 @@ public class SameState extends Thread implements UrlReceiver {
}
public synchronized void broadcast(ServiceOperation operation) {
- for (Map.Entry<String, String> e : participants.entrySet()) {
+ for (Map.Entry<String, String> e :
+ state.getParticipants().entrySet()) {
String clientId = e.getKey();
String url = e.getValue();
if (!clientId.equals(this.clientId)) {
diff --git a/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java b/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java
index 90ce658..b16f5f9 100644
--- a/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java
+++ b/jsonrpc/src/test/java/com/orbekk/same/SameStateTest.java
@@ -79,6 +79,7 @@ public class SameStateTest {
state2.internalRun();
state1.internalRun();
state3.internalRun();
+ state2.internalRun();
assertTrue(state1.getParticipants().size() == 3);
assertTrue(state2.getParticipants().size() == 3);