Added log4j logger and removed obvious comments

This commit is contained in:
Edward Capriolo
2015-01-09 14:32:30 -05:00
parent bf5b777808
commit 980e3f51e9
8 changed files with 118 additions and 205 deletions

View File

@ -39,97 +39,92 @@ abstract public class PassiveGossipThread implements Runnable {
try {
SocketAddress socketAddress = new InetSocketAddress(_gossipManager.getMyself().getHost(), _gossipManager.getMyself().getPort());
_server = new DatagramSocket(socketAddress);
GossipService.info("Gossip service successfully initialized on port " + _gossipManager.getMyself().getPort());
GossipService.debug("I am " + _gossipManager.getMyself());
GossipService.LOGGER.info("Gossip service successfully initialized on port " + _gossipManager.getMyself().getPort());
GossipService.LOGGER.debug("I am " + _gossipManager.getMyself());
} catch (SocketException ex) {
System.err.println(ex);
GossipService.LOGGER.error(ex);
_server = null;
throw new RuntimeException(ex);
}
_keepRunning = new AtomicBoolean(true);
}
@Override
public void run() {
while(_keepRunning.get()) {
try {
// Create a byte array with the size of the buffer.
byte[] buf = new byte[_server.getReceiveBufferSize()];
DatagramPacket p = new DatagramPacket(buf, buf.length);
_server.receive(p);
GossipService.debug("A message has been received from " + p.getAddress() + ":" + p.getPort() + ".");
int packet_length = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
packet_length += (buf[i] & 0x000000FF) << shift;
}
// Check whether the package is smaller than the maximal packet length.
// A package larger than this would not be possible to be send from a GossipService,
// since this is check before sending the message.
// This could normally only occur when the list of members is very big,
// or when the packet is misformed, and the first 4 bytes is not the right in anymore.
// For this reason we regards the message.
if (packet_length <= GossipManager.MAX_PACKET_SIZE) {
byte[] json_bytes = new byte[packet_length];
for (int i=0; i<packet_length; i++) {
json_bytes[i] = buf[i+4];
}
@Override
public void run() {
while (_keepRunning.get()) {
try {
byte[] buf = new byte[_server.getReceiveBufferSize()];
DatagramPacket p = new DatagramPacket(buf, buf.length);
_server.receive(p);
GossipService.LOGGER.debug("A message has been received from " + p.getAddress() + ":"
+ p.getPort() + ".");
// Extract the members out of the packet
String receivedMessage = new String(json_bytes);
GossipService.debug("Received message (" + packet_length + " bytes): " + receivedMessage);
try {
ArrayList<GossipMember> remoteGossipMembers = new ArrayList<GossipMember>();
RemoteGossipMember senderMember = null;
GossipService.debug("Received member list:");
// Convert the received JSON message to a JSON array.
JSONArray jsonArray = new JSONArray(receivedMessage);
// The JSON array should contain all members.
// Let's iterate over them.
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject memberJSONObject = jsonArray.getJSONObject(i);
// Now the array should contain 3 objects (hostname, port and heartbeat).
if (memberJSONObject.length() == 3) {
// Ok, now let's create the member object.
RemoteGossipMember member = new RemoteGossipMember(memberJSONObject.getString(GossipMember.JSON_HOST), memberJSONObject.getInt(GossipMember.JSON_PORT), memberJSONObject.getInt(GossipMember.JSON_HEARTBEAT));
GossipService.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 {
GossipService.error("The received member object does not contain 3 objects:\n" + memberJSONObject.toString());
}
}
// Merge our list with the one we just received
mergeLists(_gossipManager, senderMember, remoteGossipMembers);
} catch (JSONException e) {
GossipService.error("The received message is not well-formed JSON. The following message has been dropped:\n" + receivedMessage);
}
} else {
GossipService.error("The received message is not of the expected size, it has been dropped.");
}
int packet_length = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
packet_length += (buf[i] & 0x000000FF) << shift;
}
} catch (IOException e) {
e.printStackTrace();
_keepRunning.set(false);
}
}
}
// Check whether the package is smaller than the maximal packet length.
// A package larger than this would not be possible to be send from a GossipService,
// since this is check before sending the message.
// This could normally only occur when the list of members is very big,
// or when the packet is misformed, and the first 4 bytes is not the right in anymore.
// For this reason we regards the message.
if (packet_length <= GossipManager.MAX_PACKET_SIZE) {
byte[] json_bytes = new byte[packet_length];
for (int i = 0; i < packet_length; i++) {
json_bytes[i] = buf[i + 4];
}
String receivedMessage = new String(json_bytes);
GossipService.LOGGER.debug("Received message (" + packet_length + " bytes): "
+ receivedMessage);
try {
ArrayList<GossipMember> remoteGossipMembers = new ArrayList<GossipMember>();
RemoteGossipMember senderMember = null;
GossipService.LOGGER.debug("Received member list:");
JSONArray jsonArray = new JSONArray(receivedMessage);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject memberJSONObject = jsonArray.getJSONObject(i);
if (memberJSONObject.length() == 3) {
RemoteGossipMember member = new RemoteGossipMember(
memberJSONObject.getString(GossipMember.JSON_HOST),
memberJSONObject.getInt(GossipMember.JSON_PORT),
memberJSONObject.getInt(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 {
GossipService.LOGGER
.error("The received member object does not contain 3 objects:\n"
+ memberJSONObject.toString());
}
}
// Merge our list with the one we just received
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);
}
} else {
GossipService.LOGGER
.error("The received message is not of the expected size, it has been dropped.");
}
} catch (IOException e) {
e.printStackTrace();
_keepRunning.set(false);
}
}
}
/**
* Abstract method for merging the local and remote list.