blob: 90200e5d14a20a76e82278b2198dc4489abb39b2 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -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
10namespace nlsr
11{
12
akmhoque5a44dd42014-03-12 18:11:32 -050013 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060014
akmhoque5a44dd42014-03-12 18:11:32 -050015 void
16 RoutingTableCalculator::allocateAdjMatrix()
17 {
18 adjMatrix = new double*[numOfRouter];
19 for(int i = 0; i < numOfRouter; ++i)
akmhoqueba094742014-02-28 11:47:21 -060020 {
akmhoque5a44dd42014-03-12 18:11:32 -050021 adjMatrix[i] = new double[numOfRouter];
22 }
23 }
24
25 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 }
34
35 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) )
akmhoqueba094742014-02-28 11:47:21 -060052 {
akmhoque5a44dd42014-03-12 18:11:32 -050053 adjMatrix[row][col]=cost;
akmhoqueba094742014-02-28 11:47:21 -060054 }
akmhoque5a44dd42014-03-12 18:11:32 -050055 }
akmhoqueba094742014-02-28 11:47:21 -060056 }
akmhoque5a44dd42014-03-12 18:11:32 -050057 }
akmhoqueba094742014-02-28 11:47:21 -060058
akmhoque5a44dd42014-03-12 18:11:32 -050059 void
60 RoutingTableCalculator::printAdjMatrix()
61 {
62 for(int i=0; i<numOfRouter; i++)
akmhoqueba094742014-02-28 11:47:21 -060063 {
akmhoque5a44dd42014-03-12 18:11:32 -050064 for(int j=0; j<numOfRouter; j++)
65 printf("%f ",adjMatrix[i][j]);
66 printf("\n");
akmhoqueba094742014-02-28 11:47:21 -060067 }
akmhoque5a44dd42014-03-12 18:11:32 -050068 }
akmhoqueba094742014-02-28 11:47:21 -060069
akmhoque5a44dd42014-03-12 18:11:32 -050070 void
71 RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost)
72 {
73 for ( int i = 0; i < numOfRouter; i++ )
akmhoqueba094742014-02-28 11:47:21 -060074 {
akmhoque5a44dd42014-03-12 18:11:32 -050075 if ( i == link )
76 {
77 adjMatrix[source][i]=linkCost;
78 }
79 else
80 {
81 adjMatrix[source][i]=0;
82 }
akmhoqueba094742014-02-28 11:47:21 -060083 }
akmhoque5a44dd42014-03-12 18:11:32 -050084 }
akmhoqueba094742014-02-28 11:47:21 -060085
akmhoque5a44dd42014-03-12 18:11:32 -050086 int
87 RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter)
88 {
89 int noLink=0;
90 for(int i=0; i<numOfRouter; i++)
akmhoqueba094742014-02-28 11:47:21 -060091 {
akmhoque5a44dd42014-03-12 18:11:32 -050092 if ( adjMatrix[sRouter][i] > 0 )
93 {
94 noLink++;
95 }
akmhoqueba094742014-02-28 11:47:21 -060096 }
akmhoque5a44dd42014-03-12 18:11:32 -050097 return noLink;
98 }
akmhoqueba094742014-02-28 11:47:21 -060099
akmhoque5a44dd42014-03-12 18:11:32 -0500100 void
101 RoutingTableCalculator::getLinksFromAdjMatrix(int *links,
102 double *linkCosts, int source)
103 {
104 int j=0;
105 for (int i=0; i <numOfRouter; i++)
akmhoqueba094742014-02-28 11:47:21 -0600106 {
akmhoque5a44dd42014-03-12 18:11:32 -0500107 if ( adjMatrix[source][i] > 0 )
108 {
109 links[j]=i;
110 linkCosts[j]=adjMatrix[source][i];
111 j++;
112 }
akmhoqueba094742014-02-28 11:47:21 -0600113 }
akmhoque5a44dd42014-03-12 18:11:32 -0500114 }
akmhoqueba094742014-02-28 11:47:21 -0600115
akmhoque5a44dd42014-03-12 18:11:32 -0500116 void
117 RoutingTableCalculator::freeAdjMatrix()
118 {
119 for(int i = 0; i < numOfRouter; ++i)
akmhoqueba094742014-02-28 11:47:21 -0600120 {
akmhoque5a44dd42014-03-12 18:11:32 -0500121 delete [] adjMatrix[i];
akmhoqueba094742014-02-28 11:47:21 -0600122 }
akmhoque5a44dd42014-03-12 18:11:32 -0500123 delete [] adjMatrix;
124 }
akmhoqueba094742014-02-28 11:47:21 -0600125
akmhoque5a44dd42014-03-12 18:11:32 -0500126
127 void
128 RoutingTableCalculator::allocateLinks()
129 {
130 links=new int[vNoLink];
131 }
132
133 void RoutingTableCalculator::allocateLinkCosts()
134 {
135 linkCosts=new double[vNoLink];
136 }
137
138
139 void
140 RoutingTableCalculator::freeLinks()
141 {
142 delete [] links;
143 }
144 void
145 RoutingTableCalculator::freeLinksCosts()
146 {
147 delete [] linkCosts;
148 }
149
150 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();
165 if ( pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1 )
akmhoqueba094742014-02-28 11:47:21 -0600166 {
akmhoque5a44dd42014-03-12 18:11:32 -0500167 // 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);
akmhoqueba094742014-02-28 11:47:21 -0600173 }
akmhoque5a44dd42014-03-12 18:11:32 -0500174 else
akmhoqueba094742014-02-28 11:47:21 -0600175 {
akmhoque5a44dd42014-03-12 18:11:32 -0500176 // Multi Path
177 setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter));
178 allocateLinks();
179 allocateLinkCosts();
180 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
181 for (int i=0 ; i < vNoLink; i++)
182 {
183 adjustAdMatrix(sourceRouter,links[i], linkCosts[i]);
akmhoqueba094742014-02-28 11:47:21 -0600184 printAdjMatrix();
akmhoque5a44dd42014-03-12 18:11:32 -0500185 doDijkstraPathCalculation(sourceRouter);
186 // print all ls path -- debugging purpose
187 printAllLsPath(sourceRouter);
188 //update routing table
189 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
190 }
191 freeLinks();
192 freeLinksCosts();
akmhoqueba094742014-02-28 11:47:21 -0600193 }
akmhoque5a44dd42014-03-12 18:11:32 -0500194 freeParent();
195 freeDistance();
196 freeAdjMatrix();
197 }
akmhoqueba094742014-02-28 11:47:21 -0600198
akmhoque5a44dd42014-03-12 18:11:32 -0500199 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++)
akmhoqueba094742014-02-28 11:47:21 -0600208 {
akmhoque5a44dd42014-03-12 18:11:32 -0500209 parent[i]=EMPTY_PARENT;
210 distance[i]=INF_DISTANCE;
211 Q[i]=i;
akmhoqueba094742014-02-28 11:47:21 -0600212 }
akmhoque5a44dd42014-03-12 18:11:32 -0500213 if ( sourceRouter != NO_MAPPING_NUM )
akmhoqueba094742014-02-28 11:47:21 -0600214 {
akmhoque5a44dd42014-03-12 18:11:32 -0500215 distance[sourceRouter]=0;
216 sortQueueByDistance(Q,distance,head,numOfRouter);
217 while (head < numOfRouter )
218 {
219 u=Q[head];
220 if(distance[u] == INF_DISTANCE)
221 {
222 break;
223 }
224 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 }
235 }
236 }
237 }
238 head++;
239 sortQueueByDistance(Q,distance,head,numOfRouter);
240 }
241 }
242 delete [] Q;
243 }
244
245 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 if ( nextHopRouter != NO_NEXT_HOP )
257 {
258 double routeCost=distance[i];
259 string nextHopRouterName=
260 pMap.getRouterNameByMappingNo(nextHopRouter);
261 int nxtHopFace=
262 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
263 cout<<"Dest Router: "<<pMap.getRouterNameByMappingNo(i)<<endl;
264 cout<<"Next hop Router: "<<nextHopRouterName<<endl;
265 cout<<"Next hop Face: "<<nxtHopFace<<endl;
266 cout<<"Route Cost: "<<routeCost<<endl;
267 cout<<endl;
268 // Add next hop to routing table
269 NextHop nh(nxtHopFace,routeCost);
270 rt.addNextHop(pMap.getRouterNameByMappingNo(i),nh);
271 }
272 }
273 }
274 }
275
276 int
277 LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
278 {
279 int nextHop;
280 while ( parent[dest] != EMPTY_PARENT )
281 {
282 nextHop=dest;
283 dest=parent[dest];
284 }
285 if ( dest != source )
286 {
287 nextHop=NO_NEXT_HOP;
288 }
289 return nextHop;
290 }
291
292 void
293 LinkStateRoutingTableCalculator::printAllLsPath(int sourceRouter)
294 {
295 cout<<"LinkStateRoutingTableCalculator::printAllLsPath Called"<<endl;
296 cout<<"Source Router: "<<sourceRouter<<endl;
297 for(int i=0; i < numOfRouter ; i++)
298 {
299 if ( i!= sourceRouter )
300 {
301 printLsPath(i);
akmhoqueba094742014-02-28 11:47:21 -0600302 cout<<endl;
akmhoque5a44dd42014-03-12 18:11:32 -0500303 }
304 }
305 }
306
307 void
308 LinkStateRoutingTableCalculator::printLsPath(int destRouter)
309 {
310 if (parent[destRouter] != EMPTY_PARENT )
311 {
312 printLsPath(parent[destRouter]);
313 }
314 cout<<" "<<destRouter;
315 }
316
317 void
318 LinkStateRoutingTableCalculator::sortQueueByDistance(int *Q,
319 double *dist,int start,int element)
320 {
321 for ( int i=start ; i < element ; i ++)
322 {
323 for( int j=i+1; j<element; j ++)
324 {
325 if (dist[Q[j]] < dist[Q[i]])
akmhoqueba094742014-02-28 11:47:21 -0600326 {
akmhoque5a44dd42014-03-12 18:11:32 -0500327 int tempU=Q[j];
328 Q[j]=Q[i];
329 Q[i]=tempU;
akmhoqueba094742014-02-28 11:47:21 -0600330 }
akmhoque5a44dd42014-03-12 18:11:32 -0500331 }
akmhoqueba094742014-02-28 11:47:21 -0600332 }
akmhoque5a44dd42014-03-12 18:11:32 -0500333 }
akmhoqueba094742014-02-28 11:47:21 -0600334
akmhoque5a44dd42014-03-12 18:11:32 -0500335 int
336 LinkStateRoutingTableCalculator::isNotExplored(int *Q,
337 int u,int start, int element)
338 {
339 int ret=0;
340 for(int i=start; i< element; i++)
akmhoqueba094742014-02-28 11:47:21 -0600341 {
akmhoque5a44dd42014-03-12 18:11:32 -0500342 if ( Q[i] == u )
343 {
344 ret=1;
345 break;
346 }
347 }
348 return ret;
349 }
350
351 void
352 LinkStateRoutingTableCalculator::allocateParent()
353 {
354 parent=new int[numOfRouter];
355 }
356
357 void
358 LinkStateRoutingTableCalculator::allocateDistance()
359 {
360 distance= new double[numOfRouter];
361 }
362
363 void
364 LinkStateRoutingTableCalculator::freeParent()
365 {
366 delete [] parent;
367 }
368
369 void LinkStateRoutingTableCalculator::freeDistance()
370 {
371 delete [] distance;
372 }
373
374
375
376 void
377 HypRoutingTableCalculator::calculatePath(Map& pMap,
378 RoutingTable& rt, Nlsr& pnlsr)
379 {
380 makeAdjMatrix(pnlsr,pMap);
381 string routerName=pnlsr.getConfParameter().getRouterPrefix();
382 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
383 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
384 setNoLink(noLink);
385 allocateLinks();
386 allocateLinkCosts();
387 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
388 for(int i=0 ; i < numOfRouter ; ++i)
389 {
390 int k=0;
391 if ( i != sourceRouter)
392 {
393 allocateLinkFaces();
394 allocateDistanceToNeighbor();
395 allocateDistFromNbrToDest();
396 for(int j=0; j<vNoLink; j++)
akmhoqueba094742014-02-28 11:47:21 -0600397 {
akmhoque5a44dd42014-03-12 18:11:32 -0500398 string nextHopRouterName=pMap.getRouterNameByMappingNo(links[j]);
399 int nextHopFace=
400 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
401 double distToNbr=getHyperbolicDistance(pnlsr,pMap,
402 sourceRouter,links[j]);
403 double distToDestFromNbr=getHyperbolicDistance(pnlsr,
404 pMap,links[j],i);
405 if ( distToDestFromNbr >= 0 )
406 {
407 linkFaces[k] = nextHopFace;
408 distanceToNeighbor[k] = distToNbr;
409 distFromNbrToDest[k] = distToDestFromNbr;
410 k++;
411 }
akmhoqueba094742014-02-28 11:47:21 -0600412 }
akmhoque5a44dd42014-03-12 18:11:32 -0500413 addHypNextHopsToRoutingTable(pnlsr,pMap,rt,k,i);
414 freeLinkFaces();
415 freeDistanceToNeighbor();
416 freeDistFromNbrToDest();
417 }
akmhoqueba094742014-02-28 11:47:21 -0600418 }
akmhoque5a44dd42014-03-12 18:11:32 -0500419 freeLinks();
420 freeLinksCosts();
421 freeAdjMatrix();
422 }
akmhoqueba094742014-02-28 11:47:21 -0600423
akmhoque5a44dd42014-03-12 18:11:32 -0500424 void
425 HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr,Map& pMap,
426 RoutingTable& rt, int noFaces, int dest)
427 {
428 for(int i=0 ; i < noFaces ; ++i)
akmhoqueba094742014-02-28 11:47:21 -0600429 {
akmhoque5a44dd42014-03-12 18:11:32 -0500430 string destRouter=pMap.getRouterNameByMappingNo(dest);
431 NextHop nh(linkFaces[i],distFromNbrToDest[i]);
432 rt.addNextHop(destRouter,nh);
433 if( isDryRun == 1 )
434 {
435 rt.addNextHopToDryTable(destRouter,nh);
436 }
akmhoqueba094742014-02-28 11:47:21 -0600437 }
akmhoque5a44dd42014-03-12 18:11:32 -0500438 }
akmhoqueba094742014-02-28 11:47:21 -0600439
akmhoque5a44dd42014-03-12 18:11:32 -0500440 double
441 HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr,
442 Map& pMap, int src, int dest)
443 {
444 double distance=0.0;
445 string srcRouterKey=pMap.getRouterNameByMappingNo(src)+"/3";
446 string destRouterKey=pMap.getRouterNameByMappingNo(dest)+"/3";
447 double srcRadius=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorRadius();
448 double srcTheta=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorTheta();
449 double destRadius=(pnlsr.getLsdb().getCorLsa(
450 destRouterKey).first).getCorRadius();
451 double destTheta=(pnlsr.getLsdb().getCorLsa(destRouterKey).first).getCorTheta();
452 double diffTheta = fabs (srcTheta - destTheta);
453 if (diffTheta > MATH_PI)
akmhoqueba094742014-02-28 11:47:21 -0600454 {
akmhoque5a44dd42014-03-12 18:11:32 -0500455 diffTheta = 2 * MATH_PI - diffTheta;
akmhoqueba094742014-02-28 11:47:21 -0600456 }
akmhoque5a44dd42014-03-12 18:11:32 -0500457 if ( srcRadius != -1 && destRadius != -1 )
akmhoqueba094742014-02-28 11:47:21 -0600458 {
akmhoque5a44dd42014-03-12 18:11:32 -0500459 if (diffTheta == 0)
460 distance = fabs (srcRadius - destRadius);
461 else
462 distance = acosh((cosh(srcRadius)*cosh(destRadius))-
463 (sinh(srcRadius)*sinh(destRadius)*cos(diffTheta)));
akmhoqueba094742014-02-28 11:47:21 -0600464 }
akmhoque5a44dd42014-03-12 18:11:32 -0500465 else
akmhoqueba094742014-02-28 11:47:21 -0600466 {
akmhoque5a44dd42014-03-12 18:11:32 -0500467 distance = -1;
akmhoqueba094742014-02-28 11:47:21 -0600468 }
akmhoque5a44dd42014-03-12 18:11:32 -0500469 return distance;
470 }
akmhoqueba094742014-02-28 11:47:21 -0600471
akmhoque5a44dd42014-03-12 18:11:32 -0500472 void
473 HypRoutingTableCalculator::allocateLinkFaces()
474 {
475 linkFaces=new int[vNoLink];
476 }
akmhoqueba094742014-02-28 11:47:21 -0600477
akmhoque5a44dd42014-03-12 18:11:32 -0500478 void
479 HypRoutingTableCalculator::allocateDistanceToNeighbor()
480 {
481 distanceToNeighbor=new double[vNoLink];
482 }
akmhoqueba094742014-02-28 11:47:21 -0600483
akmhoque5a44dd42014-03-12 18:11:32 -0500484 void
485 HypRoutingTableCalculator::allocateDistFromNbrToDest()
486 {
487 distFromNbrToDest=new double[vNoLink];
488 }
akmhoqueba094742014-02-28 11:47:21 -0600489
akmhoque5a44dd42014-03-12 18:11:32 -0500490 void
491 HypRoutingTableCalculator::freeLinkFaces()
492 {
493 delete [] linkFaces;
494 }
akmhoqueba094742014-02-28 11:47:21 -0600495
akmhoque5a44dd42014-03-12 18:11:32 -0500496 void
497 HypRoutingTableCalculator::freeDistanceToNeighbor()
498 {
499 delete [] distanceToNeighbor;
500 }
akmhoqueba094742014-02-28 11:47:21 -0600501
akmhoque5a44dd42014-03-12 18:11:32 -0500502 void
503 HypRoutingTableCalculator::freeDistFromNbrToDest()
504 {
505 delete [] distFromNbrToDest;
506 }
akmhoqueba094742014-02-28 11:47:21 -0600507
508}//namespace nlsr