Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 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/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 20 | */ |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 21 | |
| 22 | #include "adj-lsa.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 23 | #include "tlv-nlsr.hpp" |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 24 | |
| 25 | namespace nlsr { |
| 26 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 27 | AdjLsa::AdjLsa(const ndn::Name& originRouter, uint64_t seqNo, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 28 | const ndn::time::system_clock::TimePoint& timepoint, |
| 29 | uint32_t noLink, AdjacencyList& adl) |
| 30 | : Lsa(originRouter, seqNo, timepoint) |
| 31 | , m_noLink(noLink) |
| 32 | { |
| 33 | for (const auto& adjacent : adl.getAdjList()) { |
| 34 | if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) { |
| 35 | addAdjacent(adjacent); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | AdjLsa::AdjLsa(const ndn::Block& block) |
| 41 | { |
| 42 | wireDecode(block); |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | AdjLsa::isEqualContent(const AdjLsa& alsa) const |
| 47 | { |
| 48 | return m_adl == alsa.getAdl(); |
| 49 | } |
| 50 | |
| 51 | template<ndn::encoding::Tag TAG> |
| 52 | size_t |
| 53 | AdjLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 54 | { |
| 55 | size_t totalLength = 0; |
| 56 | |
| 57 | auto list = m_adl.getAdjList(); |
| 58 | for (auto it = list.rbegin(); it != list.rend(); ++it) { |
| 59 | totalLength += it->wireEncode(block); |
| 60 | } |
| 61 | |
| 62 | totalLength += Lsa::wireEncode(block); |
| 63 | |
| 64 | totalLength += block.prependVarNumber(totalLength); |
| 65 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::AdjacencyLsa); |
| 66 | |
| 67 | return totalLength; |
| 68 | } |
| 69 | |
| 70 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjLsa); |
| 71 | |
| 72 | const ndn::Block& |
| 73 | AdjLsa::wireEncode() const |
| 74 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 75 | if (m_wire.hasWire()) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 76 | return m_wire; |
| 77 | } |
| 78 | |
| 79 | ndn::EncodingEstimator estimator; |
| 80 | size_t estimatedSize = wireEncode(estimator); |
| 81 | |
| 82 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 83 | wireEncode(buffer); |
| 84 | |
| 85 | m_wire = buffer.block(); |
| 86 | |
| 87 | return m_wire; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | AdjLsa::wireDecode(const ndn::Block& wire) |
| 92 | { |
| 93 | m_wire = wire; |
| 94 | |
| 95 | if (m_wire.type() != ndn::tlv::nlsr::AdjacencyLsa) { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 96 | NDN_THROW(Error("AdjacencyLsa", m_wire.type())); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | m_wire.parse(); |
| 100 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 101 | auto val = m_wire.elements_begin(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 102 | |
| 103 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Lsa) { |
| 104 | Lsa::wireDecode(*val); |
| 105 | ++val; |
| 106 | } |
| 107 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 108 | NDN_THROW(Error("Missing required Lsa field")); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | AdjacencyList adl; |
| 112 | for (; val != m_wire.elements_end(); ++val) { |
| 113 | if (val->type() == ndn::tlv::nlsr::Adjacency) { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 114 | adl.insert(Adjacent(*val)); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 115 | } |
| 116 | else { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame^] | 117 | NDN_THROW(Error("Adjacency", val->type())); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | m_adl = adl; |
| 121 | } |
| 122 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 123 | std::string |
| 124 | AdjLsa::toString() const |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 125 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 126 | std::ostringstream os; |
| 127 | os << Lsa::toString(); |
| 128 | os << " Adjacent(s):\n"; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 129 | |
| 130 | int adjacencyIndex = 0; |
| 131 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 132 | for (const auto& adjacency : m_adl) { |
| 133 | os << " Adjacent " << adjacencyIndex++ |
| 134 | << ": (name=" << adjacency.getName() |
| 135 | << ", uri=" << adjacency.getFaceUri() |
| 136 | << ", cost=" << adjacency.getLinkCost() << ")\n"; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 139 | return os.str(); |
| 140 | } |
| 141 | |
| 142 | std::ostream& |
| 143 | operator<<(std::ostream& os, const AdjLsa& lsa) |
| 144 | { |
| 145 | return os << lsa.toString(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 148 | } // namespace nlsr |