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 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 22 | #include "lsa.hpp" |
| 23 | #include "nlsr.hpp" |
| 24 | #include "name-prefix-list.hpp" |
| 25 | #include "adjacent.hpp" |
| 26 | #include "logger.hpp" |
| 27 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 28 | #include <string> |
| 29 | #include <iostream> |
| 30 | #include <sstream> |
| 31 | #include <algorithm> |
| 32 | #include <cmath> |
| 33 | #include <limits> |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 34 | #include <boost/tokenizer.hpp> |
| 35 | #include <boost/algorithm/string.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | namespace nlsr { |
| 38 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 39 | INIT_LOGGER("Lsa"); |
| 40 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 41 | const std::string NameLsa::TYPE_STRING = "name"; |
| 42 | const std::string AdjLsa::TYPE_STRING = "adjacency"; |
| 43 | const std::string CoordinateLsa::TYPE_STRING = "coordinate"; |
| 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; |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 49 | key.append(NameLsa::TYPE_STRING); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | return key; |
| 51 | } |
| 52 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 53 | NameLsa::NameLsa(const ndn::Name& origR, 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) |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 56 | : Lsa(NameLsa::TYPE_STRING) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | { |
| 58 | m_origRouter = origR; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 59 | m_lsSeqNo = lsn; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 60 | m_expirationTimePoint = lt; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 61 | for (const auto& name : npl.getNames()) { |
| 62 | addName(name); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame^] | 66 | std::string |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 67 | NameLsa::getData() |
| 68 | { |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 69 | std::ostringstream os; |
| 70 | os << m_origRouter << "|" << NameLsa::TYPE_STRING << "|" << m_lsSeqNo << "|" |
| 71 | << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_npl.getSize(); |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 72 | for (const auto& name : m_npl.getNames()) { |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 73 | os << "|" << name; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | } |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 75 | os << "|"; |
| 76 | return os.str(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 80 | NameLsa::initializeFromContent(const std::string& content) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | { |
| 82 | uint32_t numName = 0; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 83 | boost::char_separator<char> sep("|"); |
| 84 | boost::tokenizer<boost::char_separator<char> >tokens(content, sep); |
| 85 | boost::tokenizer<boost::char_separator<char> >::iterator tok_iter = |
| 86 | tokens.begin(); |
| 87 | m_origRouter = ndn::Name(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 88 | if (!(m_origRouter.size() > 0)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 89 | return false; |
| 90 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 91 | try { |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 92 | if (*tok_iter++ != NameLsa::TYPE_STRING) { |
| 93 | return false; |
| 94 | } |
| 95 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 96 | m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 97 | m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 98 | numName = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | } |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 100 | catch (const std::exception& e) { |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 101 | _LOG_ERROR(e.what()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | return false; |
| 103 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 104 | for (uint32_t i = 0; i < numName; i++) { |
akmhoque | 778f81b | 2014-06-27 10:07:56 -0500 | [diff] [blame] | 105 | ndn::Name name(*tok_iter++); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | addName(name); |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 111 | bool |
| 112 | NameLsa::isEqualContent(const NameLsa& other) const |
| 113 | { |
| 114 | return m_npl == other.getNpl(); |
| 115 | } |
| 116 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | void |
| 118 | NameLsa::writeLog() |
| 119 | { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 120 | _LOG_DEBUG("Name Lsa: "); |
| 121 | _LOG_DEBUG(" Origination Router: " << m_origRouter); |
| 122 | _LOG_DEBUG(" Ls Type: " << m_lsType); |
| 123 | _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 124 | _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 125 | _LOG_DEBUG(" Names: "); |
| 126 | int i = 1; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 127 | std::list<ndn::Name> nl = m_npl.getNames(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 128 | for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) |
| 129 | { |
| 130 | _LOG_DEBUG(" Name " << i << ": " << (*it)); |
| 131 | } |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 132 | _LOG_DEBUG("name_lsa_end"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 135 | CoordinateLsa::CoordinateLsa(const ndn::Name& origR, uint32_t lsn, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 136 | const ndn::time::system_clock::TimePoint& lt, |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 137 | double r, std::vector<double> theta) |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 138 | : Lsa(CoordinateLsa::TYPE_STRING) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | { |
| 140 | m_origRouter = origR; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | m_lsSeqNo = lsn; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 142 | m_expirationTimePoint = lt; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | m_corRad = r; |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 144 | m_angles = theta; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 145 | } |
| 146 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 147 | const ndn::Name |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 148 | CoordinateLsa::getKey() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 149 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 150 | ndn::Name key = m_origRouter; |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 151 | key.append(CoordinateLsa::TYPE_STRING); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 152 | return key; |
| 153 | } |
| 154 | |
| 155 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 156 | CoordinateLsa::isEqualContent(const CoordinateLsa& clsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | { |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 158 | if (clsa.getCorTheta().size() != m_angles.size()) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | std::vector<double> m_angles2 = clsa.getCorTheta(); |
| 163 | for (unsigned int i = 0; i < clsa.getCorTheta().size(); i++) { |
| 164 | if (std::abs(m_angles[i] - m_angles2[i]) > std::numeric_limits<double>::epsilon()) { |
| 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 169 | return (std::abs(m_corRad - clsa.getCorRadius()) < |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | std::numeric_limits<double>::epsilon()); |
| 171 | } |
| 172 | |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame^] | 173 | std::string |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 174 | CoordinateLsa::getData() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | { |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 176 | std::ostringstream os; |
| 177 | os << m_origRouter << "|" << CoordinateLsa::TYPE_STRING << "|" << m_lsSeqNo << "|" |
| 178 | << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_corRad << "|" |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 179 | << m_angles.size() << "|"; |
| 180 | |
| 181 | for (const auto& angle: m_angles) { |
| 182 | os << angle << "|"; |
| 183 | } |
| 184 | |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 185 | return os.str(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 189 | CoordinateLsa::initializeFromContent(const std::string& content) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 190 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 191 | boost::char_separator<char> sep("|"); |
| 192 | boost::tokenizer<boost::char_separator<char> >tokens(content, sep); |
| 193 | boost::tokenizer<boost::char_separator<char> >::iterator tok_iter = |
| 194 | tokens.begin(); |
| 195 | m_origRouter = ndn::Name(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 196 | if (!(m_origRouter.size() > 0)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 197 | return false; |
| 198 | } |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 199 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 200 | try { |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 201 | if (*tok_iter++ != CoordinateLsa::TYPE_STRING) { |
| 202 | return false; |
| 203 | } |
| 204 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 205 | m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 206 | m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 207 | m_corRad = boost::lexical_cast<double>(*tok_iter++); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 208 | int numAngles = boost::lexical_cast<uint32_t>(*tok_iter++); |
| 209 | |
| 210 | for (int i = 0; i < numAngles; i++) { |
| 211 | m_angles.push_back(boost::lexical_cast<double>(*tok_iter++)); |
| 212 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 213 | } |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 214 | catch (const std::exception& e) { |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 215 | _LOG_ERROR(e.what()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | return false; |
| 217 | } |
| 218 | return true; |
| 219 | } |
| 220 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 221 | void |
| 222 | CoordinateLsa::writeLog() |
| 223 | { |
| 224 | _LOG_DEBUG("Cor Lsa: "); |
| 225 | _LOG_DEBUG(" Origination Router: " << m_origRouter); |
| 226 | _LOG_DEBUG(" Ls Type: " << m_lsType); |
| 227 | _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 228 | _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 229 | _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 230 | int i = 0; |
| 231 | for(auto const& value: m_angles) { |
| 232 | _LOG_DEBUG(" Hyperbolic Theta " << i++ << ": "<< value); |
| 233 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 236 | AdjLsa::AdjLsa(const ndn::Name& origR, uint32_t lsn, |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 237 | const ndn::time::system_clock::TimePoint& lt, |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 238 | uint32_t nl , AdjacencyList& adl) |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 239 | : Lsa(AdjLsa::TYPE_STRING) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 240 | { |
| 241 | m_origRouter = origR; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 242 | m_lsSeqNo = lsn; |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 243 | m_expirationTimePoint = lt; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 244 | m_noLink = nl; |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 245 | std::list<Adjacent> al = adl.getAdjList(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 246 | for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 247 | if (it->getStatus() == Adjacent::STATUS_ACTIVE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 248 | addAdjacent((*it)); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 253 | const ndn::Name |
| 254 | AdjLsa::getKey() const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 255 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 256 | ndn::Name key = m_origRouter; |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 257 | key.append(AdjLsa::TYPE_STRING); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 258 | return key; |
| 259 | } |
| 260 | |
| 261 | bool |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 262 | AdjLsa::isEqualContent(AdjLsa& alsa) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 263 | { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 264 | return m_adl == alsa.getAdl(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 265 | } |
| 266 | |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame^] | 267 | std::string |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 268 | AdjLsa::getData() |
| 269 | { |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 270 | std::ostringstream os; |
| 271 | os << m_origRouter << "|" << AdjLsa::TYPE_STRING << "|" << m_lsSeqNo << "|" |
| 272 | << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_adl.getSize(); |
| 273 | for (const auto& adjacent : m_adl.getAdjList()) { |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 274 | os << "|" << adjacent.getName() << "|" << adjacent.getFaceUri() |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 275 | << "|" << adjacent.getLinkCost(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 276 | } |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 277 | os << "|"; |
| 278 | return os.str(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 282 | AdjLsa::initializeFromContent(const std::string& content) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 283 | { |
| 284 | uint32_t numLink = 0; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 285 | boost::char_separator<char> sep("|"); |
| 286 | boost::tokenizer<boost::char_separator<char> >tokens(content, sep); |
| 287 | boost::tokenizer<boost::char_separator<char> >::iterator tok_iter = |
| 288 | tokens.begin(); |
| 289 | m_origRouter = ndn::Name(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 290 | if (!(m_origRouter.size() > 0)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 291 | return false; |
| 292 | } |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 293 | try { |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 294 | if (*tok_iter++ != AdjLsa::TYPE_STRING) { |
| 295 | return false; |
| 296 | } |
| 297 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 298 | m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 299 | m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 300 | numLink = boost::lexical_cast<uint32_t>(*tok_iter++); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 301 | } |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 302 | catch (const std::exception& e) { |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 303 | _LOG_ERROR(e.what()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 304 | return false; |
| 305 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 306 | for (uint32_t i = 0; i < numLink; i++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 307 | try { |
akmhoque | 778f81b | 2014-06-27 10:07:56 -0500 | [diff] [blame] | 308 | ndn::Name adjName(*tok_iter++); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 309 | std::string connectingFaceUri(*tok_iter++); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 310 | double linkCost = boost::lexical_cast<double>(*tok_iter++); |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 311 | Adjacent adjacent(adjName, ndn::util::FaceUri(connectingFaceUri), linkCost, |
| 312 | Adjacent::STATUS_INACTIVE, 0, 0); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 313 | addAdjacent(adjacent); |
| 314 | } |
Nick Gordon | adad249 | 2017-05-25 10:53:07 -0500 | [diff] [blame] | 315 | catch (const std::exception& e) { |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 316 | _LOG_ERROR(e.what()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | return true; |
| 321 | } |
| 322 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 323 | void |
| 324 | AdjLsa::addNptEntries(Nlsr& pnlsr) |
| 325 | { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 326 | // Only add NPT entries if this is an adj LSA from another router. |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 327 | if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 328 | // Pass the originating router as both the name to register and |
| 329 | // where it came from. |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 330 | pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | void |
| 336 | AdjLsa::removeNptEntries(Nlsr& pnlsr) |
| 337 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 338 | if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 339 | pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 343 | void |
| 344 | AdjLsa::writeLog() |
| 345 | { |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 346 | _LOG_DEBUG(*this); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 347 | } |
| 348 | |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 349 | std::ostream& |
| 350 | operator<<(std::ostream& os, const AdjLsa& adjLsa) |
| 351 | { |
| 352 | os << "Adj Lsa:\n" |
| 353 | << " Origination Router: " << adjLsa.getOrigRouter() << "\n" |
| 354 | << " Ls Type: " << adjLsa.getLsType() << "\n" |
| 355 | << " Ls Seq No: " << adjLsa.getLsSeqNo() << "\n" |
| 356 | << " Ls Lifetime: " << adjLsa.getExpirationTimePoint() << "\n" |
| 357 | << " Adjacents: \n"; |
| 358 | |
| 359 | int adjacencyIndex = 1; |
| 360 | |
| 361 | for (const Adjacent& adjacency : adjLsa) { |
| 362 | os << " Adjacent " << adjacencyIndex++ << ":\n" |
| 363 | << " Adjacent Name: " << adjacency.getName() << "\n" |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 364 | << " Connecting FaceUri: " << adjacency.getFaceUri() << "\n" |
alvy | dce3f18 | 2015-04-09 11:23:30 -0500 | [diff] [blame] | 365 | << " Link Cost: " << adjacency.getLinkCost() << "\n"; |
| 366 | } |
| 367 | os << "adj_lsa_end"; |
| 368 | |
| 369 | return os; |
| 370 | } |
| 371 | |
| 372 | } // namespace nlsr |