akmhoque | 79d355f | 2014-02-04 15:11:16 -0600 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include "nlsr_lsdb.hpp" |
| 3 | #include "nlsr_rtc.hpp" |
| 4 | #include "nlsr_map.hpp" |
| 5 | #include "nlsr_lsa.hpp" |
| 6 | #include "nlsr.hpp" |
| 7 | |
| 8 | using namespace std; |
| 9 | |
| 10 | void |
| 11 | RoutingTableCalculator::allocateAdjMatrix() |
| 12 | { |
| 13 | adjMatrix = new double*[numOfRouter]; |
| 14 | for(int i = 0; i < numOfRouter; ++i) |
| 15 | { |
| 16 | adjMatrix[i] = new double[numOfRouter]; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | void |
| 21 | RoutingTableCalculator::makeAdjMatrix(nlsr& pnlsr, Map pMap) |
| 22 | { |
| 23 | std::list<AdjLsa> adjLsdb=pnlsr.getLsdb().getAdjLsdb(); |
| 24 | for( std::list<AdjLsa>::iterator it=adjLsdb.begin(); |
| 25 | it!= adjLsdb.end() ; it++) |
| 26 | { |
| 27 | string linkStartRouter=(*it).getOrigRouter(); |
| 28 | int row=pMap.getMappingNoByRouterName(linkStartRouter); |
| 29 | std::list<Adjacent> adl=(*it).getAdl().getAdjList(); |
| 30 | for( std::list<Adjacent>::iterator itAdl=adl.begin(); |
| 31 | itAdl!= adl.end() ; itAdl++) |
| 32 | { |
| 33 | string linkEndRouter=(*itAdl).getAdjacentName(); |
| 34 | int col=pMap.getMappingNoByRouterName(linkEndRouter); |
| 35 | double cost=(*itAdl).getLinkCost(); |
| 36 | if ( (row >= 0 && row<numOfRouter) && (col >= 0 && col<numOfRouter) ) |
| 37 | { |
| 38 | adjMatrix[row][col]=cost; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | } |
| 43 | } |
| 44 | |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame^] | 45 | void |
| 46 | RoutingTableCalculator::printAdjMatrix() |
| 47 | { |
| 48 | for(int i=0;i<numOfRouter;i++) |
| 49 | { |
| 50 | for(int j=0;j<numOfRouter;j++) |
| 51 | printf("%f ",adjMatrix[i][j]); |
| 52 | printf("\n"); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost) |
| 58 | { |
| 59 | for ( int i = 0; i < numOfRouter; i++ ){ |
| 60 | if ( i == link ){ |
| 61 | adjMatrix[source][i]=linkCost; |
| 62 | } |
| 63 | else{ |
| 64 | adjMatrix[source][i]=0; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int |
| 70 | RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter) |
| 71 | { |
| 72 | int noLink=0; |
| 73 | for(int i=0;i<numOfRouter;i++) |
| 74 | { |
| 75 | if ( adjMatrix[sRouter][i] > 0 ) |
| 76 | { |
| 77 | noLink++; |
| 78 | } |
| 79 | } |
| 80 | return noLink; |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | RoutingTableCalculator::getLinksFromAdjMatrix(int *links, |
| 85 | double *linkCosts, int source) |
| 86 | { |
| 87 | int j=0; |
| 88 | for (int i=0; i <numOfRouter; i++) |
| 89 | { |
| 90 | if ( adjMatrix[source][i] > 0 ) |
| 91 | { |
| 92 | links[j]=i; |
| 93 | linkCosts[j]=adjMatrix[source][i]; |
| 94 | j++; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
akmhoque | 79d355f | 2014-02-04 15:11:16 -0600 | [diff] [blame] | 99 | void |
| 100 | RoutingTableCalculator::freeAdjMatrix() |
| 101 | { |
| 102 | for(int i = 0; i < numOfRouter; ++i) |
| 103 | { |
| 104 | delete [] adjMatrix[i]; |
| 105 | } |
| 106 | delete [] adjMatrix; |
| 107 | } |
| 108 | |
| 109 | void |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame^] | 110 | LinkStateRoutingTableCalculator::calculatePath(Map& pMap, |
| 111 | RoutingTable& rt, nlsr& pnlsr) |
| 112 | { |
| 113 | cout<<"LinkStateRoutingTableCalculator::calculatePath Called"<<endl; |
| 114 | allocateAdjMatrix(); |
| 115 | makeAdjMatrix(pnlsr,pMap); |
| 116 | cout<<pMap; |
| 117 | printAdjMatrix(); |
| 118 | string routerName=pnlsr.getConfParameter().getRouterPrefix(); |
| 119 | int sourceRouter=pMap.getMappingNoByRouterName(routerName); |
| 120 | cout<<"Calculating Router: "<< routerName <<" Mapping no: "<<sourceRouter<<endl; |
| 121 | int noLink=getNumOfLinkfromAdjMatrix(sourceRouter); |
| 122 | allocateParent(); |
| 123 | allocateDistance(); |
| 124 | |
| 125 | if ( pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1 ) |
| 126 | { |
| 127 | // Single Path |
| 128 | doDijkstraPathCalculation(sourceRouter); |
| 129 | // print all ls path -- debugging purpose |
| 130 | printAllLsPath(sourceRouter); |
| 131 | // update routing table |
| 132 | // update NPT ( FIB ) |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | // Multi Path |
| 137 | setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter)); |
| 138 | allocateLinks(); |
| 139 | allocateLinkCosts(); |
| 140 | |
| 141 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
| 142 | for (int i=0 ; i < vNoLink; i++) |
| 143 | { |
| 144 | adjustAdMatrix(sourceRouter,links[i], linkCosts[i]); |
| 145 | doDijkstraPathCalculation(sourceRouter); |
| 146 | //update routing table |
| 147 | //update NPT ( FIB ) |
| 148 | } |
| 149 | |
| 150 | freeLinks(); |
| 151 | freeLinksCosts(); |
| 152 | } |
| 153 | |
| 154 | freeParent(); |
| 155 | freeDistance(); |
| 156 | freeAdjMatrix(); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter) |
| 161 | { |
| 162 | int i; |
| 163 | int v,u; |
| 164 | int *Q=new int[numOfRouter]; |
| 165 | int head=0; |
| 166 | /* Initiate the Parent */ |
| 167 | for (i = 0 ; i < numOfRouter; i++){ |
| 168 | parent[i]=EMPTY_PARENT; |
| 169 | distance[i]=INF_DISTANCE; |
| 170 | Q[i]=i; |
| 171 | } |
| 172 | |
| 173 | if ( sourceRouter != NO_MAPPING_NUM ){ |
| 174 | distance[sourceRouter]=0; |
| 175 | sortQueueByDistance(Q,distance,head,numOfRouter); |
| 176 | |
| 177 | while (head < numOfRouter ) |
| 178 | { |
| 179 | u=Q[head]; |
| 180 | if(distance[u] == INF_DISTANCE) |
| 181 | { |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | for(v=0 ; v <numOfRouter; v++) |
| 186 | { |
| 187 | if( adjMatrix[u][v] > 0 ) |
| 188 | { |
| 189 | if ( isNotExplored(Q,v,head+1,numOfRouter) ) |
| 190 | { |
| 191 | if( distance[u] + adjMatrix[u][v] < distance[v]) |
| 192 | { |
| 193 | distance[v]=distance[u] + adjMatrix[u][v] ; |
| 194 | parent[v]=u; |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
| 203 | head++; |
| 204 | sortQueueByDistance(Q,distance,head,numOfRouter); |
| 205 | } |
| 206 | } |
| 207 | delete [] Q; |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | LinkStateRoutingTableCalculator::printAllLsPath(int sourceRouter) |
| 212 | { |
| 213 | cout<<"LinkStateRoutingTableCalculator::printAllLsPath Called"<<endl; |
| 214 | cout<<"Source Router: "<<sourceRouter<<endl; |
| 215 | for(int i=0; i < numOfRouter ; i++) |
| 216 | { |
| 217 | if ( i!= sourceRouter ) |
| 218 | { |
| 219 | printLsPath(i); |
| 220 | cout<<endl; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | LinkStateRoutingTableCalculator::printLsPath(int destRouter) |
| 227 | { |
| 228 | if (parent[destRouter] != EMPTY_PARENT ) |
| 229 | { |
| 230 | printLsPath(parent[destRouter]); |
| 231 | } |
| 232 | |
| 233 | cout<<" "<<destRouter; |
| 234 | } |
| 235 | |
| 236 | void |
| 237 | LinkStateRoutingTableCalculator::sortQueueByDistance(int *Q, |
| 238 | double *dist,int start,int element) |
| 239 | { |
| 240 | for ( int i=start ; i < element ; i ++) |
| 241 | { |
| 242 | for( int j=i+1; j<element; j ++) |
| 243 | { |
| 244 | if (dist[Q[j]] < dist[Q[i]]) |
| 245 | { |
| 246 | int tempU=Q[j]; |
| 247 | Q[j]=Q[i]; |
| 248 | Q[i]=tempU; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | } |
| 254 | |
| 255 | int |
| 256 | LinkStateRoutingTableCalculator::isNotExplored(int *Q, |
| 257 | int u,int start, int element) |
| 258 | { |
| 259 | int ret=0; |
| 260 | for(int i=start; i< element; i++) |
| 261 | { |
| 262 | if ( Q[i] == u ) |
| 263 | { |
| 264 | ret=1; |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | void |
| 272 | LinkStateRoutingTableCalculator::allocateParent() |
| 273 | { |
| 274 | parent=new int[numOfRouter]; |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | LinkStateRoutingTableCalculator::allocateDistance() |
| 279 | { |
| 280 | distance= new double[numOfRouter]; |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | LinkStateRoutingTableCalculator::freeParent() |
| 285 | { |
| 286 | delete [] parent; |
| 287 | } |
| 288 | |
| 289 | void LinkStateRoutingTableCalculator::freeDistance() |
| 290 | { |
| 291 | delete [] distance; |
| 292 | } |
| 293 | |
| 294 | void |
| 295 | LinkStateRoutingTableCalculator::allocateLinks() |
| 296 | { |
| 297 | links=new int[vNoLink]; |
| 298 | } |
| 299 | |
| 300 | void LinkStateRoutingTableCalculator::allocateLinkCosts() |
| 301 | { |
| 302 | linkCosts=new double[vNoLink]; |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | LinkStateRoutingTableCalculator::freeLinks() |
| 307 | { |
| 308 | delete [] links; |
| 309 | } |
| 310 | void |
| 311 | LinkStateRoutingTableCalculator::freeLinksCosts() |
| 312 | { |
| 313 | delete [] linkCosts; |
| 314 | } |
| 315 | |
| 316 | void |
| 317 | HypRoutingTableCalculator::calculatePath(Map& pMap, |
| 318 | RoutingTable& rt, nlsr& pnlsr) |
akmhoque | 79d355f | 2014-02-04 15:11:16 -0600 | [diff] [blame] | 319 | { |
| 320 | } |
| 321 | |
| 322 | void |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame^] | 323 | HypDryRoutingTableCalculator::calculatePath(Map& pMap, |
| 324 | RoutingTable& rt, nlsr& pnlsr) |
akmhoque | 79d355f | 2014-02-04 15:11:16 -0600 | [diff] [blame] | 325 | { |
| 326 | } |
| 327 | |