akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 1 | #include<string> |
| 2 | #include "nlsr_lsdb.hpp" |
| 3 | #include "nlsr.hpp" |
| 4 | |
| 5 | using namespace std; |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | bool |
| 12 | Lsdb::doesLsaExist(string key, int lsType) |
| 13 | { |
| 14 | if ( lsType == 1) |
| 15 | { |
| 16 | return doesNameLsaExist(key); |
| 17 | } |
| 18 | else if ( lsType == 2) |
| 19 | { |
| 20 | return doesAdjLsaExist(key); |
| 21 | } |
| 22 | else if ( lsType == 3) |
| 23 | { |
| 24 | return doesCorLsaExist(key); |
| 25 | } |
| 26 | |
| 27 | return false; |
| 28 | |
| 29 | } |
| 30 | |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 31 | //Name LSA and LSDB related functions start here |
| 32 | |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 33 | static bool |
| 34 | nameLsaCompare(NameLsa& nlsa1, NameLsa& nlsa2){ |
| 35 | return nlsa1.getLsaKey()==nlsa1.getLsaKey(); |
| 36 | } |
| 37 | |
| 38 | static bool |
| 39 | nameLsaCompareByKey(NameLsa& nlsa1, string& key){ |
| 40 | return nlsa1.getLsaKey()==key; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | bool |
| 45 | Lsdb::buildAndInstallOwnNameLsa(nlsr& pnlsr) |
| 46 | { |
| 47 | NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 48 | , 1 |
| 49 | , pnlsr.getNameLsaSeq()+1 |
| 50 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 51 | , pnlsr.getNpl() ); |
| 52 | pnlsr.setNameLsaSeq(pnlsr.getNameLsaSeq()+1); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 53 | //cout<<nameLsa; |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 54 | return installNameLsa(nameLsa); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | NameLsa& |
| 59 | Lsdb::getNameLsa(string key) |
| 60 | { |
| 61 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 62 | nameLsdb.end(), |
| 63 | bind(nameLsaCompareByKey, _1, key)); |
| 64 | |
| 65 | if( it != nameLsdb.end()){ |
| 66 | return (*it); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | bool |
| 73 | Lsdb::installNameLsa(NameLsa &nlsa) |
| 74 | { |
| 75 | bool doesLsaExist_ = doesNameLsaExist(nlsa.getLsaKey()); |
| 76 | if ( !doesLsaExist_ ) |
| 77 | { |
| 78 | // add name LSA |
| 79 | addNameLsa(nlsa); |
| 80 | // update NPT and FIB |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | // check for newer name LSA |
| 85 | NameLsa oldNameLsa=getNameLsa(nlsa.getLsaKey()); |
| 86 | // Discard or Update Name lsa, NPT, FIB |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | Lsdb::addNameLsa(NameLsa &nlsa) |
| 94 | { |
| 95 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 96 | nameLsdb.end(), |
| 97 | bind(nameLsaCompare, _1, nlsa)); |
| 98 | |
| 99 | if( it == nameLsdb.end()){ |
| 100 | nameLsdb.push_back(nlsa); |
| 101 | return true; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | bool |
| 107 | Lsdb::removeNameLsa(string& key) |
| 108 | { |
| 109 | return false; |
| 110 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 111 | |
| 112 | bool |
| 113 | Lsdb::doesNameLsaExist(string key) |
| 114 | { |
| 115 | std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(), |
| 116 | nameLsdb.end(), |
| 117 | bind(nameLsaCompareByKey, _1, key)); |
| 118 | |
| 119 | if( it == nameLsdb.end()){ |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 126 | void |
| 127 | Lsdb::printNameLsdb() |
| 128 | { |
| 129 | for( std::list<NameLsa>::iterator it=nameLsdb.begin(); |
| 130 | it!= nameLsdb.end() ; it++) |
| 131 | { |
| 132 | cout<< (*it) <<endl; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Cor LSA and LSDB related Functions start here |
| 137 | |
| 138 | static bool |
| 139 | corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){ |
| 140 | return clsa1.getLsaKey()==clsa1.getLsaKey(); |
| 141 | } |
| 142 | |
| 143 | static bool |
| 144 | corLsaCompareByKey(CorLsa& clsa, string& key){ |
| 145 | return clsa.getLsaKey()==key; |
| 146 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 147 | |
| 148 | bool |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 149 | Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){ |
| 150 | CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix() |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame^] | 151 | , 3 |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 152 | , pnlsr.getCorLsaSeq()+1 |
| 153 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 154 | , pnlsr.getConfParameter().getCorR() |
| 155 | , pnlsr.getConfParameter().getCorTheta() ); |
| 156 | pnlsr.setCorLsaSeq(pnlsr.getCorLsaSeq()+1); |
| 157 | //cout<<corLsa; |
| 158 | installCorLsa(corLsa); |
| 159 | |
| 160 | } |
| 161 | |
| 162 | CorLsa& |
| 163 | Lsdb::getCorLsa(string key) |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 164 | { |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 165 | std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 166 | corLsdb.end(), |
| 167 | bind(corLsaCompareByKey, _1, key)); |
| 168 | |
| 169 | if( it != corLsdb.end()){ |
| 170 | return (*it); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | bool |
| 175 | Lsdb::installCorLsa(CorLsa &clsa) |
| 176 | { |
| 177 | bool doesLsaExist_ = doesCorLsaExist(clsa.getLsaKey()); |
| 178 | if ( !doesLsaExist_ ) |
| 179 | { |
| 180 | // add cor LSA |
| 181 | addCorLsa(clsa); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | // check for newer cor LSA |
| 186 | CorLsa oldCorLsa=getCorLsa(clsa.getLsaKey()); |
| 187 | |
| 188 | } |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool |
| 194 | Lsdb::addCorLsa(CorLsa& clsa) |
| 195 | { |
| 196 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 197 | corLsdb.end(), |
| 198 | bind(corLsaCompare, _1, clsa)); |
| 199 | |
| 200 | if( it == corLsdb.end()){ |
| 201 | corLsdb.push_back(clsa); |
| 202 | return true; |
| 203 | } |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | |
| 207 | bool |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 208 | Lsdb::removeCorLsa(string& key) |
| 209 | { |
| 210 | |
| 211 | } |
| 212 | |
| 213 | bool |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 214 | Lsdb::doesCorLsaExist(string key) |
| 215 | { |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 216 | std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(), |
| 217 | corLsdb.end(), |
| 218 | bind(corLsaCompareByKey, _1, key)); |
| 219 | |
| 220 | if( it == corLsdb.end()){ |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | return true; |
akmhoque | bd7c8e6 | 2014-02-01 14:57:40 -0600 | [diff] [blame] | 225 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 226 | |
| 227 | void |
| 228 | Lsdb::printCorLsdb() //debugging |
| 229 | { |
| 230 | for( std::list<CorLsa>::iterator it=corLsdb.begin(); |
| 231 | it!= corLsdb.end() ; it++) |
| 232 | { |
| 233 | cout<< (*it) <<endl; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | |
| 238 | // Adj LSA and LSDB related function starts here |
| 239 | |
| 240 | static bool |
| 241 | adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){ |
| 242 | return alsa1.getLsaKey()==alsa1.getLsaKey(); |
| 243 | } |
| 244 | |
| 245 | static bool |
| 246 | adjLsaCompareByKey(AdjLsa& alsa, string& key){ |
| 247 | return alsa.getLsaKey()==key; |
| 248 | } |
| 249 | |
| 250 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame^] | 251 | void |
| 252 | Lsdb::scheduledAdjLsaBuild(nlsr& pnlsr) |
| 253 | { |
| 254 | cout<<"scheduledAdjLsaBuild Called"<<endl; |
| 255 | pnlsr.setIsBuildAdjLsaSheduled(0); |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 256 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame^] | 257 | if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr)) |
| 258 | { |
| 259 | int adjBuildCount=pnlsr.getAdjBuildCount(); |
| 260 | if(adjBuildCount>0 ) |
| 261 | { |
| 262 | if (pnlsr.getAdl().getNumOfActiveNeighbor()>0) |
| 263 | { |
| 264 | buildAndInstallOwnAdjLsa(pnlsr); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | //remove if there is any adj lsa in LSDB |
| 269 | } |
| 270 | pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount); |
| 271 | } |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | pnlsr.setIsBuildAdjLsaSheduled(1); |
| 276 | int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()* |
| 277 | pnlsr.getConfParameter().getInterestRetryNumber(); |
| 278 | pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime), |
| 279 | ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(), |
| 280 | boost::ref(pnlsr))); |
| 281 | } |
| 282 | |
| 283 | } |
| 284 | |
| 285 | |
| 286 | bool |
| 287 | Lsdb::addAdjLsa(AdjLsa &alsa) |
| 288 | { |
| 289 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 290 | adjLsdb.end(), |
| 291 | bind(adjLsaCompare, _1, alsa)); |
| 292 | |
| 293 | if( it == adjLsdb.end()){ |
| 294 | adjLsdb.push_back(alsa); |
| 295 | return true; |
| 296 | } |
| 297 | return false; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | AdjLsa& |
| 302 | Lsdb::getAdjLsa(string key) |
| 303 | { |
| 304 | std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 305 | adjLsdb.end(), |
| 306 | bind(adjLsaCompareByKey, _1, key)); |
| 307 | |
| 308 | if( it != adjLsdb.end()){ |
| 309 | return (*it); |
| 310 | } |
| 311 | |
| 312 | } |
| 313 | |
| 314 | bool |
| 315 | Lsdb::installAdjLsa(AdjLsa &alsa) |
| 316 | { |
| 317 | bool doesLsaExist_ = doesAdjLsaExist(alsa.getLsaKey()); |
| 318 | if ( !doesLsaExist_ ) |
| 319 | { |
| 320 | // add Adj LSA |
| 321 | addAdjLsa(alsa); |
| 322 | // schedule routing table calculation |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | // check for newer name LSA |
| 327 | AdjLsa oldAdjLsa=getAdjLsa(alsa.getLsaKey()); |
| 328 | |
| 329 | } |
| 330 | |
| 331 | printAdjLsdb(); |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | bool |
| 337 | Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr) |
| 338 | { |
| 339 | AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix() |
| 340 | , 2 |
| 341 | , pnlsr.getAdjLsaSeq()+1 |
| 342 | , pnlsr.getConfParameter().getRouterDeadInterval() |
| 343 | , pnlsr.getAdl().getNumOfActiveNeighbor() |
| 344 | , pnlsr.getAdl() ); |
| 345 | pnlsr.setAdjLsaSeq(pnlsr.getAdjLsaSeq()+1); |
| 346 | return installAdjLsa(adjLsa); |
| 347 | } |
| 348 | |
| 349 | bool |
| 350 | Lsdb::removeAdjLsa(string& key) |
| 351 | { |
| 352 | |
| 353 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 354 | |
| 355 | bool |
| 356 | Lsdb::doesAdjLsaExist(string key) |
| 357 | { |
| 358 | std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(), |
| 359 | adjLsdb.end(), |
| 360 | bind(adjLsaCompareByKey, _1, key)); |
| 361 | |
| 362 | if( it == adjLsdb.end()){ |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | return true; |
| 367 | } |
| 368 | |
akmhoque | cd55247 | 2014-02-01 21:22:16 -0600 | [diff] [blame^] | 369 | void |
| 370 | Lsdb::printAdjLsdb() |
| 371 | { |
| 372 | for( std::list<AdjLsa>::iterator it=adjLsdb.begin(); |
| 373 | it!= adjLsdb.end() ; it++) |
| 374 | { |
| 375 | cout<< (*it) <<endl; |
| 376 | } |
| 377 | } |
akmhoque | 3c6bd92 | 2014-02-01 17:10:17 -0600 | [diff] [blame] | 378 | |