71 lines
1.5 KiB
Java
71 lines
1.5 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.logging.LogManager;
|
|
|
|
import node.Node;
|
|
|
|
public class Main {
|
|
|
|
public static Map<String, Node> nodes = new HashMap<String, Node>();
|
|
|
|
/**
|
|
* grow a network: Starting with one peer, repeatedly let peers spawn or
|
|
* leave at random
|
|
*
|
|
* @throws IOException
|
|
*/
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
System.setProperty("java.util.logging.config.file",
|
|
"logging.properties");
|
|
|
|
try {
|
|
LogManager.getLogManager().readConfiguration();
|
|
}
|
|
|
|
catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
nodes.put("a", new Node());
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
|
String s;
|
|
while ((s = in.readLine()) != null && s.length() != 0) {
|
|
String[] splitted = s.split(" ");
|
|
|
|
String node = splitted[0];
|
|
String cmd = splitted[1];
|
|
|
|
if (nodes.containsKey(node)) {
|
|
|
|
switch (cmd) {
|
|
case "spawn":
|
|
if (splitted.length > 2) {
|
|
Node newNode = nodes.get(node).spawn();
|
|
String newNodeName = splitted[2];
|
|
nodes.put(newNodeName, newNode);
|
|
} else {
|
|
System.out
|
|
.println("Please enter a name for the new node.");
|
|
}
|
|
break;
|
|
case "leave":
|
|
Node theNode = nodes.get(node);
|
|
theNode.leave();
|
|
nodes.remove(theNode);
|
|
break;
|
|
default:
|
|
System.out.println("Unknown command.");
|
|
break;
|
|
}
|
|
|
|
} else {
|
|
System.out.println("No such node.");
|
|
}
|
|
}
|
|
}
|
|
} |