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 | |
| 144 | Interest(const Name &name) |
| 145 | : name_(name) |
| 146 | { |
| 147 | name_ = name; |
| 148 | construct(); |
| 149 | } |
| 150 | |
| 151 | Interest() |
| 152 | { |
| 153 | construct(); |
| 154 | } |
Jeff Thompson | f59a87a | 2013-07-31 16:55:41 -0700 | [diff] [blame] | 155 | |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 156 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 157 | { |
Jeff Thompson | b0979fd | 2013-07-30 15:48:21 -0700 | [diff] [blame] | 158 | return wireFormat.encodeInterest(*this); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 159 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 160 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 161 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 162 | return wireEncode(*WireFormat::getDefaultWireFormat()); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 163 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 164 | void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 165 | { |
| 166 | wireFormat.decodeInterest(*this, input, inputLength); |
| 167 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 168 | void wireDecode(const unsigned char *input, unsigned int inputLength) |
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 | wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat()); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 171 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 172 | void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 173 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 174 | wireDecode(&input[0], input.size(), wireFormat); |
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 | void wireDecode(const std::vector<unsigned char> &input) |
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 | wireDecode(&input[0], input.size()); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 179 | } |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * Set the interestStruct to point to the components in this interest, without copying any memory. |
| 183 | * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory. |
| 184 | * @param interestStruct a C ndn_Interest struct where the name components array is already allocated. |
| 185 | */ |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 186 | void get(struct ndn_Interest &interestStruct) const; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 187 | |
Jeff Thompson | 12c2776 | 2013-07-09 15:05:11 -0700 | [diff] [blame] | 188 | Name &getName() { return name_; } |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 189 | const Name &getName() const { return name_; } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 190 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 191 | int getMinSuffixComponents() const { return minSuffixComponents_; } |
| 192 | |
| 193 | int getMaxSuffixComponents() const { return maxSuffixComponents_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 194 | |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 195 | PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; } |
| 196 | const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 197 | |
Jeff Thompson | 12c2776 | 2013-07-09 15:05:11 -0700 | [diff] [blame] | 198 | Exclude &getExclude() { return exclude_; } |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 199 | const Exclude &getExclude() const { return exclude_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 200 | |
| 201 | int getChildSelector() const { return childSelector_; } |
| 202 | |
| 203 | int getAnswerOriginKind() const { return answerOriginKind_; } |
| 204 | |
| 205 | int getScope() const { return scope_; } |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 206 | |
Jeff Thompson | 5a5e8b7 | 2013-07-11 14:28:03 -0700 | [diff] [blame] | 207 | double getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 208 | |
Jeff Thompson | 4962c62 | 2013-08-13 13:47:21 -0700 | [diff] [blame] | 209 | const std::vector<unsigned char> &getNonce() const { return nonce_; } |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 210 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Clear this interest, and set the values by copying from the interest struct. |
| 213 | * @param interestStruct a C ndn_Interest struct |
| 214 | */ |
Jeff Thompson | dd3d229 | 2013-07-10 11:59:44 -0700 | [diff] [blame] | 215 | void set(const struct ndn_Interest &interestStruct); |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 216 | |
| 217 | void setMinSuffixComponents(int value) { minSuffixComponents_ = value; } |
| 218 | |
| 219 | void setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; } |
| 220 | |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 221 | void setChildSelector(int value) { childSelector_ = value; } |
| 222 | |
| 223 | void setAnswerOriginKind(int value) { answerOriginKind_ = value; } |
| 224 | |
| 225 | void setScope(int value) { scope_ = value; } |
| 226 | |
Jeff Thompson | 5a5e8b7 | 2013-07-11 14:28:03 -0700 | [diff] [blame] | 227 | void setInterestLifetimeMilliseconds(double value) { interestLifetimeMilliseconds_ = value; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 228 | |
| 229 | void setNonce(const std::vector<unsigned char> &value) { nonce_ = value; } |
| 230 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 231 | private: |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 232 | void construct() |
| 233 | { |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 234 | minSuffixComponents_ = -1; |
| 235 | maxSuffixComponents_ = -1; |
| 236 | childSelector_ = -1; |
| 237 | answerOriginKind_ = -1; |
| 238 | scope_ = -1; |
| 239 | interestLifetimeMilliseconds_ = -1.0; |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 240 | } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 241 | |
| 242 | Name name_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 243 | int minSuffixComponents_; |
| 244 | int maxSuffixComponents_; |
| 245 | PublisherPublicKeyDigest publisherPublicKeyDigest_; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 246 | Exclude exclude_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 247 | int childSelector_; |
| 248 | int answerOriginKind_; |
| 249 | int scope_; |
| 250 | double interestLifetimeMilliseconds_; |
| 251 | std::vector<unsigned char> nonce_; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | } |
| 255 | |
| 256 | #endif |