akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 1 | #include<string> |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 2 | #include<utility> |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 3 | #include "nlsr_lsdb.hpp" |
| 4 | #include "nlsr.hpp" |
| 5 | |
| 6 | using namespace std; |
| 7 | |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 8 | static bool |
| 9 | nameLsaCompareByKey(NameLsa& nlsa1, string& key){ |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame] | 10 | return nlsa1.getNameLsaKey()==key; |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 11 | } |
| 12 | |
| 13 | |
| 14 | bool |
| 15 | Lsdb::buildAndInstallOwnNameLsa(nlsr& pnlsr) |
| 16 | { |
| 17 | NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 18 | , 1 |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 19 | , pnlsr.getSm().getNameLsaSeq()+1 |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 20 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 21 | , pnlsr.getNpl() ); |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 22 | pnlsr.getSm().setNameLsaSeq(pnlsr.getSm().getNameLsaSeq()+1); |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 23 | return installNameLsa(pnlsr,nameLsa); |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 24 | |
| 25 | } |
| 26 | |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 27 | std::pair<NameLsa&, bool> |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 28 | Lsdb::getNameLsa(string key) |
| 29 | { |
| 30 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 31 | nameLsdb.end(), |
| 32 | bind(nameLsaCompareByKey, _1, key)); |
| 33 | |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 34 | if( it != nameLsdb.end()) |
| 35 | { |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 36 | return std::make_pair(boost::ref((*it)),true); |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 37 | } |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 38 | |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 39 | NameLsa nlsa; |
| 40 | return std::make_pair(boost::ref(nlsa),false); |
| 41 | |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | bool |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 47 | Lsdb::installNameLsa(nlsr& pnlsr, NameLsa &nlsa) |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 48 | { |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 49 | std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(nlsa.getNameLsaKey()); |
| 50 | if ( !chkNameLsa.second ) |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 51 | { |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 52 | addNameLsa(nlsa); |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 53 | printNameLsdb(); |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 54 | if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 55 | { |
| 56 | pnlsr.getNpt().addNpte(nlsa.getOrigRouter(),nlsa.getOrigRouter(),pnlsr); |
| 57 | std::list<string> nameList=nlsa.getNpl().getNameList(); |
| 58 | for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end();it++) |
| 59 | { |
| 60 | pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr); |
| 61 | } |
| 62 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 63 | } |
| 64 | else |
| 65 | { |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 66 | if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() ) |
| 67 | { |
| 68 | chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo()); |
| 69 | chkNameLsa.first.setLifeTime(nlsa.getLifeTime()); |
| 70 | |
| 71 | chkNameLsa.first.getNpl().sortNpl(); |
| 72 | nlsa.getNpl().sortNpl(); |
| 73 | |
| 74 | std::list<string> nameToAdd; |
| 75 | std::set_difference(nlsa.getNpl().getNameList().begin(), |
| 76 | nlsa.getNpl().getNameList().end(), |
| 77 | chkNameLsa.first.getNpl().getNameList().begin(), |
| 78 | chkNameLsa.first.getNpl().getNameList().end(), |
| 79 | std::inserter(nameToAdd, nameToAdd.begin())); |
| 80 | for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end(); |
| 81 | ++it) |
| 82 | { |
| 83 | chkNameLsa.first.addNameToLsa((*it)); |
| 84 | if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 85 | { |
| 86 | pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | std::list<string> nameToRemove; |
| 91 | std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(), |
| 92 | chkNameLsa.first.getNpl().getNameList().end(), |
| 93 | nlsa.getNpl().getNameList().begin(), |
| 94 | nlsa.getNpl().getNameList().end(), |
| 95 | std::inserter(nameToRemove, nameToRemove.begin())); |
| 96 | for(std::list<string>::iterator it=nameToRemove.begin(); |
| 97 | it!=nameToRemove.end(); ++it) |
| 98 | { |
| 99 | chkNameLsa.first.removeNameFromLsa((*it)); |
| 100 | if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 101 | { |
| 102 | pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | bool |
| 113 | Lsdb::addNameLsa(NameLsa &nlsa) |
| 114 | { |
| 115 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 116 | nameLsdb.end(), |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame] | 117 | bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey())); |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 118 | |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 119 | if( it == nameLsdb.end()) |
| 120 | { |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 121 | nameLsdb.push_back(nlsa); |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool |
| 128 | Lsdb::removeNameLsa(string& key) |
| 129 | { |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 130 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 131 | nameLsdb.end(), |
| 132 | bind(nameLsaCompareByKey, _1, key)); |
| 133 | if ( it != nameLsdb.end() ) |
| 134 | { |
| 135 | nameLsdb.erase(it); |
| 136 | return true; |
| 137 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 138 | return false; |
| 139 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 140 | |
| 141 | bool |
| 142 | Lsdb::doesNameLsaExist(string key) |
| 143 | { |
| 144 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 145 | nameLsdb.end(), |
| 146 | bind(nameLsaCompareByKey, _1, key)); |
| 147 | |
| 148 | if( it == nameLsdb.end()){ |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 155 | void |
| 156 | Lsdb::printNameLsdb() |
| 157 | { |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 158 | cout<<"---------------Name LSDB-------------------"<<endl; |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 159 | for( std::list<NameLsa>::iterator it=nameLsdb.begin(); |
| 160 | it!= nameLsdb.end() ; it++) |
| 161 | { |
| 162 | cout<< (*it) <<endl; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Cor LSA and LSDB related Functions start here |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 167 | /* |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 168 | static bool |
| 169 | corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){ |
| 170 | return clsa1.getLsaKey()==clsa1.getLsaKey(); |
| 171 | } |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 172 | */ |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 173 | static bool |
| 174 | corLsaCompareByKey(CorLsa& clsa, string& key){ |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame] | 175 | return clsa.getCorLsaKey()==key; |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 176 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 177 | |
| 178 | bool |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 179 | Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){ |
| 180 | CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix() |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 181 | , 3 |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 182 | , pnlsr.getSm().getCorLsaSeq()+1 |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 183 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 184 | , pnlsr.getConfParameter().getCorR() |
| 185 | , pnlsr.getConfParameter().getCorTheta() ); |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 186 | pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1); |
akmhoque | f7c2c7c | 2014-02-06 11:32:43 -0600 | [diff] [blame] | 187 | installCorLsa(pnlsr, corLsa); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 188 | |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 189 | return true; |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 190 | } |
| 191 | |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 192 | std::pair<CorLsa&, bool> |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 193 | Lsdb::getCorLsa(string key) |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 194 | { |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 195 | std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 196 | corLsdb.end(), |
| 197 | bind(corLsaCompareByKey, _1, key)); |
| 198 | |
| 199 | if( it != corLsdb.end()){ |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 200 | return std::make_pair(boost::ref((*it)), true); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 201 | } |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 202 | |
| 203 | CorLsa clsa; |
| 204 | return std::make_pair(boost::ref(clsa),false); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | bool |
akmhoque | f7c2c7c | 2014-02-06 11:32:43 -0600 | [diff] [blame] | 208 | Lsdb::installCorLsa(nlsr& pnlsr, CorLsa &clsa) |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 209 | { |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 210 | std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey()); |
| 211 | if ( !chkCorLsa.second ) |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 212 | { |
| 213 | // add cor LSA |
| 214 | addCorLsa(clsa); |
akmhoque | f7c2c7c | 2014-02-06 11:32:43 -0600 | [diff] [blame] | 215 | printCorLsdb(); //debugging purpose |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 216 | if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 217 | { |
| 218 | pnlsr.getNpt().addNpte(clsa.getOrigRouter(),clsa.getOrigRouter(),pnlsr); |
| 219 | } |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 220 | //schedule routing table calculation only if |
| 221 | //hyperbolic calculation is scheduled |
akmhoque | f7c2c7c | 2014-02-06 11:32:43 -0600 | [diff] [blame] | 222 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 ) |
| 223 | { |
| 224 | if ( pnlsr.getIsRouteCalculationScheduled() != 1 ) |
| 225 | { |
| 226 | pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15), |
| 227 | ndn::bind(&RoutingTable::calculate, |
| 228 | &pnlsr.getRoutingTable(),boost::ref(pnlsr))); |
| 229 | pnlsr.setIsRouteCalculationScheduled(1); |
| 230 | } |
| 231 | } |
| 232 | |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | { |
| 236 | // check for newer cor LSA |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 237 | //CorLsa oldCorLsa=getCorLsa(clsa.getCorLsaKey()); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 238 | |
| 239 | } |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | bool |
| 245 | Lsdb::addCorLsa(CorLsa& clsa) |
| 246 | { |
| 247 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 248 | corLsdb.end(), |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame] | 249 | bind(corLsaCompareByKey, _1, clsa.getCorLsaKey())); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 250 | |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 251 | if( it == corLsdb.end()) |
| 252 | { |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 253 | corLsdb.push_back(clsa); |
| 254 | return true; |
| 255 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | |
| 259 | bool |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 260 | Lsdb::removeCorLsa(string& key) |
| 261 | { |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 262 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 263 | corLsdb.end(), |
| 264 | bind(corLsaCompareByKey, _1, key)); |
| 265 | if ( it != corLsdb.end() ) |
| 266 | { |
| 267 | corLsdb.erase(it); |
| 268 | return true; |
| 269 | } |
| 270 | return false; |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 271 | |
| 272 | } |
| 273 | |
| 274 | bool |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 275 | Lsdb::doesCorLsaExist(string key) |
| 276 | { |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 277 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 278 | corLsdb.end(), |
| 279 | bind(corLsaCompareByKey, _1, key)); |
| 280 | |
| 281 | if( it == corLsdb.end()){ |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | return true; |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 286 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 287 | |
| 288 | void |
| 289 | Lsdb::printCorLsdb() //debugging |
| 290 | { |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 291 | cout<<"---------------Cor LSDB-------------------"<<endl; |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 292 | for( std::list<CorLsa>::iterator it=corLsdb.begin(); |
| 293 | it!= corLsdb.end() ; it++) |
| 294 | { |
| 295 | cout<< (*it) <<endl; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | |
| 300 | // Adj LSA and LSDB related function starts here |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 301 | /* |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 302 | static bool |
| 303 | adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){ |
| 304 | return alsa1.getLsaKey()==alsa1.getLsaKey(); |
| 305 | } |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 306 | */ |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 307 | static bool |
| 308 | adjLsaCompareByKey(AdjLsa& alsa, string& key){ |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame] | 309 | return alsa.getAdjLsaKey()==key; |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 313 | void |
| 314 | Lsdb::scheduledAdjLsaBuild(nlsr& pnlsr) |
| 315 | { |
| 316 | cout<<"scheduledAdjLsaBuild Called"<<endl; |
| 317 | pnlsr.setIsBuildAdjLsaSheduled(0); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 318 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 319 | if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr)) |
| 320 | { |
| 321 | int adjBuildCount=pnlsr.getAdjBuildCount(); |
| 322 | if(adjBuildCount>0 ) |
| 323 | { |
| 324 | if (pnlsr.getAdl().getNumOfActiveNeighbor()>0) |
| 325 | { |
| 326 | buildAndInstallOwnAdjLsa(pnlsr); |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | //remove if there is any adj lsa in LSDB |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 331 | string key=pnlsr.getConfParameter().getRouterPrefix()+"/2"; |
| 332 | removeAdjLsa(key); |
| 333 | // Remove alll fib entries as per NPT |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 334 | } |
| 335 | pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount); |
| 336 | } |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | pnlsr.setIsBuildAdjLsaSheduled(1); |
| 341 | int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()* |
| 342 | pnlsr.getConfParameter().getInterestRetryNumber(); |
| 343 | pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime), |
| 344 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(), |
| 345 | boost::ref(pnlsr))); |
| 346 | } |
| 347 | |
| 348 | } |
| 349 | |
| 350 | |
| 351 | bool |
| 352 | Lsdb::addAdjLsa(AdjLsa &alsa) |
| 353 | { |
| 354 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 355 | adjLsdb.end(), |
akmhoque | 3fdf761 | 2014-02-04 21:18:23 -0600 | [diff] [blame] | 356 | bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey())); |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 357 | |
| 358 | if( it == adjLsdb.end()){ |
| 359 | adjLsdb.push_back(alsa); |
| 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | |
| 364 | } |
| 365 | |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 366 | std::pair<AdjLsa& , bool> |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 367 | Lsdb::getAdjLsa(string key) |
| 368 | { |
| 369 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 370 | adjLsdb.end(), |
| 371 | bind(adjLsaCompareByKey, _1, key)); |
| 372 | |
| 373 | if( it != adjLsdb.end()){ |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 374 | return std::make_pair(boost::ref((*it)),true); |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 375 | } |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 376 | |
| 377 | AdjLsa alsa; |
| 378 | return std::make_pair(boost::ref(alsa),false); |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | bool |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 382 | Lsdb::installAdjLsa(nlsr& pnlsr, AdjLsa &alsa) |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 383 | { |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 384 | //bool doesLsaExist_ = doesAdjLsaExist(alsa.getAdjLsaKey()); |
| 385 | //if ( !doesLsaExist_ ) |
| 386 | std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey()); |
| 387 | if ( !chkAdjLsa.second ) |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 388 | { |
| 389 | // add Adj LSA |
| 390 | addAdjLsa(alsa); |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 391 | // adding a NPT entry for router itself |
| 392 | if ( alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 393 | { |
| 394 | pnlsr.getNpt().addNpte(alsa.getOrigRouter(),alsa.getOrigRouter(),pnlsr); |
| 395 | } |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 396 | // schedule routing table calculation |
akmhoque | 79d355f | 2014-02-04 15:11:16 -0600 | [diff] [blame] | 397 | if ( pnlsr.getIsRouteCalculationScheduled() != 1 ) |
| 398 | { |
| 399 | pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15), |
| 400 | ndn::bind(&RoutingTable::calculate, |
| 401 | &pnlsr.getRoutingTable(),boost::ref(pnlsr))); |
| 402 | pnlsr.setIsRouteCalculationScheduled(1); |
| 403 | } |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 404 | } |
| 405 | else |
| 406 | { |
| 407 | // check for newer name LSA |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 408 | //AdjLsa oldAdjLsa=getAdjLsa(alsa.getAdjLsaKey()); |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 409 | |
| 410 | } |
| 411 | |
| 412 | printAdjLsdb(); |
| 413 | |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | bool |
| 418 | Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr) |
| 419 | { |
| 420 | AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 421 | , 2 |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 422 | , pnlsr.getSm().getAdjLsaSeq()+1 |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 423 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 424 | , pnlsr.getAdl().getNumOfActiveNeighbor() |
| 425 | , pnlsr.getAdl() ); |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 426 | pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1); |
| 427 | return installAdjLsa(pnlsr, adjLsa); |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | bool |
| 431 | Lsdb::removeAdjLsa(string& key) |
| 432 | { |
akmhoque | fcf765d | 2014-02-01 23:46:17 -0600 | [diff] [blame] | 433 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 434 | adjLsdb.end(), |
| 435 | bind(adjLsaCompareByKey, _1, key)); |
| 436 | if ( it != adjLsdb.end() ) |
| 437 | { |
| 438 | adjLsdb.erase(it); |
| 439 | return true; |
| 440 | } |
| 441 | return false; |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 442 | |
| 443 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 444 | |
| 445 | bool |
| 446 | Lsdb::doesAdjLsaExist(string key) |
| 447 | { |
| 448 | std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 449 | adjLsdb.end(), |
| 450 | bind(adjLsaCompareByKey, _1, key)); |
| 451 | |
| 452 | if( it == adjLsdb.end()){ |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
akmhoque | 79d355f | 2014-02-04 15:11:16 -0600 | [diff] [blame] | 459 | std::list<AdjLsa>& |
| 460 | Lsdb::getAdjLsdb() |
| 461 | { |
| 462 | return adjLsdb; |
| 463 | } |
| 464 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 465 | void |
| 466 | Lsdb::printAdjLsdb() |
| 467 | { |
akmhoque | dfa4a5b | 2014-02-03 20:12:29 -0600 | [diff] [blame] | 468 | cout<<"---------------Adj LSDB-------------------"<<endl; |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame] | 469 | for( std::list<AdjLsa>::iterator it=adjLsdb.begin(); |
| 470 | it!= adjLsdb.end() ; it++) |
| 471 | { |
| 472 | cout<< (*it) <<endl; |
| 473 | } |
| 474 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 475 | |
akmhoque | 62a8e40 | 2014-02-08 12:00:27 -0600 | [diff] [blame^] | 476 | //-----utility function ----- |
| 477 | bool |
| 478 | Lsdb::doesLsaExist(string key, int lsType) |
| 479 | { |
| 480 | if ( lsType == 1) |
| 481 | { |
| 482 | return doesNameLsaExist(key); |
| 483 | } |
| 484 | else if ( lsType == 2) |
| 485 | { |
| 486 | return doesAdjLsaExist(key); |
| 487 | } |
| 488 | else if ( lsType == 3) |
| 489 | { |
| 490 | return doesCorLsaExist(key); |
| 491 | } |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | |