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