blob: 539c6e9420543beb790a2139cb53a86cb96fe2c1 [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
10using namespace std;
11
12void
13RoutingTableCalculator::allocateAdjMatrix()
14{
15 adjMatrix = new double*[numOfRouter];
16 for(int i = 0; i < numOfRouter; ++i)
17 {
18 adjMatrix[i] = new double[numOfRouter];
19 }
20}
21
22void
23RoutingTableCalculator::initMatrix()
24{
25 for(int i=0;i<numOfRouter;i++)
26 {
27 for(int j=0;j<numOfRouter;j++)
28 adjMatrix[i][j]=0;
29 }
30}
31
32void
33RoutingTableCalculator::makeAdjMatrix(nlsr& pnlsr, Map pMap)
34{
35 std::list<AdjLsa> adjLsdb=pnlsr.getLsdb().getAdjLsdb();
36 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
37 it!= adjLsdb.end() ; it++)
38 {
39 string linkStartRouter=(*it).getOrigRouter();
40 int row=pMap.getMappingNoByRouterName(linkStartRouter);
41 std::list<Adjacent> adl=(*it).getAdl().getAdjList();
42 for( std::list<Adjacent>::iterator itAdl=adl.begin();
43 itAdl!= adl.end() ; itAdl++)
44 {
45 string linkEndRouter=(*itAdl).getAdjacentName();
46 int col=pMap.getMappingNoByRouterName(linkEndRouter);
47 double cost=(*itAdl).getLinkCost();
48 if ( (row >= 0 && row<numOfRouter) && (col >= 0 && col<numOfRouter) )
49 {
50 adjMatrix[row][col]=cost;
51 }
52 }
53
54 }
55}
56
57void
58RoutingTableCalculator::printAdjMatrix()
59{
60 for(int i=0;i<numOfRouter;i++)
61 {
62 for(int j=0;j<numOfRouter;j++)
63 printf("%f ",adjMatrix[i][j]);
64 printf("\n");
65 }
66}
67
68void
69RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost)
70{
71 for ( int i = 0; i < numOfRouter; i++ ){
72 if ( i == link ){
73 adjMatrix[source][i]=linkCost;
74 }
75 else{
76 adjMatrix[source][i]=0;
77 }
78 }
79}
80
81int
82RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter)
83{
84 int noLink=0;
85 for(int i=0;i<numOfRouter;i++)
86 {
87 if ( adjMatrix[sRouter][i] > 0 )
88 {
89 noLink++;
90 }
91 }
92 return noLink;
93}
94
95void
96RoutingTableCalculator::getLinksFromAdjMatrix(int *links,
97 double *linkCosts, int source)
98{
99 int j=0;
100 for (int i=0; i <numOfRouter; i++)
101 {
102 if ( adjMatrix[source][i] > 0 )
103 {
104 links[j]=i;
105 linkCosts[j]=adjMatrix[source][i];
106 j++;
107 }
108 }
109}
110
111void
112RoutingTableCalculator::freeAdjMatrix()
113{
114 for(int i = 0; i < numOfRouter; ++i)
115 {
116 delete [] adjMatrix[i];
117 }
118 delete [] adjMatrix;
119}
120
121
122void
123RoutingTableCalculator::allocateLinks()
124{
125 links=new int[vNoLink];
126}
127
128void RoutingTableCalculator::allocateLinkCosts()
129{
130 linkCosts=new double[vNoLink];
131}
132
133void
134RoutingTableCalculator::freeLinks()
135{
136 delete [] links;
137}
138void
139RoutingTableCalculator::freeLinksCosts()
140{
141 delete [] linkCosts;
142}
143
144void
145LinkStateRoutingTableCalculator::calculatePath(Map& pMap,
146 RoutingTable& rt, nlsr& pnlsr)
147{
148 cout<<"LinkStateRoutingTableCalculator::calculatePath Called"<<endl;
149 allocateAdjMatrix();
150 initMatrix();
151 makeAdjMatrix(pnlsr,pMap);
152 cout<<pMap;
153 printAdjMatrix();
154 string routerName=pnlsr.getConfParameter().getRouterPrefix();
155 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
156 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
157 allocateParent();
158 allocateDistance();
159
160 if ( pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1 )
161 {
162 // Single Path
163 doDijkstraPathCalculation(sourceRouter);
164 // print all ls path -- debugging purpose
165 printAllLsPath(sourceRouter);
166 // update routing table
167 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
168 }
169 else
170 {
171 // Multi Path
172 setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter));
173 allocateLinks();
174 allocateLinkCosts();
175
176 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
177 for (int i=0 ; i < vNoLink; i++)
178 {
179
180 adjustAdMatrix(sourceRouter,links[i], linkCosts[i]);
181 printAdjMatrix();
182 doDijkstraPathCalculation(sourceRouter);
183 // print all ls path -- debugging purpose
184 printAllLsPath(sourceRouter);
185 //update routing table
186 addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
187 }
188
189 freeLinks();
190 freeLinksCosts();
191 }
192
193 freeParent();
194 freeDistance();
195 freeAdjMatrix();
196}
197
198void
199LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter)
200{
201 int i;
202 int v,u;
203 int *Q=new int[numOfRouter];
204 int head=0;
205 /* Initiate the Parent */
206 for (i = 0 ; i < numOfRouter; i++){
207 parent[i]=EMPTY_PARENT;
208 distance[i]=INF_DISTANCE;
209 Q[i]=i;
210 }
211
212 if ( sourceRouter != NO_MAPPING_NUM ){
213 distance[sourceRouter]=0;
214 sortQueueByDistance(Q,distance,head,numOfRouter);
215
216 while (head < numOfRouter )
217 {
218 u=Q[head];
219 if(distance[u] == INF_DISTANCE)
220 {
221 break;
222 }
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 }
239
240 }
241
242 head++;
243 sortQueueByDistance(Q,distance,head,numOfRouter);
244 }
245 }
246 delete [] Q;
247}
248
249void
250LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(nlsr& pnlsr,
251 RoutingTable& rt, Map& pMap, int sourceRouter)
252{
253 cout<<"LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called";
254 cout<<endl;
255 for(int i=0; i < numOfRouter ; i++)
256 {
257 if ( i!= sourceRouter )
258 {
259 int nextHopRouter=getLsNextHop(i,sourceRouter);
260 double routeCost=distance[i];
261 string nextHopRouterName=pMap.getRouterNameByMappingNo(nextHopRouter);
262 int nxtHopFace=
263 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
264 cout<<"Dest Router: "<<pMap.getRouterNameByMappingNo(i)<<endl;
265 cout<<"Next hop Router: "<<nextHopRouterName<<endl;
266 cout<<"Next hop Face: "<<nxtHopFace<<endl;
267 cout<<"Route Cost: "<<routeCost<<endl;
268 cout<<endl;
269 // Add next hop to routing table
270 NextHop nh(nxtHopFace,routeCost);
271 rt.addNextHop(pMap.getRouterNameByMappingNo(i),nh);
272
273 }
274 }
275}
276
277int
278LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
279{
280 int nextHop;
281 while ( parent[dest] != EMPTY_PARENT )
282 {
283 nextHop=dest;
284 dest=parent[dest];
285
286 }
287
288 if ( dest != source )
289 {
290 nextHop=NO_NEXT_HOP;
291 }
292
293 return nextHop;
294}
295
296void
297LinkStateRoutingTableCalculator::printAllLsPath(int sourceRouter)
298{
299 cout<<"LinkStateRoutingTableCalculator::printAllLsPath Called"<<endl;
300 cout<<"Source Router: "<<sourceRouter<<endl;
301 for(int i=0; i < numOfRouter ; i++)
302 {
303 if ( i!= sourceRouter )
304 {
305 printLsPath(i);
306 cout<<endl;
307 }
308 }
309}
310
311void
312LinkStateRoutingTableCalculator::printLsPath(int destRouter)
313{
314 if (parent[destRouter] != EMPTY_PARENT )
315 {
316 printLsPath(parent[destRouter]);
317 }
318
319 cout<<" "<<destRouter;
320}
321
322void
323LinkStateRoutingTableCalculator::sortQueueByDistance(int *Q,
324 double *dist,int start,int element)
325{
326 for ( int i=start ; i < element ; i ++)
327 {
328 for( int j=i+1; j<element; j ++)
329 {
330 if (dist[Q[j]] < dist[Q[i]])
331 {
332 int tempU=Q[j];
333 Q[j]=Q[i];
334 Q[i]=tempU;
335 }
336 }
337 }
338
339}
340
341int
342LinkStateRoutingTableCalculator::isNotExplored(int *Q,
343 int u,int start, int element)
344{
345 int ret=0;
346 for(int i=start; i< element; i++)
347 {
348 if ( Q[i] == u )
349 {
350 ret=1;
351 break;
352 }
353 }
354 return ret;
355}
356
357void
358LinkStateRoutingTableCalculator::allocateParent()
359{
360 parent=new int[numOfRouter];
361}
362
363void
364LinkStateRoutingTableCalculator::allocateDistance()
365{
366 distance= new double[numOfRouter];
367}
368
369void
370LinkStateRoutingTableCalculator::freeParent()
371{
372 delete [] parent;
373}
374
375void LinkStateRoutingTableCalculator::freeDistance()
376{
377 delete [] distance;
378}
379
380
381
382void
383HypRoutingTableCalculator::calculatePath(Map& pMap,
384 RoutingTable& rt, nlsr& pnlsr)
385{
386 makeAdjMatrix(pnlsr,pMap);
387 string routerName=pnlsr.getConfParameter().getRouterPrefix();
388 int sourceRouter=pMap.getMappingNoByRouterName(routerName);
389 int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
390 setNoLink(noLink);
391 allocateLinks();
392 allocateLinkCosts();
393
394 getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
395
396 for(int i=0 ; i < numOfRouter ; ++i)
397 {
398 int k=0;
399 if ( i != sourceRouter)
400 {
401 allocateLinkFaces();
402 allocateDistanceToNeighbor();
403 allocateDistFromNbrToDest();
404
405 for(int j=0; j<vNoLink; j++)
406 {
407 string nextHopRouterName=pMap.getRouterNameByMappingNo(links[j]);
408 int nextHopFace=
409 pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
410 double distToNbr=getHyperbolicDistance(pnlsr,pMap,
411 sourceRouter,links[j]);
412 double distToDestFromNbr=getHyperbolicDistance(pnlsr,
413 pMap,links[j],i);
414 if ( distToDestFromNbr >= 0 )
415 {
416 linkFaces[k] = nextHopFace;
417 distanceToNeighbor[k] = distToNbr;
418 distFromNbrToDest[k] = distToDestFromNbr;
419 k++;
420 }
421 }
422
423 addHypNextHopsToRoutingTable(pnlsr,pMap,rt,k,i);
424
425 freeLinkFaces();
426 freeDistanceToNeighbor();
427 freeDistFromNbrToDest();
428 }
429 }
430
431 freeLinks();
432 freeLinksCosts();
433 freeAdjMatrix();
434}
435
436void
437HypRoutingTableCalculator::addHypNextHopsToRoutingTable(nlsr& pnlsr,Map& pMap,
438 RoutingTable& rt, int noFaces, int dest)
439{
440 for(int i=0 ; i < noFaces ; ++i)
441 {
442 string destRouter=pMap.getRouterNameByMappingNo(dest);
443 NextHop nh(linkFaces[i],distFromNbrToDest[i]);
444 rt.addNextHop(destRouter,nh);
445 if( isDryRun == 1 )
446 {
447 rt.addNextHopToDryTable(destRouter,nh);
448 }
449 }
450
451}
452
453double
454HypRoutingTableCalculator::getHyperbolicDistance(nlsr& pnlsr,
455 Map& pMap, int src, int dest)
456{
457 double distance=0.0;
458
459 string srcRouterKey=pMap.getRouterNameByMappingNo(src)+"/3";
460 string destRouterKey=pMap.getRouterNameByMappingNo(dest)+"/3";
461
462 double srcRadius=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorRadius();
463 double srcTheta=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorTheta();
464
465 double destRadius=(pnlsr.getLsdb().getCorLsa(destRouterKey).first).getCorRadius();
466 double destTheta=(pnlsr.getLsdb().getCorLsa(destRouterKey).first).getCorTheta();
467
468
469 double diffTheta = fabs (srcTheta - destTheta);
470
471 if (diffTheta > MATH_PI)
472 {
473 diffTheta = 2 * MATH_PI - diffTheta;
474 }
475
476 if ( srcRadius != -1 && destRadius != -1 )
477 {
478 if (diffTheta == 0)
479 distance = fabs (srcRadius - destRadius);
480 else
481 distance = acosh((cosh(srcRadius)*cosh(destRadius))-
482 (sinh(srcRadius)*sinh(destRadius)*cos(diffTheta)));
483 }else
484 {
485 distance = -1;
486 }
487
488 return distance;
489}
490
491void
492HypRoutingTableCalculator::allocateLinkFaces()
493{
494 linkFaces=new int[vNoLink];
495}
496
497void
498HypRoutingTableCalculator::allocateDistanceToNeighbor()
499{
500 distanceToNeighbor=new double[vNoLink];
501}
502
503void
504HypRoutingTableCalculator::allocateDistFromNbrToDest()
505{
506 distFromNbrToDest=new double[vNoLink];
507}
508
509void
510HypRoutingTableCalculator::freeLinkFaces()
511{
512 delete [] linkFaces;
513}
514
515void
516HypRoutingTableCalculator::freeDistanceToNeighbor()
517{
518 delete [] distanceToNeighbor;
519}
520
521void
522HypRoutingTableCalculator::freeDistFromNbrToDest()
523{
524 delete [] distFromNbrToDest;
525}