Sending IPv6 Addr as String.

This commit is contained in:
senft-lap 2012-11-20 14:46:41 +01:00
parent 91a8340c27
commit e68f306568

View File

@ -84,17 +84,12 @@ public class Node {
if (a.getAddress() instanceof Inet6Address) {
buf.put(MessageType.NEW_NEIGHBOR_IPV6);
// TODO Geht das so? Können wir auf der Empfängerseite einfach
// anhand der Information " es ist eine IPV6 Adresse" eine bestimmte
// Anzahl an Bytes lesen und dann nach X Bytes wissen wir,
// "jetzt kommt der Port"? Bei IPV4 geht das ja: 4 Bytes und dann 1
// Int für den Port
buf.put(a.getHostName().getBytes());
buf.putInt(a.getPort());
buf.put(addr.toString().getBytes());
} else if (a.getAddress() instanceof Inet4Address) {
buf.put(MessageType.NEW_NEIGHBOR_IPV4);
// TODO: man braucht wahrscheinlich nicht mal .toString()
// a.getHostName() und a.getPort() sollten auch gehen.
for (String part : addr.toString().split(".")) {
if (!part.contains(":")) {
@ -193,7 +188,7 @@ public class Node {
private volatile boolean running = true;
public void run() {
StringBuilder addr;
StringBuilder sb_received;
while (running) {
SocketAddress receivedFrom = null;
buf.clear();
@ -216,9 +211,9 @@ public class Node {
break;
case MessageType.LEAVE:
LOGGER.info(receivedFrom.toString()
+ " is leaving. Deleting...");
neighbors.remove(receivedFrom);
break;
case MessageType.NEW_NEIGHBOR_IPV4:
@ -245,16 +240,27 @@ public class Node {
break;
case MessageType.NEW_NEIGHBOR_IPV6:
LOGGER.info(name + " received new IPV6 neighbor"
LOGGER.info(name + " received new IPV6 neighbor from: "
+ receivedFrom.toString() + ": "
+ new String(buf.array()));
// Wie lang sind IPV6 Adressen noch gleich?
addr = new StringBuilder(512);
sb_received = new StringBuilder(512);
while (buf.hasRemaining()) {
addr.append(buf.get());
sb_received.append((char) buf.get());
}
System.out.println(addr.toString());
String str_received = sb_received.toString();
int startOfPort = str_received.lastIndexOf(":");
String new_hostname = str_received
.substring(1, startOfPort);
int new_port = Integer.valueOf(str_received
.substring(startOfPort + 1));
SocketAddress new_neighbor = new InetSocketAddress(
new_hostname, new_port);
neighbors.add(new_neighbor);
break;