diff --git a/ws2012/P2P/uebungen/4/src/network/Network.java b/ws2012/P2P/uebungen/4/src/network/Network.java index b7050b55..eac0b048 100644 --- a/ws2012/P2P/uebungen/4/src/network/Network.java +++ b/ws2012/P2P/uebungen/4/src/network/Network.java @@ -38,20 +38,20 @@ public class Network { Node d = a.spawn(); Thread.sleep(delay); - Node ba = b.spawn(); - Thread.sleep(delay); - - Node bb = b.spawn(); - Thread.sleep(delay); - - Node bc = b.spawn(); - Thread.sleep(delay); - - Node bd = b.spawn(); - Thread.sleep(delay); - - Node be = b.spawn(); - Thread.sleep(delay); + // Node ba = b.spawn(); + // Thread.sleep(delay); + // + // Node bb = b.spawn(); + // Thread.sleep(delay); + // + // Node bc = b.spawn(); + // Thread.sleep(delay); + // + // Node bd = b.spawn(); + // Thread.sleep(delay); + // + // Node be = b.spawn(); + // Thread.sleep(delay); a.leave(); Thread.sleep(delay); diff --git a/ws2012/P2P/uebungen/4/src/peer/Node.java b/ws2012/P2P/uebungen/4/src/peer/Node.java index ab8082d4..7862f4a8 100644 --- a/ws2012/P2P/uebungen/4/src/peer/Node.java +++ b/ws2012/P2P/uebungen/4/src/peer/Node.java @@ -29,7 +29,6 @@ public class Node { public Node() { System.setProperty("java.net.preferIPv4Stack", "true"); - try { channel = DatagramChannel.open(); channel.socket().bind(new InetSocketAddress("localhost", 0)); @@ -97,48 +96,22 @@ public class Node { * Circularly link all neighbors, remove itself form all neighbors and exit. */ public void leave() { - // System.out.println(neighbors); - LOGGER.info("Name: " + getName() + ", Leaving... Announcing to my neighbors: " + neighbors); + // loop over each neighbor i for (int i = 0; i < neighbors.size(); i++) { - - try { - if (neighbors.size() > 2) { - 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)); - } - - } else if (neighbors.size() == 2) { - putAddrInBuf(buf, neighbors.get(Math.abs(i - 1))); + for (int j = 0; j < neighbors.size(); j++) { + try { + // send all neighbors j to neighbor i + putAddrInBuf(buf, neighbors.get(j)); channel.send(buf, neighbors.get(i)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - } catch (IOException e) { - e.printStackTrace(); } - + // send LEAVE to neighbor i buf.clear(); buf.put(MessageType.LEAVE); buf.flip(); @@ -149,6 +122,8 @@ public class Node { e.printStackTrace(); } } + + // Destroy thread try { if (thread != null) { udpListen.terminate(); @@ -243,18 +218,16 @@ public class Node { break; default: - System.out.println(messageType); LOGGER.info(name + " received unknown command from " + receivedFrom.toString() + ": " - + new String(buf.array())); + + messageType + new String(buf.array())); } } else { try { Thread.sleep(10); } catch (InterruptedException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } @@ -269,4 +242,4 @@ public class Node { running = false; } } -} \ No newline at end of file +}