diff --git a/ws2012/P2P/uebungen/4/src/common/MessageType.java b/ws2012/P2P/uebungen/4/src/common/MessageType.java index 4673767f..3f2dbe8e 100644 --- a/ws2012/P2P/uebungen/4/src/common/MessageType.java +++ b/ws2012/P2P/uebungen/4/src/common/MessageType.java @@ -4,8 +4,7 @@ public class MessageType { public final static byte INVITE = 7; public final static byte LEAVE = 1; - public final static byte NEW_NEIGHBOR_IPV4 = 4; - public final static byte NEW_NEIGHBOR_IPV6 = 6; + public final static byte NEW_NEIGHBOR = 4; public final static byte PING = 2; public final static byte PONG = 3; diff --git a/ws2012/P2P/uebungen/4/src/peer/Node.java b/ws2012/P2P/uebungen/4/src/peer/Node.java index b2896971..04e2333b 100644 --- a/ws2012/P2P/uebungen/4/src/peer/Node.java +++ b/ws2012/P2P/uebungen/4/src/peer/Node.java @@ -31,22 +31,11 @@ public class Node { private UDPListen udpListen; public Node() { - System.setProperty("java.net.preferIPv4Stack" , "true"); // optional IPv4 (mac, windows) - - //get a port - Random r = new Random(); - boolean goodPort = false; - int port = 0; - while(!goodPort){ - port = r.nextInt(65000); - if(port > 1024){ - goodPort = true; - } - } - + System.setProperty("java.net.preferIPv4Stack", "true"); + try { channel = DatagramChannel.open(); - channel.socket().bind(new InetSocketAddress("localhost", port)); + channel.socket().bind(new InetSocketAddress("localhost", 0)); channel.configureBlocking(false); buf = ByteBuffer.allocate(BUF_SIZE); @@ -98,29 +87,12 @@ public class Node { buf.clear(); InetSocketAddress a = (InetSocketAddress) addr; - if (a.getAddress() instanceof Inet6Address) { - buf.put(MessageType.NEW_NEIGHBOR_IPV6); - buf.put(addr.toString().getBytes()); + buf.put(MessageType.NEW_NEIGHBOR); - } 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().substring(1).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; + for (String part : a.getHostString().split("\\.")) { + buf.put(Byte.valueOf(part)); } + buf.putInt(a.getPort()); buf.flip(); } @@ -206,15 +178,6 @@ public class Node { return this.name; } - public void printInfos() { - StringBuilder result = new StringBuilder(128); - result.append("Name: " + this.name + ", Neighbours: "); - for (int i = 0; i < this.neighbors.size(); i++) { - result.append(this.neighbors.get(i).toString() + ", "); - } - LOGGER.info(result.toString()); - } - public class UDPListen implements Runnable { private volatile boolean running = true; @@ -254,63 +217,33 @@ public class Node { } break; - case MessageType.NEW_NEIGHBOR_IPV4: - LOGGER.info(name + " received new IPv4 neighbor" - + receivedFrom.toString() + ": " - + new String(buf.array())); - - byte[] byte_addr = new byte[7]; + case MessageType.NEW_NEIGHBOR: + StringBuilder theAddr = new StringBuilder(); // Read 4 Bytes and 1 Int - for (int i = 0; i < 7; i = i + 2) { - byte_addr[i] = buf.get(); - if (i < 6) - byte_addr[i + 1] = '.'; + for (int i = 0; i < 4; i++) { + theAddr.append(buf.get()); + if (i < 3) + theAddr.append("."); } - int port = buf.getInt(); InetSocketAddress new_neighbor = new InetSocketAddress( - new String(byte_addr), port); + theAddr.toString(), port); // check, if we have the neighbor already. if (!hasNeighbor(new_neighbor)) { neighbors.add(new_neighbor); } - // TODO: send PING to new neighbor and expect wait - // to - // get a - // PONG + LOGGER.info(name + " from " + + receivedFrom.toString() + + " received new neighbor:" + + new_neighbor.toString()); - break; - case MessageType.NEW_NEIGHBOR_IPV6: - LOGGER.info(name - + " received new IPv6 neighbor from: " - + receivedFrom.toString() + ": " - + new String(buf.array())); + // TODO: maybe send PING to new neighbor and expect + // wait to get a PONG - sb_received = new StringBuilder(512); - while (buf.hasRemaining()) { - sb_received.append((char) buf.get()); - } - - 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)); - - new_neighbor = new InetSocketAddress(new_hostname, - new_port); - - // check, if we have the neighbor already. - if (!hasNeighbor(new_neighbor)) { - neighbors.add(new_neighbor); - } break; default: @@ -328,6 +261,7 @@ public class Node { e.printStackTrace(); } } + buf.clear(); } catch (IOException e) { e.printStackTrace(); }