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