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