Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 2 | /* |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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" |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | |
| 28 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Selectors>)); |
| 29 | BOOST_CONCEPT_ASSERT((WireEncodable<Selectors>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Selectors>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 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) |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 38 | , m_childSelector(DEFAULT_CHILD_SELECTOR) |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 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() && |
Junxiao Shi | 9bf5581 | 2017-07-05 15:04:37 +0000 | [diff] [blame] | 55 | m_childSelector == DEFAULT_CHILD_SELECTOR && |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 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 | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 61 | Selectors::wireEncode(EncodingImpl<TAG>& encoder) 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()) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 77 | totalLength += prependEmptyBlock(encoder, tlv::MustBeFresh); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // ChildSelector |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 81 | if (getChildSelector() != DEFAULT_CHILD_SELECTOR) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 82 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ChildSelector, getChildSelector()); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Exclude |
| 86 | if (!getExclude().empty()) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 87 | totalLength += getExclude().wireEncode(encoder); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // PublisherPublicKeyLocator |
| 91 | if (!getPublisherPublicKeyLocator().empty()) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 92 | totalLength += getPublisherPublicKeyLocator().wireEncode(encoder); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // MaxSuffixComponents |
| 96 | if (getMaxSuffixComponents() >= 0) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 97 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::MaxSuffixComponents, |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 98 | getMaxSuffixComponents()); |
| 99 | } |
| 100 | |
| 101 | // MinSuffixComponents |
| 102 | if (getMinSuffixComponents() >= 0) { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 103 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::MinSuffixComponents, |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 104 | getMinSuffixComponents()); |
| 105 | } |
| 106 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 107 | totalLength += encoder.prependVarNumber(totalLength); |
| 108 | totalLength += encoder.prependVarNumber(tlv::Selectors); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 109 | return totalLength; |
| 110 | } |
| 111 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 112 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Selectors); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 113 | |
| 114 | const Block& |
| 115 | Selectors::wireEncode() const |
| 116 | { |
| 117 | if (m_wire.hasWire()) |
| 118 | return m_wire; |
| 119 | |
| 120 | EncodingEstimator estimator; |
| 121 | size_t estimatedSize = wireEncode(estimator); |
| 122 | |
| 123 | EncodingBuffer buffer(estimatedSize, 0); |
| 124 | wireEncode(buffer); |
| 125 | |
| 126 | m_wire = buffer.block(); |
| 127 | return m_wire; |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | Selectors::wireDecode(const Block& wire) |
| 132 | { |
| 133 | if (wire.type() != tlv::Selectors) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 134 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Selectors")); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 135 | |
| 136 | *this = Selectors(); |
| 137 | |
| 138 | m_wire = wire; |
| 139 | m_wire.parse(); |
| 140 | |
| 141 | // MinSuffixComponents |
| 142 | Block::element_const_iterator val = m_wire.find(tlv::MinSuffixComponents); |
| 143 | if (val != m_wire.elements_end()) { |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 144 | m_minSuffixComponents = readNonNegativeIntegerAs<int>(*val); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // MaxSuffixComponents |
| 148 | val = m_wire.find(tlv::MaxSuffixComponents); |
| 149 | if (val != m_wire.elements_end()) { |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 150 | m_maxSuffixComponents = readNonNegativeIntegerAs<int>(*val); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // PublisherPublicKeyLocator |
| 154 | val = m_wire.find(tlv::KeyLocator); |
| 155 | if (val != m_wire.elements_end()) { |
| 156 | m_publisherPublicKeyLocator.wireDecode(*val); |
| 157 | } |
| 158 | |
| 159 | // Exclude |
| 160 | val = m_wire.find(tlv::Exclude); |
| 161 | if (val != m_wire.elements_end()) { |
| 162 | m_exclude.wireDecode(*val); |
| 163 | } |
| 164 | |
| 165 | // ChildSelector |
| 166 | val = m_wire.find(tlv::ChildSelector); |
| 167 | if (val != m_wire.elements_end()) { |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 168 | m_childSelector = readNonNegativeIntegerAs<bool>(*val); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 169 | } |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 170 | else { |
| 171 | m_childSelector = DEFAULT_CHILD_SELECTOR; |
| 172 | } |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 173 | |
| 174 | // MustBeFresh |
| 175 | val = m_wire.find(tlv::MustBeFresh); |
| 176 | if (val != m_wire.elements_end()) { |
| 177 | m_mustBeFresh = true; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | Selectors& |
| 182 | Selectors::setMinSuffixComponents(int minSuffixComponents) |
| 183 | { |
| 184 | m_minSuffixComponents = minSuffixComponents; |
| 185 | m_wire.reset(); |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | Selectors& |
| 190 | Selectors::setMaxSuffixComponents(int maxSuffixComponents) |
| 191 | { |
| 192 | m_maxSuffixComponents = maxSuffixComponents; |
| 193 | m_wire.reset(); |
| 194 | return *this; |
| 195 | } |
| 196 | |
| 197 | Selectors& |
| 198 | Selectors::setPublisherPublicKeyLocator(const KeyLocator& keyLocator) |
| 199 | { |
| 200 | m_publisherPublicKeyLocator = keyLocator; |
| 201 | m_wire.reset(); |
| 202 | return *this; |
| 203 | } |
| 204 | |
| 205 | Selectors& |
| 206 | Selectors::setExclude(const Exclude& exclude) |
| 207 | { |
| 208 | m_exclude = exclude; |
| 209 | m_wire.reset(); |
| 210 | return *this; |
| 211 | } |
| 212 | |
| 213 | Selectors& |
| 214 | Selectors::setChildSelector(int childSelector) |
| 215 | { |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 216 | if (childSelector != 0 && childSelector != 1) { |
| 217 | BOOST_THROW_EXCEPTION(std::invalid_argument("ChildSelector must be 0 or 1")); |
| 218 | } |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 219 | m_childSelector = childSelector; |
| 220 | m_wire.reset(); |
| 221 | return *this; |
| 222 | } |
| 223 | |
| 224 | Selectors& |
| 225 | Selectors::setMustBeFresh(bool mustBeFresh) |
| 226 | { |
| 227 | m_mustBeFresh = mustBeFresh; |
| 228 | m_wire.reset(); |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | bool |
| 233 | Selectors::operator==(const Selectors& other) const |
| 234 | { |
| 235 | return wireEncode() == other.wireEncode(); |
| 236 | } |
| 237 | |
| 238 | } // namespace ndn |