Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | bc6beb1 | 2017-01-11 23:20:39 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 22 | #include "strategy-choice.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 23 | #include "encoding/block-helpers.hpp" |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 24 | #include "encoding/encoding-buffer.hpp" |
| 25 | #include "encoding/tlv-nfd.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 26 | #include "util/concepts.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace nfd { |
| 30 | |
Davide Pesavento | eba76ad | 2017-02-05 02:48:24 -0500 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<StrategyChoice>)); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | bc6beb1 | 2017-01-11 23:20:39 +0000 | [diff] [blame] | 33 | StrategyChoice::StrategyChoice() = default; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 34 | |
| 35 | StrategyChoice::StrategyChoice(const Block& payload) |
| 36 | { |
| 37 | this->wireDecode(payload); |
| 38 | } |
| 39 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 40 | template<encoding::Tag TAG> |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 41 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 42 | StrategyChoice::wireEncode(EncodingImpl<TAG>& encoder) const |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 43 | { |
| 44 | size_t totalLength = 0; |
| 45 | |
| 46 | totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy); |
| 47 | totalLength += m_name.wireEncode(encoder); |
| 48 | |
| 49 | totalLength += encoder.prependVarNumber(totalLength); |
| 50 | totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice); |
| 51 | return totalLength; |
| 52 | } |
| 53 | |
| 54 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 55 | StrategyChoice::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 56 | |
| 57 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 58 | StrategyChoice::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 59 | |
| 60 | const Block& |
| 61 | StrategyChoice::wireEncode() const |
| 62 | { |
| 63 | if (m_wire.hasWire()) |
| 64 | return m_wire; |
| 65 | |
| 66 | EncodingEstimator estimator; |
| 67 | size_t estimatedSize = wireEncode(estimator); |
| 68 | |
| 69 | EncodingBuffer buffer(estimatedSize, 0); |
| 70 | wireEncode(buffer); |
| 71 | |
| 72 | m_wire = buffer.block(); |
| 73 | return m_wire; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | StrategyChoice::wireDecode(const Block& block) |
| 78 | { |
| 79 | if (block.type() != tlv::nfd::StrategyChoice) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 80 | BOOST_THROW_EXCEPTION(Error("expecting StrategyChoice block")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 81 | } |
| 82 | m_wire = block; |
| 83 | m_wire.parse(); |
| 84 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 85 | |
| 86 | if (val != m_wire.elements_end() && val->type() == tlv::Name) { |
| 87 | m_name.wireDecode(*val); |
| 88 | ++val; |
| 89 | } |
| 90 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 91 | BOOST_THROW_EXCEPTION(Error("missing required Name field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) { |
| 95 | val->parse(); |
| 96 | if (val->elements().empty()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 97 | BOOST_THROW_EXCEPTION(Error("expecting Strategy/Name")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 98 | } |
| 99 | else { |
| 100 | m_strategy.wireDecode(*val->elements_begin()); |
| 101 | } |
| 102 | ++val; |
| 103 | } |
| 104 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 105 | BOOST_THROW_EXCEPTION(Error("missing required Strategy field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | StrategyChoice& |
| 110 | StrategyChoice::setName(const Name& name) |
| 111 | { |
| 112 | m_wire.reset(); |
| 113 | m_name = name; |
| 114 | return *this; |
| 115 | } |
| 116 | |
| 117 | StrategyChoice& |
| 118 | StrategyChoice::setStrategy(const Name& strategy) |
| 119 | { |
| 120 | m_wire.reset(); |
| 121 | m_strategy = strategy; |
| 122 | return *this; |
| 123 | } |
| 124 | |
Junxiao Shi | bc6beb1 | 2017-01-11 23:20:39 +0000 | [diff] [blame] | 125 | bool |
| 126 | operator==(const StrategyChoice& a, const StrategyChoice& b) |
| 127 | { |
| 128 | return a.getName() == b.getName() && a.getStrategy() == b.getStrategy(); |
| 129 | } |
| 130 | |
| 131 | std::ostream& |
| 132 | operator<<(std::ostream& os, const StrategyChoice& sc) |
| 133 | { |
| 134 | return os << "StrategyChoice(" |
| 135 | << "Name: " << sc.getName() << ", " |
| 136 | << "Strategy: " << sc.getStrategy() |
| 137 | << ")"; |
| 138 | } |
| 139 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 140 | } // namespace nfd |
| 141 | } // namespace ndn |