akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #include<string> |
| 2 | #include<utility> |
| 3 | #include "nlsr_lsdb.hpp" |
| 4 | #include "nlsr.hpp" |
| 5 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame^] | 6 | namespace nlsr { |
| 7 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 8 | using namespace std; |
| 9 | |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 10 | void |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 11 | Lsdb::cancelScheduleLsaExpiringEvent(Nlsr& pnlsr, EventId eid) |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 12 | { |
| 13 | pnlsr.getScheduler().cancelEvent(eid); |
| 14 | } |
| 15 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 16 | static bool |
| 17 | nameLsaCompareByKey(NameLsa& nlsa1, string& key){ |
| 18 | return nlsa1.getNameLsaKey()==key; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 23 | Lsdb::buildAndInstallOwnNameLsa(Nlsr& pnlsr) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 24 | { |
| 25 | NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 26 | , 1 |
| 27 | , pnlsr.getSm().getNameLsaSeq()+1 |
| 28 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 29 | , pnlsr.getNpl() ); |
| 30 | pnlsr.getSm().setNameLsaSeq(pnlsr.getSm().getNameLsaSeq()+1); |
| 31 | return installNameLsa(pnlsr,nameLsa); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | std::pair<NameLsa&, bool> |
| 36 | Lsdb::getNameLsa(string key) |
| 37 | { |
| 38 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 39 | nameLsdb.end(), |
| 40 | bind(nameLsaCompareByKey, _1, key)); |
| 41 | |
| 42 | if( it != nameLsdb.end()) |
| 43 | { |
| 44 | return std::make_pair(boost::ref((*it)),true); |
| 45 | } |
| 46 | |
| 47 | NameLsa nlsa; |
| 48 | return std::make_pair(boost::ref(nlsa),false); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 53 | ndn::EventId |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 54 | Lsdb::scheduleNameLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 55 | { |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 56 | return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime), |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 57 | ndn::bind(&Lsdb::exprireOrRefreshNameLsa, |
| 58 | this,boost::ref(pnlsr), key, seqNo)); |
| 59 | } |
| 60 | |
| 61 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 62 | Lsdb::installNameLsa(Nlsr& pnlsr, NameLsa &nlsa) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 63 | { |
| 64 | int timeToExpire=lsaRefreshTime; |
| 65 | std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(nlsa.getNameLsaKey()); |
| 66 | if ( !chkNameLsa.second ) |
| 67 | { |
| 68 | addNameLsa(nlsa); |
| 69 | printNameLsdb(); |
| 70 | if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 71 | { |
| 72 | pnlsr.getNpt().addNpte(nlsa.getOrigRouter(),nlsa.getOrigRouter(),pnlsr); |
| 73 | std::list<string> nameList=nlsa.getNpl().getNameList(); |
| 74 | for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end();it++) |
| 75 | { |
| 76 | if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix()) |
| 77 | { |
| 78 | pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 84 | { |
| 85 | timeToExpire=nlsa.getLifeTime(); |
| 86 | } |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 87 | nlsa.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr, |
| 88 | nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire)); |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 89 | } |
| 90 | else |
| 91 | { |
| 92 | if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() ) |
| 93 | { |
| 94 | chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo()); |
| 95 | chkNameLsa.first.setLifeTime(nlsa.getLifeTime()); |
| 96 | |
| 97 | chkNameLsa.first.getNpl().sortNpl(); |
| 98 | nlsa.getNpl().sortNpl(); |
| 99 | |
| 100 | std::list<string> nameToAdd; |
| 101 | std::set_difference(nlsa.getNpl().getNameList().begin(), |
| 102 | nlsa.getNpl().getNameList().end(), |
| 103 | chkNameLsa.first.getNpl().getNameList().begin(), |
| 104 | chkNameLsa.first.getNpl().getNameList().end(), |
| 105 | std::inserter(nameToAdd, nameToAdd.begin())); |
| 106 | for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end(); |
| 107 | ++it) |
| 108 | { |
| 109 | chkNameLsa.first.addNameToLsa((*it)); |
| 110 | if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 111 | { |
| 112 | if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix()) |
| 113 | { |
| 114 | pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | std::list<string> nameToRemove; |
| 120 | std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(), |
| 121 | chkNameLsa.first.getNpl().getNameList().end(), |
| 122 | nlsa.getNpl().getNameList().begin(), |
| 123 | nlsa.getNpl().getNameList().end(), |
| 124 | std::inserter(nameToRemove, nameToRemove.begin())); |
| 125 | for(std::list<string>::iterator it=nameToRemove.begin(); |
| 126 | it!=nameToRemove.end(); ++it) |
| 127 | { |
| 128 | chkNameLsa.first.removeNameFromLsa((*it)); |
| 129 | if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 130 | { |
| 131 | if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix()) |
| 132 | { |
| 133 | pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 139 | { |
| 140 | timeToExpire=nlsa.getLifeTime(); |
| 141 | } |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 142 | cancelScheduleLsaExpiringEvent(pnlsr, |
| 143 | chkNameLsa.first.getLsaExpiringEventId()); |
| 144 | chkNameLsa.first.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr, |
| 145 | nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire)); |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | bool |
| 153 | Lsdb::addNameLsa(NameLsa &nlsa) |
| 154 | { |
| 155 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 156 | nameLsdb.end(), bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey())); |
| 157 | |
| 158 | if( it == nameLsdb.end()) |
| 159 | { |
| 160 | nameLsdb.push_back(nlsa); |
| 161 | return true; |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 167 | Lsdb::removeNameLsa(Nlsr& pnlsr, string& key) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 168 | { |
| 169 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 170 | nameLsdb.end(), |
| 171 | bind(nameLsaCompareByKey, _1, key)); |
| 172 | if ( it != nameLsdb.end() ) |
| 173 | { |
| 174 | if ( (*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix() ) |
| 175 | { |
| 176 | pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr); |
| 177 | for( std::list<string>::iterator nit=(*it).getNpl().getNameList().begin(); |
| 178 | nit!=(*it).getNpl().getNameList().end(); ++nit) |
| 179 | { |
| 180 | if ( (*nit) !=pnlsr.getConfParameter().getRouterPrefix()) |
| 181 | { |
| 182 | pnlsr.getNpt().removeNpte((*nit),(*it).getOrigRouter(),pnlsr); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | nameLsdb.erase(it); |
| 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | bool |
| 194 | Lsdb::doesNameLsaExist(string key) |
| 195 | { |
| 196 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 197 | nameLsdb.end(), |
| 198 | bind(nameLsaCompareByKey, _1, key)); |
| 199 | |
| 200 | if( it == nameLsdb.end()){ |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | Lsdb::printNameLsdb() |
| 209 | { |
| 210 | cout<<"---------------Name LSDB-------------------"<<endl; |
| 211 | for( std::list<NameLsa>::iterator it=nameLsdb.begin(); |
| 212 | it!= nameLsdb.end() ; it++) |
| 213 | { |
| 214 | cout<< (*it) <<endl; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Cor LSA and LSDB related Functions start here |
| 219 | |
| 220 | |
| 221 | static bool |
| 222 | corLsaCompareByKey(CorLsa& clsa, string& key){ |
| 223 | return clsa.getCorLsaKey()==key; |
| 224 | } |
| 225 | |
| 226 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 227 | Lsdb::buildAndInstallOwnCorLsa(Nlsr& pnlsr){ |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 228 | CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 229 | , 3 |
| 230 | , pnlsr.getSm().getCorLsaSeq()+1 |
| 231 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 232 | , pnlsr.getConfParameter().getCorR() |
| 233 | , pnlsr.getConfParameter().getCorTheta() ); |
| 234 | pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1); |
| 235 | installCorLsa(pnlsr, corLsa); |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | std::pair<CorLsa&, bool> |
| 241 | Lsdb::getCorLsa(string key) |
| 242 | { |
| 243 | std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 244 | corLsdb.end(), |
| 245 | bind(corLsaCompareByKey, _1, key)); |
| 246 | |
| 247 | if( it != corLsdb.end()){ |
| 248 | return std::make_pair(boost::ref((*it)), true); |
| 249 | } |
| 250 | |
| 251 | CorLsa clsa; |
| 252 | return std::make_pair(boost::ref(clsa),false); |
| 253 | } |
| 254 | |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 255 | ndn::EventId |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 256 | Lsdb::scheduleCorLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 257 | { |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 258 | return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime), |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 259 | ndn::bind(&Lsdb::exprireOrRefreshCorLsa, |
| 260 | this,boost::ref(pnlsr),key,seqNo)); |
| 261 | } |
| 262 | |
| 263 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 264 | Lsdb::installCorLsa(Nlsr& pnlsr, CorLsa &clsa) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 265 | { |
| 266 | int timeToExpire=lsaRefreshTime; |
| 267 | std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey()); |
| 268 | if ( !chkCorLsa.second ) |
| 269 | { |
| 270 | addCorLsa(clsa); |
| 271 | printCorLsdb(); //debugging purpose |
| 272 | if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 273 | { |
| 274 | pnlsr.getNpt().addNpte(clsa.getOrigRouter(),clsa.getOrigRouter(),pnlsr); |
| 275 | } |
| 276 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 ) |
| 277 | { |
| 278 | pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr); |
| 279 | } |
| 280 | |
| 281 | if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 282 | { |
| 283 | timeToExpire=clsa.getLifeTime(); |
| 284 | } |
| 285 | scheduleCorLsaExpiration(pnlsr,clsa.getCorLsaKey(), |
| 286 | clsa.getLsSeqNo(), timeToExpire); |
| 287 | |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | if ( chkCorLsa.first.getLsSeqNo() < clsa.getLsSeqNo() ) |
| 292 | { |
| 293 | chkCorLsa.first.setLsSeqNo(clsa.getLsSeqNo()); |
| 294 | chkCorLsa.first.setLifeTime(clsa.getLifeTime()); |
| 295 | if ( !chkCorLsa.first.isLsaContentEqual(clsa) ) |
| 296 | { |
| 297 | chkCorLsa.first.setCorRadius(clsa.getCorRadius()); |
| 298 | chkCorLsa.first.setCorTheta(clsa.getCorTheta()); |
| 299 | |
| 300 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 ) |
| 301 | { |
| 302 | pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr); |
| 303 | } |
| 304 | |
| 305 | } |
| 306 | |
| 307 | if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 308 | { |
| 309 | timeToExpire=clsa.getLifeTime(); |
| 310 | } |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 311 | cancelScheduleLsaExpiringEvent(pnlsr, |
| 312 | chkCorLsa.first.getLsaExpiringEventId()); |
| 313 | chkCorLsa.first.setLsaExpiringEventId(scheduleCorLsaExpiration(pnlsr, |
| 314 | clsa.getCorLsaKey(), |
| 315 | clsa.getLsSeqNo(), timeToExpire)); |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | } |
| 319 | |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | bool |
| 324 | Lsdb::addCorLsa(CorLsa& clsa) |
| 325 | { |
| 326 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 327 | corLsdb.end(), |
| 328 | bind(corLsaCompareByKey, _1, clsa.getCorLsaKey())); |
| 329 | |
| 330 | if( it == corLsdb.end()) |
| 331 | { |
| 332 | corLsdb.push_back(clsa); |
| 333 | return true; |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 339 | Lsdb::removeCorLsa(Nlsr& pnlsr, string& key) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 340 | { |
| 341 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 342 | corLsdb.end(), |
| 343 | bind(corLsaCompareByKey, _1, key)); |
| 344 | if ( it != corLsdb.end() ) |
| 345 | { |
| 346 | if ( (*it).getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 347 | { |
| 348 | pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr); |
| 349 | } |
| 350 | corLsdb.erase(it); |
| 351 | return true; |
| 352 | } |
| 353 | return false; |
| 354 | |
| 355 | } |
| 356 | |
| 357 | bool |
| 358 | Lsdb::doesCorLsaExist(string key) |
| 359 | { |
| 360 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 361 | corLsdb.end(), |
| 362 | bind(corLsaCompareByKey, _1, key)); |
| 363 | |
| 364 | if( it == corLsdb.end()){ |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | Lsdb::printCorLsdb() //debugging |
| 373 | { |
| 374 | cout<<"---------------Cor LSDB-------------------"<<endl; |
| 375 | for( std::list<CorLsa>::iterator it=corLsdb.begin(); |
| 376 | it!= corLsdb.end() ; it++) |
| 377 | { |
| 378 | cout<< (*it) <<endl; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | |
| 383 | // Adj LSA and LSDB related function starts here |
| 384 | |
| 385 | static bool |
| 386 | adjLsaCompareByKey(AdjLsa& alsa, string& key){ |
| 387 | return alsa.getAdjLsaKey()==key; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | void |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 392 | Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 393 | { |
| 394 | cout<<"scheduledAdjLsaBuild Called"<<endl; |
| 395 | pnlsr.setIsBuildAdjLsaSheduled(0); |
| 396 | |
| 397 | if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr)) |
| 398 | { |
| 399 | int adjBuildCount=pnlsr.getAdjBuildCount(); |
| 400 | if(adjBuildCount>0 ) |
| 401 | { |
| 402 | if (pnlsr.getAdl().getNumOfActiveNeighbor()>0) |
| 403 | { |
| 404 | buildAndInstallOwnAdjLsa(pnlsr); |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | string key=pnlsr.getConfParameter().getRouterPrefix()+"/2"; |
| 409 | removeAdjLsa(pnlsr,key); |
| 410 | pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr); |
| 411 | } |
| 412 | pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount); |
| 413 | } |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | pnlsr.setIsBuildAdjLsaSheduled(1); |
| 418 | int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()* |
| 419 | pnlsr.getConfParameter().getInterestResendTime(); |
| 420 | pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime), |
| 421 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(), |
| 422 | boost::ref(pnlsr))); |
| 423 | } |
| 424 | |
| 425 | } |
| 426 | |
| 427 | |
| 428 | bool |
| 429 | Lsdb::addAdjLsa(AdjLsa &alsa) |
| 430 | { |
| 431 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 432 | adjLsdb.end(), |
| 433 | bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey())); |
| 434 | |
| 435 | if( it == adjLsdb.end()){ |
| 436 | adjLsdb.push_back(alsa); |
| 437 | return true; |
| 438 | } |
| 439 | return false; |
| 440 | |
| 441 | } |
| 442 | |
| 443 | std::pair<AdjLsa& , bool> |
| 444 | Lsdb::getAdjLsa(string key) |
| 445 | { |
| 446 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 447 | adjLsdb.end(), |
| 448 | bind(adjLsaCompareByKey, _1, key)); |
| 449 | |
| 450 | if( it != adjLsdb.end()){ |
| 451 | return std::make_pair(boost::ref((*it)),true); |
| 452 | } |
| 453 | |
| 454 | AdjLsa alsa; |
| 455 | return std::make_pair(boost::ref(alsa),false); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 460 | ndn::EventId |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 461 | Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 462 | { |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 463 | return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime), |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 464 | ndn::bind(&Lsdb::exprireOrRefreshAdjLsa, |
| 465 | this,boost::ref(pnlsr),key,seqNo)); |
| 466 | } |
| 467 | |
| 468 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 469 | Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa &alsa) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 470 | { |
| 471 | int timeToExpire=lsaRefreshTime; |
| 472 | std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey()); |
| 473 | if ( !chkAdjLsa.second ) |
| 474 | { |
| 475 | addAdjLsa(alsa); |
| 476 | alsa.addNptEntriesForAdjLsa(pnlsr); |
| 477 | pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr); |
| 478 | |
| 479 | if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 480 | { |
| 481 | timeToExpire=alsa.getLifeTime(); |
| 482 | } |
| 483 | scheduleAdjLsaExpiration(pnlsr,alsa.getAdjLsaKey(), |
| 484 | alsa.getLsSeqNo(),timeToExpire); |
| 485 | |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | if ( chkAdjLsa.first.getLsSeqNo() < alsa.getLsSeqNo() ) |
| 490 | { |
| 491 | chkAdjLsa.first.setLsSeqNo(alsa.getLsSeqNo()); |
| 492 | chkAdjLsa.first.setLifeTime(alsa.getLifeTime()); |
| 493 | |
| 494 | if ( ! chkAdjLsa.first.isLsaContentEqual(alsa)) |
| 495 | { |
| 496 | chkAdjLsa.first.getAdl().resetAdl(); |
| 497 | chkAdjLsa.first.getAdl().addAdjacentsFromAdl(alsa.getAdl()); |
| 498 | pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr); |
| 499 | } |
| 500 | |
| 501 | if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 502 | { |
| 503 | timeToExpire=alsa.getLifeTime(); |
| 504 | } |
akmhoque | 85d8833 | 2014-02-17 21:11:21 -0600 | [diff] [blame] | 505 | cancelScheduleLsaExpiringEvent(pnlsr, |
| 506 | chkAdjLsa.first.getLsaExpiringEventId()); |
| 507 | chkAdjLsa.first.setLsaExpiringEventId(scheduleAdjLsaExpiration(pnlsr, |
| 508 | alsa.getAdjLsaKey(), alsa.getLsSeqNo(),timeToExpire)); |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | } |
| 512 | |
| 513 | printAdjLsdb(); |
| 514 | |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 519 | Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 520 | { |
| 521 | AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 522 | , 2 |
| 523 | , pnlsr.getSm().getAdjLsaSeq()+1 |
| 524 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 525 | , pnlsr.getAdl().getNumOfActiveNeighbor() |
| 526 | , pnlsr.getAdl() ); |
| 527 | pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1); |
| 528 | return installAdjLsa(pnlsr, adjLsa); |
| 529 | } |
| 530 | |
| 531 | bool |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 532 | Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 533 | { |
| 534 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 535 | adjLsdb.end(), |
| 536 | bind(adjLsaCompareByKey, _1, key)); |
| 537 | if ( it != adjLsdb.end() ) |
| 538 | { |
| 539 | (*it).removeNptEntriesForAdjLsa(pnlsr); |
| 540 | adjLsdb.erase(it); |
| 541 | return true; |
| 542 | } |
| 543 | return false; |
| 544 | |
| 545 | } |
| 546 | |
| 547 | bool |
| 548 | Lsdb::doesAdjLsaExist(string key) |
| 549 | { |
| 550 | std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 551 | adjLsdb.end(), |
| 552 | bind(adjLsaCompareByKey, _1, key)); |
| 553 | |
| 554 | if( it == adjLsdb.end()){ |
| 555 | return false; |
| 556 | } |
| 557 | |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | std::list<AdjLsa>& |
| 562 | Lsdb::getAdjLsdb() |
| 563 | { |
| 564 | return adjLsdb; |
| 565 | } |
| 566 | |
| 567 | void |
| 568 | Lsdb::setLsaRefreshTime(int lrt) |
| 569 | { |
| 570 | lsaRefreshTime=lrt; |
| 571 | } |
| 572 | |
| 573 | void |
| 574 | Lsdb::setThisRouterPrefix(string trp) |
| 575 | { |
| 576 | thisRouterPrefix=trp; |
| 577 | } |
| 578 | |
| 579 | void |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 580 | Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, int seqNo) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 581 | { |
| 582 | cout<<"Lsdb::exprireOrRefreshNameLsa Called "<<endl; |
| 583 | cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl; |
| 584 | std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(lsaKey); |
| 585 | if( chkNameLsa.second ) |
| 586 | { |
| 587 | cout<<" LSA Exists with seq no: "<<chkNameLsa.first.getLsSeqNo()<<endl; |
| 588 | if ( chkNameLsa.first.getLsSeqNo() == seqNo ) |
| 589 | { |
| 590 | if(chkNameLsa.first.getOrigRouter() == thisRouterPrefix ) |
| 591 | { |
| 592 | cout<<"Own Name LSA, so refreshing name LSA"<<endl; |
| 593 | chkNameLsa.first.setLsSeqNo(chkNameLsa.first.getLsSeqNo()+1); |
| 594 | pnlsr.getSm().setNameLsaSeq(chkNameLsa.first.getLsSeqNo()); |
| 595 | // publish routing update |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | cout<<"Other's Name LSA, so removing form LSDB"<<endl; |
| 600 | removeNameLsa(pnlsr, lsaKey); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | void |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 607 | Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, int seqNo) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 608 | { |
| 609 | cout<<"Lsdb::exprireOrRefreshAdjLsa Called "<<endl; |
| 610 | cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl; |
| 611 | std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(lsaKey); |
| 612 | if( chkAdjLsa.second ) |
| 613 | { |
| 614 | cout<<" LSA Exists with seq no: "<<chkAdjLsa.first.getLsSeqNo()<<endl; |
| 615 | if ( chkAdjLsa.first.getLsSeqNo() == seqNo ) |
| 616 | { |
| 617 | if(chkAdjLsa.first.getOrigRouter() == thisRouterPrefix ) |
| 618 | { |
| 619 | cout<<"Own Adj LSA, so refreshing Adj LSA"<<endl; |
| 620 | chkAdjLsa.first.setLsSeqNo(chkAdjLsa.first.getLsSeqNo()+1); |
| 621 | pnlsr.getSm().setAdjLsaSeq(chkAdjLsa.first.getLsSeqNo()); |
| 622 | // publish routing update |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | cout<<"Other's Adj LSA, so removing form LSDB"<<endl; |
| 627 | removeAdjLsa(pnlsr, lsaKey); |
| 628 | } |
| 629 | |
| 630 | // schedule Routing table calculaiton |
| 631 | pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr); |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | void |
akmhoque | 1a48109 | 2014-02-19 16:34:22 -0600 | [diff] [blame] | 637 | Lsdb::exprireOrRefreshCorLsa(Nlsr& pnlsr, string lsaKey, int seqNo) |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 638 | { |
| 639 | cout<<"Lsdb::exprireOrRefreshCorLsa Called "<<endl; |
| 640 | cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl; |
| 641 | std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(lsaKey); |
| 642 | if( chkCorLsa.second ) |
| 643 | { |
| 644 | cout<<" LSA Exists with seq no: "<<chkCorLsa.first.getLsSeqNo()<<endl; |
| 645 | if ( chkCorLsa.first.getLsSeqNo() == seqNo ) |
| 646 | { |
| 647 | if(chkCorLsa.first.getOrigRouter() == thisRouterPrefix ) |
| 648 | { |
| 649 | cout<<"Own Cor LSA, so refreshing Cor LSA"<<endl; |
| 650 | chkCorLsa.first.setLsSeqNo(chkCorLsa.first.getLsSeqNo()+1); |
| 651 | pnlsr.getSm().setCorLsaSeq(chkCorLsa.first.getLsSeqNo()); |
| 652 | // publish routing update |
| 653 | } |
| 654 | else |
| 655 | { |
| 656 | cout<<"Other's Cor LSA, so removing form LSDB"<<endl; |
| 657 | removeCorLsa(pnlsr, lsaKey); |
| 658 | } |
| 659 | |
| 660 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 ) |
| 661 | { |
| 662 | pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | |
| 669 | void |
| 670 | Lsdb::printAdjLsdb() |
| 671 | { |
| 672 | cout<<"---------------Adj LSDB-------------------"<<endl; |
| 673 | for( std::list<AdjLsa>::iterator it=adjLsdb.begin(); |
| 674 | it!= adjLsdb.end() ; it++) |
| 675 | { |
| 676 | cout<< (*it) <<endl; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | //-----utility function ----- |
| 681 | bool |
| 682 | Lsdb::doesLsaExist(string key, int lsType) |
| 683 | { |
| 684 | if ( lsType == 1) |
| 685 | { |
| 686 | return doesNameLsaExist(key); |
| 687 | } |
| 688 | else if ( lsType == 2) |
| 689 | { |
| 690 | return doesAdjLsaExist(key); |
| 691 | } |
| 692 | else if ( lsType == 3) |
| 693 | { |
| 694 | return doesCorLsaExist(key); |
| 695 | } |
| 696 | |
| 697 | return false; |
| 698 | } |
| 699 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame^] | 700 | }//namespace nlsr |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 701 | |