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 "adjacency.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<Adjacency>)); |
| 32 | BOOST_CONCEPT_ASSERT((ndn::WireDecodable<Adjacency>)); |
| 33 | static_assert(std::is_base_of<ndn::tlv::Error, Adjacency::Error>::value, |
| 34 | "Adjacency::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | Adjacency::Adjacency() |
| 37 | : m_cost(0) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | Adjacency::Adjacency(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 | Adjacency::wireEncode(ndn::EncodingImpl<TAG>& encoder) const |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 49 | { |
| 50 | size_t totalLength = 0; |
| 51 | |
Vince Lehman | f4772d4 | 2015-06-29 14:02:22 -0500 | [diff] [blame] | 52 | totalLength += prependNonNegativeIntegerBlock(encoder, ndn::tlv::nlsr::Cost, m_cost); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 53 | |
Vince Lehman | f4772d4 | 2015-06-29 14:02:22 -0500 | [diff] [blame] | 54 | totalLength += encoder.prependByteArrayBlock( |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 55 | ndn::tlv::nlsr::Uri, reinterpret_cast<const uint8_t*>(m_uri.c_str()), m_uri.size()); |
| 56 | |
Vince Lehman | f4772d4 | 2015-06-29 14:02:22 -0500 | [diff] [blame] | 57 | totalLength += m_name.wireEncode(encoder); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 58 | |
Vince Lehman | f4772d4 | 2015-06-29 14:02:22 -0500 | [diff] [blame] | 59 | totalLength += encoder.prependVarNumber(totalLength); |
| 60 | totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::Adjacency); |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 61 | |
| 62 | return totalLength; |
| 63 | } |
| 64 | |
| 65 | template size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame^] | 66 | Adjacency::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 67 | |
| 68 | template size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame^] | 69 | Adjacency::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 70 | |
| 71 | const ndn::Block& |
| 72 | Adjacency::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 | Adjacency::wireDecode(const ndn::Block& wire) |
| 91 | { |
| 92 | m_name.clear(); |
| 93 | m_uri = ""; |
| 94 | m_cost = 0; |
| 95 | |
| 96 | m_wire = wire; |
| 97 | |
| 98 | if (m_wire.type() != ndn::tlv::nlsr::Adjacency) { |
| 99 | std::stringstream error; |
| 100 | error << "Expected Adjacency Block, but Block is of a different type: #" |
| 101 | << m_wire.type(); |
| 102 | throw Error(error.str()); |
| 103 | } |
| 104 | |
| 105 | m_wire.parse(); |
| 106 | |
| 107 | ndn::Block::element_const_iterator val = m_wire.elements_begin(); |
| 108 | |
| 109 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) { |
| 110 | m_name.wireDecode(*val); |
| 111 | ++val; |
| 112 | } |
| 113 | else { |
| 114 | throw Error("Missing required Name field"); |
| 115 | } |
| 116 | |
| 117 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) { |
| 118 | m_uri.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
| 119 | ++val; |
| 120 | } |
| 121 | else { |
| 122 | throw Error("Missing required Uri field"); |
| 123 | } |
| 124 | |
| 125 | if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Cost) { |
| 126 | m_cost = ndn::readNonNegativeInteger(*val); |
| 127 | ++val; |
| 128 | } |
| 129 | else { |
| 130 | throw Error("Missing required Cost field"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | std::ostream& |
| 135 | operator<<(std::ostream& os, const Adjacency& adjacency) |
| 136 | { |
| 137 | os << "Adjacency(" |
| 138 | << "Name: " << adjacency.getName() << ", " |
| 139 | << "Uri: " << adjacency.getUri() << ", " |
| 140 | << "Cost: " << adjacency.getCost() << ")"; |
| 141 | |
| 142 | return os; |
| 143 | } |
| 144 | |
| 145 | } // namespace tlv |
| 146 | } // namespace nlsr |