blob: 56943948db206ef527d980e16450c53696482472 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07006 */
7
8#ifndef NDN_INTEREST_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_INTEREST_HPP
Jeff Thompsonb7f95562013-07-03 18:36:42 -070010
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "name.hpp"
Alexander Afanasyev84681982014-01-03 13:26:09 -080012#include "exclude.hpp"
13#include "encoding/block.hpp"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070014
15namespace ndn {
Jeff Thompsonfe556862013-07-09 13:52:55 -070016
Jeff Thompson8238d002013-07-10 11:56:49 -070017/**
Jeff Thompson8238d002013-07-10 11:56:49 -070018 * An Interest holds a Name and other fields for an interest.
19 */
Jeff Thompsonb7f95562013-07-03 18:36:42 -070020class Interest {
21public:
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080022 /**
23 * Create a new Interest for the given name and values.
24 * @param name
25 * @param minSuffixComponents
26 * @param maxSuffixComponents
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080027 * @param exclude
28 * @param childSelector
Alexander Afanasyev84681982014-01-03 13:26:09 -080029 * @param mustBeFresh
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080030 * @param scope
Alexander Afanasyev84681982014-01-03 13:26:09 -080031 * @param interestLifetime
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080032 * @param nonce
33 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080034 Interest(const Name& name,
35 int minSuffixComponents, int maxSuffixComponents,
36 const Exclude& exclude,
37 int childSelector,
38 bool mustBeFresh,
39 int scope,
40 Milliseconds interestLifetime,
41 uint32_t nonce = 0)
42 : name_(name)
43 , minSuffixComponents_(minSuffixComponents)
44 , maxSuffixComponents_(maxSuffixComponents)
45 , exclude_(exclude), childSelector_(childSelector)
46 , mustBeFresh_(mustBeFresh)
47 , scope_(scope)
48 , interestLifetime_(interestLifetime)
49 , nonce_(nonce)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070050 {
51 }
52
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080053 /**
54 * Create a new Interest with the given name and interest lifetime and "none" for other values.
55 * @param name The name for the interest.
56 * @param interestLifetimeMilliseconds The interest lifetime in milliseconds, or -1 for none.
57 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080058 Interest(const Name& name, Milliseconds interestLifetime)
Jeff Thompsonf62382a2013-08-21 16:33:39 -070059 : name_(name)
60 {
61 construct();
Alexander Afanasyev84681982014-01-03 13:26:09 -080062 interestLifetime_ = interestLifetime;
Jeff Thompsonf62382a2013-08-21 16:33:39 -070063 }
64
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080065 /**
66 * Create a new Interest with the given name and "none" for other values.
67 * @param name The name for the interest.
68 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -070069 Interest(const Name& name)
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070070 : name_(name)
71 {
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070072 construct();
73 }
74
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080075 /**
76 * Create a new Interest with an empty name and "none" for all values.
77 */
Jeff Thompson3f76e9e2013-08-21 13:14:58 -070078 Interest()
79 {
80 construct();
81 }
Jeff Thompsonf59a87a2013-07-31 16:55:41 -070082
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080083 /**
84 * Encode this Interest for a particular wire format.
85 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
86 * @return The encoded byte array.
87 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080088 const Block&
89 wireEncode() const
Jeff Thompsonb7f95562013-07-03 18:36:42 -070090 {
Alexander Afanasyev84681982014-01-03 13:26:09 -080091 return wire_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -070092 }
Jeff Thompson0050abe2013-09-17 12:50:25 -070093
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080094 /**
95 * Decode the input using a particular wire format and update this Interest.
96 * @param input The input byte array to be decoded.
97 * @param inputLength The length of input.
98 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
99 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700100 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800101 wireDecode(const Block &wire)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700102 {
Alexander Afanasyev84681982014-01-03 13:26:09 -0800103 // wireFormat.decodeInterest(*this, input, inputLength);
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700104 }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700105
Jeff Thompson1b4a7b12013-11-15 12:00:02 -0800106 /**
107 * Decode the input using a particular wire format and update this Interest.
108 * @param input The input byte array to be decoded.
109 * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat::getDefaultWireFormat().
110 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800111 // void
112 // wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
113 // {
114 // wireDecode(&input[0], input.size(), wireFormat);
115 // }
Jeff Thompsond9e278c2013-07-08 15:20:13 -0700116
117 /**
Jeff Thompson13e280b2013-12-03 13:12:23 -0800118 * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and
119 * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1".
120 * @return The URI string.
121 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800122 inline std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800123 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700124
Jeff Thompson0050abe2013-09-17 12:50:25 -0700125 Name&
126 getName() { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700127
Jeff Thompson0050abe2013-09-17 12:50:25 -0700128 const Name&
129 getName() const { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700130
Jeff Thompson0050abe2013-09-17 12:50:25 -0700131 int
132 getMinSuffixComponents() const { return minSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700133
Jeff Thompson0050abe2013-09-17 12:50:25 -0700134 int
135 getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700136
Jeff Thompson0050abe2013-09-17 12:50:25 -0700137 Exclude&
138 getExclude() { return exclude_; }
139
140 const Exclude&
141 getExclude() const { return exclude_; }
142
143 int
144 getChildSelector() const { return childSelector_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700145
Jeff Thompson0050abe2013-09-17 12:50:25 -0700146 int
Alexander Afanasyev84681982014-01-03 13:26:09 -0800147 getMustBeFresh() const { return mustBeFresh_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700148
Jeff Thompson0050abe2013-09-17 12:50:25 -0700149 int
150 getScope() const { return scope_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700151
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700152 Milliseconds
Alexander Afanasyev84681982014-01-03 13:26:09 -0800153 getInterestLifetime() const { return interestLifetime_; }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700154
Alexander Afanasyev84681982014-01-03 13:26:09 -0800155 uint32_t
Jeff Thompson0050abe2013-09-17 12:50:25 -0700156 getNonce() const { return nonce_; }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800157
Jeff Thompson4b70d3d2013-10-21 17:34:34 -0700158 void
159 setName(const Name& name) { name_ = name; }
160
Jeff Thompson0050abe2013-09-17 12:50:25 -0700161 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800162 setMinSuffixComponents(int minSuffixComponents) { minSuffixComponents_ = minSuffixComponents; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700163
Jeff Thompson0050abe2013-09-17 12:50:25 -0700164 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800165 setMaxSuffixComponents(int maxSuffixComponents) { maxSuffixComponents_ = maxSuffixComponents; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700166
Jeff Thompson0050abe2013-09-17 12:50:25 -0700167 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800168 setChildSelector(int childSelector) { childSelector_ = childSelector; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700169
Jeff Thompson0050abe2013-09-17 12:50:25 -0700170 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800171 setMustBeFresh(bool mustBeFresh) { mustBeFresh_ = mustBeFresh; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700172
Jeff Thompson0050abe2013-09-17 12:50:25 -0700173 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800174 setScope(int scope) { scope_ = scope; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700175
Jeff Thompson0050abe2013-09-17 12:50:25 -0700176 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800177 setInterestLifetime(Milliseconds interestLifetime) { interestLifetime_ = interestLifetime; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700178
Jeff Thompson0050abe2013-09-17 12:50:25 -0700179 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800180 setNonce(uint32_t nonce) { nonce_ = nonce; }
181
182 inline bool
183 hasSelectors() const;
184
185 inline bool
186 hasGuiders() const;
187
188 /**
189 * @brief Check if Interest name matches the given name (using ndn_Name_match) and the given name also conforms to the
190 * interest selectors.
191 * @param self A pointer to the ndn_Interest struct.
192 * @param name A pointer to the name to check.
193 * @return 1 if the name and interest selectors match, 0 otherwise.
194 */
195 bool
196 matchesName(const Name &name) const;
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700197
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700198private:
Jeff Thompson0050abe2013-09-17 12:50:25 -0700199 void
200 construct()
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700201 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700202 minSuffixComponents_ = -1;
203 maxSuffixComponents_ = -1;
204 childSelector_ = -1;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800205 mustBeFresh_ = false; // default
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700206 scope_ = -1;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800207 interestLifetime_ = -1.0;
208 nonce_ = 0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700209 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700210
211 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700212 int minSuffixComponents_;
213 int maxSuffixComponents_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700214 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700215 int childSelector_;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800216 bool mustBeFresh_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700217 int scope_;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800218 Milliseconds interestLifetime_;
219 uint32_t nonce_;
220
221 Block wire_;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700222};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800223
224std::ostream &
225operator << (std::ostream &os, const Interest &interest);
226
227inline std::string
228Interest::toUri() const
229{
230 std::ostringstream os;
231 os << *this;
232 return os.str();
233}
234
235inline bool
236Interest::hasSelectors() const
237{
238 return minSuffixComponents_ >= 0 ||
239 maxSuffixComponents_ >= 0 ||
240 !exclude_.empty() ||
241 childSelector_ >= 0 ||
242 mustBeFresh_ == true ||
243 scope_ >= 0;
244}
245
246inline bool
247Interest::hasGuiders() const
248{
249 return scope_ >= 0 ||
250 interestLifetime_ >= 0 ||
251 nonce_ > 0;
252}
253
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700254}
255
256#endif