Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

RIPTable.java

Go to the documentation of this file.
00001 /* Cyclops
00002  * FER - SPVP
00003  * Fakultet elektrotehnike i racunarstva (http://www.fer.hr/)
00004  * Unska 3, 10000 Zagreb, Hrvatska
00005  * (c) 2001 FER, Zagreb.
00006  */
00007 
00008 
00009 package hr.fer.zesoi.cyclops;
00010 
00011 import java.util.*;
00012 
00013 
00018 public class RIPTable
00019 {
00020 
00022     Vector table = new Vector();
00023 
00025     Node owner_node;
00026 
00031     public RIPTable (Node owner)
00032     {
00033         this.owner_node = owner;
00034     }
00035     /* RIPTable */
00036 
00043     public void addElement (Node destination, Node next_node, float cost)
00044     {
00045         RIPTableElement new_element = new RIPTableElement(destination, next_node, cost);
00046         table.addElement(new_element);
00047     }
00048     /* addElement */
00049 
00056     public Node findNextNodeForRouteTo (Node destination_node)
00057     {
00058         int i;
00059         RIPTableElement element;
00060         Node next_node;
00061 
00062         next_node = null;
00063         for (i=0; i<table.size(); i++)
00064             {
00065                 element = (RIPTableElement)table.elementAt(i);
00066                 /* Route is valid only if cost is less than infinity (16). */
00067                 if (destination_node == element.destination_node)
00068                     {
00069                         next_node = element.next_node;
00070                         if (element.cost < 16)
00071                             {
00072                                 return next_node;
00073                             }
00074                     }
00075                 /* if */
00076             }
00077         /* for */
00078         return next_node;
00079     }
00080     /* findNextNodeForRouteTo */
00081 
00088     public void updateTable (Node node)
00089     {
00090         int i;
00091         int j;
00092         RIPTable another_table = node.getRIPTable();
00093         RIPTableElement old_element;
00094         RIPTableElement new_element;
00095 
00096         /* Metric update for node. */
00097         j = findEntry(node);
00098         if (j >= 0)
00099             {
00100                 old_element = (RIPTableElement)table.elementAt(j);
00101                 old_element.cost = 1;
00102                 old_element.next_node = node;
00103                 old_element.route_change_flag = true;
00104                 old_element.route_timeout = 5;
00105                 old_element.garbage_timeout = 5;
00106             }
00107         /* Metric update. */
00108         for (i=0; i<another_table.size(); i++)
00109             {
00110                 new_element = another_table.elementAt(i);
00111                 j = findEntry(new_element.destination_node);
00112                 if ((j >= 0) &&
00113                     (new_element.destination_node != owner_node)
00114                     )
00115                     {
00116                         old_element = (RIPTableElement)table.elementAt(j);
00117                         if (new_element.next_node == owner_node)
00118                             {
00119                                 /* Split horizon with poisoned reverse. Cost is 16. */
00120                                 if (old_element.cost >= 16)
00121                                     {
00122                                         /* Better route exists. */
00123                                         old_element.cost = 16;
00124                                         old_element.next_node = node;
00125                                         old_element.route_change_flag = true;
00126                                         old_element.route_timeout = 5;
00127                                         old_element.garbage_timeout = 5;
00128                                     }
00129                                 else if (old_element.next_node == node)
00130                                     {
00131                                         /* Update is required. */
00132                                         old_element.cost = 16;
00133                                         old_element.route_change_flag = true;
00134                                         old_element.route_timeout = 5;
00135                                         old_element.garbage_timeout = 5;
00136                                     }
00137                                 else
00138                                     {
00139                                         old_element.route_change_flag = false;
00140                                     }
00141 
00142                             }
00143                         else
00144                             {
00145                                 /* Normal update. */
00146                                 if (old_element.cost >= new_element.cost + 1)
00147                                     {
00148                                         /* Better route exists. */
00149                                         old_element.cost = new_element.cost + 1;
00150                                         old_element.next_node = node;
00151                                         old_element.route_change_flag = true;
00152                                         old_element.route_timeout = 5;
00153                                         old_element.garbage_timeout = 5;
00154                                     }
00155                                 else if (old_element.next_node == node)
00156                                     {
00157                                         /* Update is required. */
00158                                         old_element.cost = new_element.cost + 1;
00159                                         old_element.route_change_flag = true;
00160                                         old_element.route_timeout = 5;
00161                                         old_element.garbage_timeout = 5;
00162                                     }
00163                                 else
00164                                     {
00165                                         old_element.route_change_flag = false;
00166                                     }
00167                             }
00168                         /* else */
00169                     }
00170                 else if(new_element.destination_node != owner_node)
00171                     {
00172                         /* Create new table element. */
00173                         addElement(new_element.destination_node, node,
00174                                    new_element.cost + 1
00175                                    );
00176                     }
00177                 /* else */
00178             }
00179         /* for */ /* Process all elements. */
00180     }
00181     /* updateTable */
00182 
00187     public void updateTable (RIPTable another_table)
00188     {
00189         int i;
00190         int j;
00191         RIPTableElement old_element;
00192         RIPTableElement new_element;
00193 
00194         for (i=0; i<another_table.size(); i++)
00195             {
00196                 new_element = another_table.elementAt(i);
00197                 j = findEntry(new_element.destination_node);
00198                 if (j != -1)
00199                     {
00200                         old_element = (RIPTableElement)table.elementAt(j);
00201                         if (old_element.cost > new_element.cost + 1)
00202                             {
00203                                 /* Better route exists. */
00204                                 old_element.cost = new_element.cost + 1;
00205                                 old_element.next_node = new_element.next_node;
00206                                 old_element.route_change_flag = true;
00207                                 old_element.route_timeout = 5;
00208                                 old_element.garbage_timeout = 5;
00209                             }
00210                         else if (old_element.next_node == new_element.next_node)
00211                             {
00212                                 /* Update if required. */
00213                                 old_element.cost = new_element.cost + 1;
00214                                 old_element.route_change_flag = true;
00215                                 old_element.route_timeout = 5;
00216                                 old_element.garbage_timeout = 5;
00217                             }
00218                         else
00219                             {
00220                                 old_element.route_change_flag = false;
00221                             }
00222                     }
00223             
00224                 else
00225                     {
00226                         /* Create new table element. */
00227                         addElement(new_element.destination_node, new_element.next_node,
00228                                    new_element.cost + 1
00229                                    );
00230                     }
00231                 /* else */
00232             }
00233         /* for */ /* Process all elements. */
00234     }
00235     /* updateTable */
00236 
00240     public void garbageCollect ()
00241     {
00242         int i;
00243         RIPTableElement element;
00244 
00245         for (i=0; i<table.size(); i++)
00246             {
00247                 element = (RIPTableElement)table.elementAt(i);
00248                 if (!element.route_change_flag)
00249                     {
00250                         if (element.route_timeout>0)
00251                             {
00252                                 element.route_timeout -= 1;
00253                             }
00254                         else if (element.garbage_timeout>0)
00255                             {
00256                                 element.cost = 16;
00257                                 element.garbage_timeout -= 1;
00258                             }
00259                         else
00260                             {
00261                                 /* Entry expired, will be terminated. */
00262                                 table.removeElementAt(i);
00263                                 i--;
00264                             }
00265                     }
00266                 /* if */
00267             }
00268         /* for */
00269     }
00270     /* garbageCollect */
00271 
00277     public void setCostToInfinity (Node node)
00278     {
00279         int i;
00280         RIPTableElement element;
00281 
00282         for (i=0; i<table.size(); i++)
00283             {
00284                 element = (RIPTableElement)table.elementAt(i);
00285                 if ((element.next_node == node) ||
00286                     (element.destination_node == node)
00287                     )
00288                     {
00289                         element.cost = 16;
00290                         element.route_change_flag = true;
00291                         element.route_timeout = 5;
00292                         element.garbage_timeout = 5;
00293                     }
00294                 /* if */
00295             }
00296         /* for */
00297     }
00298     /* setCostToInfinity */
00299 
00305     public void setCost (Node destination, float cost)
00306     {
00307         int i;
00308         RIPTableElement element;
00309 
00310         for (i=0; i<table.size(); i++)
00311             {
00312                 element = (RIPTableElement)table.elementAt(i);
00313                 if (element.destination_node == destination)
00314                     {
00315                         element.cost = cost;
00316                         element.route_change_flag = true;
00317                         element.route_timeout = 5;
00318                         element.garbage_timeout = 5;
00319                     }
00320                 /* if */
00321             }
00322         /* for */
00323     }
00324     /* setCost */
00325 
00331     public RIPTableElement elementAt (int index)
00332     {
00333         return (RIPTableElement)table.elementAt(index);
00334     }
00335     /* elementAt */
00336 
00342     public int findEntry (Node destination_node)
00343     {
00344         int i;
00345         RIPTableElement element;
00346 
00347         for (i=0; i<table.size(); i++)
00348             {
00349                 element = (RIPTableElement)table.elementAt(i);
00350                 if (element.destination_node == destination_node)
00351                     {
00352                         return i;
00353                     }
00354             }
00355         return -1;
00356     }
00357     /* findEntry */
00358 
00363     public int size ()
00364     {
00365         return table.size();
00366     }
00367     /* size */
00368 
00369 }
00370 /* RIPTable */

Generated at Thu Jun 28 03:04:21 2001 for Cyclops Network Simulator by doxygen1.2.7 written by Dimitri van Heesch, © 1997-2001