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