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