From e68f306568d28d4b508c48e9d239668a3b10e9e1 Mon Sep 17 00:00:00 2001 From: senft-lap Date: Tue, 20 Nov 2012 14:46:41 +0100 Subject: [PATCH] Sending IPv6 Addr as String. --- ws2012/P2P/uebungen/4/src/peer/Node.java | 38 ++++++++++++++---------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/ws2012/P2P/uebungen/4/src/peer/Node.java b/ws2012/P2P/uebungen/4/src/peer/Node.java index 6036eda0..55191212 100644 --- a/ws2012/P2P/uebungen/4/src/peer/Node.java +++ b/ws2012/P2P/uebungen/4/src/peer/Node.java @@ -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;