Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef NDN_INTEREST_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 7 | #define NDN_INTEREST_HPP |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 9 | #include "name.hpp" |
| 10 | #include "publisher-public-key-digest.hpp" |
| 11 | #include "c/interest.h" |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 12 | |
| 13 | namespace ndn { |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 15 | /** |
| 16 | * An ExcludeEntry holds an ndn_ExcludeType, and if it is a COMPONENT, it holds the component value. |
| 17 | */ |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 18 | class ExcludeEntry { |
| 19 | public: |
| 20 | /** |
| 21 | * Create an ExcludeEntry of type ndn_Exclude_ANY |
| 22 | */ |
| 23 | ExcludeEntry() |
| 24 | : type_(ndn_Exclude_ANY) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Create an ExcludeEntry of type ndn_Exclude_COMPONENT |
| 30 | */ |
| 31 | ExcludeEntry(unsigned char *component, unsigned int componentLen) |
Jeff Thompson | 38d0e08 | 2013-08-12 18:07:44 -0700 | [diff] [blame] | 32 | : type_(ndn_Exclude_COMPONENT), component_(component, componentLen) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 33 | { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Set the type in the excludeEntryStruct and to point to this component, without copying any memory. |
| 38 | * WARNING: The resulting pointer in excludeEntryStruct is invalid after a further use of this object which could reallocate memory. |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 39 | * @param excludeEntryStruct the C ndn_NameComponent struct to receive the pointer |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 40 | */ |
| 41 | void get(struct ndn_ExcludeEntry &excludeEntryStruct) const |
| 42 | { |
| 43 | excludeEntryStruct.type = type_; |
Jeff Thompson | 38d0e08 | 2013-08-12 18:07:44 -0700 | [diff] [blame] | 44 | if (type_ == ndn_Exclude_COMPONENT) |
| 45 | component_.get(excludeEntryStruct.component); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ndn_ExcludeType getType() const { return type_; } |
| 49 | |
Jeff Thompson | 38d0e08 | 2013-08-12 18:07:44 -0700 | [diff] [blame] | 50 | const Name::Component &getComponent() const { return component_; } |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | ndn_ExcludeType type_; |
Jeff Thompson | 38d0e08 | 2013-08-12 18:07:44 -0700 | [diff] [blame] | 54 | Name::Component component_; /**< only used if type_ is ndn_Exclude_COMPONENT */ |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 57 | /** |
| 58 | * An Exclude holds a vector of ExcludeEntry. |
| 59 | */ |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 60 | class Exclude { |
| 61 | public: |
| 62 | /** |
| 63 | * Create a new Exclude with no entries. |
| 64 | */ |
| 65 | Exclude() { |
| 66 | } |
| 67 | |
| 68 | unsigned int getEntryCount() const { |
| 69 | return entries_.size(); |
| 70 | } |
| 71 | |
| 72 | const ExcludeEntry &getEntry(unsigned int i) const { return entries_[i]; } |
| 73 | |
| 74 | /** |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 75 | * Set the excludeStruct to point to the entries in this Exclude, without copying any memory. |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 76 | * WARNING: The resulting pointers in excludeStruct are invalid after a further use of this object which could reallocate memory. |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 77 | * @param excludeStruct a C ndn_Exclude struct where the entries array is already allocated |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 78 | */ |
| 79 | void get(struct ndn_Exclude &excludeStruct) const; |
| 80 | |
| 81 | /** |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 82 | * Clear this Exclude, and set the entries by copying from the ndn_Exclude struct. |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 83 | * @param excludeStruct a C ndn_Exclude struct |
| 84 | */ |
Jeff Thompson | dd3d229 | 2013-07-10 11:59:44 -0700 | [diff] [blame] | 85 | void set(const struct ndn_Exclude &excludeStruct); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Add a new entry of type ndn_Exclude_ANY |
| 89 | */ |
| 90 | void addAny() |
| 91 | { |
| 92 | entries_.push_back(ExcludeEntry()); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Add a new entry of type ndn_Exclude_COMPONENT, copying from component of length compnentLength |
| 97 | */ |
| 98 | void addComponent(unsigned char *component, unsigned int componentLen) |
| 99 | { |
| 100 | entries_.push_back(ExcludeEntry(component, componentLen)); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Clear all the entries. |
| 105 | */ |
| 106 | void clear() { |
| 107 | entries_.clear(); |
| 108 | } |
| 109 | |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Encode this Exclude with elements separated by "," and ndn_Exclude_ANY shown as "*". |
| 112 | * @return the URI string |
| 113 | */ |
| 114 | std::string toUri() const; |
| 115 | |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 116 | private: |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 117 | std::vector<ExcludeEntry> entries_; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 118 | }; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 119 | |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 120 | /** |
| 121 | * An Interest holds a Name and other fields for an interest. |
| 122 | */ |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 123 | class Interest { |
| 124 | public: |
Jeff Thompson | f59a87a | 2013-07-31 16:55:41 -0700 | [diff] [blame] | 125 | Interest(const Name &name, int minSuffixComponents, int maxSuffixComponents, |
| 126 | const PublisherPublicKeyDigest &publisherPublicKeyDigest, const Exclude &exclude, int childSelector, int answerOriginKind, |
| 127 | int scope, double interestLifetimeMilliseconds, const std::vector<unsigned char> &nonce) |
| 128 | : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents), |
| 129 | publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector), |
| 130 | answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds), |
| 131 | nonce_(nonce) |
| 132 | { |
| 133 | } |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 134 | |
| 135 | Interest(const Name &name, int minSuffixComponents, int maxSuffixComponents, |
| 136 | const PublisherPublicKeyDigest &publisherPublicKeyDigest, const Exclude &exclude, int childSelector, int answerOriginKind, |
| 137 | int scope, double interestLifetimeMilliseconds) |
| 138 | : name_(name), minSuffixComponents_(minSuffixComponents), maxSuffixComponents_(maxSuffixComponents), |
| 139 | publisherPublicKeyDigest_(publisherPublicKeyDigest), exclude_(exclude), childSelector_(childSelector), |
| 140 | answerOriginKind_(answerOriginKind), scope_(scope), interestLifetimeMilliseconds_(interestLifetimeMilliseconds) |
| 141 | { |
| 142 | } |
| 143 | |
Jeff Thompson | f62382a | 2013-08-21 16:33:39 -0700 | [diff] [blame] | 144 | Interest(const Name &name, double interestLifetimeMilliseconds) |
| 145 | : name_(name) |
| 146 | { |
| 147 | construct(); |
| 148 | interestLifetimeMilliseconds_ = interestLifetimeMilliseconds; |
| 149 | } |
| 150 | |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 151 | Interest(const Name &name) |
| 152 | : name_(name) |
| 153 | { |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 154 | construct(); |
| 155 | } |
| 156 | |
| 157 | Interest() |
| 158 | { |
| 159 | construct(); |
| 160 | } |
Jeff Thompson | f59a87a | 2013-07-31 16:55:41 -0700 | [diff] [blame] | 161 | |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 162 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 163 | { |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 164 | return wireFormat.encodeInterest(*this); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 165 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 166 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 167 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 168 | return wireEncode(*WireFormat::getDefaultWireFormat()); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 169 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 170 | void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 171 | { |
| 172 | wireFormat.decodeInterest(*this, input, inputLength); |
| 173 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 174 | void wireDecode(const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 175 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 176 | wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat()); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 177 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 178 | void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 179 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 180 | wireDecode(&input[0], input.size(), wireFormat); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 181 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 182 | void wireDecode(const std::vector<unsigned char> &input) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 183 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 184 | wireDecode(&input[0], input.size()); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 185 | } |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * Set the interestStruct to point to the components in this interest, without copying any memory. |
| 189 | * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory. |
| 190 | * @param interestStruct a C ndn_Interest struct where the name components array is already allocated. |
| 191 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 192 | void get(struct ndn_Interest &interestStruct) const; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 193 | |
Jeff Thompson | 12c2776 | 2013-07-09 15:05:11 -0700 | [diff] [blame] | 194 | Name &getName() { return name_; } |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 195 | const Name &getName() const { return name_; } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 196 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 197 | int getMinSuffixComponents() const { return minSuffixComponents_; } |
| 198 | |
| 199 | int getMaxSuffixComponents() const { return maxSuffixComponents_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 200 | |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 201 | PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; } |
| 202 | const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 203 | |
Jeff Thompson | 12c2776 | 2013-07-09 15:05:11 -0700 | [diff] [blame] | 204 | Exclude &getExclude() { return exclude_; } |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 205 | const Exclude &getExclude() const { return exclude_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 206 | |
| 207 | int getChildSelector() const { return childSelector_; } |
| 208 | |
| 209 | int getAnswerOriginKind() const { return answerOriginKind_; } |
| 210 | |
| 211 | int getScope() const { return scope_; } |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 212 | |
Jeff Thompson | 5a5e8b7 | 2013-07-11 14:28:03 -0700 | [diff] [blame] | 213 | double getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 214 | |
Jeff Thompson | 4962c62 | 2013-08-13 13:47:21 -0700 | [diff] [blame] | 215 | const std::vector<unsigned char> &getNonce() const { return nonce_; } |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 216 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Clear this interest, and set the values by copying from the interest struct. |
| 219 | * @param interestStruct a C ndn_Interest struct |
| 220 | */ |
Jeff Thompson | dd3d229 | 2013-07-10 11:59:44 -0700 | [diff] [blame] | 221 | void set(const struct ndn_Interest &interestStruct); |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 222 | |
| 223 | void setMinSuffixComponents(int value) { minSuffixComponents_ = value; } |
| 224 | |
| 225 | void setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; } |
| 226 | |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 227 | void setChildSelector(int value) { childSelector_ = value; } |
| 228 | |
| 229 | void setAnswerOriginKind(int value) { answerOriginKind_ = value; } |
| 230 | |
| 231 | void setScope(int value) { scope_ = value; } |
| 232 | |
Jeff Thompson | 5a5e8b7 | 2013-07-11 14:28:03 -0700 | [diff] [blame] | 233 | void setInterestLifetimeMilliseconds(double value) { interestLifetimeMilliseconds_ = value; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 234 | |
| 235 | void setNonce(const std::vector<unsigned char> &value) { nonce_ = value; } |
| 236 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 237 | private: |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 238 | void construct() |
| 239 | { |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 240 | minSuffixComponents_ = -1; |
| 241 | maxSuffixComponents_ = -1; |
| 242 | childSelector_ = -1; |
| 243 | answerOriginKind_ = -1; |
| 244 | scope_ = -1; |
| 245 | interestLifetimeMilliseconds_ = -1.0; |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 246 | } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 247 | |
| 248 | Name name_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 249 | int minSuffixComponents_; |
| 250 | int maxSuffixComponents_; |
| 251 | PublisherPublicKeyDigest publisherPublicKeyDigest_; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 252 | Exclude exclude_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 253 | int childSelector_; |
| 254 | int answerOriginKind_; |
| 255 | int scope_; |
| 256 | double interestLifetimeMilliseconds_; |
| 257 | std::vector<unsigned char> nonce_; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | #endif |