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