Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.66 KB | None | 0 0
  1.  import javax.swing.*;        
  2.      
  3.     public class RouterNode {
  4.       private int myID;
  5.       private GuiTextArea myGUI;
  6.       private RouterSimulator sim;
  7.       private int [] Neighbours = new int[RouterSimulator.NUM_NODES];
  8.       private int[][] UpdatedConnectcosts = new int[RouterSimulator.NUM_NODES][RouterSimulator.NUM_NODES];
  9.      
  10.       private int[] costs = new int[RouterSimulator.NUM_NODES];
  11.       private int[] ByNode = new int[RouterSimulator.NUM_NODES];
  12.       private boolean PoisonedReverse = false;
  13.      
  14.       //--------------------------------------------------
  15.       public RouterNode(int ID, RouterSimulator sim, int[] costs) {
  16.         myID = ID;
  17.         this.sim = sim;
  18.         myGUI =new GuiTextArea("  Output window for Router #"+ ID + "  ");
  19.      
  20.         //Added
  21.         System.arraycopy(costs, 0, this.costs, 0, RouterSimulator.NUM_NODES);
  22.         System.arraycopy(costs, 0, this.Neighbours, 0, RouterSimulator.NUM_NODES);
  23.      
  24.         for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  25.         {
  26.             if (i == this.myID)
  27.             {
  28.                 //costs[this.myID] = 0;
  29.                 System.arraycopy(costs, 0, this.UpdatedConnectcosts[i], 0, RouterSimulator.NUM_NODES);
  30.                
  31.             }
  32.             else
  33.             {
  34.                 for (int j = 0; j < RouterSimulator.NUM_NODES; j++)
  35.                     {
  36.                         this.UpdatedConnectcosts[i][j] = RouterSimulator.INFINITY;
  37.                     }
  38.             }
  39.         }
  40.        
  41.         sendCosts();
  42.      
  43.         for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  44.             {
  45.                 if (costs[i] != RouterSimulator.INFINITY){
  46.                     ByNode[i] = i;
  47.                 }
  48.                 else{
  49.                     ByNode[i] = -1;
  50.                 }
  51.                    
  52.             }
  53.      
  54.       }
  55.      
  56.      
  57.       //--------------------------------------------------
  58.         private void sendCosts()
  59.         {
  60.             for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  61.             {
  62.                 if ( i != this.myID && !PoisonedReverse && Neighbours[i] != RouterSimulator.INFINITY)
  63.                     {
  64.                         //Make a Router_Packet and send it.
  65.                         RouterPacket pkt = new RouterPacket(this.myID, i, this.costs);
  66.                         sendUpdate(pkt);  
  67.                     }
  68.                 else
  69.                     {
  70.                         //PoisonedReverse
  71.                     }          
  72.             }
  73.         }
  74.      
  75.      
  76.      
  77.       //--------------------------------------------------
  78.       public void recvUpdate(RouterPacket pkt) {
  79.          
  80.      
  81.           System.arraycopy(pkt.mincost,0,this.UpdatedConnectcosts[pkt.sourceid],0,RouterSimulator.NUM_NODES);
  82.           myGUI.println ("Update received from pkt: " + pkt.destid)    ;
  83.      
  84.           // If the Bellman function modify something
  85.           if( Bellman() )
  86.               {
  87.                  
  88.                   // Send the modification to neighbors
  89.                   sendCosts();
  90.                                      
  91.               }
  92.      
  93.      
  94.       }
  95.      
  96.      
  97.       //--------------------------------------------------
  98.       private void sendUpdate(RouterPacket pkt) {
  99.         sim.toLayer2(pkt);
  100.      
  101.       }
  102.      
  103.      
  104.       //--------------------------------------------------
  105.       public void printDistanceTable() {
  106.               myGUI.println("Current table for " + myID +
  107.                             "  at time " + sim.getClocktime());
  108.               myGUI.println("Distancetable:");
  109.               myGUI.print("    dst  |");
  110.               for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  111.               {              
  112.                               myGUI.print("     " + i);    
  113.               }
  114.               myGUI.println();
  115.               myGUI.print("---------");
  116.               for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  117.               {
  118.                   myGUI.print("-------");
  119.               }
  120.               for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  121.               {
  122.                       // If it's a direct neighbor
  123.                      // if(Neighbours[i] != RouterSimulator.INFINITY && i != myID)
  124.                       //{
  125.                           myGUI.println();
  126.                           myGUI.print(F.format(" nbr", 6));
  127.                           myGUI.print(i + " |");
  128.                           for(int j = 0; j < RouterSimulator.NUM_NODES; j++)
  129.                               {
  130.                                       myGUI.print("  " + F.format(UpdatedConnectcosts[i][j], 3));
  131.                               }
  132.                      // }
  133.               }
  134.               myGUI.println();
  135.               myGUI.println();
  136.               myGUI.println("Our distance vector and routes:");
  137.               myGUI.print("    dst  |");
  138.               for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  139.               {              
  140.                   myGUI.print(F.format(i,4));      
  141.               }
  142.               myGUI.println();
  143.               myGUI.print("---------");
  144.               for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  145.               {
  146.                       myGUI.print("-------");
  147.               }
  148.               myGUI.println();
  149.               myGUI.print(" cost   |");
  150.               for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  151.               {
  152.                       myGUI.print("  " + F.format(costs[i], 3));
  153.               }
  154.               myGUI.println();
  155.               myGUI.print(F.format(" route |",8));
  156.               for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
  157.               {
  158.                   if (ByNode[i] != -1)
  159.                   {
  160.                       myGUI.print("  " + F.format(ByNode[i], 3));
  161.                   }
  162.                   else
  163.                   {
  164.                       myGUI.print("  " + F.format('-', 3));
  165.                   }
  166.               }
  167.               myGUI.println();
  168.               myGUI.println();
  169.       }
  170.      
  171.      
  172.       //--------------------------------------------------
  173.       private  boolean Bellman()
  174.         {
  175.            
  176.             boolean changed = false;
  177.            
  178.                      
  179.             for(int dest = 0; dest < RouterSimulator.NUM_NODES; dest++)
  180.             {
  181.                 int firstHop = RouterSimulator.INFINITY;
  182.                 int min_cost = RouterSimulator.INFINITY;
  183.                 if(dest==this.myID)
  184.                 {
  185.                     costs[dest] = 0;
  186.                 }
  187.                 else
  188.                 {
  189.                     for (int neig = 0; neig < RouterSimulator.NUM_NODES; neig++)
  190.                     {
  191.                         if ((Neighbours[neig] != RouterSimulator.INFINITY) && (Neighbours[neig] != 0))
  192.                         {
  193.                        
  194.                             if (Neighbours[neig] + UpdatedConnectcosts[neig][dest] < min_cost)
  195.                             {
  196.                                 min_cost = Neighbours[neig] + UpdatedConnectcosts[neig][dest];
  197.                                 firstHop = neig;                                                   
  198.                                
  199.                             }
  200.                         }
  201.                     }
  202.                    
  203.                     if ((min_cost != this.costs[dest]) && (min_cost != RouterSimulator.INFINITY))
  204.                     {
  205.                         ByNode[dest] = firstHop;
  206.                         this.costs[dest] = min_cost;  
  207.                         changed = true;
  208.                     }
  209.                 }
  210.             }
  211.             if (changed){
  212.                 UpdatedConnectcosts[this.myID] = this.costs;
  213.             }
  214.             return changed;
  215.         }
  216.      
  217.      
  218.       //--------------------------------------------------
  219.       public void updateLinkCost(int dest, int newcost) {
  220.          
  221.       }
  222.      
  223.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement