blob: 29ce962aa4f4bf13d88b6f2feeff3faa1db9a00e [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.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080085 * @return The encoded byte array.
86 */
Alexander Afanasyev84681982014-01-03 13:26:09 -080087 const Block&
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080088 wireEncode() const;
Jeff Thompson0050abe2013-09-17 12:50:25 -070089
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080090 /**
91 * Decode the input using a particular wire format and update this Interest.
92 * @param input The input byte array to be decoded.
Jeff Thompson1b4a7b12013-11-15 12:00:02 -080093 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070094 void
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080095 wireDecode(const Block &wire);
Jeff Thompsond9e278c2013-07-08 15:20:13 -070096
97 /**
Jeff Thompson13e280b2013-12-03 13:12:23 -080098 * Encode the name according to the "NDN URI Scheme". If there are interest selectors, append "?" and
99 * added the selectors as a query string. For example "/test/name?ndn.ChildSelector=1".
100 * @return The URI string.
101 */
Alexander Afanasyev84681982014-01-03 13:26:09 -0800102 inline std::string
Jeff Thompson13e280b2013-12-03 13:12:23 -0800103 toUri() const;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700104
Jeff Thompson0050abe2013-09-17 12:50:25 -0700105 Name&
106 getName() { return name_; }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700107
Jeff Thompson0050abe2013-09-17 12:50:25 -0700108 const Name&
109 getName() const { return name_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700110
Jeff Thompson0050abe2013-09-17 12:50:25 -0700111 int
112 getMinSuffixComponents() const { return minSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700113
Jeff Thompson0050abe2013-09-17 12:50:25 -0700114 int
115 getMaxSuffixComponents() const { return maxSuffixComponents_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700116
Jeff Thompson0050abe2013-09-17 12:50:25 -0700117 Exclude&
118 getExclude() { return exclude_; }
119
120 const Exclude&
121 getExclude() const { return exclude_; }
122
123 int
124 getChildSelector() const { return childSelector_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700125
Jeff Thompson0050abe2013-09-17 12:50:25 -0700126 int
Alexander Afanasyev84681982014-01-03 13:26:09 -0800127 getMustBeFresh() const { return mustBeFresh_; }
Jeff Thompsond345a5b2013-07-08 16:18:23 -0700128
Jeff Thompson0050abe2013-09-17 12:50:25 -0700129 int
130 getScope() const { return scope_; }
Jeff Thompson0a681722013-07-08 16:21:54 -0700131
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700132 Milliseconds
Alexander Afanasyev84681982014-01-03 13:26:09 -0800133 getInterestLifetime() const { return interestLifetime_; }
Jeff Thompson0050abe2013-09-17 12:50:25 -0700134
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800135 /**
136 * @brief Get Interest's nonce
137 *
138 * If nonce was not set before this call, it will be automatically assigned to a random value
Alexander Afanasyev85480842014-01-06 14:46:54 -0800139 *
140 * Const reference needed for C decoding
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800141 */
Alexander Afanasyev85480842014-01-06 14:46:54 -0800142 const uint32_t&
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800143 getNonce() const;
Yingdi Yua4e57672014-02-06 11:16:17 -0800144
145 uint64_t
146 getIncomingFaceId() const { return m_incomingFaceId; }
Alexander Afanasyev84681982014-01-03 13:26:09 -0800147
Jeff Thompson4b70d3d2013-10-21 17:34:34 -0700148 void
149 setName(const Name& name) { name_ = name; }
150
Jeff Thompson0050abe2013-09-17 12:50:25 -0700151 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800152 setMinSuffixComponents(int minSuffixComponents) { minSuffixComponents_ = minSuffixComponents; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700153
Jeff Thompson0050abe2013-09-17 12:50:25 -0700154 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800155 setMaxSuffixComponents(int maxSuffixComponents) { maxSuffixComponents_ = maxSuffixComponents; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700156
Jeff Thompson0050abe2013-09-17 12:50:25 -0700157 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800158 setChildSelector(int childSelector) { childSelector_ = childSelector; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700159
Jeff Thompson0050abe2013-09-17 12:50:25 -0700160 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800161 setMustBeFresh(bool mustBeFresh) { mustBeFresh_ = mustBeFresh; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700162
Jeff Thompson0050abe2013-09-17 12:50:25 -0700163 void
Jeff Thompson83bf07e2013-11-19 11:46:18 -0800164 setScope(int scope) { scope_ = scope; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700165
Jeff Thompson0050abe2013-09-17 12:50:25 -0700166 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800167 setInterestLifetime(Milliseconds interestLifetime) { interestLifetime_ = interestLifetime; }
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700168
Jeff Thompson0050abe2013-09-17 12:50:25 -0700169 void
Alexander Afanasyev84681982014-01-03 13:26:09 -0800170 setNonce(uint32_t nonce) { nonce_ = nonce; }
171
Yingdi Yua4e57672014-02-06 11:16:17 -0800172 void
173 setIncomingFaceId(uint64_t incomingFaceId) { m_incomingFaceId = incomingFaceId; }
174
Alexander Afanasyev84681982014-01-03 13:26:09 -0800175 inline bool
176 hasSelectors() const;
177
178 inline bool
179 hasGuiders() const;
180
181 /**
182 * @brief Check if Interest name matches the given name (using ndn_Name_match) and the given name also conforms to the
183 * interest selectors.
184 * @param self A pointer to the ndn_Interest struct.
185 * @param name A pointer to the name to check.
186 * @return 1 if the name and interest selectors match, 0 otherwise.
187 */
188 bool
189 matchesName(const Name &name) const;
Jeff Thompsoneb316a82013-07-09 15:11:17 -0700190
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700191private:
Jeff Thompson0050abe2013-09-17 12:50:25 -0700192 void
193 construct()
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700194 {
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700195 minSuffixComponents_ = -1;
196 maxSuffixComponents_ = -1;
197 childSelector_ = -1;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800198 mustBeFresh_ = false; // default
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700199 scope_ = -1;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800200 interestLifetime_ = -1.0;
201 nonce_ = 0;
Jeff Thompsonc0486c12013-07-16 14:36:16 -0700202 }
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700203
204 Name name_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700205 int minSuffixComponents_;
206 int maxSuffixComponents_;
Jeff Thompsonfe556862013-07-09 13:52:55 -0700207 Exclude exclude_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700208 int childSelector_;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800209 bool mustBeFresh_;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700210 int scope_;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800211 Milliseconds interestLifetime_;
Alexander Afanasyev840139f2013-12-28 15:02:50 -0800212 mutable uint32_t nonce_;
Alexander Afanasyev84681982014-01-03 13:26:09 -0800213
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800214 mutable Block wire_;
Yingdi Yua4e57672014-02-06 11:16:17 -0800215
216 uint64_t m_incomingFaceId;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700217};
Alexander Afanasyev84681982014-01-03 13:26:09 -0800218
219std::ostream &
220operator << (std::ostream &os, const Interest &interest);
221
222inline std::string
223Interest::toUri() const
224{
225 std::ostringstream os;
226 os << *this;
227 return os.str();
228}
229
230inline bool
231Interest::hasSelectors() const
232{
233 return minSuffixComponents_ >= 0 ||
234 maxSuffixComponents_ >= 0 ||
235 !exclude_.empty() ||
236 childSelector_ >= 0 ||
237 mustBeFresh_ == true ||
238 scope_ >= 0;
239}
240
241inline bool
242Interest::hasGuiders() const
243{
244 return scope_ >= 0 ||
245 interestLifetime_ >= 0 ||
246 nonce_ > 0;
247}
248
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700249}
250
251#endif