akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #include<string> |
| 2 | #include<iostream> |
| 3 | #include<algorithm> |
| 4 | #include<cmath> |
| 5 | #include<limits> |
| 6 | |
| 7 | #include "nlsr_lsa.hpp" |
| 8 | #include "nlsr_npl.hpp" |
| 9 | #include "nlsr_adjacent.hpp" |
| 10 | #include "nlsr.hpp" |
| 11 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 12 | namespace nlsr |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 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 | using namespace std; |
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 | string |
| 19 | NameLsa::getNameLsaKey() |
| 20 | { |
| 21 | string key; |
| 22 | key=origRouter + "/" + boost::lexical_cast<std::string>(1); |
| 23 | return key; |
| 24 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 25 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 26 | NameLsa::NameLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt, Npl npl) |
| 27 | { |
| 28 | origRouter=origR; |
| 29 | lsType=lst; |
| 30 | lsSeqNo=lsn; |
| 31 | lifeTime=lt; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 32 | std::list<string> nl=npl.getNameList(); |
| 33 | for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++) |
| 34 | { |
| 35 | addNameToLsa((*it)); |
| 36 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | string |
| 40 | NameLsa::getNameLsaData() |
| 41 | { |
| 42 | string nameLsaData; |
| 43 | nameLsaData=origRouter + "|" + boost::lexical_cast<std::string>(lsType) + "|" |
| 44 | + boost::lexical_cast<std::string>(lsSeqNo) + "|" |
| 45 | + boost::lexical_cast<std::string>(lifeTime); |
| 46 | nameLsaData+="|"; |
| 47 | nameLsaData+=boost::lexical_cast<std::string>(npl.getNplSize()); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 48 | std::list<string> nl=npl.getNameList(); |
| 49 | for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++) |
| 50 | { |
| 51 | nameLsaData+="|"; |
| 52 | nameLsaData+=(*it); |
| 53 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 54 | return nameLsaData; |
| 55 | } |
| 56 | |
| 57 | std::ostream& |
| 58 | operator<<(std::ostream& os, NameLsa& nLsa) |
| 59 | { |
| 60 | os<<"Name Lsa: "<<endl; |
| 61 | os<<" Origination Router: "<<nLsa.getOrigRouter()<<endl; |
| 62 | os<<" Ls Type: "<<(unsigned short)nLsa.getLsType()<<endl; |
| 63 | os<<" Ls Seq No: "<<(unsigned int)nLsa.getLsSeqNo()<<endl; |
| 64 | os<<" Ls Lifetime: "<<(unsigned int)nLsa.getLifeTime()<<endl; |
| 65 | os<<" Names: "<<endl; |
| 66 | int i=1; |
| 67 | std::list<string> nl=nLsa.getNpl().getNameList(); |
| 68 | for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++) |
| 69 | { |
| 70 | os<<" Name "<<i<<": "<<(*it)<<endl; |
| 71 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 72 | return os; |
| 73 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 74 | |
| 75 | |
| 76 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 77 | CorLsa::CorLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt |
| 78 | , double r, double theta) |
| 79 | { |
| 80 | origRouter=origR; |
| 81 | lsType=lst; |
| 82 | lsSeqNo=lsn; |
| 83 | lifeTime=lt; |
| 84 | corRad=r; |
| 85 | corTheta=theta; |
| 86 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 87 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 88 | string |
| 89 | CorLsa::getCorLsaKey() |
| 90 | { |
| 91 | string key; |
| 92 | key=origRouter + "/" + boost::lexical_cast<std::string>(3); |
| 93 | return key; |
| 94 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 95 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 96 | bool |
| 97 | CorLsa::isLsaContentEqual(CorLsa& clsa) |
| 98 | { |
| 99 | return (std::abs(corRad - clsa.getCorRadius()) < |
| 100 | std::numeric_limits<double>::epsilon()) && |
| 101 | (std::abs(corTheta - clsa.getCorTheta()) < |
| 102 | std::numeric_limits<double>::epsilon()); |
| 103 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 104 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 105 | string |
| 106 | CorLsa::getCorLsaData() |
| 107 | { |
| 108 | string corLsaData; |
| 109 | corLsaData=origRouter + "|"; |
| 110 | corLsaData+=(boost::lexical_cast<std::string>(lsType) + "|"); |
| 111 | corLsaData+=(boost::lexical_cast<std::string>(lsSeqNo) + "|"); |
| 112 | corLsaData+=(boost::lexical_cast<std::string>(lifeTime) + "|"); |
| 113 | corLsaData+=(boost::lexical_cast<std::string>(corRad) + "|"); |
| 114 | corLsaData+=(boost::lexical_cast<std::string>(corTheta) + "|"); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 115 | return corLsaData; |
| 116 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 117 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 118 | std::ostream& |
| 119 | operator<<(std::ostream& os, CorLsa& cLsa) |
| 120 | { |
| 121 | os<<"Cor Lsa: "<<endl; |
| 122 | os<<" Origination Router: "<<cLsa.getOrigRouter()<<endl; |
| 123 | os<<" Ls Type: "<<(unsigned short)cLsa.getLsType()<<endl; |
| 124 | os<<" Ls Seq No: "<<(unsigned int)cLsa.getLsSeqNo()<<endl; |
| 125 | os<<" Ls Lifetime: "<<(unsigned int)cLsa.getLifeTime()<<endl; |
| 126 | os<<" Hyperbolic Radius: "<<cLsa.getCorRadius()<<endl; |
| 127 | os<<" Hyperbolic Theta: "<<cLsa.getCorTheta()<<endl; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 128 | return os; |
| 129 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 130 | |
| 131 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 132 | AdjLsa::AdjLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt, |
| 133 | uint32_t nl ,Adl padl) |
| 134 | { |
| 135 | origRouter=origR; |
| 136 | lsType=lst; |
| 137 | lsSeqNo=lsn; |
| 138 | lifeTime=lt; |
| 139 | noLink=nl; |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 140 | std::list<Adjacent> al=padl.getAdjList(); |
| 141 | for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++) |
| 142 | { |
| 143 | if((*it).getStatus()==1) |
| 144 | { |
| 145 | addAdjacentToLsa((*it)); |
| 146 | } |
| 147 | } |
| 148 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 149 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 150 | string |
| 151 | AdjLsa::getAdjLsaKey() |
| 152 | { |
| 153 | string key; |
| 154 | key=origRouter + "/" + boost::lexical_cast<std::string>(2); |
| 155 | return key; |
| 156 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 157 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 158 | bool |
| 159 | AdjLsa::isLsaContentEqual(AdjLsa& alsa) |
| 160 | { |
| 161 | return adl.isAdlEqual(alsa.getAdl()); |
| 162 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 163 | |
| 164 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 165 | string |
| 166 | AdjLsa::getAdjLsaData() |
| 167 | { |
| 168 | string adjLsaData; |
| 169 | adjLsaData=origRouter + "|" + boost::lexical_cast<std::string>(lsType) + "|" |
| 170 | + boost::lexical_cast<std::string>(lsSeqNo) + "|" |
| 171 | + boost::lexical_cast<std::string>(lifeTime); |
| 172 | adjLsaData+="|"; |
| 173 | adjLsaData+=boost::lexical_cast<std::string>(adl.getAdlSize()); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 174 | std::list<Adjacent> al=adl.getAdjList(); |
| 175 | for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++) |
| 176 | { |
| 177 | adjLsaData+="|"; |
| 178 | adjLsaData+=(*it).getAdjacentName(); |
| 179 | adjLsaData+="|"; |
| 180 | adjLsaData+=boost::lexical_cast<std::string>((*it).getConnectingFace()); |
| 181 | adjLsaData+="|"; |
| 182 | adjLsaData+=boost::lexical_cast<std::string>((*it).getLinkCost()); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 183 | } |
| 184 | return adjLsaData; |
| 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 | void |
| 189 | AdjLsa::addNptEntriesForAdjLsa(Nlsr& pnlsr) |
| 190 | { |
| 191 | if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 192 | { |
| 193 | pnlsr.getNpt().addNpte(getOrigRouter(), getOrigRouter(),pnlsr); |
| 194 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 195 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 196 | |
| 197 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 198 | void |
| 199 | AdjLsa::removeNptEntriesForAdjLsa(Nlsr& pnlsr) |
| 200 | { |
| 201 | if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() ) |
| 202 | { |
| 203 | pnlsr.getNpt().removeNpte(getOrigRouter(), getOrigRouter(),pnlsr); |
| 204 | } |
| 205 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 206 | |
| 207 | |
| 208 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 209 | std::ostream& |
| 210 | operator<<(std::ostream& os, AdjLsa& aLsa) |
| 211 | { |
| 212 | os<<"Adj Lsa: "<<endl; |
| 213 | os<<" Origination Router: "<<aLsa.getOrigRouter()<<endl; |
| 214 | os<<" Ls Type: "<<(unsigned short)aLsa.getLsType()<<endl; |
| 215 | os<<" Ls Seq No: "<<(unsigned int)aLsa.getLsSeqNo()<<endl; |
| 216 | os<<" Ls Lifetime: "<<(unsigned int)aLsa.getLifeTime()<<endl; |
| 217 | os<<" No Link: "<<(unsigned int)aLsa.getNoLink()<<endl; |
| 218 | os<<" Adjacents: "<<endl; |
| 219 | int i=1; |
| 220 | std::list<Adjacent> al=aLsa.getAdl().getAdjList(); |
| 221 | for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++) |
| 222 | { |
| 223 | os<<" Adjacent "<<i<<": "<<endl; |
| 224 | os<<" Adjacent Name: "<<(*it).getAdjacentName()<<endl; |
| 225 | os<<" Connecting Face: "<<(*it).getConnectingFace()<<endl; |
| 226 | os<<" Link Cost: "<<(*it).getLinkCost()<<endl; |
| 227 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 228 | return os; |
| 229 | } |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 230 | |
| 231 | }//namespace nlsr |