akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #include<string> |
| 2 | #include<iostream> |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 3 | #include<sstream> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 4 | #include<algorithm> |
| 5 | #include<cmath> |
| 6 | #include<limits> |
| 7 | |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 8 | #include "nlsr.hpp" |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 9 | #include "nlsr_lsa.hpp" |
| 10 | #include "nlsr_npl.hpp" |
| 11 | #include "nlsr_adjacent.hpp" |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 12 | #include "utility/nlsr_tokenizer.hpp" |
| 13 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 14 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 15 | namespace nlsr |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 16 | { |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 17 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 18 | using namespace std; |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 19 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 20 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 21 | string |
| 22 | NameLsa::getNameLsaKey() |
| 23 | { |
| 24 | string key; |
| 25 | key=origRouter + "/" + boost::lexical_cast<std::string>(1); |
| 26 | return key; |
| 27 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 28 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 29 | NameLsa::NameLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt, Npl npl) |
| 30 | { |
| 31 | origRouter=origR; |
| 32 | lsType=lst; |
| 33 | lsSeqNo=lsn; |
| 34 | lifeTime=lt; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 35 | std::list<string> nl=npl.getNameList(); |
| 36 | for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++) |
| 37 | { |
| 38 | addNameToLsa((*it)); |
| 39 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | string |
| 43 | NameLsa::getNameLsaData() |
| 44 | { |
| 45 | string nameLsaData; |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 46 | nameLsaData=origRouter + "|" + boost::lexical_cast<std::string>(1) + "|" |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 47 | + boost::lexical_cast<std::string>(lsSeqNo) + "|" |
| 48 | + boost::lexical_cast<std::string>(lifeTime); |
| 49 | nameLsaData+="|"; |
| 50 | nameLsaData+=boost::lexical_cast<std::string>(npl.getNplSize()); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 51 | std::list<string> nl=npl.getNameList(); |
| 52 | for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++) |
| 53 | { |
| 54 | nameLsaData+="|"; |
| 55 | nameLsaData+=(*it); |
| 56 | } |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 57 | return nameLsaData+"|"; |
| 58 | } |
| 59 | |
| 60 | bool |
| 61 | NameLsa::initNameLsaFromContent(string content) |
| 62 | { |
| 63 | uint32_t numName=0; |
| 64 | nlsrTokenizer nt(content, "|"); |
| 65 | origRouter=nt.getNextToken(); |
| 66 | if(origRouter.empty()) |
| 67 | { |
| 68 | return false; |
| 69 | } |
| 70 | try |
| 71 | { |
| 72 | lsType=boost::lexical_cast<uint8_t>(nt.getNextToken()); |
| 73 | lsSeqNo=boost::lexical_cast<uint32_t>(nt.getNextToken()); |
| 74 | lifeTime=boost::lexical_cast<uint32_t>(nt.getNextToken()); |
| 75 | numName=boost::lexical_cast<uint32_t>(nt.getNextToken()); |
| 76 | } |
| 77 | catch(std::exception &e) |
| 78 | { |
| 79 | return false; |
| 80 | } |
| 81 | for(int i=0; i<numName; i++) |
| 82 | { |
| 83 | string name=nt.getNextToken(); |
| 84 | addNameToLsa(name); |
| 85 | } |
| 86 | return true; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | std::ostream& |
| 90 | operator<<(std::ostream& os, NameLsa& nLsa) |
| 91 | { |
| 92 | os<<"Name Lsa: "<<endl; |
| 93 | os<<" Origination Router: "<<nLsa.getOrigRouter()<<endl; |
| 94 | os<<" Ls Type: "<<(unsigned short)nLsa.getLsType()<<endl; |
| 95 | os<<" Ls Seq No: "<<(unsigned int)nLsa.getLsSeqNo()<<endl; |
| 96 | os<<" Ls Lifetime: "<<(unsigned int)nLsa.getLifeTime()<<endl; |
| 97 | os<<" Names: "<<endl; |
| 98 | int i=1; |
| 99 | std::list<string> nl=nLsa.getNpl().getNameList(); |
| 100 | for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++) |
| 101 | { |
| 102 | os<<" Name "<<i<<": "<<(*it)<<endl; |
| 103 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 104 | return os; |
| 105 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 106 | |
| 107 | |
| 108 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 109 | CorLsa::CorLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt |
| 110 | , double r, double theta) |
| 111 | { |
| 112 | origRouter=origR; |
| 113 | lsType=lst; |
| 114 | lsSeqNo=lsn; |
| 115 | lifeTime=lt; |
| 116 | corRad=r; |
| 117 | corTheta=theta; |
| 118 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 119 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 120 | string |
| 121 | CorLsa::getCorLsaKey() |
| 122 | { |
| 123 | string key; |
| 124 | key=origRouter + "/" + boost::lexical_cast<std::string>(3); |
| 125 | return key; |
| 126 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 127 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 128 | bool |
| 129 | CorLsa::isLsaContentEqual(CorLsa& clsa) |
| 130 | { |
| 131 | return (std::abs(corRad - clsa.getCorRadius()) < |
| 132 | std::numeric_limits<double>::epsilon()) && |
| 133 | (std::abs(corTheta - clsa.getCorTheta()) < |
| 134 | std::numeric_limits<double>::epsilon()); |
| 135 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 136 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 137 | string |
| 138 | CorLsa::getCorLsaData() |
| 139 | { |
| 140 | string corLsaData; |
| 141 | corLsaData=origRouter + "|"; |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 142 | corLsaData+=(boost::lexical_cast<std::string>(3) + "|"); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 143 | corLsaData+=(boost::lexical_cast<std::string>(lsSeqNo) + "|"); |
| 144 | corLsaData+=(boost::lexical_cast<std::string>(lifeTime) + "|"); |
| 145 | corLsaData+=(boost::lexical_cast<std::string>(corRad) + "|"); |
| 146 | corLsaData+=(boost::lexical_cast<std::string>(corTheta) + "|"); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 147 | return corLsaData; |
| 148 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 149 | |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 150 | bool |
| 151 | CorLsa::initCorLsaFromContent(string content) |
| 152 | { |
| 153 | nlsrTokenizer nt(content, "|"); |
| 154 | origRouter=nt.getNextToken(); |
| 155 | if(origRouter.empty()) |
| 156 | { |
| 157 | return false; |
| 158 | } |
| 159 | try |
| 160 | { |
| 161 | lsType=boost::lexical_cast<uint8_t>(nt.getNextToken()); |
| 162 | lsSeqNo=boost::lexical_cast<uint32_t>(nt.getNextToken()); |
| 163 | lifeTime=boost::lexical_cast<uint32_t>(nt.getNextToken()); |
| 164 | corRad=boost::lexical_cast<double>(nt.getNextToken()); |
| 165 | corTheta=boost::lexical_cast<double>(nt.getNextToken()); |
| 166 | } |
| 167 | catch(std::exception &e) |
| 168 | { |
| 169 | return false; |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 174 | std::ostream& |
| 175 | operator<<(std::ostream& os, CorLsa& cLsa) |
| 176 | { |
| 177 | os<<"Cor Lsa: "<<endl; |
| 178 | os<<" Origination Router: "<<cLsa.getOrigRouter()<<endl; |
| 179 | os<<" Ls Type: "<<(unsigned short)cLsa.getLsType()<<endl; |
| 180 | os<<" Ls Seq No: "<<(unsigned int)cLsa.getLsSeqNo()<<endl; |
| 181 | os<<" Ls Lifetime: "<<(unsigned int)cLsa.getLifeTime()<<endl; |
| 182 | os<<" Hyperbolic Radius: "<<cLsa.getCorRadius()<<endl; |
| 183 | os<<" Hyperbolic Theta: "<<cLsa.getCorTheta()<<endl; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 184 | return os; |
| 185 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 186 | |
| 187 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 188 | AdjLsa::AdjLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt, |
| 189 | uint32_t nl ,Adl padl) |
| 190 | { |
| 191 | origRouter=origR; |
| 192 | lsType=lst; |
| 193 | lsSeqNo=lsn; |
| 194 | lifeTime=lt; |
| 195 | noLink=nl; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 196 | std::list<Adjacent> al=padl.getAdjList(); |
| 197 | for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++) |
| 198 | { |
| 199 | if((*it).getStatus()==1) |
| 200 | { |
| 201 | addAdjacentToLsa((*it)); |
| 202 | } |
| 203 | } |
| 204 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 205 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 206 | string |
| 207 | AdjLsa::getAdjLsaKey() |
| 208 | { |
| 209 | string key; |
| 210 | key=origRouter + "/" + boost::lexical_cast<std::string>(2); |
| 211 | return key; |
| 212 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 213 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 214 | bool |
| 215 | AdjLsa::isLsaContentEqual(AdjLsa& alsa) |
| 216 | { |
| 217 | return adl.isAdlEqual(alsa.getAdl()); |
| 218 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 219 | |
| 220 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 221 | string |
| 222 | AdjLsa::getAdjLsaData() |
| 223 | { |
| 224 | string adjLsaData; |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 225 | adjLsaData=origRouter + "|" + boost::lexical_cast<std::string>(2) + "|" |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 226 | + boost::lexical_cast<std::string>(lsSeqNo) + "|" |
| 227 | + boost::lexical_cast<std::string>(lifeTime); |
| 228 | adjLsaData+="|"; |
| 229 | adjLsaData+=boost::lexical_cast<std::string>(adl.getAdlSize()); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 230 | std::list<Adjacent> al=adl.getAdjList(); |
| 231 | for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++) |
| 232 | { |
| 233 | adjLsaData+="|"; |
| 234 | adjLsaData+=(*it).getAdjacentName(); |
| 235 | adjLsaData+="|"; |
| 236 | adjLsaData+=boost::lexical_cast<std::string>((*it).getConnectingFace()); |
| 237 | adjLsaData+="|"; |
| 238 | adjLsaData+=boost::lexical_cast<std::string>((*it).getLinkCost()); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 239 | } |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame] | 240 | return adjLsaData+"|"; |
| 241 | } |
| 242 | |
| 243 | bool |
| 244 | AdjLsa::initAdjLsaFromContent(string content) |
| 245 | { |
| 246 | uint32_t numLink=0; |
| 247 | nlsrTokenizer nt(content, "|"); |
| 248 | origRouter=nt.getNextToken(); |
| 249 | if(origRouter.empty()) |
| 250 | { |
| 251 | return false; |
| 252 | } |
| 253 | try |
| 254 | { |
| 255 | lsType=boost::lexical_cast<uint8_t>(nt.getNextToken()); |
| 256 | lsSeqNo=boost::lexical_cast<uint32_t>(nt.getNextToken()); |
| 257 | lifeTime=boost::lexical_cast<uint32_t>(nt.getNextToken()); |
| 258 | numLink=boost::lexical_cast<uint32_t>(nt.getNextToken()); |
| 259 | } |
| 260 | catch(std::exception &e) |
| 261 | { |
| 262 | return false; |
| 263 | } |
| 264 | for(int i=0; i< numLink; i++) |
| 265 | { |
| 266 | try |
| 267 | { |
| 268 | string adjName=nt.getNextToken(); |
| 269 | int connectingFace=boost::lexical_cast<int>(nt.getNextToken()); |
| 270 | double linkCost=boost::lexical_cast<double>(nt.getNextToken()); |
| 271 | Adjacent adjacent(adjName, connectingFace, linkCost, 0, 0); |
| 272 | addAdjacentToLsa(adjacent); |
| 273 | } |
| 274 | catch( std::exception &e ) |
| 275 | { |
| 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | return true; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 280 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 281 | |
| 282 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 283 | void |
| 284 | AdjLsa::addNptEntriesForAdjLsa(Nlsr& pnlsr) |
| 285 | { |
| 286 | if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 287 | { |
| 288 | pnlsr.getNpt().addNpte(getOrigRouter(), getOrigRouter(),pnlsr); |
| 289 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 290 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 291 | |
| 292 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 293 | void |
| 294 | AdjLsa::removeNptEntriesForAdjLsa(Nlsr& pnlsr) |
| 295 | { |
| 296 | if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 297 | { |
| 298 | pnlsr.getNpt().removeNpte(getOrigRouter(), getOrigRouter(),pnlsr); |
| 299 | } |
| 300 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 301 | |
| 302 | |
| 303 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 304 | std::ostream& |
| 305 | operator<<(std::ostream& os, AdjLsa& aLsa) |
| 306 | { |
| 307 | os<<"Adj Lsa: "<<endl; |
| 308 | os<<" Origination Router: "<<aLsa.getOrigRouter()<<endl; |
| 309 | os<<" Ls Type: "<<(unsigned short)aLsa.getLsType()<<endl; |
| 310 | os<<" Ls Seq No: "<<(unsigned int)aLsa.getLsSeqNo()<<endl; |
| 311 | os<<" Ls Lifetime: "<<(unsigned int)aLsa.getLifeTime()<<endl; |
| 312 | os<<" No Link: "<<(unsigned int)aLsa.getNoLink()<<endl; |
| 313 | os<<" Adjacents: "<<endl; |
| 314 | int i=1; |
| 315 | std::list<Adjacent> al=aLsa.getAdl().getAdjList(); |
| 316 | for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++) |
| 317 | { |
| 318 | os<<" Adjacent "<<i<<": "<<endl; |
| 319 | os<<" Adjacent Name: "<<(*it).getAdjacentName()<<endl; |
| 320 | os<<" Connecting Face: "<<(*it).getConnectingFace()<<endl; |
| 321 | os<<" Link Cost: "<<(*it).getLinkCost()<<endl; |
| 322 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 323 | return os; |
| 324 | } |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 325 | |
| 326 | }//namespace nlsr |