blob: 51519c9a8106606a58f12385538c0b4d66dff17e [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 Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 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 Afanasyev57e00362016-06-23 13:22:54 -070094 std::string transportUri;
95
96 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
97 transportUri = getenv("NDN_CLIENT_TRANSPORT");
98 }
99 else {
100 ConfigFile config;
101 transportUri = config.getParsedConfiguration().get<std::string>("transport", "");
102 }
103
104 if (transportUri.empty()) {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800105 // transport not specified, use default Unix transport.
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700106 return UnixTransport::create("");
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800107 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700108
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800109 std::string protocol;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800110 try {
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700111 util::FaceUri uri(transportUri);
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800112 protocol = uri.getScheme();
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700113
114 if (protocol == "unix") {
115 return UnixTransport::create(transportUri);
116 }
117 else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") {
118 return TcpTransport::create(transportUri);
119 }
120 else {
121 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\""));
122 }
123 }
124 catch (const Transport::Error& error) {
125 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800126 }
127 catch (const util::FaceUri::Error& error) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700128 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800129 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700130}
131
132void
Joao Pereira68c0d882015-05-19 14:27:55 -0400133Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800134{
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800135 if (transport == nullptr) {
136 transport = makeDefaultTransport();
137 }
138 BOOST_ASSERT(transport != nullptr);
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800139 m_transport = transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800140
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800141 m_nfdController.reset(new nfd::Controller(*this, keyChain));
142
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700143 m_ioService.post([=] { m_impl->ensureConnected(false); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800144}
145
Joao Pereira68c0d882015-05-19 14:27:55 -0400146Face::~Face() = default;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700147
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800148shared_ptr<Transport>
149Face::getTransport()
150{
151 return m_transport;
152}
153
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800154const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700155Face::expressInterest(const Interest& interest,
156 const DataCallback& afterSatisfied,
157 const NackCallback& afterNacked,
158 const TimeoutCallback& afterTimeout)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800159{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700160 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800161
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700162 // Use `interestToExpress` to avoid wire format creation for the original Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700163 if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700164 BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700165 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700166
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700167 // If the same ioService thread, dispatch directly calls the method
Eric Newberry83872fd2015-08-06 17:01:24 -0700168 m_ioService.dispatch([=] { m_impl->asyncExpressInterest(interestToExpress, afterSatisfied,
169 afterNacked, afterTimeout); });
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800170
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800171 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
172}
173
174const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700175Face::expressInterest(const Interest& interest,
176 const OnData& onData,
177 const OnTimeout& onTimeout)
178{
179 return this->expressInterest(
180 interest,
181 [onData] (const Interest& interest, const Data& data) {
182 if (onData != nullptr) {
183 onData(interest, const_cast<Data&>(data));
184 }
185 },
186 [onTimeout] (const Interest& interest, const lp::Nack& nack) {
187 if (onTimeout != nullptr) {
188 onTimeout(interest);
189 }
190 },
191 onTimeout
192 );
193}
194
195const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800196Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800197 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700198 const OnData& onData, const OnTimeout& onTimeout/* = nullptr*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800199{
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700200 return expressInterest(Interest(tmpl)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700201 .setName(name)
202 .setNonce(0),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800203 onData, onTimeout);
204}
205
206void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800207Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800208{
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700209 // Use original `data`, since wire format should already exist for the original Data
210 if (data.wireEncode().size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700211 BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700212
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700213 shared_ptr<const Data> dataPtr;
214 try {
215 dataPtr = data.shared_from_this();
216 }
217 catch (const bad_weak_ptr& e) {
218 std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()"
219 << std::endl;
220 dataPtr = make_shared<Data>(data);
221 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800222
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700223 // If the same ioService thread, dispatch directly calls the method
Joao Pereira68c0d882015-05-19 14:27:55 -0400224 m_ioService.dispatch([=] { m_impl->asyncPutData(dataPtr); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800225}
226
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800227void
Eric Newberry83872fd2015-08-06 17:01:24 -0700228Face::put(const lp::Nack& nack)
229{
230 m_ioService.dispatch([=] { m_impl->asyncPutNack(make_shared<lp::Nack>(nack)); });
231}
232
233void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800234Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800235{
Joao Pereira68c0d882015-05-19 14:27:55 -0400236 m_ioService.post([=] { m_impl->asyncRemovePendingInterest(pendingInterestId); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800237}
238
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500239void
240Face::removeAllPendingInterests()
241{
242 m_ioService.post([=] { m_impl->asyncRemoveAllPendingInterests(); });
243}
244
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700245size_t
246Face::getNPendingInterests() const
247{
248 return m_impl->m_pendingInterestTable.size();
249}
250
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800251const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000252Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400253 const OnInterest& onInterest,
254 const RegisterPrefixFailureCallback& onFailure,
255 const security::SigningInfo& signingInfo,
256 uint64_t flags)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700257{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400258 return setInterestFilter(interestFilter,
259 onInterest,
260 RegisterPrefixSuccessCallback(),
261 onFailure,
262 signingInfo,
263 flags);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700264}
265
266const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000267Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400268 const OnInterest& onInterest,
269 const RegisterPrefixSuccessCallback& onSuccess,
270 const RegisterPrefixFailureCallback& onFailure,
271 const security::SigningInfo& signingInfo,
272 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700273{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400274 shared_ptr<InterestFilterRecord> filter =
275 make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700276
Joao Pereira0b3cac52015-07-02 14:49:49 -0400277 nfd::CommandOptions options;
278 options.setSigningInfo(signingInfo);
Junxiao Shi388ec252014-11-02 15:19:57 -0700279
Joao Pereira0b3cac52015-07-02 14:49:49 -0400280 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
281 onSuccess, onFailure,
282 flags, options);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700283}
284
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700285const InterestFilterId*
286Face::setInterestFilter(const InterestFilter& interestFilter,
287 const OnInterest& onInterest)
288{
289 shared_ptr<InterestFilterRecord> filter =
290 make_shared<InterestFilterRecord>(interestFilter, onInterest);
291
Joao Pereira68c0d882015-05-19 14:27:55 -0400292 getIoService().post([=] { m_impl->asyncSetInterestFilter(filter); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700293
294 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700295}
296
Joao Pereira0b3cac52015-07-02 14:49:49 -0400297#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
298
299const RegisteredPrefixId*
300Face::setInterestFilter(const InterestFilter& interestFilter,
301 const OnInterest& onInterest,
302 const RegisterPrefixSuccessCallback& onSuccess,
303 const RegisterPrefixFailureCallback& onFailure,
304 const IdentityCertificate& certificate,
305 uint64_t flags)
306{
307 security::SigningInfo signingInfo;
308 if (!certificate.getName().empty()) {
309 signingInfo = signingByCertificate(certificate.getName());
310 }
311 return setInterestFilter(interestFilter, onInterest,
312 onSuccess, onFailure,
313 signingInfo, flags);
314}
315
316const RegisteredPrefixId*
317Face::setInterestFilter(const InterestFilter& interestFilter,
318 const OnInterest& onInterest,
319 const RegisterPrefixFailureCallback& onFailure,
320 const IdentityCertificate& certificate,
321 uint64_t flags)
322{
323 security::SigningInfo signingInfo;
324 if (!certificate.getName().empty()) {
325 signingInfo = signingByCertificate(certificate.getName());
326 }
327 return setInterestFilter(interestFilter, onInterest,
328 onFailure, signingInfo, flags);
329}
330
331const RegisteredPrefixId*
332Face::setInterestFilter(const InterestFilter& interestFilter,
333 const OnInterest& onInterest,
334 const RegisterPrefixSuccessCallback& onSuccess,
335 const RegisterPrefixFailureCallback& onFailure,
336 const Name& identity,
337 uint64_t flags)
338{
339 security::SigningInfo signingInfo = signingByIdentity(identity);
340
341 return setInterestFilter(interestFilter, onInterest,
342 onSuccess, onFailure,
343 signingInfo, flags);
344}
345
346const RegisteredPrefixId*
347Face::setInterestFilter(const InterestFilter& interestFilter,
348 const OnInterest& onInterest,
349 const RegisterPrefixFailureCallback& onFailure,
350 const Name& identity,
351 uint64_t flags)
352{
353 security::SigningInfo signingInfo = signingByIdentity(identity);
354
355 return setInterestFilter(interestFilter, onInterest,
356 onFailure, signingInfo, flags);
357}
358
359#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
360
361const RegisteredPrefixId*
362Face::registerPrefix(const Name& prefix,
363 const RegisterPrefixSuccessCallback& onSuccess,
364 const RegisterPrefixFailureCallback& onFailure,
365 const security::SigningInfo& signingInfo,
366 uint64_t flags)
367{
368
369 nfd::CommandOptions options;
370 options.setSigningInfo(signingInfo);
371
372 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
373 onSuccess, onFailure,
374 flags, options);
375}
376
377#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700378const RegisteredPrefixId*
379Face::registerPrefix(const Name& prefix,
380 const RegisterPrefixSuccessCallback& onSuccess,
381 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700382 const IdentityCertificate& certificate,
383 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700384{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400385 security::SigningInfo signingInfo;
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700386 if (!certificate.getName().empty()) {
Joao Pereira0b3cac52015-07-02 14:49:49 -0400387 signingInfo = signingByCertificate(certificate.getName());
Junxiao Shi388ec252014-11-02 15:19:57 -0700388 }
Joao Pereira0b3cac52015-07-02 14:49:49 -0400389 return registerPrefix(prefix, onSuccess,
390 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700391}
392
393const RegisteredPrefixId*
394Face::registerPrefix(const Name& prefix,
395 const RegisterPrefixSuccessCallback& onSuccess,
396 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700397 const Name& identity,
398 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700399{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400400 security::SigningInfo signingInfo = signingByIdentity(identity);
401 return registerPrefix(prefix, onSuccess,
402 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700403}
Joao Pereira0b3cac52015-07-02 14:49:49 -0400404#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700405
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700406void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800407Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800408{
Joao Pereira68c0d882015-05-19 14:27:55 -0400409 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,
410 UnregisterPrefixSuccessCallback(),
411 UnregisterPrefixFailureCallback()); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800412}
413
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700414void
415Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
416{
Joao Pereira68c0d882015-05-19 14:27:55 -0400417 m_ioService.post([=] { m_impl->asyncUnsetInterestFilter(interestFilterId); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700418}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700419
420void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700421Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
422 const UnregisterPrefixSuccessCallback& onSuccess,
423 const UnregisterPrefixFailureCallback& onFailure)
424{
Joao Pereira68c0d882015-05-19 14:27:55 -0400425 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700426}
427
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800428void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700429Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
430 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800431{
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500432 if (m_ioService.stopped()) {
433 m_ioService.reset(); // ensure that run()/poll() will do some work
434 }
435
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700436 try {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800437 if (timeout < time::milliseconds::zero()) {
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700438 // do not block if timeout is negative, but process pending events
Junxiao Shi2cced062014-11-02 21:27:38 -0700439 m_ioService.poll();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700440 return;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800441 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800442
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800443 if (timeout > time::milliseconds::zero()) {
444 boost::asio::io_service& ioService = m_ioService;
445 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
446 m_impl->m_processEventsTimeoutEvent =
447 m_impl->m_scheduler.scheduleEvent(timeout, [&ioService, &work] {
448 ioService.stop();
449 work.reset();
450 });
451 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700452
453 if (keepThread) {
454 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800455 m_impl->m_ioServiceWork.reset(new boost::asio::io_service::work(m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800456 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700457
Junxiao Shi2cced062014-11-02 21:27:38 -0700458 m_ioService.run();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700459 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700460 catch (...) {
461 m_impl->m_ioServiceWork.reset();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700462 m_impl->m_pendingInterestTable.clear();
463 m_impl->m_registeredPrefixTable.clear();
464 throw;
465 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700466}
467
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800468void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700469Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700470{
Joao Pereira68c0d882015-05-19 14:27:55 -0400471 m_ioService.post([this] { this->asyncShutdown(); });
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700472}
473
474void
475Face::asyncShutdown()
476{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700477 m_impl->m_pendingInterestTable.clear();
478 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800479
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700480 if (m_transport->isConnected())
481 m_transport->close();
482
Alexander Afanasyev1f5486e2014-07-10 17:45:49 -0700483 m_impl->m_ioServiceWork.reset();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700484}
485
Eric Newberry83872fd2015-08-06 17:01:24 -0700486/**
487 * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet
488 */
489template<typename NETPKT>
490static void
491extractLpLocalFields(NETPKT& netPacket, const lp::Packet& lpPacket)
492{
493 if (lpPacket.has<lp::IncomingFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000494 netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700495 }
496}
497
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800498void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800499Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800500{
Eric Newberry83872fd2015-08-06 17:01:24 -0700501 lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
502 // no need to distinguish
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800503
Eric Newberry83872fd2015-08-06 17:01:24 -0700504 Buffer::const_iterator begin, end;
505 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
506 Block netPacket(&*begin, std::distance(begin, end));
507 switch (netPacket.type()) {
508 case tlv::Interest: {
509 shared_ptr<Interest> interest = make_shared<Interest>(netPacket);
510 if (lpPacket.has<lp::NackField>()) {
511 auto nack = make_shared<lp::Nack>(std::move(*interest));
512 nack->setHeader(lpPacket.get<lp::NackField>());
513 extractLpLocalFields(*nack, lpPacket);
514 m_impl->nackPendingInterests(*nack);
515 }
516 else {
517 extractLpLocalFields(*interest, lpPacket);
518 m_impl->processInterestFilters(*interest);
519 }
520 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800521 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700522 case tlv::Data: {
523 shared_ptr<Data> data = make_shared<Data>(netPacket);
524 extractLpLocalFields(*data, lpPacket);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700525 m_impl->satisfyPendingInterests(*data);
Eric Newberry83872fd2015-08-06 17:01:24 -0700526 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800527 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700528 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800529}
530
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800531} // namespace ndn