NetworkDumper dumps network to different file every X seconds

This commit is contained in:
senft-lap 2012-12-04 14:27:29 +01:00
parent b20d77a032
commit ad87916472
2 changed files with 58 additions and 56 deletions

View File

@ -12,6 +12,7 @@ public class RandomGenerator2 {
public static double BIRTH_RATE = 0.50;
private static int START_NODES = 100;
private static int ROUND_INTERVAL = 1000; // ms
public static List<Node> nodes = new ArrayList<Node>();
public static Random gen = new Random();
@ -27,7 +28,7 @@ public class RandomGenerator2 {
nodes.add(firstNode);
new NetworkDumper(firstNode);
for (int i = 0; i < START_NODES; i++) {
for (int i = 0; i < START_NODES - 1; i++) {
spawn();
}
@ -40,7 +41,7 @@ public class RandomGenerator2 {
}
try {
Thread.sleep(5000);
Thread.sleep(ROUND_INTERVAL);
} catch (InterruptedException e) {
}
}

View File

@ -11,73 +11,74 @@ import node.Node;
public class NetworkDumper implements Runnable {
Node node = null;
private boolean running = true;
Node node = null;
private boolean running = true;
public NetworkDumper(Node n) {
this.node = n;
public NetworkDumper(Node n) {
this.node = n;
new Thread(this).start();
}
new Thread(this).start();
}
public String networkToDot(Map<String, List<String>> network) {
StringBuilder result = new StringBuilder(512);
result.append("graph g{\n");
public String networkToDot(Map<String, List<String>> network) {
StringBuilder result = new StringBuilder(512);
result.append("graph g{\n");
Set<String> alreadyIn = new HashSet<String>();
Set<String> alreadyIn = new HashSet<String>();
for (Map.Entry<String, List<String>> entry : network.entrySet()) {
for (Map.Entry<String, List<String>> entry : network.entrySet()) {
for (String s : entry.getValue()) {
String nodeA = getName(entry.getKey());
String nodeB = getName(s);
for (String s : entry.getValue()) {
String nodeA = getName(entry.getKey());
String nodeB = getName(s);
if (!alreadyIn.contains(nodeB + nodeA)) {
if (!alreadyIn.contains(nodeB + nodeA)) {
result.append("\t");
result.append(nodeA).append(" -- ").append(nodeB);
result.append(";\n");
result.append("\t");
result.append(nodeA).append(" -- ").append(nodeB);
result.append(";\n");
alreadyIn.add(nodeA + nodeB);
}
}
}
result.append("}\n");
return result.toString();
}
alreadyIn.add(nodeA + nodeB);
}
}
}
result.append("}\n");
return result.toString();
}
private String getName(String s) {
return s.split(":")[1];
}
private String getName(String s) {
return s.split(":")[1];
}
private void write(String s) {
try {
// Create file
FileWriter fstream = new FileWriter("graph.dot");
BufferedWriter out = new BufferedWriter(fstream);
out.write(s);
// Close the output stream
out.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
private void write(String s) {
try {
// Create file
FileWriter fstream = new FileWriter("graphs/"
+ System.currentTimeMillis() + ".dot");
BufferedWriter out = new BufferedWriter(fstream);
out.write(s);
// Close the output stream
out.close();
} catch (Exception e) {// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
@Override
public void run() {
while (running) {
try {
node.gatherInformationOfNetwork();
@Override
public void run() {
while (running) {
try {
node.gatherInformationOfNetwork();
// Wait 1s to broadcast to network
Thread.sleep(1000);
// Wait 1s to broadcast to network
Thread.sleep(1000);
write(networkToDot(node.getNetwork()));
write(networkToDot(node.getNetwork()));
// Wait 10s for update
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
// Wait 10s for update
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
}