Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef NDN_INTEREST_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 9 | #define NDN_INTEREST_HPP |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "name.hpp" |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 12 | #include "exclude.hpp" |
| 13 | #include "encoding/block.hpp" |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 16 | |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 17 | /** |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 18 | * An Interest holds a Name and other fields for an interest. |
| 19 | */ |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 20 | class Interest { |
| 21 | public: |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 22 | /** |
| 23 | * Create a new Interest for the given name and values. |
| 24 | * @param name |
| 25 | * @param minSuffixComponents |
| 26 | * @param maxSuffixComponents |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 27 | * @param exclude |
| 28 | * @param childSelector |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 29 | * @param mustBeFresh |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 30 | * @param scope |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 31 | * @param interestLifetime |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 32 | * @param nonce |
| 33 | */ |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 34 | Interest(const Name& name, |
| 35 | int minSuffixComponents, int maxSuffixComponents, |
| 36 | const Exclude& exclude, |
| 37 | int childSelector, |
| 38 | bool mustBeFresh, |
| 39 | int scope, |
| 40 | Milliseconds interestLifetime, |
| 41 | uint32_t nonce = 0) |
| 42 | : name_(name) |
| 43 | , minSuffixComponents_(minSuffixComponents) |
| 44 | , maxSuffixComponents_(maxSuffixComponents) |
| 45 | , exclude_(exclude), childSelector_(childSelector) |
| 46 | , mustBeFresh_(mustBeFresh) |
| 47 | , scope_(scope) |
| 48 | , interestLifetime_(interestLifetime) |
| 49 | , nonce_(nonce) |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Create a new Interest with the given name and interest lifetime and "none" for other values. |
| 55 | * @param name The name for the interest. |
| 56 | * @param interestLifetimeMilliseconds The interest lifetime in milliseconds, or -1 for none. |
| 57 | */ |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 58 | Interest(const Name& name, Milliseconds interestLifetime) |
Jeff Thompson | f62382a | 2013-08-21 16:33:39 -0700 | [diff] [blame] | 59 | : name_(name) |
| 60 | { |
| 61 | construct(); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 62 | interestLifetime_ = interestLifetime; |
Jeff Thompson | f62382a | 2013-08-21 16:33:39 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Create a new Interest with the given name and "none" for other values. |
| 67 | * @param name The name for the interest. |
| 68 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 69 | Interest(const Name& name) |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 70 | : name_(name) |
| 71 | { |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 72 | construct(); |
| 73 | } |
| 74 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Create a new Interest with an empty name and "none" for all values. |
| 77 | */ |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 78 | Interest() |
| 79 | { |
| 80 | construct(); |
| 81 | } |
Jeff Thompson | f59a87a | 2013-07-31 16:55:41 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Encode this Interest for a particular wire format. |
| 85 | * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat(). |
| 86 | * @return The encoded byte array. |
| 87 | */ |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 88 | const Block& |
| 89 | wireEncode() const |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 90 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 91 | return wire_; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 92 | } |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 93 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Decode the input using a particular wire format and update this Interest. |
| 96 | * @param input The input byte array to be decoded. |
| 97 | * @param inputLength The length of input. |
| 98 | * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat(). |
| 99 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 100 | void |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 101 | wireDecode(const Block &wire) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 102 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 103 | // wireFormat.decodeInterest(*this, input, inputLength); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 104 | } |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 105 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Decode the input using a particular wire format and update this Interest. |
| 108 | * @param input The input byte array to be decoded. |
| 109 | * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat(). |
| 110 | */ |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 111 | // void |
| 112 | // wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) |
| 113 | // { |
| 114 | // wireDecode(&input[0], input.size(), wireFormat); |
| 115 | // } |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 118 | * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and |
| 119 | * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1". |
| 120 | * @return The URI string. |
| 121 | */ |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 122 | inline std::string |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 123 | toUri() const; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 124 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 125 | Name& |
| 126 | getName() { return name_; } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 127 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 128 | const Name& |
| 129 | getName() const { return name_; } |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 130 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 131 | int |
| 132 | getMinSuffixComponents() const { return minSuffixComponents_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 133 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 134 | int |
| 135 | getMaxSuffixComponents() const { return maxSuffixComponents_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 136 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 137 | Exclude& |
| 138 | getExclude() { return exclude_; } |
| 139 | |
| 140 | const Exclude& |
| 141 | getExclude() const { return exclude_; } |
| 142 | |
| 143 | int |
| 144 | getChildSelector() const { return childSelector_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 145 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 146 | int |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 147 | getMustBeFresh() const { return mustBeFresh_; } |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 148 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 149 | int |
| 150 | getScope() const { return scope_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 151 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 152 | Milliseconds |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 153 | getInterestLifetime() const { return interestLifetime_; } |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 155 | uint32_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 156 | getNonce() const { return nonce_; } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 157 | |
Jeff Thompson | 4b70d3d | 2013-10-21 17:34:34 -0700 | [diff] [blame] | 158 | void |
| 159 | setName(const Name& name) { name_ = name; } |
| 160 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 161 | void |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 162 | setMinSuffixComponents(int minSuffixComponents) { minSuffixComponents_ = minSuffixComponents; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 163 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 164 | void |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 165 | setMaxSuffixComponents(int maxSuffixComponents) { maxSuffixComponents_ = maxSuffixComponents; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 166 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 167 | void |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 168 | setChildSelector(int childSelector) { childSelector_ = childSelector; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 169 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 170 | void |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 171 | setMustBeFresh(bool mustBeFresh) { mustBeFresh_ = mustBeFresh; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 172 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 173 | void |
Jeff Thompson | 83bf07e | 2013-11-19 11:46:18 -0800 | [diff] [blame] | 174 | setScope(int scope) { scope_ = scope; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 175 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 176 | void |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 177 | setInterestLifetime(Milliseconds interestLifetime) { interestLifetime_ = interestLifetime; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 178 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 179 | void |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 180 | setNonce(uint32_t nonce) { nonce_ = nonce; } |
| 181 | |
| 182 | inline bool |
| 183 | hasSelectors() const; |
| 184 | |
| 185 | inline bool |
| 186 | hasGuiders() const; |
| 187 | |
| 188 | /** |
| 189 | * @brief Check if Interest name matches the given name (using ndn_Name_match) and the given name also conforms to the |
| 190 | * interest selectors. |
| 191 | * @param self A pointer to the ndn_Interest struct. |
| 192 | * @param name A pointer to the name to check. |
| 193 | * @return 1 if the name and interest selectors match, 0 otherwise. |
| 194 | */ |
| 195 | bool |
| 196 | matchesName(const Name &name) const; |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 197 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 198 | private: |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 199 | void |
| 200 | construct() |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 201 | { |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 202 | minSuffixComponents_ = -1; |
| 203 | maxSuffixComponents_ = -1; |
| 204 | childSelector_ = -1; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 205 | mustBeFresh_ = false; // default |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 206 | scope_ = -1; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 207 | interestLifetime_ = -1.0; |
| 208 | nonce_ = 0; |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 209 | } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 210 | |
| 211 | Name name_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 212 | int minSuffixComponents_; |
| 213 | int maxSuffixComponents_; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 214 | Exclude exclude_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 215 | int childSelector_; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 216 | bool mustBeFresh_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 217 | int scope_; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 218 | Milliseconds interestLifetime_; |
| 219 | uint32_t nonce_; |
| 220 | |
| 221 | Block wire_; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 222 | }; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame^] | 223 | |
| 224 | std::ostream & |
| 225 | operator << (std::ostream &os, const Interest &interest); |
| 226 | |
| 227 | inline std::string |
| 228 | Interest::toUri() const |
| 229 | { |
| 230 | std::ostringstream os; |
| 231 | os << *this; |
| 232 | return os.str(); |
| 233 | } |
| 234 | |
| 235 | inline bool |
| 236 | Interest::hasSelectors() const |
| 237 | { |
| 238 | return minSuffixComponents_ >= 0 || |
| 239 | maxSuffixComponents_ >= 0 || |
| 240 | !exclude_.empty() || |
| 241 | childSelector_ >= 0 || |
| 242 | mustBeFresh_ == true || |
| 243 | scope_ >= 0; |
| 244 | } |
| 245 | |
| 246 | inline bool |
| 247 | Interest::hasGuiders() const |
| 248 | { |
| 249 | return scope_ >= 0 || |
| 250 | interestLifetime_ >= 0 || |
| 251 | nonce_ > 0; |
| 252 | } |
| 253 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | #endif |