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