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