blob: 0a4c8d744716be38ee44a670b6477575f59d3609 [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
89 wire_ = Block(Tlv::Interest);
90 wire_.push_back(getName().wireEncode());
91
92 // selectors
93 {
94 Block selectors(Tlv::Selectors);
95
96 if (getMinSuffixComponents() >= 0) {
97 OBufferStream os;
98 Tlv::writeVarNumber(os, Tlv::MinSuffixComponents);
99 Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(getMinSuffixComponents()));
100 Tlv::writeNonNegativeInteger(os, getMinSuffixComponents());
101
102 selectors.push_back(Block(os.buf()));
103 }
104 if (getMaxSuffixComponents() >= 0) {
105 OBufferStream os;
106 Tlv::writeVarNumber(os, Tlv::MaxSuffixComponents);
107 Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(getMaxSuffixComponents()));
108 Tlv::writeNonNegativeInteger(os, getMaxSuffixComponents());
109
110 selectors.push_back(Block(os.buf()));
111 }
112 if (!getExclude().empty()) {
113 selectors.push_back(getExclude().wireEncode());
114 }
115 if (getChildSelector() >= 0) {
116 OBufferStream os;
117 Tlv::writeVarNumber(os, Tlv::ChildSelector);
118 Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(getChildSelector()));
119 Tlv::writeNonNegativeInteger(os, getChildSelector());
120
121 selectors.push_back(Block(os.buf()));
122 }
123 if (getMustBeFresh()) {
124 OBufferStream os;
125 Tlv::writeVarNumber(os, Tlv::MustBeFresh);
126 Tlv::writeVarNumber(os, 0);
127
128 selectors.push_back(Block(os.buf()));
129 }
130
131 selectors.encode();
132 wire_.push_back(selectors);
133 }
134
135 // Nonce
136 {
137 OBufferStream os;
138 Tlv::writeVarNumber(os, Tlv::Nonce);
139 Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(getNonce()));
140 Tlv::writeNonNegativeInteger(os, getNonce());
141
142 wire_.push_back(Block(os.buf()));
143 }
144
145 if (getScope() >= 0) {
146 OBufferStream os;
147 Tlv::writeVarNumber(os, Tlv::Scope);
148 Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(getScope()));
149 Tlv::writeNonNegativeInteger(os, getScope());
150
151 wire_.push_back(Block(os.buf()));
152 }
153 if (getInterestLifetime() >= 0) {
154 OBufferStream os;
155 Tlv::writeVarNumber(os, Tlv::InterestLifetime);
156 Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(getInterestLifetime()));
157 Tlv::writeNonNegativeInteger(os, getInterestLifetime());
158
159 wire_.push_back(Block(os.buf()));
160 }
161
162 wire_.encode();
163 return wire_;
164}
165
166void
167Interest::wireDecode(const Block &wire)
168{
169 wire_ = wire;
170 wire_.parse();
171
172 // Name
173 name_.wireDecode(wire_.get(Tlv::Name));
174
175 // Selectors
176 Block::element_iterator selectors = wire_.find(Tlv::Selectors);
177 if (selectors != wire_.getAll().end())
178 {
179 selectors->parse();
180
181 // MinSuffixComponents
182 Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents);
183 if (val != selectors->getAll().end())
184 {
185 Buffer::const_iterator begin = val->value_begin();
186 minSuffixComponents_ = Tlv::readNonNegativeInteger(val->value_size(), begin, val->value_end());
187 }
188
189 // MaxSuffixComponents
190 val = selectors->find(Tlv::MaxSuffixComponents);
191 if (val != selectors->getAll().end())
192 {
193 Buffer::const_iterator begin = val->value_begin();
194 maxSuffixComponents_ = Tlv::readNonNegativeInteger(val->value_size(), begin, val->value_end());
195 }
196
197 // Exclude
198 val = selectors->find(Tlv::Exclude);
199 if (val != selectors->getAll().end())
200 {
201 exclude_.wireDecode(*val);
202 }
203
204 // ChildSelector
205 val = selectors->find(Tlv::ChildSelector);
206 if (val != selectors->getAll().end())
207 {
208 Buffer::const_iterator begin = val->value_begin();
209 childSelector_ = Tlv::readNonNegativeInteger(val->value_size(), begin, val->value_end());
210 }
211
212 //MustBeFresh aka AnswerOriginKind
213 val = selectors->find(Tlv::MustBeFresh);
214 if (val != selectors->getAll().end())
215 {
216 mustBeFresh_ = true;
217 }
218 }
219
220 // Nonce
221 Block::element_iterator val = wire_.find(Tlv::Nonce);
222 if (val != wire_.getAll().end())
223 {
224 Buffer::const_iterator begin = val->value_begin();
225 nonce_ = Tlv::readNonNegativeInteger(val->value_size(), begin, val->value_end());
226 }
227
228 // Scope
229 val = wire_.find(Tlv::Scope);
230 if (val != wire_.getAll().end())
231 {
232 Buffer::const_iterator begin = val->value_begin();
233 scope_ = Tlv::readNonNegativeInteger(val->value_size(), begin, val->value_end());
234 }
235
236 // InterestLifetime
237 val = wire_.find(Tlv::InterestLifetime);
238 if (val != wire_.getAll().end())
239 {
240 Buffer::const_iterator begin = val->value_begin();
241 interestLifetime_ = Tlv::readNonNegativeInteger(val->value_size(), begin, val->value_end());
242 }
243}
244
245
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700246}