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