Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, 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/>. |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 20 | */ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 21 | |
| 22 | #include "name-lsa.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 { |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 29 | namespace tlv { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 30 | |
| 31 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<NameLsa>)); |
| 32 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<NameLsa>)); |
| 33 | static_assert(std::is_base_of<ndn::tlv::Error, NameLsa::Error>::value, |
| 34 | "NameLsa::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | NameLsa::NameLsa() |
| 37 | : m_hasNames(false) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | NameLsa::NameLsa(const ndn::Block& block) |
| 42 | { |
| 43 | wireDecode(block); |
| 44 | } |
| 45 | |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 46 | template<ndn::encoding::Tag TAG> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 47 | size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 48 | NameLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 49 | { |
| 50 | size_t totalLength = 0; |
| 51 | |
| 52 | for (std::list<ndn::Name>::const_reverse_iterator it = m_names.rbegin(); |
| 53 | it != m_names.rend(); ++it) { |
| 54 | totalLength += it->wireEncode(block); |
| 55 | } |
| 56 | |
| 57 | totalLength += m_lsaInfo.wireEncode(block); |
| 58 | |
| 59 | totalLength += block.prependVarNumber(totalLength); |
| 60 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::NameLsa); |
| 61 | |
| 62 | return totalLength; |
| 63 | } |
| 64 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 65 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NameLsa); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 66 | |
| 67 | const ndn::Block& |
| 68 | NameLsa::wireEncode() const |
| 69 | { |
| 70 | if (m_wire.hasWire()) { |
| 71 | return m_wire; |
| 72 | } |
| 73 | |
| 74 | ndn::EncodingEstimator estimator; |
| 75 | size_t estimatedSize = wireEncode(estimator); |
| 76 | |
| 77 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 78 | wireEncode(buffer); |
| 79 | |
| 80 | m_wire = buffer.block(); |
| 81 | |
| 82 | return m_wire; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | NameLsa::wireDecode(const ndn::Block& wire) |
| 87 | { |
| 88 | m_hasNames = false; |
| 89 | m_names.clear(); |
| 90 | |
| 91 | m_wire = wire; |
| 92 | |
| 93 | if (m_wire.type() != ndn::tlv::nlsr::NameLsa) { |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 94 | BOOST_THROW_EXCEPTION(Error("Expected NameLsa Block, but Block is of a different type: #" + |
| 95 | ndn::to_string(m_wire.type()))); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | m_wire.parse(); |
| 99 | |
| 100 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 101 | |
| 102 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) { |
| 103 | m_lsaInfo.wireDecode(*val); |
| 104 | ++val; |
| 105 | } |
| 106 | else { |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 107 | BOOST_THROW_EXCEPTION(Error("Missing required LsaInfo field")); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | for (; val != m_wire.elements_end(); ++val) { |
| 111 | if (val->type() == ndn::tlv::Name) { |
| 112 | m_names.push_back(ndn::Name(*val)); |
| 113 | m_hasNames = true; |
| 114 | } |
| 115 | else { |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame^] | 116 | BOOST_THROW_EXCEPTION(Error("Expected Name Block, but Block is of a different type: #" + |
| 117 | ndn::to_string(m_wire.type()))); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | std::ostream& |
| 123 | operator<<(std::ostream& os, const NameLsa& nameLsa) |
| 124 | { |
| 125 | os << "NameLsa(" |
| 126 | << nameLsa.getLsaInfo(); |
| 127 | |
| 128 | for (const auto& name : nameLsa) { |
| 129 | os << ", Name: " << name; |
| 130 | } |
| 131 | |
| 132 | os << ")"; |
| 133 | |
| 134 | return os; |
| 135 | } |
| 136 | |
| 137 | } // namespace tlv |
| 138 | } // namespace nlsr |