akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 1 | #include<string> |
| 2 | #include "nlsr_lsdb.hpp" |
| 3 | #include "nlsr.hpp" |
| 4 | |
| 5 | using namespace std; |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | bool |
| 12 | Lsdb::doesLsaExist(string key, int lsType) |
| 13 | { |
| 14 | if ( lsType == 1) |
| 15 | { |
| 16 | return doesNameLsaExist(key); |
| 17 | } |
| 18 | else if ( lsType == 2) |
| 19 | { |
| 20 | return doesAdjLsaExist(key); |
| 21 | } |
| 22 | else if ( lsType == 3) |
| 23 | { |
| 24 | return doesCorLsaExist(key); |
| 25 | } |
| 26 | |
| 27 | return false; |
| 28 | |
| 29 | } |
| 30 | |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 31 | //Name LSA and LSDB related functions start here |
| 32 | |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 33 | static bool |
| 34 | nameLsaCompare(NameLsa& nlsa1, NameLsa& nlsa2){ |
| 35 | return nlsa1.getLsaKey()==nlsa1.getLsaKey(); |
| 36 | } |
| 37 | |
| 38 | static bool |
| 39 | nameLsaCompareByKey(NameLsa& nlsa1, string& key){ |
| 40 | return nlsa1.getLsaKey()==key; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | bool |
| 45 | Lsdb::buildAndInstallOwnNameLsa(nlsr& pnlsr) |
| 46 | { |
| 47 | NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 48 | , 1 |
| 49 | , pnlsr.getNameLsaSeq()+1 |
| 50 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 51 | , pnlsr.getNpl() ); |
| 52 | pnlsr.setNameLsaSeq(pnlsr.getNameLsaSeq()+1); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 53 | //cout<<nameLsa; |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 54 | return installNameLsa(nameLsa); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | NameLsa& |
| 59 | Lsdb::getNameLsa(string key) |
| 60 | { |
| 61 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 62 | nameLsdb.end(), |
| 63 | bind(nameLsaCompareByKey, _1, key)); |
| 64 | |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 65 | if( it != nameLsdb.end()) |
| 66 | { |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 67 | return (*it); |
| 68 | } |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 69 | |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | bool |
| 75 | Lsdb::installNameLsa(NameLsa &nlsa) |
| 76 | { |
| 77 | bool doesLsaExist_ = doesNameLsaExist(nlsa.getLsaKey()); |
| 78 | if ( !doesLsaExist_ ) |
| 79 | { |
| 80 | // add name LSA |
| 81 | addNameLsa(nlsa); |
| 82 | // update NPT and FIB |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 83 | // if its not own LSA |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 84 | } |
| 85 | else |
| 86 | { |
| 87 | // check for newer name LSA |
| 88 | NameLsa oldNameLsa=getNameLsa(nlsa.getLsaKey()); |
| 89 | // Discard or Update Name lsa, NPT, FIB |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 90 | // if its not own LSA |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool |
| 97 | Lsdb::addNameLsa(NameLsa &nlsa) |
| 98 | { |
| 99 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 100 | nameLsdb.end(), |
| 101 | bind(nameLsaCompare, _1, nlsa)); |
| 102 | |
| 103 | if( it == nameLsdb.end()){ |
| 104 | nameLsdb.push_back(nlsa); |
| 105 | return true; |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | bool |
| 111 | Lsdb::removeNameLsa(string& key) |
| 112 | { |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 113 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 114 | nameLsdb.end(), |
| 115 | bind(nameLsaCompareByKey, _1, key)); |
| 116 | if ( it != nameLsdb.end() ) |
| 117 | { |
| 118 | nameLsdb.erase(it); |
| 119 | return true; |
| 120 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 121 | return false; |
| 122 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 123 | |
| 124 | bool |
| 125 | Lsdb::doesNameLsaExist(string key) |
| 126 | { |
| 127 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 128 | nameLsdb.end(), |
| 129 | bind(nameLsaCompareByKey, _1, key)); |
| 130 | |
| 131 | if( it == nameLsdb.end()){ |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 138 | void |
| 139 | Lsdb::printNameLsdb() |
| 140 | { |
| 141 | for( std::list<NameLsa>::iterator it=nameLsdb.begin(); |
| 142 | it!= nameLsdb.end() ; it++) |
| 143 | { |
| 144 | cout<< (*it) <<endl; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Cor LSA and LSDB related Functions start here |
| 149 | |
| 150 | static bool |
| 151 | corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){ |
| 152 | return clsa1.getLsaKey()==clsa1.getLsaKey(); |
| 153 | } |
| 154 | |
| 155 | static bool |
| 156 | corLsaCompareByKey(CorLsa& clsa, string& key){ |
| 157 | return clsa.getLsaKey()==key; |
| 158 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 159 | |
| 160 | bool |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 161 | Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){ |
| 162 | CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix() |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 163 | , 3 |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 164 | , pnlsr.getCorLsaSeq()+1 |
| 165 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 166 | , pnlsr.getConfParameter().getCorR() |
| 167 | , pnlsr.getConfParameter().getCorTheta() ); |
| 168 | pnlsr.setCorLsaSeq(pnlsr.getCorLsaSeq()+1); |
| 169 | //cout<<corLsa; |
| 170 | installCorLsa(corLsa); |
| 171 | |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 172 | return true; |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | CorLsa& |
| 176 | Lsdb::getCorLsa(string key) |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 177 | { |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 178 | std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 179 | corLsdb.end(), |
| 180 | bind(corLsaCompareByKey, _1, key)); |
| 181 | |
| 182 | if( it != corLsdb.end()){ |
| 183 | return (*it); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | bool |
| 188 | Lsdb::installCorLsa(CorLsa &clsa) |
| 189 | { |
| 190 | bool doesLsaExist_ = doesCorLsaExist(clsa.getLsaKey()); |
| 191 | if ( !doesLsaExist_ ) |
| 192 | { |
| 193 | // add cor LSA |
| 194 | addCorLsa(clsa); |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 195 | //schedule routing table calculation only if |
| 196 | //hyperbolic calculation is scheduled |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 197 | } |
| 198 | else |
| 199 | { |
| 200 | // check for newer cor LSA |
| 201 | CorLsa oldCorLsa=getCorLsa(clsa.getLsaKey()); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | bool |
| 209 | Lsdb::addCorLsa(CorLsa& clsa) |
| 210 | { |
| 211 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 212 | corLsdb.end(), |
| 213 | bind(corLsaCompare, _1, clsa)); |
| 214 | |
| 215 | if( it == corLsdb.end()){ |
| 216 | corLsdb.push_back(clsa); |
| 217 | return true; |
| 218 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | |
| 222 | bool |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 223 | Lsdb::removeCorLsa(string& key) |
| 224 | { |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 225 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 226 | corLsdb.end(), |
| 227 | bind(corLsaCompareByKey, _1, key)); |
| 228 | if ( it != corLsdb.end() ) |
| 229 | { |
| 230 | corLsdb.erase(it); |
| 231 | return true; |
| 232 | } |
| 233 | return false; |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 234 | |
| 235 | } |
| 236 | |
| 237 | bool |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 238 | Lsdb::doesCorLsaExist(string key) |
| 239 | { |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 240 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 241 | corLsdb.end(), |
| 242 | bind(corLsaCompareByKey, _1, key)); |
| 243 | |
| 244 | if( it == corLsdb.end()){ |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | return true; |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 249 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 250 | |
| 251 | void |
| 252 | Lsdb::printCorLsdb() //debugging |
| 253 | { |
| 254 | for( std::list<CorLsa>::iterator it=corLsdb.begin(); |
| 255 | it!= corLsdb.end() ; it++) |
| 256 | { |
| 257 | cout<< (*it) <<endl; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | // Adj LSA and LSDB related function starts here |
| 263 | |
| 264 | static bool |
| 265 | adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){ |
| 266 | return alsa1.getLsaKey()==alsa1.getLsaKey(); |
| 267 | } |
| 268 | |
| 269 | static bool |
| 270 | adjLsaCompareByKey(AdjLsa& alsa, string& key){ |
| 271 | return alsa.getLsaKey()==key; |
| 272 | } |
| 273 | |
| 274 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 275 | void |
| 276 | Lsdb::scheduledAdjLsaBuild(nlsr& pnlsr) |
| 277 | { |
| 278 | cout<<"scheduledAdjLsaBuild Called"<<endl; |
| 279 | pnlsr.setIsBuildAdjLsaSheduled(0); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 280 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 281 | if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr)) |
| 282 | { |
| 283 | int adjBuildCount=pnlsr.getAdjBuildCount(); |
| 284 | if(adjBuildCount>0 ) |
| 285 | { |
| 286 | if (pnlsr.getAdl().getNumOfActiveNeighbor()>0) |
| 287 | { |
| 288 | buildAndInstallOwnAdjLsa(pnlsr); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | //remove if there is any adj lsa in LSDB |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 293 | string key=pnlsr.getConfParameter().getRouterPrefix()+"/2"; |
| 294 | removeAdjLsa(key); |
| 295 | // Remove alll fib entries as per NPT |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 296 | } |
| 297 | pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount); |
| 298 | } |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | pnlsr.setIsBuildAdjLsaSheduled(1); |
| 303 | int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()* |
| 304 | pnlsr.getConfParameter().getInterestRetryNumber(); |
| 305 | pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime), |
| 306 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(), |
| 307 | boost::ref(pnlsr))); |
| 308 | } |
| 309 | |
| 310 | } |
| 311 | |
| 312 | |
| 313 | bool |
| 314 | Lsdb::addAdjLsa(AdjLsa &alsa) |
| 315 | { |
| 316 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 317 | adjLsdb.end(), |
| 318 | bind(adjLsaCompare, _1, alsa)); |
| 319 | |
| 320 | if( it == adjLsdb.end()){ |
| 321 | adjLsdb.push_back(alsa); |
| 322 | return true; |
| 323 | } |
| 324 | return false; |
| 325 | |
| 326 | } |
| 327 | |
| 328 | AdjLsa& |
| 329 | Lsdb::getAdjLsa(string key) |
| 330 | { |
| 331 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 332 | adjLsdb.end(), |
| 333 | bind(adjLsaCompareByKey, _1, key)); |
| 334 | |
| 335 | if( it != adjLsdb.end()){ |
| 336 | return (*it); |
| 337 | } |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | bool |
| 341 | Lsdb::installAdjLsa(AdjLsa &alsa) |
| 342 | { |
| 343 | bool doesLsaExist_ = doesAdjLsaExist(alsa.getLsaKey()); |
| 344 | if ( !doesLsaExist_ ) |
| 345 | { |
| 346 | // add Adj LSA |
| 347 | addAdjLsa(alsa); |
| 348 | // schedule routing table calculation |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | // check for newer name LSA |
| 353 | AdjLsa oldAdjLsa=getAdjLsa(alsa.getLsaKey()); |
| 354 | |
| 355 | } |
| 356 | |
| 357 | printAdjLsdb(); |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | bool |
| 363 | Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr) |
| 364 | { |
| 365 | AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 366 | , 2 |
| 367 | , pnlsr.getAdjLsaSeq()+1 |
| 368 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 369 | , pnlsr.getAdl().getNumOfActiveNeighbor() |
| 370 | , pnlsr.getAdl() ); |
| 371 | pnlsr.setAdjLsaSeq(pnlsr.getAdjLsaSeq()+1); |
| 372 | return installAdjLsa(adjLsa); |
| 373 | } |
| 374 | |
| 375 | bool |
| 376 | Lsdb::removeAdjLsa(string& key) |
| 377 | { |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 378 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 379 | adjLsdb.end(), |
| 380 | bind(adjLsaCompareByKey, _1, key)); |
| 381 | if ( it != adjLsdb.end() ) |
| 382 | { |
| 383 | adjLsdb.erase(it); |
| 384 | return true; |
| 385 | } |
| 386 | return false; |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 387 | |
| 388 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 389 | |
| 390 | bool |
| 391 | Lsdb::doesAdjLsaExist(string key) |
| 392 | { |
| 393 | std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 394 | adjLsdb.end(), |
| 395 | bind(adjLsaCompareByKey, _1, key)); |
| 396 | |
| 397 | if( it == adjLsdb.end()){ |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | return true; |
| 402 | } |
| 403 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 404 | void |
| 405 | Lsdb::printAdjLsdb() |
| 406 | { |
| 407 | for( std::list<AdjLsa>::iterator it=adjLsdb.begin(); |
| 408 | it!= adjLsdb.end() ; it++) |
| 409 | { |
| 410 | cout<< (*it) <<endl; |
| 411 | } |
| 412 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 413 | |