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