use jackson

This commit is contained in:
Edward Capriolo
2016-05-16 18:54:17 -04:00
parent 176e173fe5
commit 29f5966463
4 changed files with 129 additions and 38 deletions

View File

@ -28,6 +28,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -35,6 +36,7 @@ import org.json.JSONObject;
import com.google.code.gossip.GossipMember;
import com.google.code.gossip.GossipService;
import com.google.code.gossip.RemoteGossipMember;
import com.google.code.gossip.model.ActiveGossipMessage;
/**
* [The passive thread: reply to incoming gossip request.] This class handles the passive cycle,
@ -54,6 +56,8 @@ abstract public class PassiveGossipThread implements Runnable {
private AtomicBoolean keepRunning;
private final String cluster;
private ObjectMapper MAPPER = new ObjectMapper();
public PassiveGossipThread(GossipManager gossipManager) {
this.gossipManager = gossipManager;
@ -93,46 +97,35 @@ abstract public class PassiveGossipThread implements Runnable {
json_bytes[i] = buf[i + 4];
}
String receivedMessage = new String(json_bytes);
GossipService.LOGGER.debug("Received message (" + packet_length + " bytes): "
GossipService.LOGGER.warn("Received message (" + packet_length + " bytes): "
+ receivedMessage);
try {
List<GossipMember> remoteGossipMembers = new ArrayList<>();
RemoteGossipMember senderMember = null;
JSONArray jsonArray = new JSONArray(receivedMessage);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject memberJSONObject = jsonArray.getJSONObject(i);
if (memberJSONObject.length() == 5
&& cluster.equals(memberJSONObject.get(GossipMember.JSON_CLUSTER))) {
RemoteGossipMember member = new RemoteGossipMember(
memberJSONObject.getString(GossipMember.JSON_CLUSTER),
memberJSONObject.getString(GossipMember.JSON_HOST),
memberJSONObject.getInt(GossipMember.JSON_PORT),
memberJSONObject.getString(GossipMember.JSON_ID),
memberJSONObject.getLong(GossipMember.JSON_HEARTBEAT));
GossipService.LOGGER.debug(member.toString());
// This is the first member found, so this should be the member who is communicating
// with me.
if (i == 0) {
senderMember = member;
}
remoteGossipMembers.add(member);
} else if (memberJSONObject.length() == 5) {
GossipService.LOGGER.warn("The member object does not belong to this cluster.");
} else {
GossipService.LOGGER
.error("The received member object does not contain 5 objects:\n"
+ memberJSONObject.toString());
ActiveGossipMessage activeGossipMessage = MAPPER.readValue(json_bytes,
ActiveGossipMessage.class);
for (int i = 0; i < activeGossipMessage.getMembers().size(); i++) {
RemoteGossipMember member = new RemoteGossipMember(
activeGossipMessage.getMembers().get(i).getCluster(),
activeGossipMessage.getMembers().get(i).getHost(),
activeGossipMessage.getMembers().get(i).getPort(),
activeGossipMessage.getMembers().get(i).getId(),
activeGossipMessage.getMembers().get(i).getHeartbeat());
if (!(member.getClusterName().equals(cluster))){
GossipService.LOGGER.warn("Note a member of this cluster " + i);
continue;
}
// This is the first member found, so this should be the member who is communicating
// with me.
if (i == 0) {
senderMember = member;
}
remoteGossipMembers.add(member);
}
mergeLists(gossipManager, senderMember, remoteGossipMembers);
} catch (JSONException e) {
GossipService.LOGGER
.error("The received message is not well-formed JSON. The following message has been dropped:\n"
+ receivedMessage);
System.out.println(e);
} catch (RuntimeException ex) {
GossipService.LOGGER.error("Unable to process message", ex);
}
} else {
GossipService.LOGGER
.error("The received message is not of the expected size, it has been dropped.");