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