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 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 41 | void get(struct ndn_ExcludeEntry& excludeEntryStruct) const |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 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 | 1656e6a | 2013-08-29 18:01:48 -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 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 72 | const ExcludeEntry& getEntry(unsigned int i) const { return entries_[i]; } |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 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 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 79 | void get(struct ndn_Exclude& excludeStruct) const; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 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 | 1656e6a | 2013-08-29 18:01:48 -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 | 1656e6a | 2013-08-29 18:01:48 -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) |
Jeff Thompson | f59a87a | 2013-07-31 16:55:41 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 135 | Interest(const Name& name, int minSuffixComponents, int maxSuffixComponents, |
| 136 | const PublisherPublicKeyDigest& publisherPublicKeyDigest, const Exclude& exclude, int childSelector, int answerOriginKind, |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 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 | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 144 | Interest(const Name& name, double interestLifetimeMilliseconds) |
Jeff Thompson | f62382a | 2013-08-21 16:33:39 -0700 | [diff] [blame] | 145 | : name_(name) |
| 146 | { |
| 147 | construct(); |
| 148 | interestLifetimeMilliseconds_ = interestLifetimeMilliseconds; |
| 149 | } |
| 150 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 151 | Interest(const Name& name) |
Jeff Thompson | 3f76e9e | 2013-08-21 13:14:58 -0700 | [diff] [blame] | 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 | a7516e0 | 2013-09-11 17:12:25 -0700 | [diff] [blame^] | 162 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) 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 | a7516e0 | 2013-09-11 17:12:25 -0700 | [diff] [blame^] | 166 | void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 167 | { |
| 168 | wireFormat.decodeInterest(*this, input, inputLength); |
| 169 | } |
Jeff Thompson | a7516e0 | 2013-09-11 17:12:25 -0700 | [diff] [blame^] | 170 | void wireDecode(const std::vector<unsigned char>& input, WireFormat& wireFormat = *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 | wireDecode(&input[0], input.size(), wireFormat); |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 173 | } |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Set the interestStruct to point to the components in this interest, without copying any memory. |
| 177 | * WARNING: The resulting pointers in interestStruct are invalid after a further use of this object which could reallocate memory. |
| 178 | * @param interestStruct a C ndn_Interest struct where the name components array is already allocated. |
| 179 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 180 | void get(struct ndn_Interest& interestStruct) const; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 181 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 182 | Name& getName() { return name_; } |
| 183 | const Name& getName() const { return name_; } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 184 | |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 185 | int getMinSuffixComponents() const { return minSuffixComponents_; } |
| 186 | |
| 187 | int getMaxSuffixComponents() const { return maxSuffixComponents_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 188 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 189 | PublisherPublicKeyDigest& getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; } |
| 190 | const PublisherPublicKeyDigest& getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 191 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 192 | Exclude& getExclude() { return exclude_; } |
| 193 | const Exclude& getExclude() const { return exclude_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 194 | |
| 195 | int getChildSelector() const { return childSelector_; } |
| 196 | |
| 197 | int getAnswerOriginKind() const { return answerOriginKind_; } |
| 198 | |
| 199 | int getScope() const { return scope_; } |
Jeff Thompson | d345a5b | 2013-07-08 16:18:23 -0700 | [diff] [blame] | 200 | |
Jeff Thompson | 5a5e8b7 | 2013-07-11 14:28:03 -0700 | [diff] [blame] | 201 | double getInterestLifetimeMilliseconds() const { return interestLifetimeMilliseconds_; } |
Jeff Thompson | 0a68172 | 2013-07-08 16:21:54 -0700 | [diff] [blame] | 202 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 203 | const std::vector<unsigned char>& getNonce() const { return nonce_; } |
Jeff Thompson | 2255290 | 2013-07-07 21:26:20 -0700 | [diff] [blame] | 204 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Clear this interest, and set the values by copying from the interest struct. |
| 207 | * @param interestStruct a C ndn_Interest struct |
| 208 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 209 | void set(const struct ndn_Interest& interestStruct); |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 210 | |
| 211 | void setMinSuffixComponents(int value) { minSuffixComponents_ = value; } |
| 212 | |
| 213 | void setMaxSuffixComponents(int value) { maxSuffixComponents_ = value; } |
| 214 | |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 215 | void setChildSelector(int value) { childSelector_ = value; } |
| 216 | |
| 217 | void setAnswerOriginKind(int value) { answerOriginKind_ = value; } |
| 218 | |
| 219 | void setScope(int value) { scope_ = value; } |
| 220 | |
Jeff Thompson | 5a5e8b7 | 2013-07-11 14:28:03 -0700 | [diff] [blame] | 221 | void setInterestLifetimeMilliseconds(double value) { interestLifetimeMilliseconds_ = value; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 222 | |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 223 | void setNonce(const std::vector<unsigned char>& value) { nonce_ = value; } |
Jeff Thompson | eb316a8 | 2013-07-09 15:11:17 -0700 | [diff] [blame] | 224 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 225 | private: |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 226 | void construct() |
| 227 | { |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 228 | minSuffixComponents_ = -1; |
| 229 | maxSuffixComponents_ = -1; |
| 230 | childSelector_ = -1; |
| 231 | answerOriginKind_ = -1; |
| 232 | scope_ = -1; |
| 233 | interestLifetimeMilliseconds_ = -1.0; |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 234 | } |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 235 | |
| 236 | Name name_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 237 | int minSuffixComponents_; |
| 238 | int maxSuffixComponents_; |
| 239 | PublisherPublicKeyDigest publisherPublicKeyDigest_; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 240 | Exclude exclude_; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 241 | int childSelector_; |
| 242 | int answerOriginKind_; |
| 243 | int scope_; |
| 244 | double interestLifetimeMilliseconds_; |
| 245 | std::vector<unsigned char> nonce_; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | } |
| 249 | |
| 250 | #endif |