blob: 4d68927c81f372a48b35bc728d57f0df3ef56de5 [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 Afanasyev8cf1c562016-06-23 16:01:55 -070069Face::Face(shared_ptr<Transport> transport, KeyChain& keyChain)
70 : m_internalIoService(new boost::asio::io_service())
71 , m_ioService(*m_internalIoService)
72 , m_internalKeyChain(nullptr)
73 , m_impl(new Impl(*this))
74{
75 construct(transport, keyChain);
76}
77
78Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService)
Junxiao Shi2cced062014-11-02 21:27:38 -070079 : m_ioService(ioService)
80 , m_internalKeyChain(new KeyChain())
Junxiao Shiedd834e2014-10-28 20:28:58 -070081 , m_impl(new Impl(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080082{
Joao Pereira68c0d882015-05-19 14:27:55 -040083 construct(transport, *m_internalKeyChain);
Junxiao Shiedd834e2014-10-28 20:28:58 -070084}
85
Alexander Afanasyev8cf1c562016-06-23 16:01:55 -070086Face::Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain)
Junxiao Shi2cced062014-11-02 21:27:38 -070087 : m_ioService(ioService)
88 , m_internalKeyChain(nullptr)
Junxiao Shiedd834e2014-10-28 20:28:58 -070089 , m_impl(new Impl(*this))
90{
Joao Pereira68c0d882015-05-19 14:27:55 -040091 construct(transport, keyChain);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080092}
93
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080094shared_ptr<Transport>
95Face::makeDefaultTransport()
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070096{
97 // transport=unix:///var/run/nfd.sock
98 // transport=tcp://localhost:6363
99
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700100 std::string transportUri;
101
102 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
103 transportUri = getenv("NDN_CLIENT_TRANSPORT");
104 }
105 else {
106 ConfigFile config;
107 transportUri = config.getParsedConfiguration().get<std::string>("transport", "");
108 }
109
110 if (transportUri.empty()) {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800111 // transport not specified, use default Unix transport.
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700112 return UnixTransport::create("");
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800113 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700114
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800115 std::string protocol;
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800116 try {
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700117 util::FaceUri uri(transportUri);
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800118 protocol = uri.getScheme();
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700119
120 if (protocol == "unix") {
121 return UnixTransport::create(transportUri);
122 }
123 else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6") {
124 return TcpTransport::create(transportUri);
125 }
126 else {
127 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\""));
128 }
129 }
130 catch (const Transport::Error& error) {
131 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800132 }
133 catch (const util::FaceUri::Error& error) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700134 BOOST_THROW_EXCEPTION(ConfigFile::Error(error.what()));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800135 }
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700136}
137
138void
Joao Pereira68c0d882015-05-19 14:27:55 -0400139Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800140{
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800141 if (transport == nullptr) {
142 transport = makeDefaultTransport();
143 }
144 BOOST_ASSERT(transport != nullptr);
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800145 m_transport = transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800146
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800147 m_nfdController.reset(new nfd::Controller(*this, keyChain));
148
Alexander Afanasyeve508f142015-09-01 15:14:45 -0700149 m_ioService.post([=] { m_impl->ensureConnected(false); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800150}
151
Joao Pereira68c0d882015-05-19 14:27:55 -0400152Face::~Face() = default;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700153
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800154shared_ptr<Transport>
155Face::getTransport()
156{
157 return m_transport;
158}
159
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800160const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700161Face::expressInterest(const Interest& interest,
162 const DataCallback& afterSatisfied,
163 const NackCallback& afterNacked,
164 const TimeoutCallback& afterTimeout)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800165{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700166 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800167
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700168 // Use `interestToExpress` to avoid wire format creation for the original Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700169 if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700170 BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700171 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700172
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700173 // If the same ioService thread, dispatch directly calls the method
Eric Newberry83872fd2015-08-06 17:01:24 -0700174 m_ioService.dispatch([=] { m_impl->asyncExpressInterest(interestToExpress, afterSatisfied,
175 afterNacked, afterTimeout); });
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800176
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800177 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
178}
179
180const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700181Face::expressInterest(const Interest& interest,
182 const OnData& onData,
183 const OnTimeout& onTimeout)
184{
185 return this->expressInterest(
186 interest,
187 [onData] (const Interest& interest, const Data& data) {
188 if (onData != nullptr) {
189 onData(interest, const_cast<Data&>(data));
190 }
191 },
192 [onTimeout] (const Interest& interest, const lp::Nack& nack) {
193 if (onTimeout != nullptr) {
194 onTimeout(interest);
195 }
196 },
197 onTimeout
198 );
199}
200
201const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800202Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800203 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700204 const OnData& onData, const OnTimeout& onTimeout/* = nullptr*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800205{
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700206 return expressInterest(Interest(tmpl)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700207 .setName(name)
208 .setNonce(0),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800209 onData, onTimeout);
210}
211
212void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800213Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800214{
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700215 // Use original `data`, since wire format should already exist for the original Data
216 if (data.wireEncode().size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700217 BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700218
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700219 shared_ptr<const Data> dataPtr;
220 try {
221 dataPtr = data.shared_from_this();
222 }
223 catch (const bad_weak_ptr& e) {
224 std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()"
225 << std::endl;
226 dataPtr = make_shared<Data>(data);
227 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800228
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700229 // If the same ioService thread, dispatch directly calls the method
Joao Pereira68c0d882015-05-19 14:27:55 -0400230 m_ioService.dispatch([=] { m_impl->asyncPutData(dataPtr); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800231}
232
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800233void
Eric Newberry83872fd2015-08-06 17:01:24 -0700234Face::put(const lp::Nack& nack)
235{
236 m_ioService.dispatch([=] { m_impl->asyncPutNack(make_shared<lp::Nack>(nack)); });
237}
238
239void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800240Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800241{
Joao Pereira68c0d882015-05-19 14:27:55 -0400242 m_ioService.post([=] { m_impl->asyncRemovePendingInterest(pendingInterestId); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800243}
244
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500245void
246Face::removeAllPendingInterests()
247{
248 m_ioService.post([=] { m_impl->asyncRemoveAllPendingInterests(); });
249}
250
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700251size_t
252Face::getNPendingInterests() const
253{
254 return m_impl->m_pendingInterestTable.size();
255}
256
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800257const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000258Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400259 const OnInterest& onInterest,
260 const RegisterPrefixFailureCallback& onFailure,
261 const security::SigningInfo& signingInfo,
262 uint64_t flags)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700263{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400264 return setInterestFilter(interestFilter,
265 onInterest,
266 RegisterPrefixSuccessCallback(),
267 onFailure,
268 signingInfo,
269 flags);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700270}
271
272const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000273Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400274 const OnInterest& onInterest,
275 const RegisterPrefixSuccessCallback& onSuccess,
276 const RegisterPrefixFailureCallback& onFailure,
277 const security::SigningInfo& signingInfo,
278 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700279{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400280 shared_ptr<InterestFilterRecord> filter =
281 make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700282
Joao Pereira0b3cac52015-07-02 14:49:49 -0400283 nfd::CommandOptions options;
284 options.setSigningInfo(signingInfo);
Junxiao Shi388ec252014-11-02 15:19:57 -0700285
Joao Pereira0b3cac52015-07-02 14:49:49 -0400286 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
287 onSuccess, onFailure,
288 flags, options);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700289}
290
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700291const InterestFilterId*
292Face::setInterestFilter(const InterestFilter& interestFilter,
293 const OnInterest& onInterest)
294{
295 shared_ptr<InterestFilterRecord> filter =
296 make_shared<InterestFilterRecord>(interestFilter, onInterest);
297
Joao Pereira68c0d882015-05-19 14:27:55 -0400298 getIoService().post([=] { m_impl->asyncSetInterestFilter(filter); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700299
300 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700301}
302
Joao Pereira0b3cac52015-07-02 14:49:49 -0400303#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
304
305const RegisteredPrefixId*
306Face::setInterestFilter(const InterestFilter& interestFilter,
307 const OnInterest& onInterest,
308 const RegisterPrefixSuccessCallback& onSuccess,
309 const RegisterPrefixFailureCallback& onFailure,
310 const IdentityCertificate& certificate,
311 uint64_t flags)
312{
313 security::SigningInfo signingInfo;
314 if (!certificate.getName().empty()) {
315 signingInfo = signingByCertificate(certificate.getName());
316 }
317 return setInterestFilter(interestFilter, onInterest,
318 onSuccess, onFailure,
319 signingInfo, flags);
320}
321
322const RegisteredPrefixId*
323Face::setInterestFilter(const InterestFilter& interestFilter,
324 const OnInterest& onInterest,
325 const RegisterPrefixFailureCallback& onFailure,
326 const IdentityCertificate& certificate,
327 uint64_t flags)
328{
329 security::SigningInfo signingInfo;
330 if (!certificate.getName().empty()) {
331 signingInfo = signingByCertificate(certificate.getName());
332 }
333 return setInterestFilter(interestFilter, onInterest,
334 onFailure, signingInfo, flags);
335}
336
337const RegisteredPrefixId*
338Face::setInterestFilter(const InterestFilter& interestFilter,
339 const OnInterest& onInterest,
340 const RegisterPrefixSuccessCallback& onSuccess,
341 const RegisterPrefixFailureCallback& onFailure,
342 const Name& identity,
343 uint64_t flags)
344{
345 security::SigningInfo signingInfo = signingByIdentity(identity);
346
347 return setInterestFilter(interestFilter, onInterest,
348 onSuccess, onFailure,
349 signingInfo, flags);
350}
351
352const RegisteredPrefixId*
353Face::setInterestFilter(const InterestFilter& interestFilter,
354 const OnInterest& onInterest,
355 const RegisterPrefixFailureCallback& onFailure,
356 const Name& identity,
357 uint64_t flags)
358{
359 security::SigningInfo signingInfo = signingByIdentity(identity);
360
361 return setInterestFilter(interestFilter, onInterest,
362 onFailure, signingInfo, flags);
363}
364
365#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
366
367const RegisteredPrefixId*
368Face::registerPrefix(const Name& prefix,
369 const RegisterPrefixSuccessCallback& onSuccess,
370 const RegisterPrefixFailureCallback& onFailure,
371 const security::SigningInfo& signingInfo,
372 uint64_t flags)
373{
374
375 nfd::CommandOptions options;
376 options.setSigningInfo(signingInfo);
377
378 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
379 onSuccess, onFailure,
380 flags, options);
381}
382
383#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700384const RegisteredPrefixId*
385Face::registerPrefix(const Name& prefix,
386 const RegisterPrefixSuccessCallback& onSuccess,
387 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700388 const IdentityCertificate& certificate,
389 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700390{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400391 security::SigningInfo signingInfo;
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700392 if (!certificate.getName().empty()) {
Joao Pereira0b3cac52015-07-02 14:49:49 -0400393 signingInfo = signingByCertificate(certificate.getName());
Junxiao Shi388ec252014-11-02 15:19:57 -0700394 }
Joao Pereira0b3cac52015-07-02 14:49:49 -0400395 return registerPrefix(prefix, onSuccess,
396 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700397}
398
399const RegisteredPrefixId*
400Face::registerPrefix(const Name& prefix,
401 const RegisterPrefixSuccessCallback& onSuccess,
402 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700403 const Name& identity,
404 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700405{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400406 security::SigningInfo signingInfo = signingByIdentity(identity);
407 return registerPrefix(prefix, onSuccess,
408 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700409}
Joao Pereira0b3cac52015-07-02 14:49:49 -0400410#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700411
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700412void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800413Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800414{
Joao Pereira68c0d882015-05-19 14:27:55 -0400415 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,
416 UnregisterPrefixSuccessCallback(),
417 UnregisterPrefixFailureCallback()); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800418}
419
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700420void
421Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
422{
Joao Pereira68c0d882015-05-19 14:27:55 -0400423 m_ioService.post([=] { m_impl->asyncUnsetInterestFilter(interestFilterId); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700424}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700425
426void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700427Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
428 const UnregisterPrefixSuccessCallback& onSuccess,
429 const UnregisterPrefixFailureCallback& onFailure)
430{
Joao Pereira68c0d882015-05-19 14:27:55 -0400431 m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700432}
433
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800434void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700435Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
436 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800437{
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500438 if (m_ioService.stopped()) {
439 m_ioService.reset(); // ensure that run()/poll() will do some work
440 }
441
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700442 try {
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800443 if (timeout < time::milliseconds::zero()) {
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700444 // do not block if timeout is negative, but process pending events
Junxiao Shi2cced062014-11-02 21:27:38 -0700445 m_ioService.poll();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700446 return;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800447 }
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800448
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800449 if (timeout > time::milliseconds::zero()) {
450 boost::asio::io_service& ioService = m_ioService;
451 unique_ptr<boost::asio::io_service::work>& work = m_impl->m_ioServiceWork;
452 m_impl->m_processEventsTimeoutEvent =
453 m_impl->m_scheduler.scheduleEvent(timeout, [&ioService, &work] {
454 ioService.stop();
455 work.reset();
456 });
457 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700458
459 if (keepThread) {
460 // work will ensure that m_ioService is running until work object exists
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800461 m_impl->m_ioServiceWork.reset(new boost::asio::io_service::work(m_ioService));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800462 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700463
Junxiao Shi2cced062014-11-02 21:27:38 -0700464 m_ioService.run();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700465 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700466 catch (...) {
467 m_impl->m_ioServiceWork.reset();
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700468 m_impl->m_pendingInterestTable.clear();
469 m_impl->m_registeredPrefixTable.clear();
470 throw;
471 }
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700472}
473
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800474void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700475Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700476{
Joao Pereira68c0d882015-05-19 14:27:55 -0400477 m_ioService.post([this] { this->asyncShutdown(); });
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700478}
479
480void
481Face::asyncShutdown()
482{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700483 m_impl->m_pendingInterestTable.clear();
484 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800485
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700486 if (m_transport->isConnected())
487 m_transport->close();
488
Alexander Afanasyev1f5486e2014-07-10 17:45:49 -0700489 m_impl->m_ioServiceWork.reset();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700490}
491
Eric Newberry83872fd2015-08-06 17:01:24 -0700492/**
493 * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet
494 */
495template<typename NETPKT>
496static void
497extractLpLocalFields(NETPKT& netPacket, const lp::Packet& lpPacket)
498{
499 if (lpPacket.has<lp::IncomingFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000500 netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700501 }
502}
503
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800504void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800505Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800506{
Eric Newberry83872fd2015-08-06 17:01:24 -0700507 lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
508 // no need to distinguish
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800509
Eric Newberry83872fd2015-08-06 17:01:24 -0700510 Buffer::const_iterator begin, end;
511 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
512 Block netPacket(&*begin, std::distance(begin, end));
513 switch (netPacket.type()) {
514 case tlv::Interest: {
515 shared_ptr<Interest> interest = make_shared<Interest>(netPacket);
516 if (lpPacket.has<lp::NackField>()) {
517 auto nack = make_shared<lp::Nack>(std::move(*interest));
518 nack->setHeader(lpPacket.get<lp::NackField>());
519 extractLpLocalFields(*nack, lpPacket);
520 m_impl->nackPendingInterests(*nack);
521 }
522 else {
523 extractLpLocalFields(*interest, lpPacket);
524 m_impl->processInterestFilters(*interest);
525 }
526 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800527 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700528 case tlv::Data: {
529 shared_ptr<Data> data = make_shared<Data>(netPacket);
530 extractLpLocalFields(*data, lpPacket);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700531 m_impl->satisfyPendingInterests(*data);
Eric Newberry83872fd2015-08-06 17:01:24 -0700532 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800533 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700534 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800535}
536
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800537} // namespace ndn