Really keep track of network when receiving status commands

This commit is contained in:
senft-desktop 2012-11-30 12:18:18 +01:00
parent 378ad5f1e0
commit b994f691a8

View File

@ -7,6 +7,7 @@ import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
@ -49,6 +50,8 @@ public class Node {
this.name = channel.socket().getLocalSocketAddress().toString();
network.put(getName(), new LinkedList<String>());
udpListen = new UDPHandler(this);
thread = new Thread(udpListen);
thread.start();
@ -71,7 +74,7 @@ public class Node {
Node newNode = new Node();
sendInvite(newNode);
neighbors.add(newNode.getAddress());
addNeighbor(newNode.getAddress());
return newNode;
}
@ -201,13 +204,13 @@ public class Node {
buffer.put(command);
buffer.put(data);
buffer.flip();
// Needed because the buffer gets cleared after first send, so we save
// the current buffer in a byte[]
int bytesWrittenToBuffer = buffer.position();
byte[] packet = new byte[bytesWrittenToBuffer];
System.arraycopy(buffer.array(), 0, packet, 0, bytesWrittenToBuffer);
buffer.flip();
for (SocketAddress n : neighbors) {
if (!n.equals(receivedFrom)) {
@ -275,9 +278,12 @@ public class Node {
return acks.remove(ack_id);
}
protected boolean addNeighbor(SocketAddress node) {
if (!hasNeighbor(node)) {
neighbors.add(node);
protected boolean addNeighbor(SocketAddress newNeighbor) {
if (!hasNeighbor(newNeighbor)) {
neighbors.add(newNeighbor);
network.get(getName()).add(newNeighbor.toString());
return true;
}
return false;
@ -370,4 +376,8 @@ public class Node {
return result.toString();
}
public Map<String, List<String>> getNetwork() {
return network;
}
}