fixed some issues with a node leaving and sending the new neighbors

This commit is contained in:
Denis 2012-11-20 16:11:18 +01:00
parent 57e66c347e
commit 24002e9c35

View File

@ -3,6 +3,7 @@ package peer;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
@ -36,7 +37,7 @@ public class Node {
channel.configureBlocking(false);
buf = ByteBuffer.allocate(BUF_SIZE);
this.name = channel.socket().getLocalSocketAddress().toString();
udpListen = new UDPListen();
@ -173,6 +174,15 @@ public class Node {
return (this.neighbors.size() > 0);
}
public boolean hasNeighbor(SocketAddress adr) {
for (SocketAddress n : neighbors) {
if (n.toString().equals(adr.toString())) {
return true;
}
}
return false;
}
public String getName() {
return this.name;
}
@ -214,7 +224,13 @@ public class Node {
case MessageType.LEAVE:
LOGGER.info(name + ": " + receivedFrom.toString()
+ " is leaving. Deleting...");
neighbors.remove(receivedFrom);
// search the neighbor in the list and remove him.
for (int i = 0; i < neighbors.size(); i++) {
if (neighbors.get(i).equals(receivedFrom)) {
neighbors.remove(i);
}
}
break;
case MessageType.NEW_NEIGHBOR_IPV4:
@ -233,8 +249,13 @@ public class Node {
int port = buf.getInt();
neighbors.add(new InetSocketAddress(new String(
byte_addr), port));
InetSocketAddress new_neighbor = new InetSocketAddress(
new String(byte_addr), port);
// check, if we have the neighbor already.
if (!hasNeighbor(new_neighbor)) {
neighbors.add(new_neighbor);
}
// TODO: send PING to new neighbor and expect wait
// to
@ -262,9 +283,13 @@ public class Node {
int new_port = Integer.valueOf(str_received
.substring(startOfPort + 1));
SocketAddress new_neighbor = new InetSocketAddress(
new_hostname, new_port);
neighbors.add(new_neighbor);
new_neighbor = new InetSocketAddress(new_hostname,
new_port);
// check, if we have the neighbor already.
if (!hasNeighbor(new_neighbor)) {
neighbors.add(new_neighbor);
}
break;
@ -273,7 +298,7 @@ public class Node {
+ " received unknown command from "
+ receivedFrom.toString() + ": "
+ new String(buf.array()));
}
} else {
try {