akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #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 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 10 | namespace nlsr |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 11 | { |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 12 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 13 | using namespace std; |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 14 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 15 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 24 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 34 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 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) ) |
| 52 | { |
| 53 | adjMatrix[row][col]=cost; |
| 54 | } |
| 55 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 56 | } |
| 57 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 58 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 59 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 69 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 70 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 125 | |
| 126 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 127 | void |
| 128 | RoutingTableCalculator::allocateLinks() |
| 129 | { |
| 130 | links=new int[vNoLink]; |
| 131 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 132 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 133 | void RoutingTableCalculator::allocateLinkCosts() |
| 134 | { |
| 135 | linkCosts=new double[vNoLink]; |
| 136 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 137 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 138 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 139 | void |
| 140 | RoutingTableCalculator::freeLinks() |
| 141 | { |
| 142 | delete [] links; |
| 143 | } |
| 144 | void |
| 145 | RoutingTableCalculator::freeLinksCosts() |
| 146 | { |
| 147 | delete [] linkCosts; |
| 148 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 149 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 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(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 165 | 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(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 180 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
| 181 | for (int i=0 ; i < vNoLink; i++) |
| 182 | { |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 183 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 191 | freeLinks(); |
| 192 | freeLinksCosts(); |
| 193 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 194 | freeParent(); |
| 195 | freeDistance(); |
| 196 | freeAdjMatrix(); |
| 197 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 198 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 199 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 213 | if ( sourceRouter != NO_MAPPING_NUM ) |
| 214 | { |
| 215 | distance[sourceRouter]=0; |
| 216 | sortQueueByDistance(Q,distance,head,numOfRouter); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 217 | while (head < numOfRouter ) |
| 218 | { |
| 219 | u=Q[head]; |
| 220 | if(distance[u] == INF_DISTANCE) |
| 221 | { |
| 222 | break; |
| 223 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 235 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 236 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 237 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 238 | head++; |
| 239 | sortQueueByDistance(Q,distance,head,numOfRouter); |
| 240 | } |
| 241 | } |
| 242 | delete [] Q; |
| 243 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 244 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 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 | 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); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 271 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 272 | 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]; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 280 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 281 | if ( dest != source ) |
| 282 | { |
| 283 | nextHop=NO_NEXT_HOP; |
| 284 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 285 | return nextHop; |
| 286 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 287 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 288 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 302 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 303 | void |
| 304 | LinkStateRoutingTableCalculator::printLsPath(int destRouter) |
| 305 | { |
| 306 | if (parent[destRouter] != EMPTY_PARENT ) |
| 307 | { |
| 308 | printLsPath(parent[destRouter]); |
| 309 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 310 | cout<<" "<<destRouter; |
| 311 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 312 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 313 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 329 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 330 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 331 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 346 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 347 | 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 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 369 | |
| 370 | |
| 371 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 372 | 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(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 383 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 384 | for(int i=0 ; i < numOfRouter ; ++i) |
| 385 | { |
| 386 | int k=0; |
| 387 | if ( i != sourceRouter) |
| 388 | { |
| 389 | allocateLinkFaces(); |
| 390 | allocateDistanceToNeighbor(); |
| 391 | allocateDistFromNbrToDest(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 392 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 409 | addHypNextHopsToRoutingTable(pnlsr,pMap,rt,k,i); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 410 | freeLinkFaces(); |
| 411 | freeDistanceToNeighbor(); |
| 412 | freeDistFromNbrToDest(); |
| 413 | } |
| 414 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 415 | freeLinks(); |
| 416 | freeLinksCosts(); |
| 417 | freeAdjMatrix(); |
| 418 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 419 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 420 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 434 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 435 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 436 | double |
| 437 | HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr, |
| 438 | Map& pMap, int src, int dest) |
| 439 | { |
| 440 | double distance=0.0; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 441 | string srcRouterKey=pMap.getRouterNameByMappingNo(src)+"/3"; |
| 442 | string destRouterKey=pMap.getRouterNameByMappingNo(dest)+"/3"; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 443 | double srcRadius=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorRadius(); |
| 444 | double srcTheta=(pnlsr.getLsdb().getCorLsa(srcRouterKey).first).getCorTheta(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 445 | double destRadius=(pnlsr.getLsdb().getCorLsa( |
| 446 | destRouterKey).first).getCorRadius(); |
| 447 | double destTheta=(pnlsr.getLsdb().getCorLsa(destRouterKey).first).getCorTheta(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 448 | double diffTheta = fabs (srcTheta - destTheta); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 449 | if (diffTheta > MATH_PI) |
| 450 | { |
| 451 | diffTheta = 2 * MATH_PI - diffTheta; |
| 452 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 453 | 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 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 465 | return distance; |
| 466 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 467 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 468 | void |
| 469 | HypRoutingTableCalculator::allocateLinkFaces() |
| 470 | { |
| 471 | linkFaces=new int[vNoLink]; |
| 472 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 473 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 474 | void |
| 475 | HypRoutingTableCalculator::allocateDistanceToNeighbor() |
| 476 | { |
| 477 | distanceToNeighbor=new double[vNoLink]; |
| 478 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 479 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 480 | void |
| 481 | HypRoutingTableCalculator::allocateDistFromNbrToDest() |
| 482 | { |
| 483 | distFromNbrToDest=new double[vNoLink]; |
| 484 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 485 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 486 | void |
| 487 | HypRoutingTableCalculator::freeLinkFaces() |
| 488 | { |
| 489 | delete [] linkFaces; |
| 490 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 491 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 492 | void |
| 493 | HypRoutingTableCalculator::freeDistanceToNeighbor() |
| 494 | { |
| 495 | delete [] distanceToNeighbor; |
| 496 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 497 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 498 | void |
| 499 | HypRoutingTableCalculator::freeDistFromNbrToDest() |
| 500 | { |
| 501 | delete [] distFromNbrToDest; |
| 502 | } |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 503 | |
| 504 | }//namespace nlsr |