Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [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, |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -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/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "lsdb-status.hpp" |
| 23 | #include "tlv-nlsr.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/util/concepts.hpp> |
| 26 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 27 | |
| 28 | namespace nlsr { |
| 29 | namespace tlv { |
| 30 | |
| 31 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<LsdbStatus>)); |
| 32 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<LsdbStatus>)); |
| 33 | static_assert(std::is_base_of<ndn::tlv::Error, LsdbStatus::Error>::value, |
| 34 | "LsdbStatus::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | LsdbStatus::LsdbStatus() |
| 37 | : m_hasAdjacencyLsas(false) |
| 38 | , m_hasCoordinateLsas(false) |
| 39 | , m_hasNameLsas(false) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | LsdbStatus::LsdbStatus(const ndn::Block& block) |
| 44 | { |
| 45 | wireDecode(block); |
| 46 | } |
| 47 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 48 | template<ndn::encoding::Tag TAG> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 49 | size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 50 | LsdbStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 51 | { |
| 52 | size_t totalLength = 0; |
| 53 | |
| 54 | for (std::list<NameLsa>::const_reverse_iterator it = m_nameLsas.rbegin(); |
| 55 | it != m_nameLsas.rend(); ++it) { |
| 56 | totalLength += it->wireEncode(block); |
| 57 | } |
| 58 | |
| 59 | for (std::list<CoordinateLsa>::const_reverse_iterator it = m_coordinateLsas.rbegin(); |
| 60 | it != m_coordinateLsas.rend(); ++it) { |
| 61 | totalLength += it->wireEncode(block); |
| 62 | } |
| 63 | |
| 64 | for (std::list<AdjacencyLsa>::const_reverse_iterator it = m_adjacencyLsas.rbegin(); |
| 65 | it != m_adjacencyLsas.rend(); ++it) { |
| 66 | totalLength += it->wireEncode(block); |
| 67 | } |
| 68 | |
| 69 | totalLength += block.prependVarNumber(totalLength); |
| 70 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::LsdbStatus); |
| 71 | |
| 72 | return totalLength; |
| 73 | } |
| 74 | |
| 75 | template size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 76 | LsdbStatus::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 77 | |
| 78 | template size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 79 | LsdbStatus::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 80 | |
| 81 | const ndn::Block& |
| 82 | LsdbStatus::wireEncode() const |
| 83 | { |
| 84 | if (m_wire.hasWire()) { |
| 85 | return m_wire; |
| 86 | } |
| 87 | |
| 88 | ndn::EncodingEstimator estimator; |
| 89 | size_t estimatedSize = wireEncode(estimator); |
| 90 | |
| 91 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 92 | wireEncode(buffer); |
| 93 | |
| 94 | m_wire = buffer.block(); |
| 95 | |
| 96 | return m_wire; |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | LsdbStatus::wireDecode(const ndn::Block& wire) |
| 101 | { |
| 102 | m_adjacencyLsas.clear(); |
| 103 | m_coordinateLsas.clear(); |
| 104 | m_nameLsas.clear(); |
| 105 | |
| 106 | m_hasAdjacencyLsas = false; |
| 107 | m_hasCoordinateLsas = false; |
| 108 | m_hasNameLsas = false; |
| 109 | |
| 110 | m_wire = wire; |
| 111 | |
| 112 | if (m_wire.type() != ndn::tlv::nlsr::LsdbStatus) { |
| 113 | std::stringstream error; |
| 114 | error << "Expected LsdbStatus Block, but Block is of a different type: #" |
| 115 | << m_wire.type(); |
| 116 | throw Error(error.str()); |
| 117 | } |
| 118 | |
| 119 | m_wire.parse(); |
| 120 | |
| 121 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 122 | |
| 123 | for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::AdjacencyLsa; ++val) { |
| 124 | m_adjacencyLsas.push_back(AdjacencyLsa(*val)); |
| 125 | m_hasAdjacencyLsas = true; |
| 126 | } |
| 127 | |
| 128 | for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::CoordinateLsa; ++val) { |
| 129 | m_coordinateLsas.push_back(CoordinateLsa(*val)); |
| 130 | m_hasCoordinateLsas = true; |
| 131 | } |
| 132 | |
| 133 | for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::NameLsa; ++val) { |
| 134 | m_nameLsas.push_back(NameLsa(*val)); |
| 135 | m_hasNameLsas = true; |
| 136 | } |
| 137 | |
| 138 | if (val != m_wire.elements_end()) { |
| 139 | std::stringstream error; |
| 140 | error << "Expected the end of elements, but Block is of a different type: #" |
| 141 | << val->type(); |
| 142 | throw Error(error.str()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | std::ostream& |
| 147 | operator<<(std::ostream& os, const LsdbStatus& lsdbStatus) |
| 148 | { |
| 149 | os << "LsdbStatus("; |
| 150 | |
| 151 | bool isFirst = true; |
| 152 | |
| 153 | for (const auto& adjacencyLsa : lsdbStatus.getAdjacencyLsas()) { |
| 154 | if (isFirst) { |
| 155 | isFirst = false; |
| 156 | } |
| 157 | else { |
| 158 | os << ", "; |
| 159 | } |
| 160 | |
| 161 | os << adjacencyLsa; |
| 162 | } |
| 163 | |
| 164 | for (const auto& coordinateLsa : lsdbStatus.getCoordinateLsas()) { |
| 165 | if (isFirst) { |
| 166 | isFirst = false; |
| 167 | } |
| 168 | else { |
| 169 | os << ", "; |
| 170 | } |
| 171 | |
| 172 | os << coordinateLsa; |
| 173 | } |
| 174 | |
| 175 | for (const auto& nameLsa : lsdbStatus.getNameLsas()) { |
| 176 | if (isFirst) { |
| 177 | isFirst = false; |
| 178 | } |
| 179 | else { |
| 180 | os << ", "; |
| 181 | } |
| 182 | |
| 183 | os << nameLsa; |
| 184 | } |
| 185 | |
| 186 | os << ")"; |
| 187 | |
| 188 | return os; |
| 189 | } |
| 190 | |
| 191 | } // namespace tlv |
| 192 | } // namespace nlsr |