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