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