akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * \author A K M Mahmudul Hoque <ahoque1@memphis.edu> |
| 21 | * |
| 22 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <iostream> |
| 25 | #include <sstream> |
| 26 | #include <algorithm> |
| 27 | #include <cmath> |
| 28 | #include <limits> |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 29 | #include <boost/tokenizer.hpp> |
| 30 | #include <boost/algorithm/string.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | |
| 32 | #include "nlsr.hpp" |
| 33 | #include "lsa.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 34 | #include "name-prefix-list.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | #include "adjacent.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 36 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | |
| 38 | |
| 39 | namespace nlsr { |
| 40 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 41 | INIT_LOGGER("Lsa"); |
| 42 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | using namespace std; |
| 44 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 45 | const ndn::Name |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 46 | NameLsa::getKey() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 48 | ndn::Name key = m_origRouter; |
| 49 | key.append("name"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | return key; |
| 51 | } |
| 52 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 53 | NameLsa::NameLsa(const ndn::Name& origR, const string& lst, uint32_t lsn, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 54 | const ndn::time::system_clock::TimePoint& lt, |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 55 | NamePrefixList& npl) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 56 | { |
| 57 | m_origRouter = origR; |
| 58 | m_lsType = lst; |
| 59 | m_lsSeqNo = lsn; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 60 | m_expirationTimePoint = lt; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 61 | std::list<ndn::Name>& nl = npl.getNameList(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 62 | for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 63 | addName((*it)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | string |
| 68 | NameLsa::getData() |
| 69 | { |
| 70 | string nameLsaData; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 71 | nameLsaData = m_origRouter.toUri() + "|" + "name" + "|" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 72 | + boost::lexical_cast<std::string>(m_lsSeqNo) + "|" |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 73 | + ndn::time::toIsoString(m_expirationTimePoint); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | nameLsaData += "|"; |
| 75 | nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize()); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 76 | std::list<ndn::Name> nl = m_npl.getNameList(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 77 | for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 78 | nameLsaData += "|"; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 79 | nameLsaData += (*it).toUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 80 | } |
| 81 | return nameLsaData + "|"; |
| 82 | } |
| 83 | |
| 84 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 85 | NameLsa::initializeFromContent(const std::string& content) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 86 | { |
| 87 | uint32_t numName = 0; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 88 | boost::char_separator<char> sep("|"); |
| 89 | boost::tokenizer<boost::char_separator<char> >tokens(content, sep); |
| 90 | boost::tokenizer<boost::char_separator<char> >::iterator tok_iter = |
| 91 | tokens.begin(); |
| 92 | m_origRouter = ndn::Name(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 93 | if (!(m_origRouter.size() > 0)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | return false; |
| 95 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 96 | try { |
| 97 | m_lsType = *tok_iter++; |
| 98 | m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 99 | m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 100 | numName = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 101 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 102 | catch (std::exception& e) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 103 | return false; |
| 104 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 105 | for (uint32_t i = 0; i < numName; i++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 106 | string name(*tok_iter++); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 107 | addName(name); |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | NameLsa::writeLog() |
| 114 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 115 | _LOG_DEBUG("Name Lsa: "); |
| 116 | _LOG_DEBUG(" Origination Router: " << m_origRouter); |
| 117 | _LOG_DEBUG(" Ls Type: " << m_lsType); |
| 118 | _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 119 | _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 120 | _LOG_DEBUG(" Names: "); |
| 121 | int i = 1; |
| 122 | std::list<ndn::Name> nl = m_npl.getNameList(); |
| 123 | for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) |
| 124 | { |
| 125 | _LOG_DEBUG(" Name " << i << ": " << (*it)); |
| 126 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | std::ostream& |
| 130 | operator<<(std::ostream& os, NameLsa& nLsa) |
| 131 | { |
| 132 | os << "Name Lsa: " << endl; |
| 133 | os << " Origination Router: " << nLsa.getOrigRouter() << endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 134 | os << " Ls Type: " << nLsa.getLsType() << endl; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 135 | os << " Ls Seq No: " << nLsa.getLsSeqNo() << endl; |
| 136 | os << " Ls Lifetime: " << nLsa.getExpirationTimePoint() << endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | os << " Names: " << endl; |
| 138 | int i = 1; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 139 | std::list<ndn::Name> nl = nLsa.getNpl().getNameList(); |
| 140 | for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | { |
| 142 | os << " Name " << i << ": " << (*it) << endl; |
| 143 | } |
| 144 | return os; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 149 | CoordinateLsa::CoordinateLsa(const ndn::Name& origR, const string lst, |
| 150 | uint32_t lsn, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 151 | const ndn::time::system_clock::TimePoint& lt, |
| 152 | double r, double theta) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 153 | { |
| 154 | m_origRouter = origR; |
| 155 | m_lsType = lst; |
| 156 | m_lsSeqNo = lsn; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 157 | m_expirationTimePoint = lt; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 158 | m_corRad = r; |
| 159 | m_corTheta = theta; |
| 160 | } |
| 161 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 162 | const ndn::Name |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 163 | CoordinateLsa::getKey() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 164 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 165 | ndn::Name key = m_origRouter; |
| 166 | key.append("coordinate"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 167 | return key; |
| 168 | } |
| 169 | |
| 170 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 171 | CoordinateLsa::isEqualContent(const CoordinateLsa& clsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 172 | { |
| 173 | return (std::abs(m_corRad - clsa.getCorRadius()) < |
| 174 | std::numeric_limits<double>::epsilon()) && |
| 175 | (std::abs(m_corTheta - clsa.getCorTheta()) < |
| 176 | std::numeric_limits<double>::epsilon()); |
| 177 | } |
| 178 | |
| 179 | string |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 180 | CoordinateLsa::getData() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 181 | { |
| 182 | string corLsaData; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 183 | corLsaData = m_origRouter.toUri() + "|"; |
| 184 | corLsaData += "coordinate"; |
| 185 | corLsaData += "|"; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 186 | corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|"); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 187 | corLsaData += (ndn::time::toIsoString(m_expirationTimePoint) + "|"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 188 | corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|"); |
| 189 | corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|"); |
| 190 | return corLsaData; |
| 191 | } |
| 192 | |
| 193 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 194 | CoordinateLsa::initializeFromContent(const std::string& content) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 196 | boost::char_separator<char> sep("|"); |
| 197 | boost::tokenizer<boost::char_separator<char> >tokens(content, sep); |
| 198 | boost::tokenizer<boost::char_separator<char> >::iterator tok_iter = |
| 199 | tokens.begin(); |
| 200 | m_origRouter = ndn::Name(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 201 | if (!(m_origRouter.size() > 0)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 202 | return false; |
| 203 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 204 | try { |
| 205 | m_lsType = *tok_iter++; |
| 206 | m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 207 | m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 208 | m_corRad = boost::lexical_cast<double>(*tok_iter++); |
| 209 | m_corTheta = boost::lexical_cast<double>(*tok_iter++); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 210 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 211 | catch (std::exception& e) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 212 | return false; |
| 213 | } |
| 214 | return true; |
| 215 | } |
| 216 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 217 | void |
| 218 | CoordinateLsa::writeLog() |
| 219 | { |
| 220 | _LOG_DEBUG("Cor Lsa: "); |
| 221 | _LOG_DEBUG(" Origination Router: " << m_origRouter); |
| 222 | _LOG_DEBUG(" Ls Type: " << m_lsType); |
| 223 | _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 224 | _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 225 | _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad); |
| 226 | _LOG_DEBUG(" Hyperbolic Theta: " << m_corRad); |
| 227 | } |
| 228 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 229 | std::ostream& |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 230 | operator<<(std::ostream& os, const CoordinateLsa& cLsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 231 | { |
| 232 | os << "Cor Lsa: " << endl; |
| 233 | os << " Origination Router: " << cLsa.getOrigRouter() << endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 234 | os << " Ls Type: " << cLsa.getLsType() << endl; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 235 | os << " Ls Seq No: " << cLsa.getLsSeqNo() << endl; |
| 236 | os << " Ls Lifetime: " << cLsa.getExpirationTimePoint() << endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 237 | os << " Hyperbolic Radius: " << cLsa.getCorRadius() << endl; |
| 238 | os << " Hyperbolic Theta: " << cLsa.getCorTheta() << endl; |
| 239 | return os; |
| 240 | } |
| 241 | |
| 242 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 243 | AdjLsa::AdjLsa(const ndn::Name& origR, const string& lst, uint32_t lsn, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 244 | const ndn::time::system_clock::TimePoint& lt, |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 245 | uint32_t nl , AdjacencyList& adl) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 246 | { |
| 247 | m_origRouter = origR; |
| 248 | m_lsType = lst; |
| 249 | m_lsSeqNo = lsn; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 250 | m_expirationTimePoint = lt; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 251 | m_noLink = nl; |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 252 | std::list<Adjacent> al = adl.getAdjList(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 253 | for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) { |
| 254 | if ((*it).getStatus() == 1) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 255 | addAdjacent((*it)); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 260 | const ndn::Name |
| 261 | AdjLsa::getKey() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 262 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 263 | ndn::Name key = m_origRouter; |
| 264 | key.append("adjacency"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 265 | return key; |
| 266 | } |
| 267 | |
| 268 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 269 | AdjLsa::isEqualContent(AdjLsa& alsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 270 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 271 | return m_adl == alsa.getAdl(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | string |
| 276 | AdjLsa::getData() |
| 277 | { |
| 278 | string adjLsaData; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 279 | adjLsaData = m_origRouter.toUri() + "|" + "adjacency" + "|" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 280 | + boost::lexical_cast<std::string>(m_lsSeqNo) + "|" |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 281 | + ndn::time::toIsoString(m_expirationTimePoint); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 282 | adjLsaData += "|"; |
| 283 | adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize()); |
| 284 | std::list<Adjacent> al = m_adl.getAdjList(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 285 | for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 286 | adjLsaData += "|"; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 287 | adjLsaData += (*it).getName().toUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 288 | adjLsaData += "|"; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 289 | adjLsaData += (*it).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 290 | adjLsaData += "|"; |
| 291 | adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost()); |
| 292 | } |
| 293 | return adjLsaData + "|"; |
| 294 | } |
| 295 | |
| 296 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 297 | AdjLsa::initializeFromContent(const std::string& content) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 298 | { |
| 299 | uint32_t numLink = 0; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 300 | boost::char_separator<char> sep("|"); |
| 301 | boost::tokenizer<boost::char_separator<char> >tokens(content, sep); |
| 302 | boost::tokenizer<boost::char_separator<char> >::iterator tok_iter = |
| 303 | tokens.begin(); |
| 304 | m_origRouter = ndn::Name(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 305 | if (!(m_origRouter.size() > 0)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 306 | return false; |
| 307 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 308 | try { |
| 309 | m_lsType = *tok_iter++; |
| 310 | m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 311 | m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 312 | numLink = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 313 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 314 | catch (std::exception& e) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 315 | return false; |
| 316 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 317 | for (uint32_t i = 0; i < numLink; i++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 318 | try { |
| 319 | string adjName(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 320 | std::string connectingFaceUri(*tok_iter++); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 321 | double linkCost = boost::lexical_cast<double>(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 322 | Adjacent adjacent(adjName, connectingFaceUri, linkCost, 0, 0); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 323 | addAdjacent(adjacent); |
| 324 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 325 | catch (std::exception& e) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | } |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | void |
| 334 | AdjLsa::addNptEntries(Nlsr& pnlsr) |
| 335 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 336 | if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 337 | pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | void |
| 343 | AdjLsa::removeNptEntries(Nlsr& pnlsr) |
| 344 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 345 | if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 346 | pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 350 | void |
| 351 | AdjLsa::writeLog() |
| 352 | { |
| 353 | _LOG_DEBUG("Adj Lsa: "); |
| 354 | _LOG_DEBUG(" Origination Router: " << m_origRouter); |
| 355 | _LOG_DEBUG(" Ls Type: " << m_lsType); |
| 356 | _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 357 | _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 358 | _LOG_DEBUG(" Adjacents: "); |
| 359 | int i = 1; |
| 360 | std::list<Adjacent> al = m_adl.getAdjList(); |
| 361 | for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) |
| 362 | { |
| 363 | _LOG_DEBUG(" Adjacent " << i << ": ");; |
| 364 | _LOG_DEBUG(" Adjacent Name: " << (*it).getName()); |
| 365 | _LOG_DEBUG(" Connecting FaceUri: " << (*it).getConnectingFaceUri()); |
| 366 | _LOG_DEBUG(" Link Cost: " << (*it).getLinkCost()); |
| 367 | } |
| 368 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 369 | |
| 370 | std::ostream& |
| 371 | operator<<(std::ostream& os, AdjLsa& aLsa) |
| 372 | { |
| 373 | os << "Adj Lsa: " << endl; |
| 374 | os << " Origination Router: " << aLsa.getOrigRouter() << endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 375 | os << " Ls Type: " << aLsa.getLsType() << endl; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 376 | os << " Ls Seq No: " << aLsa.getLsSeqNo() << endl; |
| 377 | os << " Ls Lifetime: " << aLsa.getExpirationTimePoint() << endl; |
| 378 | os << " No Link: " << aLsa.getNoLink() << endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 379 | os << " Adjacents: " << endl; |
| 380 | int i = 1; |
| 381 | std::list<Adjacent> al = aLsa.getAdl().getAdjList(); |
| 382 | for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) |
| 383 | { |
| 384 | os << " Adjacent " << i << ": " << endl; |
| 385 | os << " Adjacent Name: " << (*it).getName() << endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 386 | os << " Connecting FaceUri: " << (*it).getConnectingFaceUri() << endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 387 | os << " Link Cost: " << (*it).getLinkCost() << endl; |
| 388 | } |
| 389 | return os; |
| 390 | } |
| 391 | |
| 392 | }//namespace nlsr |