Extracted ack-checking

This commit is contained in:
senft-lap 2012-11-27 16:58:24 +01:00
parent 50b8614723
commit 9972e5ddfe

View File

@ -217,15 +217,16 @@ public class Node {
private volatile boolean running = true;
public void run() {
SocketAddress receivedFrom = null;
int ack_id;
while (running) {
SocketAddress receivedFrom = null;
int ack_id;
try {
receivedFrom = channel.receive(buf);
if (buf.remaining() != 512) {
// channel.receive() is non blocking. So we need to check if
// something actually has been written to the buffer
if (buf.remaining() != BUF_SIZE) {
buf.flip();
byte messageType = buf.get();
@ -248,17 +249,8 @@ public class Node {
LOGGER.info(name + " received ack from "
+ receivedFrom.toString());
if (acks.containsKey(ack_id)) {
Ack theAck = acks.get(ack_id);
if (theAck.check(receivedFrom)) {
acks.remove(theAck);
} else {
LOGGER.info("Received unexpected ack from "
+ receivedFrom.toString());
}
} else {
LOGGER.info("Received unexpected ack from "
if (!checkAck(receivedFrom, ack_id)) {
LOGGER.warning("Received unexpected ack from: "
+ receivedFrom.toString());
}
@ -271,17 +263,18 @@ public class Node {
int idToRemove = getNeighborId(receivedFrom);
if (idToRemove != -1) {
neighbors.remove(idToRemove);
ack_id = buf.getInt();
sendAckTo(receivedFrom, ack_id);
}
ack_id = buf.getInt();
sendAckTo(receivedFrom, ack_id);
// If we don't know that neighbor, we don't have to
// ack
break;
case MessageType.NEW_NEIGHBOR:
ack_id = buf.getInt();
StringBuilder theAddr = new StringBuilder();
// Read 4 Bytes and 1 int
// Read 4 Bytes and 1 Integer = 1 IP address
for (int i = 0; i < 4; i++) {
theAddr.append(buf.get());
if (i < 3)
@ -292,8 +285,9 @@ public class Node {
InetSocketAddress new_neighbor = new InetSocketAddress(
theAddr.toString(), port);
// check, if we have the neighbor already.
if (!hasNeighbor(new_neighbor)) {
// Add this neighbor to my neighbor list if it
// was not present before
neighbors.add(new_neighbor);
LOGGER.info(name + " from "
@ -327,6 +321,17 @@ public class Node {
}
}
private boolean checkAck(SocketAddress receivedFrom, int ack_id) {
if (acks.containsKey(ack_id)) {
Ack theAck = acks.get(ack_id);
if (theAck.check(receivedFrom)) {
acks.remove(theAck);
return true;
}
}
return false;
}
public void terminate() {
running = false;
}