blob: 0c77d88500335f0a40c0a26bdcfe31e91b5a83ab [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
Jeff Thompsonfe556862013-07-09 13:52:55 -07008#include <stdexcept>
Jeff Thompson25b4e612013-10-10 16:03:24 -07009#include <ndn-cpp/common.hpp>
10#include <ndn-cpp/interest.hpp>
Jeff Thompsonb7f95562013-07-03 18:36:42 -070011
12using namespace std;
13
14namespace ndn {
Alexander Afanasyev84681982014-01-03 13:26:09 -080015
16bool
17Interest::matchesName(const Name &name) const
Jeff Thompson25b4e612013-10-10 16:03:24 -070018{
Alexander Afanasyev84681982014-01-03 13:26:09 -080019 if (!name_.isPrefixOf(name))
20 return false;
21
22 if (minSuffixComponents_ >= 0 &&
23 // Add 1 for the implicit digest.
24 !(name.size() + 1 - name_.size() >= minSuffixComponents_))
25 return false;
26
27 if (maxSuffixComponents_ >= 0 &&
28 // Add 1 for the implicit digest.
29 !(name.size() + 1 - name_.size() <= maxSuffixComponents_))
30 return false;
31
32 if (!exclude_.empty() && name.size() > name_.size() &&
33 exclude_.isExcluded(name[name_.size()]))
34 return false;
35
36 return true;
Jeff Thompson25b4e612013-10-10 16:03:24 -070037}
38
Alexander Afanasyev84681982014-01-03 13:26:09 -080039std::ostream &
40operator << (std::ostream &os, const Interest &interest)
Jeff Thompsonfe556862013-07-09 13:52:55 -070041{
Alexander Afanasyev84681982014-01-03 13:26:09 -080042 os << interest.getName();
Jeff Thompsonfe556862013-07-09 13:52:55 -070043
Alexander Afanasyev84681982014-01-03 13:26:09 -080044 char delim = '?';
45
46 if (interest.getMinSuffixComponents() >= 0) {
47 os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents();
48 delim = '&';
Jeff Thompsonfe556862013-07-09 13:52:55 -070049 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080050 if (interest.getMaxSuffixComponents() >= 0) {
51 os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents();
52 delim = '&';
Jeff Thompson37527d62013-08-21 11:15:54 -070053 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080054 if (interest.getChildSelector() >= 0) {
55 os << delim << "ndn.ChildSelector=" << interest.getChildSelector();
56 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080057 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080058 if (interest.getMustBeFresh()) {
59 os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh();
60 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080061 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080062 if (interest.getScope() >= 0) {
63 os << delim << "ndn.Scope=" << interest.getScope();
64 delim = '&';
Jeff Thompson13e280b2013-12-03 13:12:23 -080065 }
Alexander Afanasyev84681982014-01-03 13:26:09 -080066 if (interest.getInterestLifetime() >= 0) {
67 os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime();
68 delim = '&';
69 }
70
71 if (interest.getNonce() > 0) {
72 os << delim << "ndn.Nonce=" << interest.getNonce();
73 delim = '&';
74 }
75 if (!interest.getExclude().empty()) {
76 os << delim << "ndn.Exclude=" << interest.getExclude();
77 delim = '&';
78 }
79
80 return os;
Jeff Thompson13e280b2013-12-03 13:12:23 -080081}
82
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080083const Block&
84Interest::wireEncode() const
85{
86 if (wire_.hasWire())
87 return wire_;
88
Alexander Afanasyevff0a3942014-01-03 15:31:28 -080089 // Interest ::= INTEREST-TYPE TLV-LENGTH
90 // Name
91 // Selectors?
92 // Nonce
93 // Scope?
94 // InterestLifetime?
95
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -080096 wire_ = Block(Tlv::Interest);
97 wire_.push_back(getName().wireEncode());
98
99 // selectors
100 {
101 Block selectors(Tlv::Selectors);
102
103 if (getMinSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800104 selectors.push_back
105 (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800106 }
107 if (getMaxSuffixComponents() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800108 selectors.push_back
109 (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800110 }
111 if (!getExclude().empty()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800112 selectors.push_back
113 (getExclude().wireEncode());
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800114 }
115 if (getChildSelector() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800116 selectors.push_back
117 (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800118 }
119 if (getMustBeFresh()) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800120 selectors.push_back
121 (booleanBlock(Tlv::MustBeFresh));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800122 }
123
124 selectors.encode();
125 wire_.push_back(selectors);
126 }
127
128 // Nonce
129 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800130 wire_.push_back
131 (nonNegativeIntegerBlock(Tlv::Nonce, getNonce()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800132 }
133
134 if (getScope() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800135 wire_.push_back
136 (nonNegativeIntegerBlock(Tlv::Scope, getScope()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800137 }
138 if (getInterestLifetime() >= 0) {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800139 wire_.push_back
140 (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime()));
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800141 }
142
143 wire_.encode();
144 return wire_;
145}
146
147void
148Interest::wireDecode(const Block &wire)
149{
150 wire_ = wire;
151 wire_.parse();
152
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800153 // Interest ::= INTEREST-TYPE TLV-LENGTH
154 // Name
155 // Selectors?
156 // Nonce
157 // Scope?
158 // InterestLifetime?
159
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800160 // Name
161 name_.wireDecode(wire_.get(Tlv::Name));
162
163 // Selectors
164 Block::element_iterator selectors = wire_.find(Tlv::Selectors);
165 if (selectors != wire_.getAll().end())
166 {
167 selectors->parse();
168
169 // MinSuffixComponents
170 Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents);
171 if (val != selectors->getAll().end())
172 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800173 minSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800174 }
175
176 // MaxSuffixComponents
177 val = selectors->find(Tlv::MaxSuffixComponents);
178 if (val != selectors->getAll().end())
179 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800180 maxSuffixComponents_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800181 }
182
183 // Exclude
184 val = selectors->find(Tlv::Exclude);
185 if (val != selectors->getAll().end())
186 {
187 exclude_.wireDecode(*val);
188 }
189
190 // ChildSelector
191 val = selectors->find(Tlv::ChildSelector);
192 if (val != selectors->getAll().end())
193 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800194 childSelector_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800195 }
196
197 //MustBeFresh aka AnswerOriginKind
198 val = selectors->find(Tlv::MustBeFresh);
199 if (val != selectors->getAll().end())
200 {
201 mustBeFresh_ = true;
202 }
203 }
204
205 // Nonce
206 Block::element_iterator val = wire_.find(Tlv::Nonce);
207 if (val != wire_.getAll().end())
208 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800209 nonce_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800210 }
211
212 // Scope
213 val = wire_.find(Tlv::Scope);
214 if (val != wire_.getAll().end())
215 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800216 scope_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800217 }
218
219 // InterestLifetime
220 val = wire_.find(Tlv::InterestLifetime);
221 if (val != wire_.getAll().end())
222 {
Alexander Afanasyevff0a3942014-01-03 15:31:28 -0800223 interestLifetime_ = readNonNegativeInteger(*val);
Alexander Afanasyev1eb961a2014-01-03 13:51:49 -0800224 }
225}
226
227
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700228}