Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, The University of Memphis, |
| 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 "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 { |
| 29 | namespace tlv { |
| 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 | |
| 46 | template<bool T> |
| 47 | size_t |
| 48 | NameLsa::wireEncode(ndn::EncodingImpl<T>& block) const |
| 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 | |
| 65 | template size_t |
| 66 | NameLsa::wireEncode<true>(ndn::EncodingImpl<true>& block) const; |
| 67 | |
| 68 | template size_t |
| 69 | NameLsa::wireEncode<false>(ndn::EncodingImpl<false>& block) const; |
| 70 | |
| 71 | const ndn::Block& |
| 72 | NameLsa::wireEncode() const |
| 73 | { |
| 74 | if (m_wire.hasWire()) { |
| 75 | return m_wire; |
| 76 | } |
| 77 | |
| 78 | ndn::EncodingEstimator estimator; |
| 79 | size_t estimatedSize = wireEncode(estimator); |
| 80 | |
| 81 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 82 | wireEncode(buffer); |
| 83 | |
| 84 | m_wire = buffer.block(); |
| 85 | |
| 86 | return m_wire; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | NameLsa::wireDecode(const ndn::Block& wire) |
| 91 | { |
| 92 | m_hasNames = false; |
| 93 | m_names.clear(); |
| 94 | |
| 95 | m_wire = wire; |
| 96 | |
| 97 | if (m_wire.type() != ndn::tlv::nlsr::NameLsa) { |
| 98 | std::stringstream error; |
| 99 | error << "Expected NameLsa Block, but Block is of a different type: #" |
| 100 | << m_wire.type(); |
| 101 | throw Error(error.str()); |
| 102 | } |
| 103 | |
| 104 | m_wire.parse(); |
| 105 | |
| 106 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 107 | |
| 108 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) { |
| 109 | m_lsaInfo.wireDecode(*val); |
| 110 | ++val; |
| 111 | } |
| 112 | else { |
| 113 | throw Error("Missing required LsaInfo field"); |
| 114 | } |
| 115 | |
| 116 | for (; val != m_wire.elements_end(); ++val) { |
| 117 | if (val->type() == ndn::tlv::Name) { |
| 118 | m_names.push_back(ndn::Name(*val)); |
| 119 | m_hasNames = true; |
| 120 | } |
| 121 | else { |
| 122 | std::stringstream error; |
| 123 | error << "Expected Name Block, but Block is of a different type: #" |
| 124 | << m_wire.type(); |
| 125 | throw Error(error.str()); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | std::ostream& |
| 131 | operator<<(std::ostream& os, const NameLsa& nameLsa) |
| 132 | { |
| 133 | os << "NameLsa(" |
| 134 | << nameLsa.getLsaInfo(); |
| 135 | |
| 136 | for (const auto& name : nameLsa) { |
| 137 | os << ", Name: " << name; |
| 138 | } |
| 139 | |
| 140 | os << ")"; |
| 141 | |
| 142 | return os; |
| 143 | } |
| 144 | |
| 145 | } // namespace tlv |
| 146 | } // namespace nlsr |