From 59916d04e79018668ab9609d0d1d53c3c3c4e0e5 Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 9 Dec 2012 14:01:29 +0100 Subject: [PATCH] added some new features, for example to remove nodes with a min neighborcount --- .../P2P/uebungen/4/src/RandomGenerator.java | 97 ++++++++++++++++--- 1 file changed, 85 insertions(+), 12 deletions(-) diff --git a/ws2012/P2P/uebungen/4/src/RandomGenerator.java b/ws2012/P2P/uebungen/4/src/RandomGenerator.java index 892be9e7..83af91a3 100644 --- a/ws2012/P2P/uebungen/4/src/RandomGenerator.java +++ b/ws2012/P2P/uebungen/4/src/RandomGenerator.java @@ -11,12 +11,13 @@ import analysis.NetworkDumper; import node.Node; public class RandomGenerator { - public static int initNodeCount = 99; + public static int initNodeCount = 100; public static double initBirthRate = 0.50; public static double initDeathRate = 0.20; public static int initRounds = 13; public static List nodes = new ArrayList(); + public static List neighborCount = new ArrayList(); public static Random gen = new Random(); /** @@ -27,8 +28,7 @@ public class RandomGenerator { System.setProperty("java.util.logging.config.file", "logging.properties"); - - nodes.add(new Node()); + add(initNodeCount); try { @@ -88,12 +88,40 @@ public class RandomGenerator { } System.out.println("Nodecount: " + nodes.size()); break; - case "status": - if(count < nodes.size()){ - System.out.println(nodes.get(count)); - } else { - System.out.println("Node doesn't exist. Max index at the moment is " + (nodes.size() - 1)); + case "simulate": + double rate; + int add = 0; + int remove = 0; + if (splitted.length > 1) { + rate = Double.valueOf(splitted[2]); + for (int i = 0; i < count; i++) { + System.out.println("Round: " + i); + System.out.println("NodeCount: " + nodes.size()); + if (gen.nextDouble() < rate) { + add(1); + add++; + } + if (gen.nextDouble() < rate) { + remove(1); + remove++; + } + if (i % (count / 4) == 0) { + nodes.get(0).gatherInformationOfNetwork(); + try { + System.out.println(i); + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + NetworkDumper nd = new NetworkDumper(nodes.get(0)); + nd.write(nd.networkToDot(nodes.get(0).getNetwork())); + } + } } + System.out.println("Nodecount: " + nodes.size()); + System.out.println("We added " + add + " Nodes"); + System.out.println("We removed " + remove + " Nodes"); break; case "go": for (int i = 0; i < initRounds; i++) { @@ -104,6 +132,15 @@ public class RandomGenerator { System.out.println("Nodecount: " + nodes.size()); } break; + case "prioritize": + int neighbors = Integer.valueOf(splitted[2]); + if (count < nodes.size()) { + removeWithNeighborCount(count, neighbors); + } else { + System.out.println("Can't remove that many nodes."); + } + System.out.println("Nodecount: " + nodes.size()); + break; default: System.out.println("Unknown command."); break; @@ -113,17 +150,53 @@ public class RandomGenerator { public static void add(int count) throws IOException { for (int i = 0; i < count; i++) { - Node newNode = nodes.get(gen.nextInt(nodes.size())).spawn(); - nodes.add(newNode); + if (nodes.size() == 0) { + Node newNode = new Node(); + nodes.add(newNode); + neighborCount.add(0); + } else { + int index = gen.nextInt(nodes.size()); + Node newNode = nodes.get(index).spawn(); + neighborCount.set(index, + neighborCount.get(index).intValue() + 1); + nodes.add(newNode); + neighborCount.add(0); + + } } } public static void remove(int count) { for (int i = 0; i < count; i++) { - int index = gen.nextInt(nodes.size()); - if (index < nodes.size()) { + int index = 0; + if (0 < nodes.size()) { + index = gen.nextInt(nodes.size()); nodes.get(index).leave(); nodes.remove(index); + neighborCount.remove(index); + } + } + } + + public static void removeWithNeighborCount(int count, int neighbors) { + for (int i = 0; i < count; i++) { + int index = 0; + boolean done = false; + if (0 < nodes.size()) { + do { + index = gen.nextInt(nodes.size()); + System.out.println("Node " + index + " has " + neighborCount.get(index) + " neighbors."); + if (neighborCount.get(index) >= neighbors) { + System.out.println("Node " + index + " had " + + neighborCount.get(index) + " neighbors >= " + + neighbors); + nodes.get(index).leave(); + nodes.remove(index); + neighborCount.remove(index); + done = true; + } + } while (!done); + } } }