00001
00002
00003
00004
00005
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
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
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
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
00076 }
00077
00078 return next_node;
00079 }
00080
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
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
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
00120 if (old_element.cost >= 16)
00121 {
00122
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
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
00146 if (old_element.cost >= new_element.cost + 1)
00147 {
00148
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
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
00169 }
00170 else if(new_element.destination_node != owner_node)
00171 {
00172
00173 addElement(new_element.destination_node, node,
00174 new_element.cost + 1
00175 );
00176 }
00177
00178 }
00179
00180 }
00181
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
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
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
00227 addElement(new_element.destination_node, new_element.next_node,
00228 new_element.cost + 1
00229 );
00230 }
00231
00232 }
00233
00234 }
00235
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
00262 table.removeElementAt(i);
00263 i--;
00264 }
00265 }
00266
00267 }
00268
00269 }
00270
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
00295 }
00296
00297 }
00298
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
00321 }
00322
00323 }
00324
00325
00331 public RIPTableElement elementAt (int index)
00332 {
00333 return (RIPTableElement)table.elementAt(index);
00334 }
00335
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
00358
00363 public int size ()
00364 {
00365 return table.size();
00366 }
00367
00368
00369 }
00370