From fed78ba9563acfc264f59e3ba2d7fe204b84947d Mon Sep 17 00:00:00 2001 From: senft-lap Date: Mon, 19 Nov 2012 13:05:15 +0100 Subject: [PATCH] IPv4 specific transfer of adresse --- ws2012/P2P/uebungen/4/src/peer/Node.java | 51 ++++++++++++++++-------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/ws2012/P2P/uebungen/4/src/peer/Node.java b/ws2012/P2P/uebungen/4/src/peer/Node.java index be6af73e..49080a0b 100644 --- a/ws2012/P2P/uebungen/4/src/peer/Node.java +++ b/ws2012/P2P/uebungen/4/src/peer/Node.java @@ -11,8 +11,6 @@ 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 { @@ -91,17 +89,32 @@ 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()); + } else if (a.getAddress() instanceof Inet4Address) { buf.put(MessageType.NEW_NEIGHBOR_IPV4); + + for (String part : addr.toString().split(".")) { + if (!part.contains(":")) { + buf.put(Byte.valueOf(part)); + } else { + // Last part (contains port) + String[] lastPart = part.split(":"); + buf.put(Byte.valueOf(lastPart[0])); + buf.putInt(Integer.valueOf(lastPart[1])); + } + } + } else { return; } - - // 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(); } @@ -217,16 +230,22 @@ 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? + byte[] byte_addr = new byte[7]; - addr = new StringBuilder(512); - while (buf.hasRemaining()) { - addr.append(buf.get()); + // Read 4 Bytes and 1 Int + for (int i = 0; i < 8; i = i + 2) { + byte_addr[i] = buf.get(); + if (i < 7) + byte_addr[i + 1] = '.'; } - System.out.println(addr.toString()); + + int port = buf.getInt(); + + neighbors.add(new InetSocketAddress(new String(byte_addr), + port)); + + // TODO: send PING to new neighbor and expect wait to get a + // PONG break; case MessageType.NEW_NEIGHBOR_IPV6: