Adresse transfer when communication neighbors

This commit is contained in:
senft-lap 2012-11-19 12:36:13 +01:00
parent a3466c121d
commit 5f5a3c0913

View File

@ -11,6 +11,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import com.sun.org.apache.bcel.internal.generic.NEW;
import common.MessageType;
public class Node {
@ -83,20 +85,24 @@ public class Node {
return null;
}
private byte[] addrToByte(SocketAddress addr) {
// TODO Wäre cool, die IP nicht einfach als String zu schicken.
// Was machen mit IPV4 vs IPV6?
private void putAddrInBuf(ByteBuffer buf, SocketAddress addr) {
buf.clear();
InetSocketAddress a = (InetSocketAddress) addr;
if (a.getAddress() instanceof Inet6Address) {
// seems to be IPv6
buf.put(MessageType.NEW_NEIGHBOR_IPV6);
} else if (a.getAddress() instanceof Inet4Address) {
// seems to be IPv4
buf.put(MessageType.NEW_NEIGHBOR_IPV4);
} else {
// invalid ip address
return;
}
return a.getAddress().getAddress();
// TODO Geht das so? Können wir auf der Empfängerseite einfach anhand
// der Information ob IPV4 oder IPV6 eine bestimmte Anzahl an Bytes
// lesen und dann nach X Bytes wissen wir, "jetzt kommt der Port"?
buf.put(a.getHostName().getBytes());
buf.putInt(a.getPort());
buf.flip();
}
/**
@ -108,26 +114,31 @@ public class Node {
for (int i = 0; i < neighbors.size(); i++) {
if (neighbors.size() > 1) {
buf.clear();
buf.put(MessageType.NEW_NEIGHBOR_IPV6);
if (i == 0) {
buf.put(addrToByte(neighbors.get(1)));
buf.put(addrToByte(neighbors.get(neighbors.size() - 1)));
} else if (i == neighbors.size() - 1) {
buf.put(addrToByte(neighbors.get(0)));
buf.put(addrToByte(neighbors.get(i - 1)));
} else {
buf.put(addrToByte(neighbors.get(i - 1)));
buf.put(addrToByte(neighbors.get(i + 1)));
}
buf.flip();
try {
channel.send(buf, neighbors.get(i));
if (i == 0) {
putAddrInBuf(buf, neighbors.get(1));
channel.send(buf, neighbors.get(i));
putAddrInBuf(buf, neighbors.get(neighbors.size() - 1));
channel.send(buf, neighbors.get(i));
} else if (i == neighbors.size() - 1) {
putAddrInBuf(buf, neighbors.get(0));
channel.send(buf, neighbors.get(i));
putAddrInBuf(buf, neighbors.get(i - 1));
channel.send(buf, neighbors.get(i));
} else {
putAddrInBuf(buf, neighbors.get(i - 1));
channel.send(buf, neighbors.get(i));
putAddrInBuf(buf, neighbors.get(i + 1));
channel.send(buf, neighbors.get(i));
}
} catch (IOException e) {
e.printStackTrace();
}
@ -174,6 +185,7 @@ public class Node {
private volatile boolean running = true;
public void run() {
StringBuilder addr;
while (running) {
SocketAddress receivedFrom = null;
buf.clear();
@ -205,11 +217,30 @@ public class Node {
+ receivedFrom.toString() + ": "
+ new String(buf.array()));
// TODO: Geht es, hier X Bytes zu lesen und dann ein
// buf.getInt() für den Port zu machen? Oder braucht man
// irgendwie ein Trennzeichen? Oder irgendwie eine fixe
// Anzahl an Bytes und dann irgendwelche Füllzeichen?
addr = new StringBuilder(512);
while (buf.hasRemaining()) {
addr.append(buf.get());
}
System.out.println(addr.toString());
break;
case MessageType.NEW_NEIGHBOR_IPV6:
LOGGER.info(name + " received new IPV6 neighbor"
+ receivedFrom.toString() + ": "
+ new String(buf.array()));
// Wie lang sind IPV6 Adressen noch gleich?
addr = new StringBuilder(512);
while (buf.hasRemaining()) {
addr.append(buf.get());
}
System.out.println(addr.toString());
break;
default: