Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP |
| 24 | |
| 25 | #include "../encoding/tlv-nfd.hpp" |
| 26 | #include "../encoding/encoding-buffer.hpp" |
| 27 | #include "../encoding/block-helpers.hpp" |
| 28 | #include "../name.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace nfd { |
| 32 | |
| 33 | /** |
| 34 | * @ingroup management |
| 35 | * @brief represents NFD StrategyChoice dataset |
| 36 | * @sa http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Strategy-Choice-Dataset |
| 37 | */ |
| 38 | class StrategyChoice |
| 39 | { |
| 40 | public: |
| 41 | class Error : public Tlv::Error |
| 42 | { |
| 43 | public: |
| 44 | explicit |
| 45 | Error(const std::string& what) |
| 46 | : Tlv::Error(what) |
| 47 | { |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | StrategyChoice() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | explicit |
| 56 | StrategyChoice(const Block& payload) |
| 57 | { |
| 58 | this->wireDecode(payload); |
| 59 | } |
| 60 | |
| 61 | template<bool T> |
| 62 | size_t |
| 63 | wireEncode(EncodingImpl<T>& encoder) const; |
| 64 | |
| 65 | const Block& |
| 66 | wireEncode() const; |
| 67 | |
| 68 | void |
| 69 | wireDecode(const Block& wire); |
| 70 | |
| 71 | public: // getters & setters |
| 72 | const Name& |
| 73 | getName() const |
| 74 | { |
| 75 | return m_name; |
| 76 | } |
| 77 | |
| 78 | StrategyChoice& |
| 79 | setName(const Name& name) |
| 80 | { |
| 81 | m_wire.reset(); |
| 82 | m_name = name; |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | const Name& |
| 87 | getStrategy() const |
| 88 | { |
| 89 | return m_strategy; |
| 90 | } |
| 91 | |
| 92 | StrategyChoice& |
| 93 | setStrategy(const Name& strategy) |
| 94 | { |
| 95 | m_wire.reset(); |
| 96 | m_strategy = strategy; |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | Name m_name; // namespace |
| 102 | Name m_strategy; // strategy for the namespace |
| 103 | |
| 104 | mutable Block m_wire; |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | template<bool T> |
| 109 | inline size_t |
| 110 | StrategyChoice::wireEncode(EncodingImpl<T>& encoder) const |
| 111 | { |
| 112 | size_t totalLength = 0; |
| 113 | |
| 114 | totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy); |
| 115 | totalLength += m_name.wireEncode(encoder); |
| 116 | |
| 117 | totalLength += encoder.prependVarNumber(totalLength); |
| 118 | totalLength += encoder.prependVarNumber(tlv::nfd::StrategyChoice); |
| 119 | return totalLength; |
| 120 | } |
| 121 | |
| 122 | inline const Block& |
| 123 | StrategyChoice::wireEncode() const |
| 124 | { |
| 125 | if (m_wire.hasWire()) |
| 126 | return m_wire; |
| 127 | |
| 128 | EncodingEstimator estimator; |
| 129 | size_t estimatedSize = wireEncode(estimator); |
| 130 | |
| 131 | EncodingBuffer buffer(estimatedSize, 0); |
| 132 | wireEncode(buffer); |
| 133 | |
| 134 | m_wire = buffer.block(); |
| 135 | return m_wire; |
| 136 | } |
| 137 | |
| 138 | inline void |
| 139 | StrategyChoice::wireDecode(const Block& block) |
| 140 | { |
| 141 | if (block.type() != tlv::nfd::StrategyChoice) { |
| 142 | throw Error("expecting StrategyChoice block"); |
| 143 | } |
| 144 | m_wire = block; |
| 145 | m_wire.parse(); |
| 146 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 147 | |
| 148 | if (val != m_wire.elements_end() && val->type() == Tlv::Name) { |
| 149 | m_name.wireDecode(*val); |
| 150 | ++val; |
| 151 | } |
| 152 | else { |
| 153 | throw Error("missing required Name field"); |
| 154 | } |
| 155 | |
| 156 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Strategy) { |
| 157 | val->parse(); |
| 158 | if (val->elements().empty()) { |
| 159 | throw Error("expecting Strategy/Name"); |
| 160 | } |
| 161 | else { |
| 162 | m_strategy.wireDecode(*val->elements_begin()); |
| 163 | } |
| 164 | ++val; |
| 165 | } |
| 166 | else { |
| 167 | throw Error("missing required Strategy field"); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | } // namespace nfd |
| 172 | } // namespace ndn |
| 173 | |
| 174 | #endif // NDN_MANAGEMENT_NFD_STRATEGY_CHOICE_HPP |