Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_SELECTORS_HPP |
| 8 | #define NDN_SELECTORS_HPP |
| 9 | |
| 10 | #include "common.hpp" |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 11 | #include "key-locator.hpp" |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 12 | #include "exclude.hpp" |
| 13 | #include "encoding/encoding-buffer.hpp" |
| 14 | |
| 15 | namespace ndn { |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 16 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 17 | /** |
| 18 | * @brief Abstraction implementing Interest selectors |
| 19 | */ |
| 20 | class Selectors |
| 21 | { |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 22 | public: |
| 23 | Selectors() |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 24 | : m_minSuffixComponents(-1) |
| 25 | , m_maxSuffixComponents(-1) |
| 26 | , m_childSelector(-1) |
| 27 | , m_mustBeFresh(false) |
| 28 | { |
| 29 | } |
| 30 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 31 | /** @deprecated Selectors().setX(...).setY(...) |
| 32 | */ |
| 33 | Selectors(int minSuffixComponents, int maxSuffixComponents, |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 34 | const Exclude& exclude, |
| 35 | int childSelector, |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 36 | bool mustBeFresh) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 37 | : m_minSuffixComponents(minSuffixComponents) |
| 38 | , m_maxSuffixComponents(maxSuffixComponents) |
| 39 | , m_exclude(exclude) |
| 40 | , m_childSelector(childSelector) |
| 41 | , m_mustBeFresh(mustBeFresh) |
| 42 | { |
| 43 | } |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 44 | |
| 45 | Selectors(const Block& wire) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 46 | { |
| 47 | wireDecode(wire); |
| 48 | } |
| 49 | |
| 50 | bool |
| 51 | empty() const; |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 53 | /** |
| 54 | * @brief Fast encoding or block size estimation |
| 55 | */ |
| 56 | template<bool T> |
| 57 | size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 58 | wireEncode(EncodingImpl<T>& block) const; |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 60 | /** |
| 61 | * @brief Encode to a wire format |
| 62 | */ |
| 63 | const Block& |
| 64 | wireEncode() const; |
| 65 | |
| 66 | /** |
| 67 | * @brief Decode the input from wire format |
| 68 | */ |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 69 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 70 | wireDecode(const Block& wire); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 71 | |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 75 | |
| 76 | int |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 77 | getMinSuffixComponents() const |
| 78 | { |
| 79 | return m_minSuffixComponents; |
| 80 | } |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 81 | |
| 82 | Selectors& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 83 | setMinSuffixComponents(int minSuffixComponents) |
| 84 | { |
| 85 | m_minSuffixComponents = minSuffixComponents; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 86 | m_wire.reset(); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | // |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 91 | |
| 92 | int |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 93 | getMaxSuffixComponents() const |
| 94 | { |
| 95 | return m_maxSuffixComponents; |
| 96 | } |
| 97 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 98 | Selectors& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 99 | setMaxSuffixComponents(int maxSuffixComponents) |
| 100 | { |
| 101 | m_maxSuffixComponents = maxSuffixComponents; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 102 | m_wire.reset(); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 103 | return *this; |
| 104 | } |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 106 | // |
| 107 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 108 | const KeyLocator& |
| 109 | getPublisherPublicKeyLocator() const |
| 110 | { |
| 111 | return m_publisherPublicKeyLocator; |
| 112 | } |
| 113 | |
| 114 | Selectors& |
| 115 | setPublisherPublicKeyLocator(const KeyLocator& keyLocator) |
| 116 | { |
| 117 | m_publisherPublicKeyLocator = keyLocator; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 118 | m_wire.reset(); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | // |
| 123 | |
| 124 | const Exclude& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 125 | getExclude() const |
| 126 | { |
| 127 | return m_exclude; |
| 128 | } |
| 129 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 130 | Selectors& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 131 | setExclude(const Exclude& exclude) |
| 132 | { |
| 133 | m_exclude = exclude; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 134 | m_wire.reset(); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | // |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 139 | |
| 140 | int |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 141 | getChildSelector() const |
| 142 | { |
| 143 | return m_childSelector; |
| 144 | } |
| 145 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 146 | Selectors& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 147 | setChildSelector(int childSelector) |
| 148 | { |
| 149 | m_childSelector = childSelector; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 150 | m_wire.reset(); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | // |
| 155 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 156 | int |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 157 | getMustBeFresh() const |
| 158 | { |
| 159 | return m_mustBeFresh; |
| 160 | } |
| 161 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 162 | Selectors& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 163 | setMustBeFresh(bool mustBeFresh) |
| 164 | { |
| 165 | m_mustBeFresh = mustBeFresh; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 166 | m_wire.reset(); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 167 | return *this; |
| 168 | } |
| 169 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 170 | public: // EqualityComparable concept |
| 171 | bool |
| 172 | operator==(const Selectors& other) const |
| 173 | { |
| 174 | return wireEncode() == other.wireEncode(); |
| 175 | } |
| 176 | |
| 177 | bool |
| 178 | operator!=(const Selectors& other) const |
| 179 | { |
| 180 | return !(*this == other); |
| 181 | } |
| 182 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 183 | private: |
| 184 | int m_minSuffixComponents; |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 185 | int m_maxSuffixComponents; |
| 186 | KeyLocator m_publisherPublicKeyLocator; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 187 | Exclude m_exclude; |
| 188 | int m_childSelector; |
| 189 | bool m_mustBeFresh; |
| 190 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 191 | mutable Block m_wire; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | inline bool |
| 195 | Selectors::empty() const |
| 196 | { |
| 197 | return |
| 198 | (m_minSuffixComponents < 0 && |
| 199 | m_maxSuffixComponents < 0 && |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 200 | m_publisherPublicKeyLocator.empty() && |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 201 | m_exclude.empty() && |
| 202 | m_childSelector < 0 && |
| 203 | !m_mustBeFresh); |
| 204 | } |
| 205 | |
| 206 | template<bool T> |
| 207 | inline size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 208 | Selectors::wireEncode(EncodingImpl<T>& block) const |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 209 | { |
| 210 | size_t total_len = 0; |
| 211 | |
| 212 | // Selectors ::= SELECTORS-TYPE TLV-LENGTH |
| 213 | // MinSuffixComponents? |
| 214 | // MaxSuffixComponents? |
| 215 | // PublisherPublicKeyLocator? |
| 216 | // Exclude? |
| 217 | // ChildSelector? |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 218 | // MustBeFresh? |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 219 | |
| 220 | // (reverse encoding) |
| 221 | |
| 222 | // MustBeFresh |
| 223 | if (getMustBeFresh()) |
| 224 | { |
| 225 | total_len += prependBooleanBlock(block, Tlv::MustBeFresh); |
| 226 | } |
| 227 | |
| 228 | // ChildSelector |
| 229 | if (getChildSelector() >= 0) |
| 230 | { |
| 231 | total_len += prependNonNegativeIntegerBlock(block, Tlv::ChildSelector, getChildSelector()); |
| 232 | } |
| 233 | |
| 234 | // Exclude |
| 235 | if (!getExclude().empty()) |
| 236 | { |
| 237 | total_len += getExclude().wireEncode(block); |
| 238 | } |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 239 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 240 | // PublisherPublicKeyLocator |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 241 | if (!getPublisherPublicKeyLocator().empty()) |
| 242 | { |
| 243 | total_len += getPublisherPublicKeyLocator().wireEncode(block); |
| 244 | } |
| 245 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 246 | // MaxSuffixComponents |
| 247 | if (getMaxSuffixComponents() >= 0) |
| 248 | { |
| 249 | total_len += prependNonNegativeIntegerBlock(block, Tlv::MaxSuffixComponents, |
| 250 | getMaxSuffixComponents()); |
| 251 | } |
| 252 | |
| 253 | // MinSuffixComponents |
| 254 | if (getMinSuffixComponents() >= 0) |
| 255 | { |
| 256 | total_len += prependNonNegativeIntegerBlock(block, Tlv::MinSuffixComponents, |
| 257 | getMinSuffixComponents()); |
| 258 | } |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 259 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 260 | total_len += block.prependVarNumber(total_len); |
| 261 | total_len += block.prependVarNumber(Tlv::Selectors); |
| 262 | return total_len; |
| 263 | } |
| 264 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 265 | inline const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 266 | Selectors::wireEncode() const |
| 267 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 268 | if (m_wire.hasWire()) |
| 269 | return m_wire; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 270 | |
| 271 | EncodingEstimator estimator; |
| 272 | size_t estimatedSize = wireEncode(estimator); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 273 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 274 | EncodingBuffer buffer(estimatedSize, 0); |
| 275 | wireEncode(buffer); |
| 276 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 277 | m_wire = buffer.block(); |
| 278 | return m_wire; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | inline void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 282 | Selectors::wireDecode(const Block& wire) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 283 | { |
| 284 | if (wire.type() != Tlv::Selectors) |
| 285 | throw Tlv::Error("Unexpected TLV type when decoding Selectors"); |
| 286 | |
| 287 | *this = Selectors(); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 288 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 289 | m_wire = wire; |
| 290 | m_wire.parse(); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 291 | |
| 292 | // MinSuffixComponents |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 293 | Block::element_const_iterator val = m_wire.find(Tlv::MinSuffixComponents); |
| 294 | if (val != m_wire.elements_end()) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 295 | { |
| 296 | m_minSuffixComponents = readNonNegativeInteger(*val); |
| 297 | } |
| 298 | |
| 299 | // MaxSuffixComponents |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 300 | val = m_wire.find(Tlv::MaxSuffixComponents); |
| 301 | if (val != m_wire.elements_end()) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 302 | { |
| 303 | m_maxSuffixComponents = readNonNegativeInteger(*val); |
| 304 | } |
| 305 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 306 | // PublisherPublicKeyLocator |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 307 | val = m_wire.find(Tlv::KeyLocator); |
| 308 | if (val != m_wire.elements_end()) |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 309 | { |
| 310 | m_publisherPublicKeyLocator.wireDecode(*val); |
| 311 | } |
| 312 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 313 | // Exclude |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 314 | val = m_wire.find(Tlv::Exclude); |
| 315 | if (val != m_wire.elements_end()) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 316 | { |
| 317 | m_exclude.wireDecode(*val); |
| 318 | } |
| 319 | |
| 320 | // ChildSelector |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 321 | val = m_wire.find(Tlv::ChildSelector); |
| 322 | if (val != m_wire.elements_end()) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 323 | { |
| 324 | m_childSelector = readNonNegativeInteger(*val); |
| 325 | } |
| 326 | |
| 327 | //MustBeFresh aka AnswerOriginKind |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 328 | val = m_wire.find(Tlv::MustBeFresh); |
| 329 | if (val != m_wire.elements_end()) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 330 | { |
| 331 | m_mustBeFresh = true; |
| 332 | } |
| 333 | } |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 334 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 335 | } // namespace ndn |
| 336 | |
| 337 | #endif // NDN_SELECTORS_HPP |