📄 network.java
字号:
/** * simulator/Network.java */package simulator;import implementations.SimpleContact;import implementations.SimpleNode;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.lang.reflect.Constructor;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import util.CommandStatus;import util.Verbose;/** * */public class Network extends Simulator{ private TrafficGenerator traffic = null; private Node defaultNode = null; private Constructor newNodeConstructor = null; private Contact defaultContact = null; private Constructor newContactConstructor = null; private HashMap<String, Node> nodes = new HashMap<String, Node>(); private HashMap<String, Contact> contacts = new HashMap<String, Contact>(); private Stats stats = new Stats(this); private int nextMessageId = 1; private int nextRoutingMessageId = 1; public Network() { setDefaultNode(new SimpleNode()); setDefaultContact(new SimpleContact()); setTrafficGenerator(new TrafficGenerator()); } public Stats stats() { return stats; } public int getNextMessageId() { if (vShouldLog(Verbose.DEBUG5)) vprint("USED_MSG_ID: " + nextMessageId); return nextMessageId++; } public int getNextRoutingMessageId() { if (vShouldLog(Verbose.DEBUG5)) vprint("USED_R_MSG_ID: " + nextRoutingMessageId); return nextRoutingMessageId++; } public Iterator<Node> getAllNodes() { if (nodes == null) return (new ArrayList<Node>()).iterator(); return nodes.values().iterator(); } public Node getDefaultNode() { return defaultNode; } public Node getNode(String nodeName) { String tmpN = nodeName.toLowerCase(); Node n = nodes.get(tmpN); if (n == null) { Throwable t; try { n = (Node) newNodeConstructor.newInstance(new Object[] { defaultNode }); } catch (InstantiationException e) { if (e.getCause() == null) t = e; else t = e.getCause(); // Rethrow with a friendly message // Did I mention that I hate checked exceptions? // TODO: We could use Bruce Eckel's ExceptionAdapter to preserve // the stack: // http://www.mindview.net/Etc/Discussions/CheckedExceptions throw new RuntimeException("Constructing the node failed: " + t); } catch (IllegalAccessException e) { if (e.getCause() == null) t = e; else t = e.getCause(); throw new RuntimeException("Constructing the node failed: " + t); } catch (java.lang.reflect.InvocationTargetException e) { if (e.getCause() == null) t = e; else t = e.getCause(); throw new RuntimeException("Constructing the node failed: " + t); } n.initNode(); n.setName(nodeName); nodes.put(tmpN, n); if (vShouldLog(Verbose.DEBUG1)) vprint("CREATED: " + n); return n; } return n; } public Contact getDefaultContact() { return defaultContact; } public Iterator<Contact> getAllContacts() { if (contacts == null) return (new ArrayList<Contact>()).iterator(); return contacts.values().iterator(); } // This will not create new contact if one does not exist, // the other getContact versions will public Contact getContact(String contactName) { return contacts.get(contactName.toLowerCase()); } public Contact getContact(String a, String b) { return getContact(a, b, null); } public Contact getContact(String cNameA, String cNameB, String cNameC) { String cName; if (cNameC == null || cNameC.length() < 1) { cName = cNameA + "->" + cNameB; } else { cName = cNameA + "->" + cNameB + ":" + cNameC; } String tmpC = cName.toLowerCase(); Contact c = contacts.get(tmpC); if (c == null) { Throwable t; try { c = (Contact) newContactConstructor.newInstance(new Object[] { defaultContact }); } catch (InstantiationException e) { if (e.getCause() == null) t = e; else t = e.getCause(); throw new RuntimeException("Constructing the contact failed: " + t); } catch (IllegalAccessException e) { if (e.getCause() == null) t = e; else t = e.getCause(); throw new RuntimeException("Constructing the contact failed: " + t); } catch (java.lang.reflect.InvocationTargetException e) { if (e.getCause() == null) t = e; else t = e.getCause(); throw new RuntimeException("Constructing the contact failed: " + t); } assert c != null; c.setName(cName); Node src = getNode(cNameA); c.setSource(src); c.setDest(getNode(cNameB)); src.addContact(c); contacts.put(tmpC, c); if (vShouldLog(Verbose.DEBUG1)) vprint("CREATED: " + c); return c; } return c; } public CommandStatus parseLocalCommand(String cmd, ArrayList<ArrayList<String>> args, String parentPath) { if (cmd.equals("network_element")) { String ret = setElementType(args); if (ret != null) return new CommandStatus(ret); return new CommandStatus(CommandStatus.COMMAND_OK); } else if (cmd.equals("traffic") || cmd.equals("default_traffic")) { assert traffic != null; return traffic.parseLocalCommand(cmd, args, parentPath); } else if (cmd.equals("stats")) { assert stats != null; return stats.parseLocalCommand(cmd, args, parentPath); } else if (cmd.equals("simulator")) { if (args.size() < 1 || args.get(0).size() != 2 || !args.get(0).get(0).equals("notify_step")) { return new CommandStatus("Wrong simulator command"); } else { try { setTimeNotifyStep(Double.parseDouble(args.get(0).get(1))); return new CommandStatus(CommandStatus.COMMAND_OK); } catch (NumberFormatException e) { return new CommandStatus("Error parsing value of parameter '" + args.get(0).get(0) + "': " + e); } } } else if (cmd.equals("network_graph")) { if (args.size() != 1 || args.get(0).size() != 2 || !args.get(0).get(0).equals("file")) { return new CommandStatus("You have to give file=filename parameter for network_graph command!"); } File outputFile = new File(parentPath, args.get(0).get(1)); if (outputFile.exists()) { return new CommandStatus("File '" + args.get(0).get(1) + "' exists."); } BufferedWriter output = null; try { output = new BufferedWriter(new FileWriter(outputFile, true)); } catch (IOException e) { return new CommandStatus("Error opening file '" + outputFile + "' for writing/appending: " + e); } try { output.append("graph G {\n"); Iterator<Node> i1 = nodes.values().iterator(); HashSet<Node> nDone = new HashSet<Node>(); while (i1.hasNext()) { Node n1 = i1.next(); Iterator<Contact> i2 = n1.getContacts(); while (i2.hasNext()) { Node n2 = i2.next().getDest(); if (!nDone.contains(n2)) { output.append("\t" + n1.getName() + " -- " + n2.getName() + ";\n"); } } nDone.add(n1); } output.append("}\n"); } catch (IOException e) { return new CommandStatus("Error writing to file '" + outputFile + "': " + e); } try { output.close(); } catch (IOException e) { return new CommandStatus("Error closing file '" + outputFile + "' after writing: " + e); } return new CommandStatus(CommandStatus.COMMAND_OK); } return null; } public void setDefaultContact(Contact dC) { defaultContact = dC; dC.setNetwork(this); try { newContactConstructor = dC.getClass().getConstructor(new Class[] { dC.getClass() }); } catch (NoSuchMethodException e) { throw new RuntimeException("Could not create new constructor for specified class '" + dC.getClass().getName() + "': " + e); } assert newContactConstructor != null; } public void setDefaultNode(Node dN) { assert dN != null; if (defaultNode != null) defaultNode.defaultUnload(); defaultNode = dN; dN.setNetwork(this); Constructor newConstr = null; try { newConstr = dN.getClass().getConstructor(new Class[] { dN.getClass() }); } catch (NoSuchMethodException e) { throw new RuntimeException("Could not get the constructor for specified class '" + dN.getClass().getName() + "': " + e); } assert newConstr != null; newNodeConstructor = newConstr; defaultNode.defaultLoad(); return; } public void setTrafficGenerator(TrafficGenerator tg) { assert tg != null; traffic = tg; traffic.setNetwork(this); return; } public String setElementType(ArrayList<ArrayList<String>> cmd) { if (cmd.size() < 1) { return "Error! Command network_element needs parameters!"; } Class newType = null; for (int i = 0, sz = cmd.size(); i < sz; ++i) { if (cmd.get(i).size() != 2) { return "Error! Command needs parameters with single values!"; } try { newType = Class.forName(cmd.get(i).get(1)); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Specified class not found: " + e.getMessage()); } assert newType != null; Object newDefault = null; try { newDefault = newType.newInstance(); } catch (Exception e) { throw new RuntimeException("Could not create new instance of specified class '" + newType.getName() + "': " + e); } assert newDefault != null; if (cmd.get(i).get(0).equals("node")) { if (!Node.class.isAssignableFrom(newType)) { return "simulator.Node can not be assigned from specified class '" + newType + "'!"; } setDefaultNode((Node) newDefault); if (vShouldLog(Verbose.DEBUG1)) vprint("NEW_NODE_CLASS: " + cmd.get(i).get(1)); } else if (cmd.get(i).get(0).equals("contact")) { if (!Contact.class.isAssignableFrom(newType)) { return "simulator.Contact can not be assigned from specified class '" + newType + "'!"; } setDefaultContact((Contact) newDefault); if (vShouldLog(Verbose.DEBUG1)) vprint("NEW_CONTACT_CLASS: " + cmd.get(i).get(1)); } else if (cmd.get(i).get(0).equals("traffic_generator")) { if (!TrafficGenerator.class.isAssignableFrom(newType)) { return "simulator.TrafficGenerator can not be assigned from specified class '" + newType + "'!"; } setTrafficGenerator((TrafficGenerator) newDefault); if (vShouldLog(Verbose.DEBUG1)) vprint("NEW_TRAFFIC_GEN_CLASS: " + cmd.get(i).get(1)); } else { return "Unknown default element type: '" + cmd.get(i).get(0) + "'!"; } } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -