Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -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 | |
| 22 | #include "selectors.hpp" |
| 23 | #include "encoding/encoding-buffer.hpp" |
| 24 | #include "encoding/block-helpers.hpp" |
| 25 | #include "util/concepts.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | |
| 29 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Selectors>)); |
| 30 | BOOST_CONCEPT_ASSERT((WireEncodable<Selectors>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireDecodable<Selectors>)); |
| 32 | static_assert(std::is_base_of<tlv::Error, Selectors::Error>::value, |
| 33 | "Selectors::Error must inherit from tlv::Error"); |
| 34 | |
| 35 | Selectors::Selectors() |
| 36 | : m_minSuffixComponents(-1) |
| 37 | , m_maxSuffixComponents(-1) |
| 38 | , m_childSelector(-1) |
| 39 | , m_mustBeFresh(false) |
| 40 | { |
| 41 | } |
| 42 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 43 | Selectors::Selectors(const Block& wire) |
| 44 | { |
| 45 | wireDecode(wire); |
| 46 | } |
| 47 | |
| 48 | bool |
| 49 | Selectors::empty() const |
| 50 | { |
| 51 | return m_minSuffixComponents < 0 && |
| 52 | m_maxSuffixComponents < 0 && |
| 53 | m_publisherPublicKeyLocator.empty() && |
| 54 | m_exclude.empty() && |
| 55 | m_childSelector < 0 && |
| 56 | !m_mustBeFresh; |
| 57 | } |
| 58 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 59 | template<encoding::Tag TAG> |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 60 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 61 | Selectors::wireEncode(EncodingImpl<TAG>& block) const |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 62 | { |
| 63 | size_t totalLength = 0; |
| 64 | |
| 65 | // Selectors ::= SELECTORS-TYPE TLV-LENGTH |
| 66 | // MinSuffixComponents? |
| 67 | // MaxSuffixComponents? |
| 68 | // PublisherPublicKeyLocator? |
| 69 | // Exclude? |
| 70 | // ChildSelector? |
| 71 | // MustBeFresh? |
| 72 | |
| 73 | // (reverse encoding) |
| 74 | |
| 75 | // MustBeFresh |
| 76 | if (getMustBeFresh()) { |
| 77 | totalLength += prependBooleanBlock(block, tlv::MustBeFresh); |
| 78 | } |
| 79 | |
| 80 | // ChildSelector |
| 81 | if (getChildSelector() >= 0) { |
| 82 | totalLength += prependNonNegativeIntegerBlock(block, tlv::ChildSelector, getChildSelector()); |
| 83 | } |
| 84 | |
| 85 | // Exclude |
| 86 | if (!getExclude().empty()) { |
| 87 | totalLength += getExclude().wireEncode(block); |
| 88 | } |
| 89 | |
| 90 | // PublisherPublicKeyLocator |
| 91 | if (!getPublisherPublicKeyLocator().empty()) { |
| 92 | totalLength += getPublisherPublicKeyLocator().wireEncode(block); |
| 93 | } |
| 94 | |
| 95 | // MaxSuffixComponents |
| 96 | if (getMaxSuffixComponents() >= 0) { |
| 97 | totalLength += prependNonNegativeIntegerBlock(block, tlv::MaxSuffixComponents, |
| 98 | getMaxSuffixComponents()); |
| 99 | } |
| 100 | |
| 101 | // MinSuffixComponents |
| 102 | if (getMinSuffixComponents() >= 0) { |
| 103 | totalLength += prependNonNegativeIntegerBlock(block, tlv::MinSuffixComponents, |
| 104 | getMinSuffixComponents()); |
| 105 | } |
| 106 | |
| 107 | totalLength += block.prependVarNumber(totalLength); |
| 108 | totalLength += block.prependVarNumber(tlv::Selectors); |
| 109 | return totalLength; |
| 110 | } |
| 111 | |
| 112 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 113 | Selectors::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& estimator) const; |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 114 | |
| 115 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 116 | Selectors::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 117 | |
| 118 | const Block& |
| 119 | Selectors::wireEncode() const |
| 120 | { |
| 121 | if (m_wire.hasWire()) |
| 122 | return m_wire; |
| 123 | |
| 124 | EncodingEstimator estimator; |
| 125 | size_t estimatedSize = wireEncode(estimator); |
| 126 | |
| 127 | EncodingBuffer buffer(estimatedSize, 0); |
| 128 | wireEncode(buffer); |
| 129 | |
| 130 | m_wire = buffer.block(); |
| 131 | return m_wire; |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | Selectors::wireDecode(const Block& wire) |
| 136 | { |
| 137 | if (wire.type() != tlv::Selectors) |
| 138 | throw tlv::Error("Unexpected TLV type when decoding Selectors"); |
| 139 | |
| 140 | *this = Selectors(); |
| 141 | |
| 142 | m_wire = wire; |
| 143 | m_wire.parse(); |
| 144 | |
| 145 | // MinSuffixComponents |
| 146 | Block::element_const_iterator val = m_wire.find(tlv::MinSuffixComponents); |
| 147 | if (val != m_wire.elements_end()) { |
| 148 | m_minSuffixComponents = readNonNegativeInteger(*val); |
| 149 | } |
| 150 | |
| 151 | // MaxSuffixComponents |
| 152 | val = m_wire.find(tlv::MaxSuffixComponents); |
| 153 | if (val != m_wire.elements_end()) { |
| 154 | m_maxSuffixComponents = readNonNegativeInteger(*val); |
| 155 | } |
| 156 | |
| 157 | // PublisherPublicKeyLocator |
| 158 | val = m_wire.find(tlv::KeyLocator); |
| 159 | if (val != m_wire.elements_end()) { |
| 160 | m_publisherPublicKeyLocator.wireDecode(*val); |
| 161 | } |
| 162 | |
| 163 | // Exclude |
| 164 | val = m_wire.find(tlv::Exclude); |
| 165 | if (val != m_wire.elements_end()) { |
| 166 | m_exclude.wireDecode(*val); |
| 167 | } |
| 168 | |
| 169 | // ChildSelector |
| 170 | val = m_wire.find(tlv::ChildSelector); |
| 171 | if (val != m_wire.elements_end()) { |
| 172 | m_childSelector = readNonNegativeInteger(*val); |
| 173 | } |
| 174 | |
| 175 | // MustBeFresh |
| 176 | val = m_wire.find(tlv::MustBeFresh); |
| 177 | if (val != m_wire.elements_end()) { |
| 178 | m_mustBeFresh = true; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | Selectors& |
| 183 | Selectors::setMinSuffixComponents(int minSuffixComponents) |
| 184 | { |
| 185 | m_minSuffixComponents = minSuffixComponents; |
| 186 | m_wire.reset(); |
| 187 | return *this; |
| 188 | } |
| 189 | |
| 190 | Selectors& |
| 191 | Selectors::setMaxSuffixComponents(int maxSuffixComponents) |
| 192 | { |
| 193 | m_maxSuffixComponents = maxSuffixComponents; |
| 194 | m_wire.reset(); |
| 195 | return *this; |
| 196 | } |
| 197 | |
| 198 | Selectors& |
| 199 | Selectors::setPublisherPublicKeyLocator(const KeyLocator& keyLocator) |
| 200 | { |
| 201 | m_publisherPublicKeyLocator = keyLocator; |
| 202 | m_wire.reset(); |
| 203 | return *this; |
| 204 | } |
| 205 | |
| 206 | Selectors& |
| 207 | Selectors::setExclude(const Exclude& exclude) |
| 208 | { |
| 209 | m_exclude = exclude; |
| 210 | m_wire.reset(); |
| 211 | return *this; |
| 212 | } |
| 213 | |
| 214 | Selectors& |
| 215 | Selectors::setChildSelector(int childSelector) |
| 216 | { |
| 217 | m_childSelector = childSelector; |
| 218 | m_wire.reset(); |
| 219 | return *this; |
| 220 | } |
| 221 | |
| 222 | Selectors& |
| 223 | Selectors::setMustBeFresh(bool mustBeFresh) |
| 224 | { |
| 225 | m_mustBeFresh = mustBeFresh; |
| 226 | m_wire.reset(); |
| 227 | return *this; |
| 228 | } |
| 229 | |
| 230 | bool |
| 231 | Selectors::operator==(const Selectors& other) const |
| 232 | { |
| 233 | return wireEncode() == other.wireEncode(); |
| 234 | } |
| 235 | |
| 236 | } // namespace ndn |