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