blob: acdf6d585ef1fbbc05b5c2af05153814295bff4b [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include <iostream>
2#include <cmath>
3#include "nlsr_lsdb.hpp"
4#include "nlsr_rtc.hpp"
5#include "nlsr_map.hpp"
6#include "nlsr_lsa.hpp"
7#include "nlsr_nexthop.hpp"
8#include "nlsr.hpp"
9
akmhoque1fd8c1e2014-02-19 19:41:49 -060010namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060011{
akmhoque298385a2014-02-13 14:13:09 -060012
akmhoque1fd8c1e2014-02-19 19:41:49 -060013 using namespace std;
akmhoque298385a2014-02-13 14:13:09 -060014
akmhoque1fd8c1e2014-02-19 19:41:49 -060015 void
16 RoutingTableCalculator::allocateAdjMatrix()
17 {
18 adjMatrix = new double*[numOfRouter];
19 for(int i = 0; i < numOfRouter; ++i)
20 {
21 adjMatrix[i] = new double[numOfRouter];
22 }
23 }
akmhoque298385a2014-02-13 14:13:09 -060024
akmhoque1fd8c1e2014-02-19 19:41:49 -060025 void
26 RoutingTableCalculator::initMatrix()
27 {
28 for(int i=0; i<numOfRouter; i++)
29 {
30 for(int j=0; j<numOfRouter; j++)
31 adjMatrix[i][j]=0;
32 }
33 }
akmhoque298385a2014-02-13 14:13:09 -060034
akmhoque1fd8c1e2014-02-19 19:41:49 -060035 void
36 RoutingTableCalculator::makeAdjMatrix(Nlsr& pnlsr, Map pMap)
37 {
38 std::list<AdjLsa> adjLsdb=pnlsr.getLsdb().getAdjLsdb();
39 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
40 it!= adjLsdb.end() ; it++)
41 {
42 string linkStartRouter=(*it).getOrigRouter();
43 int row=pMap.getMappingNoByRouterName(linkStartRouter);
44 std::list<Adjacent> adl=(*it).getAdl().getAdjList();
45 for( std::list<Adjacent>::iterator itAdl=adl.begin();
46 itAdl!= adl.end() ; itAdl++)
47 {
48 string linkEndRouter=(*itAdl).getAdjacentName();
49 int col=pMap.getMappingNoByRouterName(linkEndRouter);
50 double cost=(*itAdl).getLinkCost();
51 if ( (row >= 0 && row<numOfRouter) && (col >= 0 && col<numOfRouter) )
52 {
53 adjMatrix[row][col]=cost;
54 }
55 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060056 }
57 }
akmhoque298385a2014-02-13 14:13:09 -060058
akmhoque1fd8c1e2014-02-19 19:41:49 -060059 void
60 RoutingTableCalculator::printAdjMatrix()
61 {
62 for(int i=0; i<numOfRouter; i++)
63 {
64 for(int j=0; j<numOfRouter; j++)
65 printf("%f ",adjMatrix[i][j]);
66 printf("\n");
67 }
68 }
akmhoque298385a2014-02-13 14:13:09 -060069
akmhoque1fd8c1e2014-02-19 19:41:49 -060070 void
71 RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost)
72 {
73 for ( int i = 0; i < numOfRouter; i++ )
74 {
75 if ( i == link )
76 {
77 adjMatrix[source][i]=linkCost;
78 }
79 else
80 {
81 adjMatrix[source][i]=0;
82 }
83 }
84 }
85
86 int
87 RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter)
88 {
89 int noLink=0;
90 for(int i=0; i<numOfRouter; i++)
91 {
92 if ( adjMatrix[sRouter][i] > 0 )
93 {
94 noLink++;
95 }
96 }
97 return noLink;
98 }
99
100 void
101 RoutingTableCalculator::getLinksFromAdjMatrix(int *links,
102 double *linkCosts, int source)
103 {
104 int j=0;
105 for (int i=0; i <numOfRouter; i++)
106 {
107 if ( adjMatrix[source][i] > 0 )
108 {
109 links[j]=i;
110 linkCosts[j]=adjMatrix[source][i];
111 j++;
112 }
113 }
114 }
115
116 void
117 RoutingTableCalculator::freeAdjMatrix()
118 {
119 for(int i = 0; i < numOfRouter; ++i)
120 {
121 delete [] adjMatrix[i];
122 }
123 delete [] adjMatrix;
124 }
akmhoque298385a2014-02-13 14:13:09 -0600125
126
akmhoque1fd8c1e2014-02-19 19:41:49 -0600127 void
128 RoutingTableCalculator::allocateLinks()
129 {
130 links=new int[vNoLink];
131 }
akmhoque298385a2014-02-13 14:13:09 -0600132
akmhoque1fd8c1e2014-02-19 19:41:49 -0600133 void RoutingTableCalculator::allocateLinkCosts()
134 {
135 linkCosts=new double[vNoLink];
136 }
akmhoque298385a2014-02-13 14:13:09 -0600137
akmhoqueb1710aa2014-02-19 17:13:36 -0600138
akmhoque1fd8c1e2014-02-19 19:41:49 -0600139 void
140 RoutingTableCalculator::freeLinks()
141 {
142 delete [] links;
143 }
144 void
145 RoutingTableCalculator::freeLinksCosts()
146 {
147 delete [] linkCosts;
148 }
akmhoque298385a2014-02-13 14:13:09 -0600149
akmhoque1fd8c1e2014-02-19 19:41:49 -0600150 void
151 LinkStateRoutingTableCalculator::calculatePath(Map& pMap,
152 RoutingTable& rt, Nlsr& pnlsr)
153 {
154 cout<<"LinkStateRoutingTableCalculator::calculatePath Called"<<endl;
155 allocateAdjMatrix();
156 initMatrix();
157 makeAdjMatrix(pnlsr,pMap);
158 cout<<pMap;
159 printAdjMatrix();
160 string routerName=pnlsr.getConfParameter().getRouterPrefix();
161 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
162 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
163 allocateParent();
164 allocateDistance();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600165 if ( pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1 )
166 {
167 // Single Path
168 doDijkstraPathCalculation(sourceRouter);
169 // print all ls path -- debugging purpose
170 printAllLsPath(sourceRouter);
171 // update routing table
172 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
173 }
174 else
175 {
176 // Multi Path
177 setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter));
178 allocateLinks();
179 allocateLinkCosts();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600180 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
181 for (int i=0 ; i < vNoLink; i++)
182 {
akmhoque1fd8c1e2014-02-19 19:41:49 -0600183 adjustAdMatrix(sourceRouter,links[i], linkCosts[i]);
184 printAdjMatrix();
185 doDijkstraPathCalculation(sourceRouter);
186 // print all ls path -- debugging purpose
187 printAllLsPath(sourceRouter);
188 //update routing table
189 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
190 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600191 freeLinks();
192 freeLinksCosts();
193 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600194 freeParent();
195 freeDistance();
196 freeAdjMatrix();
197 }
akmhoque298385a2014-02-13 14:13:09 -0600198
akmhoque1fd8c1e2014-02-19 19:41:49 -0600199 void
200 LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter)
201 {
202 int i;
203 int v,u;
204 int *Q=new int[numOfRouter];
205 int head=0;
206 /* Initiate the Parent */
207 for (i = 0 ; i < numOfRouter; i++)
208 {
209 parent[i]=EMPTY_PARENT;
210 distance[i]=INF_DISTANCE;
211 Q[i]=i;
212 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600213 if ( sourceRouter != NO_MAPPING_NUM )
214 {
215 distance[sourceRouter]=0;
216 sortQueueByDistance(Q,distance,head,numOfRouter);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600217 while (head < numOfRouter )
218 {
219 u=Q[head];
220 if(distance[u] == INF_DISTANCE)
221 {
222 break;
223 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600224 for(v=0 ; v <numOfRouter; v++)
225 {
226 if( adjMatrix[u][v] > 0 )
227 {
228 if ( isNotExplored(Q,v,head+1,numOfRouter) )
229 {
230 if( distance[u] + adjMatrix[u][v] < distance[v])
231 {
232 distance[v]=distance[u] + adjMatrix[u][v] ;
233 parent[v]=u;
234 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600235 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600236 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600237 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600238 head++;
239 sortQueueByDistance(Q,distance,head,numOfRouter);
240 }
241 }
242 delete [] Q;
243 }
akmhoque298385a2014-02-13 14:13:09 -0600244
akmhoque1fd8c1e2014-02-19 19:41:49 -0600245 void
246 LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr,
247 RoutingTable& rt, Map& pMap, int sourceRouter)
248 {
249 cout<<"LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called";
250 cout<<endl;
251 for(int i=0; i < numOfRouter ; i++)
252 {
253 if ( i!= sourceRouter )
254 {
255 int nextHopRouter=getLsNextHop(i,sourceRouter);
256 double routeCost=distance[i];
257 string nextHopRouterName=pMap.getRouterNameByMappingNo(nextHopRouter);
258 int nxtHopFace=
259 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
260 cout<<"Dest Router: "<<pMap.getRouterNameByMappingNo(i)<<endl;
261 cout<<"Next hop Router: "<<nextHopRouterName<<endl;
262 cout<<"Next hop Face: "<<nxtHopFace<<endl;
263 cout<<"Route Cost: "<<routeCost<<endl;
264 cout<<endl;
265 // Add next hop to routing table
266 NextHop nh(nxtHopFace,routeCost);
267 rt.addNextHop(pMap.getRouterNameByMappingNo(i),nh);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600268 }
269 }
270 }
akmhoque298385a2014-02-13 14:13:09 -0600271
akmhoque1fd8c1e2014-02-19 19:41:49 -0600272 int
273 LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
274 {
275 int nextHop;
276 while ( parent[dest] != EMPTY_PARENT )
277 {
278 nextHop=dest;
279 dest=parent[dest];
akmhoque1fd8c1e2014-02-19 19:41:49 -0600280 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600281 if ( dest != source )
282 {
283 nextHop=NO_NEXT_HOP;
284 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600285 return nextHop;
286 }
akmhoque298385a2014-02-13 14:13:09 -0600287
akmhoque1fd8c1e2014-02-19 19:41:49 -0600288 void
289 LinkStateRoutingTableCalculator::printAllLsPath(int sourceRouter)
290 {
291 cout<<"LinkStateRoutingTableCalculator::printAllLsPath Called"<<endl;
292 cout<<"Source Router: "<<sourceRouter<<endl;
293 for(int i=0; i < numOfRouter ; i++)
294 {
295 if ( i!= sourceRouter )
296 {
297 printLsPath(i);
298 cout<<endl;
299 }
300 }
301 }
akmhoque298385a2014-02-13 14:13:09 -0600302
akmhoque1fd8c1e2014-02-19 19:41:49 -0600303 void
304 LinkStateRoutingTableCalculator::printLsPath(int destRouter)
305 {
306 if (parent[destRouter] != EMPTY_PARENT )
307 {
308 printLsPath(parent[destRouter]);
309 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600310 cout<<" "<<destRouter;
311 }
akmhoque298385a2014-02-13 14:13:09 -0600312
akmhoque1fd8c1e2014-02-19 19:41:49 -0600313 void
314 LinkStateRoutingTableCalculator::sortQueueByDistance(int *Q,
315 double *dist,int start,int element)
316 {
317 for ( int i=start ; i < element ; i ++)
318 {
319 for( int j=i+1; j<element; j ++)
320 {
321 if (dist[Q[j]] < dist[Q[i]])
322 {
323 int tempU=Q[j];
324 Q[j]=Q[i];
325 Q[i]=tempU;
326 }
327 }
328 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600329 }
akmhoque298385a2014-02-13 14:13:09 -0600330
akmhoque1fd8c1e2014-02-19 19:41:49 -0600331 int
332 LinkStateRoutingTableCalculator::isNotExplored(int *Q,
333 int u,int start, int element)
334 {
335 int ret=0;
336 for(int i=start; i< element; i++)
337 {
338 if ( Q[i] == u )
339 {
340 ret=1;
341 break;
342 }
343 }
344 return ret;
345 }
akmhoque298385a2014-02-13 14:13:09 -0600346
akmhoque1fd8c1e2014-02-19 19:41:49 -0600347 void
348 LinkStateRoutingTableCalculator::allocateParent()
349 {
350 parent=new int[numOfRouter];
351 }
352
353 void
354 LinkStateRoutingTableCalculator::allocateDistance()
355 {
356 distance= new double[numOfRouter];
357 }
358
359 void
360 LinkStateRoutingTableCalculator::freeParent()
361 {
362 delete [] parent;
363 }
364
365 void LinkStateRoutingTableCalculator::freeDistance()
366 {
367 delete [] distance;
368 }
akmhoque298385a2014-02-13 14:13:09 -0600369
370
371
akmhoque1fd8c1e2014-02-19 19:41:49 -0600372 void
373 HypRoutingTableCalculator::calculatePath(Map& pMap,
374 RoutingTable& rt, Nlsr& pnlsr)
375 {
376 makeAdjMatrix(pnlsr,pMap);
377 string routerName=pnlsr.getConfParameter().getRouterPrefix();
378 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
379 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
380 setNoLink(noLink);
381 allocateLinks();
382 allocateLinkCosts();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600383 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600384 for(int i=0 ; i < numOfRouter ; ++i)
385 {
386 int k=0;
387 if ( i != sourceRouter)
388 {
389 allocateLinkFaces();
390 allocateDistanceToNeighbor();
391 allocateDistFromNbrToDest();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600392 for(int j=0; j<vNoLink; j++)
393 {
394 string nextHopRouterName=pMap.getRouterNameByMappingNo(links[j]);
395 int nextHopFace=
396 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
397 double distToNbr=getHyperbolicDistance(pnlsr,pMap,
398 sourceRouter,links[j]);
399 double distToDestFromNbr=getHyperbolicDistance(pnlsr,
400 pMap,links[j],i);
401 if ( distToDestFromNbr >= 0 )
402 {
403 linkFaces[k] = nextHopFace;
404 distanceToNeighbor[k] = distToNbr;
405 distFromNbrToDest[k] = distToDestFromNbr;
406 k++;
407 }
408 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600409 addHypNextHopsToRoutingTable(pnlsr,pMap,rt,k,i);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600410 freeLinkFaces();
411 freeDistanceToNeighbor();
412 freeDistFromNbrToDest();
413 }
414 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600415 freeLinks();
416 freeLinksCosts();
417 freeAdjMatrix();
418 }
akmhoque298385a2014-02-13 14:13:09 -0600419
akmhoque1fd8c1e2014-02-19 19:41:49 -0600420 void
421 HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr,Map& pMap,
422 RoutingTable& rt, int noFaces, int dest)
423 {
424 for(int i=0 ; i < noFaces ; ++i)
425 {
426 string destRouter=pMap.getRouterNameByMappingNo(dest);
427 NextHop nh(linkFaces[i],distFromNbrToDest[i]);
428 rt.addNextHop(destRouter,nh);
429 if( isDryRun == 1 )
430 {
431 rt.addNextHopToDryTable(destRouter,nh);
432 }
433 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600434 }
akmhoque298385a2014-02-13 14:13:09 -0600435
akmhoque1fd8c1e2014-02-19 19:41:49 -0600436 double
437 HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr,
438 Map& pMap, int src, int dest)
439 {
440 double distance=0.0;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600441 string srcRouterKey=pMap.getRouterNameByMappingNo(src)+"/3";
442 string destRouterKey=pMap.getRouterNameByMappingNo(dest)+"/3";
akmhoque1fd8c1e2014-02-19 19:41:49 -0600443 double srcRadius=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorRadius();
444 double srcTheta=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorTheta();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600445 double destRadius=(pnlsr.getLsdb().getCorLsa(
446 destRouterKey).first).getCorRadius();
447 double destTheta=(pnlsr.getLsdb().getCorLsa(destRouterKey).first).getCorTheta();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600448 double diffTheta = fabs (srcTheta - destTheta);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600449 if (diffTheta > MATH_PI)
450 {
451 diffTheta = 2 * MATH_PI - diffTheta;
452 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600453 if ( srcRadius != -1 && destRadius != -1 )
454 {
455 if (diffTheta == 0)
456 distance = fabs (srcRadius - destRadius);
457 else
458 distance = acosh((cosh(srcRadius)*cosh(destRadius))-
459 (sinh(srcRadius)*sinh(destRadius)*cos(diffTheta)));
460 }
461 else
462 {
463 distance = -1;
464 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600465 return distance;
466 }
akmhoque298385a2014-02-13 14:13:09 -0600467
akmhoque1fd8c1e2014-02-19 19:41:49 -0600468 void
469 HypRoutingTableCalculator::allocateLinkFaces()
470 {
471 linkFaces=new int[vNoLink];
472 }
akmhoque298385a2014-02-13 14:13:09 -0600473
akmhoque1fd8c1e2014-02-19 19:41:49 -0600474 void
475 HypRoutingTableCalculator::allocateDistanceToNeighbor()
476 {
477 distanceToNeighbor=new double[vNoLink];
478 }
akmhoque298385a2014-02-13 14:13:09 -0600479
akmhoque1fd8c1e2014-02-19 19:41:49 -0600480 void
481 HypRoutingTableCalculator::allocateDistFromNbrToDest()
482 {
483 distFromNbrToDest=new double[vNoLink];
484 }
akmhoque298385a2014-02-13 14:13:09 -0600485
akmhoque1fd8c1e2014-02-19 19:41:49 -0600486 void
487 HypRoutingTableCalculator::freeLinkFaces()
488 {
489 delete [] linkFaces;
490 }
akmhoque298385a2014-02-13 14:13:09 -0600491
akmhoque1fd8c1e2014-02-19 19:41:49 -0600492 void
493 HypRoutingTableCalculator::freeDistanceToNeighbor()
494 {
495 delete [] distanceToNeighbor;
496 }
akmhoque298385a2014-02-13 14:13:09 -0600497
akmhoque1fd8c1e2014-02-19 19:41:49 -0600498 void
499 HypRoutingTableCalculator::freeDistFromNbrToDest()
500 {
501 delete [] distFromNbrToDest;
502 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600503
504}//namespace nlsr