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