blob: 0212fd7aad471de38bfd099f9a9bf929dd68f66b [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 Afanasyev0222fba2014-02-09 23:16:02 -0800136const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700137Face::expressInterest(const Interest& interest,
138 const DataCallback& afterSatisfied,
139 const NackCallback& afterNacked,
140 const TimeoutCallback& afterTimeout)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800141{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700142 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800143
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700144 // Use `interestToExpress` to avoid wire format creation for the original Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700145 if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700146 BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700147 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700148
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700149 // If the same ioService thread, dispatch directly calls the method
Eric Newberry83872fd2015-08-06 17:01:24 -0700150 m_ioService.dispatch([=] { m_impl->asyncExpressInterest(interestToExpress, afterSatisfied,
151 afterNacked, afterTimeout); });
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800152
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800153 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
154}
155
156const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700157Face::expressInterest(const Interest& interest,
158 const OnData& onData,
159 const OnTimeout& onTimeout)
160{
161 return this->expressInterest(
162 interest,
163 [onData] (const Interest& interest, const Data& data) {
164 if (onData != nullptr) {
165 onData(interest, const_cast<Data&>(data));
166 }
167 },
168 [onTimeout] (const Interest& interest, const lp::Nack& nack) {
169 if (onTimeout != nullptr) {
170 onTimeout(interest);
171 }
172 },
173 onTimeout
174 );
175}
176
177const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800178Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800179 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700180 const OnData& onData, const OnTimeout& onTimeout/* = nullptr*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800181{
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700182 return expressInterest(Interest(tmpl)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700183 .setName(name)
184 .setNonce(0),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800185 onData, onTimeout);
186}
187
188void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800189Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800190{
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700191 // Use original `data`, since wire format should already exist for the original Data
192 if (data.wireEncode().size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700193 BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700194
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700195 shared_ptr<const Data> dataPtr;
196 try {
197 dataPtr = data.shared_from_this();
198 }
199 catch (const bad_weak_ptr& e) {
200 std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()"
201 << std::endl;
202 dataPtr = make_shared<Data>(data);
203 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800204
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700205 // If the same ioService thread, dispatch directly calls the method
Joao Pereira68c0d882015-05-19 14:27:55 -0400206 m_ioService.dispatch([=] { m_impl->asyncPutData(dataPtr); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800207}
208
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800209void
Eric Newberry83872fd2015-08-06 17:01:24 -0700210Face::put(const lp::Nack& nack)
211{
212 m_ioService.dispatch([=] { m_impl->asyncPutNack(make_shared<lp::Nack>(nack)); });
213}
214
215void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800216Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800217{
Joao Pereira68c0d882015-05-19 14:27:55 -0400218 m_ioService.post([=] { m_impl->asyncRemovePendingInterest(pendingInterestId); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800219}
220
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500221void
222Face::removeAllPendingInterests()
223{
224 m_ioService.post([=] { m_impl->asyncRemoveAllPendingInterests(); });
225}
226
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700227size_t
228Face::getNPendingInterests() const
229{
230 return m_impl->m_pendingInterestTable.size();
231}
232
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800233const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000234Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400235 const OnInterest& onInterest,
236 const RegisterPrefixFailureCallback& onFailure,
237 const security::SigningInfo& signingInfo,
238 uint64_t flags)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700239{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400240 return setInterestFilter(interestFilter,
241 onInterest,
242 RegisterPrefixSuccessCallback(),
243 onFailure,
244 signingInfo,
245 flags);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700246}
247
248const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000249Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400250 const OnInterest& onInterest,
251 const RegisterPrefixSuccessCallback& onSuccess,
252 const RegisterPrefixFailureCallback& onFailure,
253 const security::SigningInfo& signingInfo,
254 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700255{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400256 shared_ptr<InterestFilterRecord> filter =
257 make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700258
Joao Pereira0b3cac52015-07-02 14:49:49 -0400259 nfd::CommandOptions options;
260 options.setSigningInfo(signingInfo);
Junxiao Shi388ec252014-11-02 15:19:57 -0700261
Joao Pereira0b3cac52015-07-02 14:49:49 -0400262 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
263 onSuccess, onFailure,
264 flags, options);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700265}
266
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700267const InterestFilterId*
268Face::setInterestFilter(const InterestFilter& interestFilter,
269 const OnInterest& onInterest)
270{
271 shared_ptr<InterestFilterRecord> filter =
272 make_shared<InterestFilterRecord>(interestFilter, onInterest);
273
Joao Pereira68c0d882015-05-19 14:27:55 -0400274 getIoService().post([=] { m_impl->asyncSetInterestFilter(filter); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700275
276 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700277}
278
Joao Pereira0b3cac52015-07-02 14:49:49 -0400279#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
280
281const RegisteredPrefixId*
282Face::setInterestFilter(const InterestFilter& interestFilter,
283 const OnInterest& onInterest,
284 const RegisterPrefixSuccessCallback& onSuccess,
285 const RegisterPrefixFailureCallback& onFailure,
286 const IdentityCertificate& certificate,
287 uint64_t flags)
288{
289 security::SigningInfo signingInfo;
290 if (!certificate.getName().empty()) {
291 signingInfo = signingByCertificate(certificate.getName());
292 }
293 return setInterestFilter(interestFilter, onInterest,
294 onSuccess, onFailure,
295 signingInfo, flags);
296}
297
298const RegisteredPrefixId*
299Face::setInterestFilter(const InterestFilter& interestFilter,
300 const OnInterest& onInterest,
301 const RegisterPrefixFailureCallback& onFailure,
302 const IdentityCertificate& certificate,
303 uint64_t flags)
304{
305 security::SigningInfo signingInfo;
306 if (!certificate.getName().empty()) {
307 signingInfo = signingByCertificate(certificate.getName());
308 }
309 return setInterestFilter(interestFilter, onInterest,
310 onFailure, signingInfo, flags);
311}
312
313const RegisteredPrefixId*
314Face::setInterestFilter(const InterestFilter& interestFilter,
315 const OnInterest& onInterest,
316 const RegisterPrefixSuccessCallback& onSuccess,
317 const RegisterPrefixFailureCallback& onFailure,
318 const Name& identity,
319 uint64_t flags)
320{
321 security::SigningInfo signingInfo = signingByIdentity(identity);
322
323 return setInterestFilter(interestFilter, onInterest,
324 onSuccess, onFailure,
325 signingInfo, flags);
326}
327
328const RegisteredPrefixId*
329Face::setInterestFilter(const InterestFilter& interestFilter,
330 const OnInterest& onInterest,
331 const RegisterPrefixFailureCallback& onFailure,
332 const Name& identity,
333 uint64_t flags)
334{
335 security::SigningInfo signingInfo = signingByIdentity(identity);
336
337 return setInterestFilter(interestFilter, onInterest,
338 onFailure, signingInfo, flags);
339}
340
341#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
342
343const RegisteredPrefixId*
344Face::registerPrefix(const Name& prefix,
345 const RegisterPrefixSuccessCallback& onSuccess,
346 const RegisterPrefixFailureCallback& onFailure,
347 const security::SigningInfo& signingInfo,
348 uint64_t flags)
349{
350
351 nfd::CommandOptions options;
352 options.setSigningInfo(signingInfo);
353
354 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
355 onSuccess, onFailure,
356 flags, options);
357}
358
359#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700360const RegisteredPrefixId*
361Face::registerPrefix(const Name& prefix,
362 const RegisterPrefixSuccessCallback& onSuccess,
363 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700364 const IdentityCertificate& certificate,
365 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700366{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400367 security::SigningInfo signingInfo;
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700368 if (!certificate.getName().empty()) {
Joao Pereira0b3cac52015-07-02 14:49:49 -0400369 signingInfo = signingByCertificate(certificate.getName());
Junxiao Shi388ec252014-11-02 15:19:57 -0700370 }
Joao Pereira0b3cac52015-07-02 14:49:49 -0400371 return registerPrefix(prefix, onSuccess,
372 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700373}
374
375const RegisteredPrefixId*
376Face::registerPrefix(const Name& prefix,
377 const RegisterPrefixSuccessCallback& onSuccess,
378 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700379 const Name& identity,
380 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700381{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400382 security::SigningInfo signingInfo = signingByIdentity(identity);
383 return registerPrefix(prefix, onSuccess,
384 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700385}
Joao Pereira0b3cac52015-07-02 14:49:49 -0400386#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700387
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700388void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800389Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800390{
Joao Pereira68c0d882015-05-19 14:27:55 -0400391 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,
392 UnregisterPrefixSuccessCallback(),
393 UnregisterPrefixFailureCallback()); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800394}
395
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700396void
397Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
398{
Joao Pereira68c0d882015-05-19 14:27:55 -0400399 m_ioService.post([=] { m_impl->asyncUnsetInterestFilter(interestFilterId); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700400}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700401
402void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700403Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
404 const UnregisterPrefixSuccessCallback& onSuccess,
405 const UnregisterPrefixFailureCallback& onFailure)
406{
Joao Pereira68c0d882015-05-19 14:27:55 -0400407 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700408}
409
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800410void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700411Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
412 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800413{
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500414 if (m_ioService.stopped()) {
415 m_ioService.reset(); // ensure that run()/poll() will do some work
416 }
417
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700418 try {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800419 if (timeout < time::milliseconds::zero()) {
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700420 // do not block if timeout is negative, but process pending events
Junxiao Shi2cced062014-11-02 21:27:38 -0700421 m_ioService.poll();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700422 return;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800423 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800424
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800425 if (timeout > time::milliseconds::zero()) {
426 boost::asio::io_service& ioService = m_ioService;
427 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
428 m_impl->m_processEventsTimeoutEvent =
429 m_impl->m_scheduler.scheduleEvent(timeout, [&ioService, &work] {
430 ioService.stop();
431 work.reset();
432 });
433 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700434
435 if (keepThread) {
436 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800437 m_impl->m_ioServiceWork.reset(new boost::asio::io_service::work(m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800438 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700439
Junxiao Shi2cced062014-11-02 21:27:38 -0700440 m_ioService.run();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700441 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700442 catch (...) {
443 m_impl->m_ioServiceWork.reset();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700444 m_impl->m_pendingInterestTable.clear();
445 m_impl->m_registeredPrefixTable.clear();
446 throw;
447 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700448}
449
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800450void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700451Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700452{
Joao Pereira68c0d882015-05-19 14:27:55 -0400453 m_ioService.post([this] { this->asyncShutdown(); });
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700454}
455
456void
457Face::asyncShutdown()
458{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700459 m_impl->m_pendingInterestTable.clear();
460 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800461
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700462 if (m_transport->isConnected())
463 m_transport->close();
464
Alexander Afanasyev1f5486e2014-07-10 17:45:49 -0700465 m_impl->m_ioServiceWork.reset();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700466}
467
Eric Newberry83872fd2015-08-06 17:01:24 -0700468/**
469 * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet
470 */
471template<typename NETPKT>
472static void
473extractLpLocalFields(NETPKT& netPacket, const lp::Packet& lpPacket)
474{
475 if (lpPacket.has<lp::IncomingFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000476 netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700477 }
478}
479
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800480void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800481Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800482{
Eric Newberry83872fd2015-08-06 17:01:24 -0700483 lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
484 // no need to distinguish
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800485
Eric Newberry83872fd2015-08-06 17:01:24 -0700486 Buffer::const_iterator begin, end;
487 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
488 Block netPacket(&*begin, std::distance(begin, end));
489 switch (netPacket.type()) {
490 case tlv::Interest: {
491 shared_ptr<Interest> interest = make_shared<Interest>(netPacket);
492 if (lpPacket.has<lp::NackField>()) {
493 auto nack = make_shared<lp::Nack>(std::move(*interest));
494 nack->setHeader(lpPacket.get<lp::NackField>());
495 extractLpLocalFields(*nack, lpPacket);
496 m_impl->nackPendingInterests(*nack);
497 }
498 else {
499 extractLpLocalFields(*interest, lpPacket);
500 m_impl->processInterestFilters(*interest);
501 }
502 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800503 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700504 case tlv::Data: {
505 shared_ptr<Data> data = make_shared<Data>(netPacket);
506 extractLpLocalFields(*data, lpPacket);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700507 m_impl->satisfyPendingInterests(*data);
Eric Newberry83872fd2015-08-06 17:01:24 -0700508 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800509 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700510 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800511}
512
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800513} // namespace ndn