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