blob: 98581b3fc477f01028d532fac0503f903df272bf [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
Junxiao Shia1ea5062014-12-27 22:33:39 -0700131 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*
137Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
138{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700139 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800140
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700141 // Use `interestToExpress` to avoid wire format creation for the original Interest
142 if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700143 BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700144
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700145 // If the same ioService thread, dispatch directly calls the method
Joao Pereira68c0d882015-05-19 14:27:55 -0400146 m_ioService.dispatch([=] { m_impl->asyncExpressInterest(interestToExpress, onData, onTimeout); });
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800147
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800148 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
149}
150
151const PendingInterestId*
152Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800153 const Interest& tmpl,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800154 const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
155{
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700156 return expressInterest(Interest(tmpl)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700157 .setName(name)
158 .setNonce(0),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800159 onData, onTimeout);
160}
161
162void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800163Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800164{
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700165 // Use original `data`, since wire format should already exist for the original Data
166 if (data.wireEncode().size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700167 BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700168
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700169 shared_ptr<const Data> dataPtr;
170 try {
171 dataPtr = data.shared_from_this();
172 }
173 catch (const bad_weak_ptr& e) {
174 std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()"
175 << std::endl;
176 dataPtr = make_shared<Data>(data);
177 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800178
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700179 // If the same ioService thread, dispatch directly calls the method
Joao Pereira68c0d882015-05-19 14:27:55 -0400180 m_ioService.dispatch([=] { m_impl->asyncPutData(dataPtr); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800181}
182
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800183void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800184Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800185{
Joao Pereira68c0d882015-05-19 14:27:55 -0400186 m_ioService.post([=] { m_impl->asyncRemovePendingInterest(pendingInterestId); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800187}
188
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700189size_t
190Face::getNPendingInterests() const
191{
192 return m_impl->m_pendingInterestTable.size();
193}
194
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800195const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000196Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400197 const OnInterest& onInterest,
198 const RegisterPrefixFailureCallback& onFailure,
199 const security::SigningInfo& signingInfo,
200 uint64_t flags)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700201{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400202 return setInterestFilter(interestFilter,
203 onInterest,
204 RegisterPrefixSuccessCallback(),
205 onFailure,
206 signingInfo,
207 flags);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700208}
209
210const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000211Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400212 const OnInterest& onInterest,
213 const RegisterPrefixSuccessCallback& onSuccess,
214 const RegisterPrefixFailureCallback& onFailure,
215 const security::SigningInfo& signingInfo,
216 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700217{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400218 shared_ptr<InterestFilterRecord> filter =
219 make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700220
Joao Pereira0b3cac52015-07-02 14:49:49 -0400221 nfd::CommandOptions options;
222 options.setSigningInfo(signingInfo);
Junxiao Shi388ec252014-11-02 15:19:57 -0700223
Joao Pereira0b3cac52015-07-02 14:49:49 -0400224 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
225 onSuccess, onFailure,
226 flags, options);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700227}
228
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700229const InterestFilterId*
230Face::setInterestFilter(const InterestFilter& interestFilter,
231 const OnInterest& onInterest)
232{
233 shared_ptr<InterestFilterRecord> filter =
234 make_shared<InterestFilterRecord>(interestFilter, onInterest);
235
Joao Pereira68c0d882015-05-19 14:27:55 -0400236 getIoService().post([=] { m_impl->asyncSetInterestFilter(filter); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700237
238 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700239}
240
Joao Pereira0b3cac52015-07-02 14:49:49 -0400241#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
242
243const RegisteredPrefixId*
244Face::setInterestFilter(const InterestFilter& interestFilter,
245 const OnInterest& onInterest,
246 const RegisterPrefixSuccessCallback& onSuccess,
247 const RegisterPrefixFailureCallback& onFailure,
248 const IdentityCertificate& certificate,
249 uint64_t flags)
250{
251 security::SigningInfo signingInfo;
252 if (!certificate.getName().empty()) {
253 signingInfo = signingByCertificate(certificate.getName());
254 }
255 return setInterestFilter(interestFilter, onInterest,
256 onSuccess, onFailure,
257 signingInfo, flags);
258}
259
260const RegisteredPrefixId*
261Face::setInterestFilter(const InterestFilter& interestFilter,
262 const OnInterest& onInterest,
263 const RegisterPrefixFailureCallback& onFailure,
264 const IdentityCertificate& certificate,
265 uint64_t flags)
266{
267 security::SigningInfo signingInfo;
268 if (!certificate.getName().empty()) {
269 signingInfo = signingByCertificate(certificate.getName());
270 }
271 return setInterestFilter(interestFilter, onInterest,
272 onFailure, signingInfo, flags);
273}
274
275const RegisteredPrefixId*
276Face::setInterestFilter(const InterestFilter& interestFilter,
277 const OnInterest& onInterest,
278 const RegisterPrefixSuccessCallback& onSuccess,
279 const RegisterPrefixFailureCallback& onFailure,
280 const Name& identity,
281 uint64_t flags)
282{
283 security::SigningInfo signingInfo = signingByIdentity(identity);
284
285 return setInterestFilter(interestFilter, onInterest,
286 onSuccess, onFailure,
287 signingInfo, flags);
288}
289
290const RegisteredPrefixId*
291Face::setInterestFilter(const InterestFilter& interestFilter,
292 const OnInterest& onInterest,
293 const RegisterPrefixFailureCallback& onFailure,
294 const Name& identity,
295 uint64_t flags)
296{
297 security::SigningInfo signingInfo = signingByIdentity(identity);
298
299 return setInterestFilter(interestFilter, onInterest,
300 onFailure, signingInfo, flags);
301}
302
303#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
304
305const RegisteredPrefixId*
306Face::registerPrefix(const Name& prefix,
307 const RegisterPrefixSuccessCallback& onSuccess,
308 const RegisterPrefixFailureCallback& onFailure,
309 const security::SigningInfo& signingInfo,
310 uint64_t flags)
311{
312
313 nfd::CommandOptions options;
314 options.setSigningInfo(signingInfo);
315
316 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
317 onSuccess, onFailure,
318 flags, options);
319}
320
321#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700322const RegisteredPrefixId*
323Face::registerPrefix(const Name& prefix,
324 const RegisterPrefixSuccessCallback& onSuccess,
325 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700326 const IdentityCertificate& certificate,
327 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700328{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400329 security::SigningInfo signingInfo;
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700330 if (!certificate.getName().empty()) {
Joao Pereira0b3cac52015-07-02 14:49:49 -0400331 signingInfo = signingByCertificate(certificate.getName());
Junxiao Shi388ec252014-11-02 15:19:57 -0700332 }
Joao Pereira0b3cac52015-07-02 14:49:49 -0400333 return registerPrefix(prefix, onSuccess,
334 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700335}
336
337const RegisteredPrefixId*
338Face::registerPrefix(const Name& prefix,
339 const RegisterPrefixSuccessCallback& onSuccess,
340 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700341 const Name& identity,
342 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700343{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400344 security::SigningInfo signingInfo = signingByIdentity(identity);
345 return registerPrefix(prefix, onSuccess,
346 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700347}
Joao Pereira0b3cac52015-07-02 14:49:49 -0400348#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700349
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700350void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800351Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800352{
Joao Pereira68c0d882015-05-19 14:27:55 -0400353 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,
354 UnregisterPrefixSuccessCallback(),
355 UnregisterPrefixFailureCallback()); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800356}
357
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700358void
359Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
360{
Joao Pereira68c0d882015-05-19 14:27:55 -0400361 m_ioService.post([=] { m_impl->asyncUnsetInterestFilter(interestFilterId); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700362}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700363
364void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700365Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
366 const UnregisterPrefixSuccessCallback& onSuccess,
367 const UnregisterPrefixFailureCallback& onFailure)
368{
Joao Pereira68c0d882015-05-19 14:27:55 -0400369 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700370}
371
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800372void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700373Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
374 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800375{
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500376 if (m_ioService.stopped()) {
377 m_ioService.reset(); // ensure that run()/poll() will do some work
378 }
379
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700380 try {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800381 if (timeout < time::milliseconds::zero()) {
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700382 // do not block if timeout is negative, but process pending events
Junxiao Shi2cced062014-11-02 21:27:38 -0700383 m_ioService.poll();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700384 return;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800385 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800386
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800387 if (timeout > time::milliseconds::zero()) {
388 boost::asio::io_service& ioService = m_ioService;
389 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
390 m_impl->m_processEventsTimeoutEvent =
391 m_impl->m_scheduler.scheduleEvent(timeout, [&ioService, &work] {
392 ioService.stop();
393 work.reset();
394 });
395 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700396
397 if (keepThread) {
398 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800399 m_impl->m_ioServiceWork.reset(new boost::asio::io_service::work(m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800400 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700401
Junxiao Shi2cced062014-11-02 21:27:38 -0700402 m_ioService.run();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700403 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700404 catch (...) {
405 m_impl->m_ioServiceWork.reset();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700406 m_impl->m_pendingInterestTable.clear();
407 m_impl->m_registeredPrefixTable.clear();
408 throw;
409 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700410}
411
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800412void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700413Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700414{
Joao Pereira68c0d882015-05-19 14:27:55 -0400415 m_ioService.post([this] { this->asyncShutdown(); });
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700416}
417
418void
419Face::asyncShutdown()
420{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700421 m_impl->m_pendingInterestTable.clear();
422 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800423
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700424 if (m_transport->isConnected())
425 m_transport->close();
426
Alexander Afanasyev1f5486e2014-07-10 17:45:49 -0700427 m_impl->m_ioServiceWork.reset();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700428}
429
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800430void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800431Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800432{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800433 const Block& block = nfd::LocalControlHeader::getPayload(blockFromDaemon);
434
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600435 if (block.type() == tlv::Interest)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800436 {
Alexander Afanasyevea719672015-02-10 20:25:23 -0800437 shared_ptr<Interest> interest = make_shared<Interest>(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800438 if (&block != &blockFromDaemon)
439 interest->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800440
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700441 m_impl->processInterestFilters(*interest);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800442 }
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600443 else if (block.type() == tlv::Data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800444 {
Alexander Afanasyevea719672015-02-10 20:25:23 -0800445 shared_ptr<Data> data = make_shared<Data>(block);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800446 if (&block != &blockFromDaemon)
447 data->getLocalControlHeader().wireDecode(blockFromDaemon);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800448
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700449 m_impl->satisfyPendingInterests(*data);
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800450 }
Yingdi Yuf9fa52f2014-02-06 12:27:32 -0800451 // ignore any other type
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800452}
453
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800454} // namespace ndn