diff --git a/ws2012/P2P/uebungen/4/src/RandomGenerator.java b/ws2012/P2P/uebungen/4/src/RandomGenerator.java index 8388a733..9ef7119d 100644 --- a/ws2012/P2P/uebungen/4/src/RandomGenerator.java +++ b/ws2012/P2P/uebungen/4/src/RandomGenerator.java @@ -8,65 +8,111 @@ import java.util.logging.LogManager; import node.Node; public class RandomGenerator { + public static int initNodeCount = 10; + public static double initBirthRate = 0.50; + public static double initDeathRate = 0.20; + public static int initRounds = 10; - /** - * @param args - * @throws IOException - */ - public static void main(String[] args) throws IOException { - ArrayList nodes = new ArrayList(); - Random gen = new Random(); + public static ArrayList nodes = new ArrayList(); + public static Random gen = new Random(); - System.setProperty("java.util.logging.config.file", - "logging.properties"); + /** + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { - nodes.add(new Node()); + System.setProperty("java.util.logging.config.file", + "logging.properties"); - try { - LogManager.getLogManager().readConfiguration(); - } catch (Exception e) { - e.printStackTrace(); - } + for (int i = 0; i < initNodeCount; i++) { + nodes.add(new Node()); + } - BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); - String s; - while ((s = in.readLine()) != null && s.length() != 0) { - String[] splitted = s.split(" "); + try { + LogManager.getLogManager().readConfiguration(); + } catch (Exception e) { + e.printStackTrace(); + } - String cmd = splitted[0]; - int number = Integer.valueOf(splitted[1]); + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + String s; + while ((s = in.readLine()) != null && s.length() != 0) { + String[] splitted = s.split(" "); - switch (cmd) { - case "br": - nodes.get(number).gatherInformationOfNetwork(); - break; - case "populate": - for (int i = 0; i < number; i++) { - Node newNode = nodes.get(gen.nextInt(nodes.size())).spawn(); - nodes.add(newNode); - } - break; - case "remove": - if (number < nodes.size()) { - for (int i = 0; i < number; i++) { - int index = gen.nextInt(nodes.size()); - nodes.get(index).leave(); - nodes.remove(index); - } - } else { - System.out.println("Can't remove that many nodes."); - } - break; - case "status": - System.out.println(nodes.get(number)); - break; - default: - System.out.println("Unknown command."); - break; - } - System.out.println("Length of the List: " + nodes.size()); + String cmd = splitted[0]; + int count = 0; + if (splitted.length > 1) { + count = Integer.valueOf(splitted[1]); + } - } - } + switch (cmd) { + case "br": + nodes.get(count).gatherInformationOfNetwork(); + break; + case "add": + add(count); + break; + case "remove": + if (count < nodes.size()) { + remove(count); + } else { + System.out.println("Can't remove that many nodes."); + } + break; + case "populate": + if (splitted.length == 4) { + for (int i = 0; i < count; i++) { + System.out.println("Round: " + (i + 1)); + System.out.println("Nodecount: " + nodes.size()); + int oldsize = nodes.size(); + add((int) Math.round(oldsize + * Double.valueOf(splitted[2]))); + remove((int) Math.round(oldsize + * Double.valueOf(splitted[3]))); + } + } else { + System.out.println("command has to have 3 parameters."); + } + 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)); + } + break; + case "go": + for (int i = 0; i < initRounds; i++) { + System.out.println("Round: " + (i + 1)); + System.out.println("Nodecount: " + nodes.size()); + int oldsize = nodes.size(); + add((int) Math.round(oldsize * initBirthRate)); + remove((int) Math.round(oldsize * initDeathRate)); + } + break; + default: + System.out.println("Unknown command."); + break; + } + } + } + + 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); + } + } + + public static void remove(int count) { + for (int i = 0; i < count; i++) { + int index = gen.nextInt(nodes.size()); + if (index < nodes.size()) { + nodes.get(index).leave(); + nodes.remove(index); + } + } + } }