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