Guest User

Untitled

a guest
Oct 21st, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 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.         // TODO:
  42.         // Go through each unique pair (src,dst) of leaves, and compute the path from src to dst.
  43.         // For a single path, you can use the Interface class to store each hop on the path (iface can be used for
  44.         // the input interface, and iface2 can be used for the output interface).
  45.     }
  46. }
  47.  
  48. class Interface {
  49.     int iface;
  50.     TreeNode node;
  51.  
  52.     private int iface2 = 0;
  53.  
  54.     Interface(TreeNode node) {
  55.         this(0, node);
  56.     }
  57.  
  58.     Interface(int iface, TreeNode node) {
  59.         this.iface = iface;
  60.         this.node = node;
  61.     }
  62.  
  63.     Interface(int iface1, TreeNode node, int iface2) {
  64.         this.iface = iface1;
  65.         this.iface2 = iface2;
  66.         this.node = node;
  67.     }
  68.  
  69.     int getIface2() {
  70.         return iface2;
  71.     }
  72.  
  73.     void setIface2(int iface2) {
  74.         this.iface2 = iface2;
  75.     }
  76.  
  77.     public String toString() {
  78.         String s;
  79.        
  80.         if(iface2 == 0) s= String.format("(%d->%s)", iface, node.getName());
  81.         else s = String.format("(%d->%s->%d)", iface, node.getName(), iface2);
  82.  
  83.         return s;
  84.     }
  85. }
  86.  
  87. class TreeNode {
  88.     private Interface parent;
  89.     int id;
  90.     LinkedList<Interface> children = new LinkedList<Interface>();
  91.     boolean isLeaf;
  92.  
  93.     private static int routerId;
  94.     private static int hostId;
  95.  
  96.     TreeNode() {
  97.         this.parent = null;
  98.         this.id = 1;
  99.     }
  100.  
  101.     TreeNode(TreeNode parent, int id) {
  102.         this(parent, id, false);
  103.     }
  104.  
  105.     TreeNode(TreeNode parent, int id, boolean isLeaf) {
  106.         this.parent = new Interface(parent);
  107.         this.id = id;
  108.         this.isLeaf = isLeaf;
  109.         updateParent();
  110.     }
  111.  
  112.     int getChildInterface(TreeNode child) {
  113.         for(Interface i : children) {
  114.             if(child.equals(i.node)) return i.iface;
  115.         }
  116.         return 0;
  117.     }
  118.  
  119.     TreeNode getParent() {
  120.         return (parent != null ? parent.node : null);
  121.     }
  122.  
  123.     int getParentInterface() {
  124.         return (parent != null ? parent.iface : 0);
  125.     }
  126.  
  127.     void addChild(TreeNode n) {
  128.         int i = children.size() + (isLeaf() ? 0 : 1);
  129.         children.add(new Interface(i, n));
  130.         updateParent();
  131.     }
  132.  
  133.     boolean isLeaf() {
  134.         return isLeaf;
  135.     }
  136.  
  137.     String getName() {
  138.         return String.format("%s%d", isLeaf() ? "h" : "s", id);
  139.     }
  140.  
  141.     LinkedList<TreeNode> getLeaves() {
  142.         LinkedList<TreeNode> l = new LinkedList<TreeNode>();
  143.  
  144.         if(isLeaf()) {
  145.             l.add(this);
  146.         } else {
  147.             for(Interface i : children) {
  148.                 l.addAll(i.node.getLeaves());
  149.             }
  150.         }
  151.  
  152.         return l;
  153.     }
  154.  
  155.     void build(int depth, int fanout) {
  156.         routerId = id;
  157.         hostId = 0;
  158.         build(this, depth, fanout);
  159.     }
  160.  
  161.     private static void build(TreeNode root, int depth, int fanout) {
  162.         for(int i = 0; i < fanout; i++) {
  163.             boolean isLeaf = (depth == 1);
  164.             int id = isLeaf ? ++hostId : ++routerId;
  165.             TreeNode n = new TreeNode(root, id, isLeaf);
  166.             root.addChild(n);
  167.             if(!isLeaf) build(n, depth-1, fanout);
  168.         }
  169.     }
  170.  
  171.     private void updateParent() {
  172.         if(parent != null) {
  173.             if(isLeaf()) parent.iface = 0;
  174.             else parent.iface = children.size()+1;
  175.         }
  176.     }
  177.  
  178.     public String toString() {
  179.         return toString(0);
  180.     }
  181.  
  182.     public String toString(int num) {
  183.         String str = "";
  184.         String space = num == 0 ? "" : String.format("%"+num+"s", " ");
  185.         for(Interface i : children) {
  186.             TreeNode n = i.node;
  187.             str += String.format("%s%d->%s,\n", space+"   ", i.iface, n.toString(num+3));
  188.         }
  189.         return String.format("%s{name=%s, parent=%s,\n%s%s}", isLeaf() ? "Host" : "Router", getName(), parent != null ? parent : "None", str, space);
  190.     }
  191.  
  192.     String toDotString() {
  193.         String s = String.format("%s [];\n", getName());
  194.         for(Interface i : children) {
  195.             s += i.node.toDotString();
  196.             s += String.format("%s -- %s [taillabel=\"%d\", headlabel=\"%d\"];\n", getName(), i.node.getName(), i.iface, i.node.getParentInterface());
  197.         }
  198.         return s;
  199.     }
  200.  
  201.     void saveDOT(File f) {
  202.         try {
  203.             PrintWriter out = new PrintWriter(new FileWriter(f), true);
  204.             out.println("graph {");
  205.             out.print(toDotString());
  206.             out.println("}");
  207.             out.close();
  208.         } catch(Exception ex) {
  209.        
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment