akmhoque | ba09474 | 2014-02-28 11:47:21 -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 | |
| 10 | namespace nlsr |
| 11 | { |
| 12 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 13 | using namespace std; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 14 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 15 | void |
| 16 | RoutingTableCalculator::allocateAdjMatrix() |
| 17 | { |
| 18 | adjMatrix = new double*[numOfRouter]; |
| 19 | for(int i = 0; i < numOfRouter; ++i) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 20 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 21 | 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) ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 52 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 53 | adjMatrix[row][col]=cost; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 54 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 55 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 56 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 57 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 58 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 59 | void |
| 60 | RoutingTableCalculator::printAdjMatrix() |
| 61 | { |
| 62 | for(int i=0; i<numOfRouter; i++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 63 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 64 | for(int j=0; j<numOfRouter; j++) |
| 65 | printf("%f ",adjMatrix[i][j]); |
| 66 | printf("\n"); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 67 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 68 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 69 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 70 | void |
| 71 | RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost) |
| 72 | { |
| 73 | for ( int i = 0; i < numOfRouter; i++ ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 74 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 75 | if ( i == link ) |
| 76 | { |
| 77 | adjMatrix[source][i]=linkCost; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | adjMatrix[source][i]=0; |
| 82 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 83 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 84 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 85 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 86 | int |
| 87 | RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter) |
| 88 | { |
| 89 | int noLink=0; |
| 90 | for(int i=0; i<numOfRouter; i++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 91 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 92 | if ( adjMatrix[sRouter][i] > 0 ) |
| 93 | { |
| 94 | noLink++; |
| 95 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 96 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 97 | return noLink; |
| 98 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 99 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 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++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 106 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 107 | if ( adjMatrix[source][i] > 0 ) |
| 108 | { |
| 109 | links[j]=i; |
| 110 | linkCosts[j]=adjMatrix[source][i]; |
| 111 | j++; |
| 112 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 113 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 114 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 115 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 116 | void |
| 117 | RoutingTableCalculator::freeAdjMatrix() |
| 118 | { |
| 119 | for(int i = 0; i < numOfRouter; ++i) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 120 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 121 | delete [] adjMatrix[i]; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 122 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 123 | delete [] adjMatrix; |
| 124 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 125 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 126 | |
| 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 ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 166 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 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); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 173 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 174 | else |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 175 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 176 | // 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]); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 184 | printAdjMatrix(); |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 185 | 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(); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 193 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 194 | freeParent(); |
| 195 | freeDistance(); |
| 196 | freeAdjMatrix(); |
| 197 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 198 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [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++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 208 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 209 | parent[i]=EMPTY_PARENT; |
| 210 | distance[i]=INF_DISTANCE; |
| 211 | Q[i]=i; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 212 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 213 | if ( sourceRouter != NO_MAPPING_NUM ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 214 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 215 | 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); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 302 | cout<<endl; |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 303 | } |
| 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]]) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 326 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 327 | int tempU=Q[j]; |
| 328 | Q[j]=Q[i]; |
| 329 | Q[i]=tempU; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 330 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 331 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 332 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 333 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 334 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 335 | 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++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 341 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 342 | 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++) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 397 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 398 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 412 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 413 | addHypNextHopsToRoutingTable(pnlsr,pMap,rt,k,i); |
| 414 | freeLinkFaces(); |
| 415 | freeDistanceToNeighbor(); |
| 416 | freeDistFromNbrToDest(); |
| 417 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 418 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 419 | freeLinks(); |
| 420 | freeLinksCosts(); |
| 421 | freeAdjMatrix(); |
| 422 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 423 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 424 | void |
| 425 | HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr,Map& pMap, |
| 426 | RoutingTable& rt, int noFaces, int dest) |
| 427 | { |
| 428 | for(int i=0 ; i < noFaces ; ++i) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 429 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 430 | 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 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 437 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 438 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 439 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 440 | 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) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 454 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 455 | diffTheta = 2 * MATH_PI - diffTheta; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 456 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 457 | if ( srcRadius != -1 && destRadius != -1 ) |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 458 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 459 | if (diffTheta == 0) |
| 460 | distance = fabs (srcRadius - destRadius); |
| 461 | else |
| 462 | distance = acosh((cosh(srcRadius)*cosh(destRadius))- |
| 463 | (sinh(srcRadius)*sinh(destRadius)*cos(diffTheta))); |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 464 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 465 | else |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 466 | { |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 467 | distance = -1; |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 468 | } |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 469 | return distance; |
| 470 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 471 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 472 | void |
| 473 | HypRoutingTableCalculator::allocateLinkFaces() |
| 474 | { |
| 475 | linkFaces=new int[vNoLink]; |
| 476 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 477 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 478 | void |
| 479 | HypRoutingTableCalculator::allocateDistanceToNeighbor() |
| 480 | { |
| 481 | distanceToNeighbor=new double[vNoLink]; |
| 482 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 483 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 484 | void |
| 485 | HypRoutingTableCalculator::allocateDistFromNbrToDest() |
| 486 | { |
| 487 | distFromNbrToDest=new double[vNoLink]; |
| 488 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 489 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 490 | void |
| 491 | HypRoutingTableCalculator::freeLinkFaces() |
| 492 | { |
| 493 | delete [] linkFaces; |
| 494 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 495 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 496 | void |
| 497 | HypRoutingTableCalculator::freeDistanceToNeighbor() |
| 498 | { |
| 499 | delete [] distanceToNeighbor; |
| 500 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 501 | |
akmhoque | 5a44dd4 | 2014-03-12 18:11:32 -0500 | [diff] [blame^] | 502 | void |
| 503 | HypRoutingTableCalculator::freeDistFromNbrToDest() |
| 504 | { |
| 505 | delete [] distFromNbrToDest; |
| 506 | } |
akmhoque | ba09474 | 2014-02-28 11:47:21 -0600 | [diff] [blame] | 507 | |
| 508 | }//namespace nlsr |