Guest User

Untitled

a guest
Oct 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.PrintWriter;
  3. import java.io.FileWriter;
  4. import java.util.Collections;
  5. import java.util.LinkedList;
  6.  
  7. public class Forwarding {
  8. static void dieUsage() {
  9. System.err.println("Usage:");
  10. System.err.println("java Forwarding <depth> <fanout> [dotfile]");
  11. System.err.println("(depth and fanout must be integers >= 1)");
  12. System.exit(1);
  13. }
  14.  
  15. public static void main(String[] argv) {
  16. int depth = -1;
  17. int fanout = -1;
  18. String filename = null;
  19.  
  20. if(argv.length >= 2) {
  21. depth = Integer.parseInt(argv[0]);
  22. fanout = Integer.parseInt(argv[1]);
  23. } else dieUsage();
  24.  
  25. if(depth < 1 || fanout < 1) dieUsage();
  26.  
  27. if(argv.length >= 3) {
  28. filename = argv[2];
  29. }
  30.  
  31. TreeNode root = new TreeNode();
  32. root.build(depth, fanout);
  33. System.out.println(root);
  34.  
  35. if(filename != null) {
  36. root.saveDOT(new File(filename));
  37. }
  38.  
  39. LinkedList<TreeNode> leaves = root.getLeaves(); // these are the HOSTS
  40.  
  41.  
  42. // Go through each unique pair (src,dst) of leaves, and compute the path from src to dst.
  43.  
  44. public void printAllPaths(int src, int dst)
  45. {
  46. boolean[] isVisited = new boolean[v];
  47. ArrayList<Integer> pathList = new ArrayList<>();
  48.  
  49. //add source to path[]
  50. pathList.add(s);
  51.  
  52. //Call recursive utility
  53. printAllPathsUtil(src, dst, isVisited, pathList);
  54. }
  55.  
  56. // For a single path, you can use the Interface class to store each hop on the path (iface can be used for
  57. // the input interface, and iface2 can be used for the output interface).
  58. }
  59. }
  60.  
  61. class Interface {
  62. int iface;
  63. TreeNode node;
  64.  
  65. private int iface2 = 0;
  66.  
  67. Interface(TreeNode node) {
  68. this(0, node);
  69. }
  70.  
  71. Interface(int iface, TreeNode node) {
  72. this.iface = iface;
  73. this.node = node;
  74. }
  75.  
  76. Interface(int iface1, TreeNode node, int iface2) {
  77. this.iface = iface1;
  78. this.iface2 = iface2;
  79. this.node = node;
  80. }
  81.  
  82. int getIface2() {
  83. return iface2;
  84. }
  85.  
  86. void setIface2(int iface2) {
  87. this.iface2 = iface2;
  88. }
  89.  
  90. public String toString() {
  91. String s;
  92.  
  93. if(iface2 == 0) s= String.format("(%d->%s)", iface, node.getName());
  94. else s = String.format("(%d->%s->%d)", iface, node.getName(), iface2);
  95.  
  96. return s;
  97. }
  98. }
  99.  
  100. class TreeNode {
  101. private Interface parent;
  102. int id;
  103. LinkedList<Interface> children = new LinkedList<Interface>();
  104. boolean isLeaf;
  105.  
  106. private static int routerId;
  107. private static int hostId;
  108.  
  109. TreeNode() {
  110. this.parent = null;
  111. this.id = 1;
  112. }
  113.  
  114. TreeNode(TreeNode parent, int id) {
  115. this(parent, id, false);
  116. }
  117.  
  118. TreeNode(TreeNode parent, int id, boolean isLeaf) {
  119. this.parent = new Interface(parent);
  120. this.id = id;
  121. this.isLeaf = isLeaf;
  122. updateParent();
  123. }
  124.  
  125. int getChildInterface(TreeNode child) {
  126. for(Interface i : children) {
  127. if(child.equals(i.node)) return i.iface;
  128. }
  129. return 0;
  130. }
  131.  
  132. TreeNode getParent() {
  133. return (parent != null ? parent.node : null);
  134. }
  135.  
  136. int getParentInterface() {
  137. return (parent != null ? parent.iface : 0);
  138. }
  139.  
  140. void addChild(TreeNode n) {
  141. int i = children.size() + (isLeaf() ? 0 : 1);
  142. children.add(new Interface(i, n));
  143. updateParent();
  144. }
  145.  
  146. boolean isLeaf() {
  147. return isLeaf;
  148. }
  149.  
  150. String getName() {
  151. return String.format("%s%d", isLeaf() ? "h" : "s", id);
  152. }
  153.  
  154. LinkedList<TreeNode> getLeaves() {
  155. LinkedList<TreeNode> l = new LinkedList<TreeNode>();
  156.  
  157. if(isLeaf()) {
  158. l.add(this);
  159. } else {
  160. for(Interface i : children) {
  161. l.addAll(i.node.getLeaves());
  162. }
  163. }
  164.  
  165. return l;
  166. }
  167.  
  168. void build(int depth, int fanout) {
  169. routerId = id;
  170. hostId = 0;
  171. build(this, depth, fanout);
  172. }
  173.  
  174. private static void build(TreeNode root, int depth, int fanout) {
  175. for(int i = 0; i < fanout; i++) {
  176. boolean isLeaf = (depth == 1);
  177. int id = isLeaf ? ++hostId : ++routerId;
  178. TreeNode n = new TreeNode(root, id, isLeaf);
  179. root.addChild(n);
  180. if(!isLeaf) build(n, depth-1, fanout);
  181. }
  182. }
  183.  
  184. private void updateParent() {
  185. if(parent != null) {
  186. if(isLeaf()) parent.iface = 0;
  187. else parent.iface = children.size()+1;
  188. }
  189. }
  190.  
  191. public String toString() {
  192. return toString(0);
  193. }
  194.  
  195. public String toString(int num) {
  196. String str = "";
  197. String space = num == 0 ? "" : String.format("%"+num+"s", " ");
  198. for(Interface i : children) {
  199. TreeNode n = i.node;
  200. str += String.format("%s%d->%s,n", space+" ", i.iface, n.toString(num+3));
  201. }
  202. return String.format("%s{name=%s, parent=%s,n%s%s}", isLeaf() ? "Host" : "Router", getName(), parent != null ? parent : "None", str, space);
  203. }
  204.  
  205. String toDotString() {
  206. String s = String.format("%s [];n", getName());
  207. for(Interface i : children) {
  208. s += i.node.toDotString();
  209. s += String.format("%s -- %s [taillabel="%d", headlabel="%d"];n", getName(), i.node.getName(), i.iface, i.node.getParentInterface());
  210. }
  211. return s;
  212. }
  213.  
  214. void saveDOT(File f) {
  215. try {
  216. PrintWriter out = new PrintWriter(new FileWriter(f), true);
  217. out.println("graph {");
  218. out.print(toDotString());
  219. out.println("}");
  220. out.close();
  221. } catch(Exception ex) {
  222.  
  223. }
  224. }
  225. }
Add Comment
Please, Sign In to add comment