blob: a4f3615f958b35ed9874e69af6c4fd8cb3db86f4 [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{
Joao Pereira68c0d882015-05-19 14:27:55 -040040 construct(*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{
Joao Pereira68c0d882015-05-19 14:27:55 -040048 construct(*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 Afanasyevf7ca3202014-02-14 22:28:31 -080060Face::Face(const 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
69Face::Face(const 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 Afanasyev0222fba2014-02-09 23:16:02 -080088void
Joao Pereira68c0d882015-05-19 14:27:55 -040089Face::construct(KeyChain& keyChain)
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;
95 const auto& transportType = config.getParsedConfiguration()
96 .get_optional<std::string>("transport");
97 if (!transportType) {
98 // transport not specified, use default Unix transport.
99 construct(UnixTransport::create(config), keyChain);
100 return;
101 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700102
103 unique_ptr<util::FaceUri> uri;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800104 try {
105 uri.reset(new util::FaceUri(*transportType));
106 }
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
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700111 const std::string protocol = uri->getScheme();
112
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800113 if (protocol == "unix") {
114 construct(UnixTransport::create(config), keyChain);
115 }
116 else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") {
117 construct(TcpTransport::create(config), keyChain);
118 }
119 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700120 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\""));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800121 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700122}
123
124void
Joao Pereira68c0d882015-05-19 14:27:55 -0400125Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800126{
Joao Pereira68c0d882015-05-19 14:27:55 -0400127 m_nfdController.reset(new nfd::Controller(*this, keyChain));
Junxiao Shiedd834e2014-10-28 20:28:58 -0700128
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800129 m_transport = transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800130
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700131 m_ioService.post([=] { m_impl->ensureConnected(false); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800132}
133
Joao Pereira68c0d882015-05-19 14:27:55 -0400134Face::~Face() = default;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700135
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800136shared_ptr<Transport>
137Face::getTransport()
138{
139 return m_transport;
140}
141
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800142const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700143Face::expressInterest(const Interest& interest,
144 const DataCallback& afterSatisfied,
145 const NackCallback& afterNacked,
146 const TimeoutCallback& afterTimeout)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800147{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700148 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800149
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700150 // Use `interestToExpress` to avoid wire format creation for the original Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700151 if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700152 BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700153 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700154
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700155 // If the same ioService thread, dispatch directly calls the method
Eric Newberry83872fd2015-08-06 17:01:24 -0700156 m_ioService.dispatch([=] { m_impl->asyncExpressInterest(interestToExpress, afterSatisfied,
157 afterNacked, afterTimeout); });
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800158
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800159 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
160}
161
162const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700163Face::expressInterest(const Interest& interest,
164 const OnData& onData,
165 const OnTimeout& onTimeout)
166{
167 return this->expressInterest(
168 interest,
169 [onData] (const Interest& interest, const Data& data) {
170 if (onData != nullptr) {
171 onData(interest, const_cast<Data&>(data));
172 }
173 },
174 [onTimeout] (const Interest& interest, const lp::Nack& nack) {
175 if (onTimeout != nullptr) {
176 onTimeout(interest);
177 }
178 },
179 onTimeout
180 );
181}
182
183const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800184Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800185 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700186 const OnData& onData, const OnTimeout& onTimeout/* = nullptr*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800187{
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700188 return expressInterest(Interest(tmpl)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700189 .setName(name)
190 .setNonce(0),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800191 onData, onTimeout);
192}
193
194void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800195Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800196{
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700197 // Use original `data`, since wire format should already exist for the original Data
198 if (data.wireEncode().size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700199 BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700200
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700201 shared_ptr<const Data> dataPtr;
202 try {
203 dataPtr = data.shared_from_this();
204 }
205 catch (const bad_weak_ptr& e) {
206 std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()"
207 << std::endl;
208 dataPtr = make_shared<Data>(data);
209 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800210
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700211 // If the same ioService thread, dispatch directly calls the method
Joao Pereira68c0d882015-05-19 14:27:55 -0400212 m_ioService.dispatch([=] { m_impl->asyncPutData(dataPtr); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800213}
214
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800215void
Eric Newberry83872fd2015-08-06 17:01:24 -0700216Face::put(const lp::Nack& nack)
217{
218 m_ioService.dispatch([=] { m_impl->asyncPutNack(make_shared<lp::Nack>(nack)); });
219}
220
221void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800222Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800223{
Joao Pereira68c0d882015-05-19 14:27:55 -0400224 m_ioService.post([=] { m_impl->asyncRemovePendingInterest(pendingInterestId); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800225}
226
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500227void
228Face::removeAllPendingInterests()
229{
230 m_ioService.post([=] { m_impl->asyncRemoveAllPendingInterests(); });
231}
232
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700233size_t
234Face::getNPendingInterests() const
235{
236 return m_impl->m_pendingInterestTable.size();
237}
238
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800239const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000240Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400241 const OnInterest& onInterest,
242 const RegisterPrefixFailureCallback& onFailure,
243 const security::SigningInfo& signingInfo,
244 uint64_t flags)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700245{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400246 return setInterestFilter(interestFilter,
247 onInterest,
248 RegisterPrefixSuccessCallback(),
249 onFailure,
250 signingInfo,
251 flags);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700252}
253
254const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000255Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400256 const OnInterest& onInterest,
257 const RegisterPrefixSuccessCallback& onSuccess,
258 const RegisterPrefixFailureCallback& onFailure,
259 const security::SigningInfo& signingInfo,
260 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700261{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400262 shared_ptr<InterestFilterRecord> filter =
263 make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700264
Joao Pereira0b3cac52015-07-02 14:49:49 -0400265 nfd::CommandOptions options;
266 options.setSigningInfo(signingInfo);
Junxiao Shi388ec252014-11-02 15:19:57 -0700267
Joao Pereira0b3cac52015-07-02 14:49:49 -0400268 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
269 onSuccess, onFailure,
270 flags, options);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700271}
272
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700273const InterestFilterId*
274Face::setInterestFilter(const InterestFilter& interestFilter,
275 const OnInterest& onInterest)
276{
277 shared_ptr<InterestFilterRecord> filter =
278 make_shared<InterestFilterRecord>(interestFilter, onInterest);
279
Joao Pereira68c0d882015-05-19 14:27:55 -0400280 getIoService().post([=] { m_impl->asyncSetInterestFilter(filter); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700281
282 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700283}
284
Joao Pereira0b3cac52015-07-02 14:49:49 -0400285#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
286
287const RegisteredPrefixId*
288Face::setInterestFilter(const InterestFilter& interestFilter,
289 const OnInterest& onInterest,
290 const RegisterPrefixSuccessCallback& onSuccess,
291 const RegisterPrefixFailureCallback& onFailure,
292 const IdentityCertificate& certificate,
293 uint64_t flags)
294{
295 security::SigningInfo signingInfo;
296 if (!certificate.getName().empty()) {
297 signingInfo = signingByCertificate(certificate.getName());
298 }
299 return setInterestFilter(interestFilter, onInterest,
300 onSuccess, onFailure,
301 signingInfo, flags);
302}
303
304const RegisteredPrefixId*
305Face::setInterestFilter(const InterestFilter& interestFilter,
306 const OnInterest& onInterest,
307 const RegisterPrefixFailureCallback& onFailure,
308 const IdentityCertificate& certificate,
309 uint64_t flags)
310{
311 security::SigningInfo signingInfo;
312 if (!certificate.getName().empty()) {
313 signingInfo = signingByCertificate(certificate.getName());
314 }
315 return setInterestFilter(interestFilter, onInterest,
316 onFailure, signingInfo, flags);
317}
318
319const RegisteredPrefixId*
320Face::setInterestFilter(const InterestFilter& interestFilter,
321 const OnInterest& onInterest,
322 const RegisterPrefixSuccessCallback& onSuccess,
323 const RegisterPrefixFailureCallback& onFailure,
324 const Name& identity,
325 uint64_t flags)
326{
327 security::SigningInfo signingInfo = signingByIdentity(identity);
328
329 return setInterestFilter(interestFilter, onInterest,
330 onSuccess, onFailure,
331 signingInfo, flags);
332}
333
334const RegisteredPrefixId*
335Face::setInterestFilter(const InterestFilter& interestFilter,
336 const OnInterest& onInterest,
337 const RegisterPrefixFailureCallback& onFailure,
338 const Name& identity,
339 uint64_t flags)
340{
341 security::SigningInfo signingInfo = signingByIdentity(identity);
342
343 return setInterestFilter(interestFilter, onInterest,
344 onFailure, signingInfo, flags);
345}
346
347#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
348
349const RegisteredPrefixId*
350Face::registerPrefix(const Name& prefix,
351 const RegisterPrefixSuccessCallback& onSuccess,
352 const RegisterPrefixFailureCallback& onFailure,
353 const security::SigningInfo& signingInfo,
354 uint64_t flags)
355{
356
357 nfd::CommandOptions options;
358 options.setSigningInfo(signingInfo);
359
360 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
361 onSuccess, onFailure,
362 flags, options);
363}
364
365#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700366const RegisteredPrefixId*
367Face::registerPrefix(const Name& prefix,
368 const RegisterPrefixSuccessCallback& onSuccess,
369 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700370 const IdentityCertificate& certificate,
371 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700372{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400373 security::SigningInfo signingInfo;
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700374 if (!certificate.getName().empty()) {
Joao Pereira0b3cac52015-07-02 14:49:49 -0400375 signingInfo = signingByCertificate(certificate.getName());
Junxiao Shi388ec252014-11-02 15:19:57 -0700376 }
Joao Pereira0b3cac52015-07-02 14:49:49 -0400377 return registerPrefix(prefix, onSuccess,
378 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700379}
380
381const RegisteredPrefixId*
382Face::registerPrefix(const Name& prefix,
383 const RegisterPrefixSuccessCallback& onSuccess,
384 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700385 const Name& identity,
386 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700387{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400388 security::SigningInfo signingInfo = signingByIdentity(identity);
389 return registerPrefix(prefix, onSuccess,
390 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700391}
Joao Pereira0b3cac52015-07-02 14:49:49 -0400392#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700393
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700394void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800395Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800396{
Joao Pereira68c0d882015-05-19 14:27:55 -0400397 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,
398 UnregisterPrefixSuccessCallback(),
399 UnregisterPrefixFailureCallback()); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800400}
401
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700402void
403Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
404{
Joao Pereira68c0d882015-05-19 14:27:55 -0400405 m_ioService.post([=] { m_impl->asyncUnsetInterestFilter(interestFilterId); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700406}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700407
408void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700409Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
410 const UnregisterPrefixSuccessCallback& onSuccess,
411 const UnregisterPrefixFailureCallback& onFailure)
412{
Joao Pereira68c0d882015-05-19 14:27:55 -0400413 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700414}
415
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800416void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700417Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
418 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800419{
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500420 if (m_ioService.stopped()) {
421 m_ioService.reset(); // ensure that run()/poll() will do some work
422 }
423
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700424 try {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800425 if (timeout < time::milliseconds::zero()) {
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700426 // do not block if timeout is negative, but process pending events
Junxiao Shi2cced062014-11-02 21:27:38 -0700427 m_ioService.poll();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700428 return;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800429 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800430
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800431 if (timeout > time::milliseconds::zero()) {
432 boost::asio::io_service& ioService = m_ioService;
433 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
434 m_impl->m_processEventsTimeoutEvent =
435 m_impl->m_scheduler.scheduleEvent(timeout, [&ioService, &work] {
436 ioService.stop();
437 work.reset();
438 });
439 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700440
441 if (keepThread) {
442 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800443 m_impl->m_ioServiceWork.reset(new boost::asio::io_service::work(m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800444 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700445
Junxiao Shi2cced062014-11-02 21:27:38 -0700446 m_ioService.run();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700447 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700448 catch (...) {
449 m_impl->m_ioServiceWork.reset();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700450 m_impl->m_pendingInterestTable.clear();
451 m_impl->m_registeredPrefixTable.clear();
452 throw;
453 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700454}
455
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800456void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700457Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700458{
Joao Pereira68c0d882015-05-19 14:27:55 -0400459 m_ioService.post([this] { this->asyncShutdown(); });
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700460}
461
462void
463Face::asyncShutdown()
464{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700465 m_impl->m_pendingInterestTable.clear();
466 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800467
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700468 if (m_transport->isConnected())
469 m_transport->close();
470
Alexander Afanasyev1f5486e2014-07-10 17:45:49 -0700471 m_impl->m_ioServiceWork.reset();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700472}
473
Eric Newberry83872fd2015-08-06 17:01:24 -0700474/**
475 * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet
476 */
477template<typename NETPKT>
478static void
479extractLpLocalFields(NETPKT& netPacket, const lp::Packet& lpPacket)
480{
481 if (lpPacket.has<lp::IncomingFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000482 netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700483 }
484}
485
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800486void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800487Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800488{
Eric Newberry83872fd2015-08-06 17:01:24 -0700489 lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
490 // no need to distinguish
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800491
Eric Newberry83872fd2015-08-06 17:01:24 -0700492 Buffer::const_iterator begin, end;
493 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
494 Block netPacket(&*begin, std::distance(begin, end));
495 switch (netPacket.type()) {
496 case tlv::Interest: {
497 shared_ptr<Interest> interest = make_shared<Interest>(netPacket);
498 if (lpPacket.has<lp::NackField>()) {
499 auto nack = make_shared<lp::Nack>(std::move(*interest));
500 nack->setHeader(lpPacket.get<lp::NackField>());
501 extractLpLocalFields(*nack, lpPacket);
502 m_impl->nackPendingInterests(*nack);
503 }
504 else {
505 extractLpLocalFields(*interest, lpPacket);
506 m_impl->processInterestFilters(*interest);
507 }
508 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800509 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700510 case tlv::Data: {
511 shared_ptr<Data> data = make_shared<Data>(netPacket);
512 extractLpLocalFields(*data, lpPacket);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700513 m_impl->satisfyPendingInterests(*data);
Eric Newberry83872fd2015-08-06 17:01:24 -0700514 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800515 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700516 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800517}
518
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800519} // namespace ndn