blob: 9f64aa69a1896732a106ec79535bf5eb792c861e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -07002/**
Alexander Afanasyev9d158f02015-02-17 21:30:19 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070020 */
21
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080022#include "face.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070023#include "detail/face-impl.hpp"
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070024
Junxiao Shi468abc32014-11-04 09:12:47 -070025#include "encoding/tlv.hpp"
Junxiao Shiedd834e2014-10-28 20:28:58 -070026#include "security/key-chain.hpp"
Junxiao Shic6acc7a2015-06-23 10:03:56 -070027#include "security/signing-helpers.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080028#include "util/time.hpp"
29#include "util/random.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070030#include "util/face-uri.hpp"
Jeff Thompsonb982b6d2013-07-15 18:15:45 -070031
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070032namespace ndn {
Alexander Afanasyevb790d952014-01-24 12:07:53 -080033
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080034Face::Face()
Junxiao Shi2cced062014-11-02 21:27:38 -070035 : m_internalIoService(new boost::asio::io_service())
36 , m_ioService(*m_internalIoService)
37 , m_internalKeyChain(new KeyChain())
Junxiao Shiedd834e2014-10-28 20:28:58 -070038 , m_impl(new Impl(*this))
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070039{
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080040 construct(nullptr, *m_internalKeyChain);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080041}
42
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070043Face::Face(boost::asio::io_service& ioService)
Junxiao Shi2cced062014-11-02 21:27:38 -070044 : m_ioService(ioService)
45 , m_internalKeyChain(new KeyChain())
Junxiao Shiedd834e2014-10-28 20:28:58 -070046 , m_impl(new Impl(*this))
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070047{
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080048 construct(nullptr, *m_internalKeyChain);
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070049}
50
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080051Face::Face(const std::string& host, const std::string& port/* = "6363"*/)
Junxiao Shi2cced062014-11-02 21:27:38 -070052 : m_internalIoService(new boost::asio::io_service())
53 , m_ioService(*m_internalIoService)
54 , m_internalKeyChain(new KeyChain())
Junxiao Shiedd834e2014-10-28 20:28:58 -070055 , m_impl(new Impl(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080056{
Joao Pereira68c0d882015-05-19 14:27:55 -040057 construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080058}
59
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080060Face::Face(shared_ptr<Transport> transport)
Junxiao Shi2cced062014-11-02 21:27:38 -070061 : m_internalIoService(new boost::asio::io_service())
62 , m_ioService(*m_internalIoService)
63 , m_internalKeyChain(new KeyChain())
Junxiao Shiedd834e2014-10-28 20:28:58 -070064 , m_impl(new Impl(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080065{
Joao Pereira68c0d882015-05-19 14:27:55 -040066 construct(transport, *m_internalKeyChain);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080067}
68
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080069Face::Face(shared_ptr<Transport> transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070070 boost::asio::io_service& ioService)
Junxiao Shi2cced062014-11-02 21:27:38 -070071 : m_ioService(ioService)
72 , m_internalKeyChain(new KeyChain())
Junxiao Shiedd834e2014-10-28 20:28:58 -070073 , m_impl(new Impl(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080074{
Joao Pereira68c0d882015-05-19 14:27:55 -040075 construct(transport, *m_internalKeyChain);
Junxiao Shiedd834e2014-10-28 20:28:58 -070076}
77
78Face::Face(shared_ptr<Transport> transport,
79 boost::asio::io_service& ioService,
80 KeyChain& keyChain)
Junxiao Shi2cced062014-11-02 21:27:38 -070081 : m_ioService(ioService)
82 , m_internalKeyChain(nullptr)
Junxiao Shiedd834e2014-10-28 20:28:58 -070083 , m_impl(new Impl(*this))
84{
Joao Pereira68c0d882015-05-19 14:27:55 -040085 construct(transport, keyChain);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080086}
87
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080088shared_ptr<Transport>
89Face::makeDefaultTransport()
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070090{
91 // transport=unix:///var/run/nfd.sock
92 // transport=tcp://localhost:6363
93
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080094 ConfigFile config;
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080095 const auto& transportUri = config.getParsedConfiguration()
96 .get_optional<std::string>("transport");
97 if (!transportUri) {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -080098 // transport not specified, use default Unix transport.
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080099 return UnixTransport::create(config);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800100 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700101
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800102 std::string protocol;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800103 try {
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800104 util::FaceUri uri(*transportUri);
105 protocol = uri.getScheme();
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800106 }
107 catch (const util::FaceUri::Error& error) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700108 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800109 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700110
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800111 if (protocol == "unix") {
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800112 return UnixTransport::create(config);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800113 }
114 else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") {
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800115 return TcpTransport::create(config);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800116 }
117 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700118 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\""));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800119 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700120}
121
122void
Joao Pereira68c0d882015-05-19 14:27:55 -0400123Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800124{
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800125 if (transport == nullptr) {
126 transport = makeDefaultTransport();
127 }
128 BOOST_ASSERT(transport != nullptr);
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800129 m_transport = transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800130
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800131 m_nfdController.reset(new nfd::Controller(*this, keyChain));
132
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700133 m_ioService.post([=] { m_impl->ensureConnected(false); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800134}
135
Joao Pereira68c0d882015-05-19 14:27:55 -0400136Face::~Face() = default;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700137
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800138shared_ptr<Transport>
139Face::getTransport()
140{
141 return m_transport;
142}
143
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800144const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700145Face::expressInterest(const Interest& interest,
146 const DataCallback& afterSatisfied,
147 const NackCallback& afterNacked,
148 const TimeoutCallback& afterTimeout)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800149{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700150 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800151
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700152 // Use `interestToExpress` to avoid wire format creation for the original Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700153 if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700154 BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700155 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700156
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700157 // If the same ioService thread, dispatch directly calls the method
Eric Newberry83872fd2015-08-06 17:01:24 -0700158 m_ioService.dispatch([=] { m_impl->asyncExpressInterest(interestToExpress, afterSatisfied,
159 afterNacked, afterTimeout); });
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800160
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800161 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
162}
163
164const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700165Face::expressInterest(const Interest& interest,
166 const OnData& onData,
167 const OnTimeout& onTimeout)
168{
169 return this->expressInterest(
170 interest,
171 [onData] (const Interest& interest, const Data& data) {
172 if (onData != nullptr) {
173 onData(interest, const_cast<Data&>(data));
174 }
175 },
176 [onTimeout] (const Interest& interest, const lp::Nack& nack) {
177 if (onTimeout != nullptr) {
178 onTimeout(interest);
179 }
180 },
181 onTimeout
182 );
183}
184
185const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800186Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800187 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700188 const OnData& onData, const OnTimeout& onTimeout/* = nullptr*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800189{
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700190 return expressInterest(Interest(tmpl)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700191 .setName(name)
192 .setNonce(0),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800193 onData, onTimeout);
194}
195
196void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800197Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800198{
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700199 // Use original `data`, since wire format should already exist for the original Data
200 if (data.wireEncode().size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700201 BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700202
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700203 shared_ptr<const Data> dataPtr;
204 try {
205 dataPtr = data.shared_from_this();
206 }
207 catch (const bad_weak_ptr& e) {
208 std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()"
209 << std::endl;
210 dataPtr = make_shared<Data>(data);
211 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800212
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700213 // If the same ioService thread, dispatch directly calls the method
Joao Pereira68c0d882015-05-19 14:27:55 -0400214 m_ioService.dispatch([=] { m_impl->asyncPutData(dataPtr); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800215}
216
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800217void
Eric Newberry83872fd2015-08-06 17:01:24 -0700218Face::put(const lp::Nack& nack)
219{
220 m_ioService.dispatch([=] { m_impl->asyncPutNack(make_shared<lp::Nack>(nack)); });
221}
222
223void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800224Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800225{
Joao Pereira68c0d882015-05-19 14:27:55 -0400226 m_ioService.post([=] { m_impl->asyncRemovePendingInterest(pendingInterestId); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800227}
228
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500229void
230Face::removeAllPendingInterests()
231{
232 m_ioService.post([=] { m_impl->asyncRemoveAllPendingInterests(); });
233}
234
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700235size_t
236Face::getNPendingInterests() const
237{
238 return m_impl->m_pendingInterestTable.size();
239}
240
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800241const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000242Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400243 const OnInterest& onInterest,
244 const RegisterPrefixFailureCallback& onFailure,
245 const security::SigningInfo& signingInfo,
246 uint64_t flags)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700247{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400248 return setInterestFilter(interestFilter,
249 onInterest,
250 RegisterPrefixSuccessCallback(),
251 onFailure,
252 signingInfo,
253 flags);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700254}
255
256const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000257Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400258 const OnInterest& onInterest,
259 const RegisterPrefixSuccessCallback& onSuccess,
260 const RegisterPrefixFailureCallback& onFailure,
261 const security::SigningInfo& signingInfo,
262 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700263{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400264 shared_ptr<InterestFilterRecord> filter =
265 make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700266
Joao Pereira0b3cac52015-07-02 14:49:49 -0400267 nfd::CommandOptions options;
268 options.setSigningInfo(signingInfo);
Junxiao Shi388ec252014-11-02 15:19:57 -0700269
Joao Pereira0b3cac52015-07-02 14:49:49 -0400270 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
271 onSuccess, onFailure,
272 flags, options);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700273}
274
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700275const InterestFilterId*
276Face::setInterestFilter(const InterestFilter& interestFilter,
277 const OnInterest& onInterest)
278{
279 shared_ptr<InterestFilterRecord> filter =
280 make_shared<InterestFilterRecord>(interestFilter, onInterest);
281
Joao Pereira68c0d882015-05-19 14:27:55 -0400282 getIoService().post([=] { m_impl->asyncSetInterestFilter(filter); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700283
284 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700285}
286
Joao Pereira0b3cac52015-07-02 14:49:49 -0400287#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
288
289const RegisteredPrefixId*
290Face::setInterestFilter(const InterestFilter& interestFilter,
291 const OnInterest& onInterest,
292 const RegisterPrefixSuccessCallback& onSuccess,
293 const RegisterPrefixFailureCallback& onFailure,
294 const IdentityCertificate& certificate,
295 uint64_t flags)
296{
297 security::SigningInfo signingInfo;
298 if (!certificate.getName().empty()) {
299 signingInfo = signingByCertificate(certificate.getName());
300 }
301 return setInterestFilter(interestFilter, onInterest,
302 onSuccess, onFailure,
303 signingInfo, flags);
304}
305
306const RegisteredPrefixId*
307Face::setInterestFilter(const InterestFilter& interestFilter,
308 const OnInterest& onInterest,
309 const RegisterPrefixFailureCallback& onFailure,
310 const IdentityCertificate& certificate,
311 uint64_t flags)
312{
313 security::SigningInfo signingInfo;
314 if (!certificate.getName().empty()) {
315 signingInfo = signingByCertificate(certificate.getName());
316 }
317 return setInterestFilter(interestFilter, onInterest,
318 onFailure, signingInfo, flags);
319}
320
321const RegisteredPrefixId*
322Face::setInterestFilter(const InterestFilter& interestFilter,
323 const OnInterest& onInterest,
324 const RegisterPrefixSuccessCallback& onSuccess,
325 const RegisterPrefixFailureCallback& onFailure,
326 const Name& identity,
327 uint64_t flags)
328{
329 security::SigningInfo signingInfo = signingByIdentity(identity);
330
331 return setInterestFilter(interestFilter, onInterest,
332 onSuccess, onFailure,
333 signingInfo, flags);
334}
335
336const RegisteredPrefixId*
337Face::setInterestFilter(const InterestFilter& interestFilter,
338 const OnInterest& onInterest,
339 const RegisterPrefixFailureCallback& onFailure,
340 const Name& identity,
341 uint64_t flags)
342{
343 security::SigningInfo signingInfo = signingByIdentity(identity);
344
345 return setInterestFilter(interestFilter, onInterest,
346 onFailure, signingInfo, flags);
347}
348
349#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
350
351const RegisteredPrefixId*
352Face::registerPrefix(const Name& prefix,
353 const RegisterPrefixSuccessCallback& onSuccess,
354 const RegisterPrefixFailureCallback& onFailure,
355 const security::SigningInfo& signingInfo,
356 uint64_t flags)
357{
358
359 nfd::CommandOptions options;
360 options.setSigningInfo(signingInfo);
361
362 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
363 onSuccess, onFailure,
364 flags, options);
365}
366
367#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700368const RegisteredPrefixId*
369Face::registerPrefix(const Name& prefix,
370 const RegisterPrefixSuccessCallback& onSuccess,
371 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700372 const IdentityCertificate& certificate,
373 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700374{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400375 security::SigningInfo signingInfo;
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700376 if (!certificate.getName().empty()) {
Joao Pereira0b3cac52015-07-02 14:49:49 -0400377 signingInfo = signingByCertificate(certificate.getName());
Junxiao Shi388ec252014-11-02 15:19:57 -0700378 }
Joao Pereira0b3cac52015-07-02 14:49:49 -0400379 return registerPrefix(prefix, onSuccess,
380 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700381}
382
383const RegisteredPrefixId*
384Face::registerPrefix(const Name& prefix,
385 const RegisterPrefixSuccessCallback& onSuccess,
386 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700387 const Name& identity,
388 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700389{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400390 security::SigningInfo signingInfo = signingByIdentity(identity);
391 return registerPrefix(prefix, onSuccess,
392 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700393}
Joao Pereira0b3cac52015-07-02 14:49:49 -0400394#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700395
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700396void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800397Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800398{
Joao Pereira68c0d882015-05-19 14:27:55 -0400399 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,
400 UnregisterPrefixSuccessCallback(),
401 UnregisterPrefixFailureCallback()); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800402}
403
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700404void
405Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
406{
Joao Pereira68c0d882015-05-19 14:27:55 -0400407 m_ioService.post([=] { m_impl->asyncUnsetInterestFilter(interestFilterId); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700408}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700409
410void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700411Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
412 const UnregisterPrefixSuccessCallback& onSuccess,
413 const UnregisterPrefixFailureCallback& onFailure)
414{
Joao Pereira68c0d882015-05-19 14:27:55 -0400415 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700416}
417
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800418void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700419Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
420 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800421{
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500422 if (m_ioService.stopped()) {
423 m_ioService.reset(); // ensure that run()/poll() will do some work
424 }
425
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700426 try {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800427 if (timeout < time::milliseconds::zero()) {
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700428 // do not block if timeout is negative, but process pending events
Junxiao Shi2cced062014-11-02 21:27:38 -0700429 m_ioService.poll();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700430 return;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800431 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800432
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800433 if (timeout > time::milliseconds::zero()) {
434 boost::asio::io_service& ioService = m_ioService;
435 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
436 m_impl->m_processEventsTimeoutEvent =
437 m_impl->m_scheduler.scheduleEvent(timeout, [&ioService, &work] {
438 ioService.stop();
439 work.reset();
440 });
441 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700442
443 if (keepThread) {
444 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800445 m_impl->m_ioServiceWork.reset(new boost::asio::io_service::work(m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800446 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700447
Junxiao Shi2cced062014-11-02 21:27:38 -0700448 m_ioService.run();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700449 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700450 catch (...) {
451 m_impl->m_ioServiceWork.reset();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700452 m_impl->m_pendingInterestTable.clear();
453 m_impl->m_registeredPrefixTable.clear();
454 throw;
455 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700456}
457
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800458void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700459Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700460{
Joao Pereira68c0d882015-05-19 14:27:55 -0400461 m_ioService.post([this] { this->asyncShutdown(); });
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700462}
463
464void
465Face::asyncShutdown()
466{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700467 m_impl->m_pendingInterestTable.clear();
468 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800469
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700470 if (m_transport->isConnected())
471 m_transport->close();
472
Alexander Afanasyev1f5486e2014-07-10 17:45:49 -0700473 m_impl->m_ioServiceWork.reset();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700474}
475
Eric Newberry83872fd2015-08-06 17:01:24 -0700476/**
477 * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet
478 */
479template<typename NETPKT>
480static void
481extractLpLocalFields(NETPKT& netPacket, const lp::Packet& lpPacket)
482{
483 if (lpPacket.has<lp::IncomingFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000484 netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700485 }
486}
487
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800488void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800489Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800490{
Eric Newberry83872fd2015-08-06 17:01:24 -0700491 lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
492 // no need to distinguish
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800493
Eric Newberry83872fd2015-08-06 17:01:24 -0700494 Buffer::const_iterator begin, end;
495 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
496 Block netPacket(&*begin, std::distance(begin, end));
497 switch (netPacket.type()) {
498 case tlv::Interest: {
499 shared_ptr<Interest> interest = make_shared<Interest>(netPacket);
500 if (lpPacket.has<lp::NackField>()) {
501 auto nack = make_shared<lp::Nack>(std::move(*interest));
502 nack->setHeader(lpPacket.get<lp::NackField>());
503 extractLpLocalFields(*nack, lpPacket);
504 m_impl->nackPendingInterests(*nack);
505 }
506 else {
507 extractLpLocalFields(*interest, lpPacket);
508 m_impl->processInterestFilters(*interest);
509 }
510 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800511 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700512 case tlv::Data: {
513 shared_ptr<Data> data = make_shared<Data>(netPacket);
514 extractLpLocalFields(*data, lpPacket);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700515 m_impl->satisfyPendingInterests(*data);
Eric Newberry83872fd2015-08-06 17:01:24 -0700516 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800517 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700518 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800519}
520
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800521} // namespace ndn