Merge branch 'master' of mojotrollz.eu:college

This commit is contained in:
Denis 2012-12-04 14:46:59 +01:00
commit 103ae0deed
4 changed files with 80 additions and 65 deletions

View File

@ -6,10 +6,12 @@ import java.util.List;
import java.util.Random;
import java.util.logging.LogManager;
import analysis.NetworkDumper;
import node.Node;
public class RandomGenerator {
public static int initNodeCount = 20;
public static int initNodeCount = 99;
public static double initBirthRate = 0.50;
public static double initDeathRate = 0.20;
public static int initRounds = 13;
@ -47,8 +49,16 @@ public class RandomGenerator {
}
switch (cmd) {
case "br":
//
case "dump":
nodes.get(0).gatherInformationOfNetwork();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
NetworkDumper d = new NetworkDumper(nodes.get(0));
d.write(d.networkToDot(nodes.get(0).getNetwork()));
break;
case "add":
add(count);

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();
@ -25,10 +26,10 @@ public class RandomGenerator2 {
public RandomGenerator2() {
firstNode = new Node();
nodes.add(firstNode);
new NetworkDumper(firstNode);
new NetworkDumper(firstNode).start();
for (int i = 0; i < START_NODES; i++) {
nodes.add(new Node());
for (int i = 0; i < START_NODES - 1; i++) {
spawn();
}
while (true) {
@ -40,7 +41,7 @@ public class RandomGenerator2 {
}
try {
Thread.sleep(5000);
Thread.sleep(ROUND_INTERVAL);
} catch (InterruptedException e) {
}
}

View File

@ -11,73 +11,77 @@ 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();
}
}
public String networkToDot(Map<String, List<String>> network) {
StringBuilder result = new StringBuilder(512);
result.append("graph g{\n");
public void start() {
new Thread(this).start();
}
Set<String> alreadyIn = new HashSet<String>();
public String networkToDot(Map<String, List<String>> network) {
StringBuilder result = new StringBuilder(512);
result.append("graph g{\n");
for (Map.Entry<String, List<String>> entry : network.entrySet()) {
Set<String> alreadyIn = new HashSet<String>();
for (String s : entry.getValue()) {
String nodeA = getName(entry.getKey());
String nodeB = getName(s);
for (Map.Entry<String, List<String>> entry : network.entrySet()) {
if (!alreadyIn.contains(nodeB + nodeA)) {
for (String s : entry.getValue()) {
String nodeA = getName(entry.getKey());
String nodeB = getName(s);
result.append("\t");
result.append(nodeA).append(" -- ").append(nodeB);
result.append(";\n");
if (!alreadyIn.contains(nodeB + nodeA)) {
alreadyIn.add(nodeA + nodeB);
}
}
}
result.append("}\n");
return result.toString();
}
result.append("\t");
result.append(nodeA).append(" -- ").append(nodeB);
result.append(";\n");
private String getName(String s) {
return s.split(":")[1];
}
alreadyIn.add(nodeA + nodeB);
}
}
}
result.append("}\n");
return result.toString();
}
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 String getName(String s) {
return s.split(":")[1];
}
@Override
public void run() {
while (running) {
try {
node.gatherInformationOfNetwork();
public 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());
}
}
// Wait 1s to broadcast to network
Thread.sleep(1000);
@Override
public void run() {
while (running) {
try {
node.gatherInformationOfNetwork();
write(networkToDot(node.getNetwork()));
// Wait 1s to broadcast to network
Thread.sleep(1000);
// Wait 10s for update
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
write(networkToDot(node.getNetwork()));
// Wait 10s for update
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
}

View File

@ -33,7 +33,7 @@ public class Node {
/**
* Saves the neighbor of each node in the network
*/
private Map<String, List<String>> network = new HashMap<>();
private Map<String, List<String>> network = new HashMap<String, List<String>>();
private volatile Thread thread;
private UDPHandler udpListen;
@ -221,14 +221,14 @@ public class Node {
}
public void gatherInformationOfNetwork() {
byte[] myAddr;
network = new HashMap<String, List<String>>();
try {
myAddr = BufferUtil.addrToBytes(((InetSocketAddress) channel
byte[] myAddr = BufferUtil.addrToBytes(((InetSocketAddress) channel
.getLocalAddress()));
sendBroadcast(MessageType.STATUS, myAddr);
} catch (IOException e) {
}
}
protected void setNeighborsOfNode(String node, List<String> neighbors) {