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