blob: dde85eb0a92c1abaf091c4ba3b824ddd620bf183 [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
Alexander Afanasyev787be412016-09-08 15:15:43 -070032#include "ns3/node-list.h"
33#include "ns3/ndnSIM/helper/ndn-stack-helper.hpp"
34#include "ns3/ndnSIM/NFD/daemon/face/generic-link-service.hpp"
35#include "ns3/ndnSIM/NFD/daemon/face/internal-transport.hpp"
36
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -070037namespace ndn {
Alexander Afanasyevb790d952014-01-24 12:07:53 -080038
Alexander Afanasyevf7ca3202014-02-14 22:28:31 -080039Face::Face()
Alexander Afanasyev787be412016-09-08 15:15:43 -070040 : m_impl(new Impl(*this))
Jeff Thompsonfb29cda2013-08-24 10:26:54 -070041{
Alexander Afanasyev787be412016-09-08 15:15:43 -070042 construct(nullptr, ns3::ndn::StackHelper::getKeyChain());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080043}
44
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070045Face::Face(boost::asio::io_service& ioService)
Alexander Afanasyev787be412016-09-08 15:15:43 -070046 : m_impl(new Impl(*this))
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070047{
Alexander Afanasyev787be412016-09-08 15:15:43 -070048 construct(nullptr, ns3::ndn::StackHelper::getKeyChain());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080049}
50
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080051Face::Face(shared_ptr<Transport> transport)
Alexander Afanasyev787be412016-09-08 15:15:43 -070052 : m_impl(new Impl(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080053{
Alexander Afanasyev787be412016-09-08 15:15:43 -070054 construct(transport, ns3::ndn::StackHelper::getKeyChain());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080055}
56
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080057Face::Face(shared_ptr<Transport> transport,
Alexander Afanasyev691c3ce2014-04-23 14:28:04 -070058 boost::asio::io_service& ioService)
Alexander Afanasyev787be412016-09-08 15:15:43 -070059 : m_impl(new Impl(*this))
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080060{
Alexander Afanasyev787be412016-09-08 15:15:43 -070061 construct(transport, ns3::ndn::StackHelper::getKeyChain());
Junxiao Shiedd834e2014-10-28 20:28:58 -070062}
63
64Face::Face(shared_ptr<Transport> transport,
65 boost::asio::io_service& ioService,
66 KeyChain& keyChain)
Alexander Afanasyev787be412016-09-08 15:15:43 -070067 : m_impl(new Impl(*this))
Junxiao Shiedd834e2014-10-28 20:28:58 -070068{
Joao Pereira68c0d882015-05-19 14:27:55 -040069 construct(transport, keyChain);
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -080070}
71
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080072shared_ptr<Transport>
73Face::makeDefaultTransport()
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070074{
Alexander Afanasyev787be412016-09-08 15:15:43 -070075 ns3::Ptr<ns3::Node> node = ns3::NodeList::GetNode(ns3::Simulator::GetContext());
76 NS_ASSERT_MSG(node->GetObject<ns3::ndn::L3Protocol>() != 0,
77 "NDN stack should be installed on the node " << node);
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070078
Alexander Afanasyev787be412016-09-08 15:15:43 -070079 auto uri = ::nfd::FaceUri("ndnFace://" + boost::lexical_cast<std::string>(node->GetId()));
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070080
Alexander Afanasyev787be412016-09-08 15:15:43 -070081 ::nfd::face::GenericLinkService::Options serviceOpts;
82 serviceOpts.allowLocalFields = true;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070083
Alexander Afanasyev787be412016-09-08 15:15:43 -070084 auto nfdFace = make_shared<::nfd::Face>(make_unique<::nfd::face::GenericLinkService>(serviceOpts),
85 make_unique<::nfd::face::InternalForwarderTransport>(uri, uri));
86 auto forwarderTransport = static_cast<::nfd::face::InternalForwarderTransport*>(nfdFace->getTransport());
87
88 auto clientTransport = make_shared<::nfd::face::InternalClientTransport>();
89 clientTransport->connectToForwarder(forwarderTransport);
90
91 node->GetObject<ns3::ndn::L3Protocol>()->addFace(nfdFace);;
92
93 return clientTransport;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070094}
95
96void
Joao Pereira68c0d882015-05-19 14:27:55 -040097Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080098{
Alexander Afanasyevbb64c172015-12-29 20:32:45 -080099 if (transport == nullptr) {
100 transport = makeDefaultTransport();
101 }
102 BOOST_ASSERT(transport != nullptr);
Alexander Afanasyevf39c5372014-02-17 19:42:56 -0800103 m_transport = transport;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800104
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800105 m_nfdController.reset(new nfd::Controller(*this, keyChain));
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800106}
107
Joao Pereira68c0d882015-05-19 14:27:55 -0400108Face::~Face() = default;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700109
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800110shared_ptr<Transport>
111Face::getTransport()
112{
113 return m_transport;
114}
115
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800116const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700117Face::expressInterest(const Interest& interest,
118 const DataCallback& afterSatisfied,
119 const NackCallback& afterNacked,
120 const TimeoutCallback& afterTimeout)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800121{
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700122 shared_ptr<Interest> interestToExpress = make_shared<Interest>(interest);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800123
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700124 // Use `interestToExpress` to avoid wire format creation for the original Interest
Eric Newberry83872fd2015-08-06 17:01:24 -0700125 if (interestToExpress->wireEncode().size() > MAX_NDN_PACKET_SIZE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700126 BOOST_THROW_EXCEPTION(Error("Interest size exceeds maximum limit"));
Eric Newberry83872fd2015-08-06 17:01:24 -0700127 }
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700128
Alexander Afanasyev4e50b972014-03-25 10:57:50 -0700129 // If the same ioService thread, dispatch directly calls the method
Alexander Afanasyev787be412016-09-08 15:15:43 -0700130 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] {
131 m_impl->asyncExpressInterest(interestToExpress, afterSatisfied,
132 afterNacked, afterTimeout);
133 });
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800134
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800135 return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
136}
137
138const PendingInterestId*
Eric Newberry83872fd2015-08-06 17:01:24 -0700139Face::expressInterest(const Interest& interest,
140 const OnData& onData,
141 const OnTimeout& onTimeout)
142{
143 return this->expressInterest(
144 interest,
145 [onData] (const Interest& interest, const Data& data) {
146 if (onData != nullptr) {
147 onData(interest, const_cast<Data&>(data));
148 }
149 },
150 [onTimeout] (const Interest& interest, const lp::Nack& nack) {
151 if (onTimeout != nullptr) {
152 onTimeout(interest);
153 }
154 },
155 onTimeout
156 );
157}
158
159const PendingInterestId*
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800160Face::expressInterest(const Name& name,
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800161 const Interest& tmpl,
Eric Newberry83872fd2015-08-06 17:01:24 -0700162 const OnData& onData, const OnTimeout& onTimeout/* = nullptr*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800163{
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700164 return expressInterest(Interest(tmpl)
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -0700165 .setName(name)
166 .setNonce(0),
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800167 onData, onTimeout);
168}
169
170void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800171Face::put(const Data& data)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800172{
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700173 // Use original `data`, since wire format should already exist for the original Data
174 if (data.wireEncode().size() > MAX_NDN_PACKET_SIZE)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700175 BOOST_THROW_EXCEPTION(Error("Data size exceeds maximum limit"));
Alexander Afanasyev49bb1fb2014-07-21 12:54:01 -0700176
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700177 shared_ptr<const Data> dataPtr;
178 try {
179 dataPtr = data.shared_from_this();
180 }
181 catch (const bad_weak_ptr& e) {
182 std::cerr << "Face::put WARNING: the supplied Data should be created using make_shared<Data>()"
183 << std::endl;
184 dataPtr = make_shared<Data>(data);
185 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800186
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700187 // If the same ioService thread, dispatch directly calls the method
Alexander Afanasyev787be412016-09-08 15:15:43 -0700188 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] { m_impl->asyncPutData(dataPtr); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800189}
190
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800191void
Eric Newberry83872fd2015-08-06 17:01:24 -0700192Face::put(const lp::Nack& nack)
193{
Alexander Afanasyev787be412016-09-08 15:15:43 -0700194 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] { m_impl->asyncPutNack(make_shared<lp::Nack>(nack)); });
Eric Newberry83872fd2015-08-06 17:01:24 -0700195}
196
197void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800198Face::removePendingInterest(const PendingInterestId* pendingInterestId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800199{
Alexander Afanasyev787be412016-09-08 15:15:43 -0700200 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] { m_impl->asyncRemovePendingInterest(pendingInterestId); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800201}
202
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500203void
204Face::removeAllPendingInterests()
205{
Alexander Afanasyev787be412016-09-08 15:15:43 -0700206 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] { m_impl->asyncRemoveAllPendingInterests(); });
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500207}
208
Alexander Afanasyev6fcdde22014-08-22 19:03:36 -0700209size_t
210Face::getNPendingInterests() const
211{
212 return m_impl->m_pendingInterestTable.size();
213}
214
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800215const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000216Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400217 const OnInterest& onInterest,
218 const RegisterPrefixFailureCallback& onFailure,
219 const security::SigningInfo& signingInfo,
220 uint64_t flags)
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700221{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400222 return setInterestFilter(interestFilter,
223 onInterest,
224 RegisterPrefixSuccessCallback(),
225 onFailure,
226 signingInfo,
227 flags);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700228}
229
230const RegisteredPrefixId*
Alexander Afanasyev90164962014-03-06 08:29:59 +0000231Face::setInterestFilter(const InterestFilter& interestFilter,
Joao Pereira0b3cac52015-07-02 14:49:49 -0400232 const OnInterest& onInterest,
233 const RegisterPrefixSuccessCallback& onSuccess,
234 const RegisterPrefixFailureCallback& onFailure,
235 const security::SigningInfo& signingInfo,
236 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700237{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400238 shared_ptr<InterestFilterRecord> filter =
239 make_shared<InterestFilterRecord>(interestFilter, onInterest);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700240
Joao Pereira0b3cac52015-07-02 14:49:49 -0400241 nfd::CommandOptions options;
242 options.setSigningInfo(signingInfo);
Junxiao Shi388ec252014-11-02 15:19:57 -0700243
Joao Pereira0b3cac52015-07-02 14:49:49 -0400244 return m_impl->registerPrefix(interestFilter.getPrefix(), filter,
245 onSuccess, onFailure,
246 flags, options);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700247}
248
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700249const InterestFilterId*
250Face::setInterestFilter(const InterestFilter& interestFilter,
251 const OnInterest& onInterest)
252{
253 shared_ptr<InterestFilterRecord> filter =
254 make_shared<InterestFilterRecord>(interestFilter, onInterest);
255
Alexander Afanasyev787be412016-09-08 15:15:43 -0700256 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] {
257 m_impl->asyncSetInterestFilter(filter);
258 });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700259
260 return reinterpret_cast<const InterestFilterId*>(filter.get());
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700261}
262
Joao Pereira0b3cac52015-07-02 14:49:49 -0400263#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
264
265const RegisteredPrefixId*
266Face::setInterestFilter(const InterestFilter& interestFilter,
267 const OnInterest& onInterest,
268 const RegisterPrefixSuccessCallback& onSuccess,
269 const RegisterPrefixFailureCallback& onFailure,
270 const IdentityCertificate& certificate,
271 uint64_t flags)
272{
273 security::SigningInfo signingInfo;
274 if (!certificate.getName().empty()) {
275 signingInfo = signingByCertificate(certificate.getName());
276 }
277 return setInterestFilter(interestFilter, onInterest,
278 onSuccess, onFailure,
279 signingInfo, flags);
280}
281
282const RegisteredPrefixId*
283Face::setInterestFilter(const InterestFilter& interestFilter,
284 const OnInterest& onInterest,
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 onFailure, signingInfo, flags);
295}
296
297const RegisteredPrefixId*
298Face::setInterestFilter(const InterestFilter& interestFilter,
299 const OnInterest& onInterest,
300 const RegisterPrefixSuccessCallback& onSuccess,
301 const RegisterPrefixFailureCallback& onFailure,
302 const Name& identity,
303 uint64_t flags)
304{
305 security::SigningInfo signingInfo = signingByIdentity(identity);
306
307 return setInterestFilter(interestFilter, onInterest,
308 onSuccess, onFailure,
309 signingInfo, flags);
310}
311
312const RegisteredPrefixId*
313Face::setInterestFilter(const InterestFilter& interestFilter,
314 const OnInterest& onInterest,
315 const RegisterPrefixFailureCallback& onFailure,
316 const Name& identity,
317 uint64_t flags)
318{
319 security::SigningInfo signingInfo = signingByIdentity(identity);
320
321 return setInterestFilter(interestFilter, onInterest,
322 onFailure, signingInfo, flags);
323}
324
325#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
326
327const RegisteredPrefixId*
328Face::registerPrefix(const Name& prefix,
329 const RegisterPrefixSuccessCallback& onSuccess,
330 const RegisterPrefixFailureCallback& onFailure,
331 const security::SigningInfo& signingInfo,
332 uint64_t flags)
333{
334
335 nfd::CommandOptions options;
336 options.setSigningInfo(signingInfo);
337
338 return m_impl->registerPrefix(prefix, shared_ptr<InterestFilterRecord>(),
339 onSuccess, onFailure,
340 flags, options);
341}
342
343#ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700344const RegisteredPrefixId*
345Face::registerPrefix(const Name& prefix,
346 const RegisterPrefixSuccessCallback& onSuccess,
347 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700348 const IdentityCertificate& certificate,
349 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700350{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400351 security::SigningInfo signingInfo;
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700352 if (!certificate.getName().empty()) {
Joao Pereira0b3cac52015-07-02 14:49:49 -0400353 signingInfo = signingByCertificate(certificate.getName());
Junxiao Shi388ec252014-11-02 15:19:57 -0700354 }
Joao Pereira0b3cac52015-07-02 14:49:49 -0400355 return registerPrefix(prefix, onSuccess,
356 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700357}
358
359const RegisteredPrefixId*
360Face::registerPrefix(const Name& prefix,
361 const RegisterPrefixSuccessCallback& onSuccess,
362 const RegisterPrefixFailureCallback& onFailure,
Alexander Afanasyev0866f512014-08-11 13:25:09 -0700363 const Name& identity,
364 uint64_t flags)
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700365{
Joao Pereira0b3cac52015-07-02 14:49:49 -0400366 security::SigningInfo signingInfo = signingByIdentity(identity);
367 return registerPrefix(prefix, onSuccess,
368 onFailure, signingInfo, flags);
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700369}
Joao Pereira0b3cac52015-07-02 14:49:49 -0400370#endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700371
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700372void
Alexander Afanasyev7682ccb2014-02-20 10:29:35 -0800373Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800374{
Alexander Afanasyev787be412016-09-08 15:15:43 -0700375 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,
Joao Pereira68c0d882015-05-19 14:27:55 -0400376 UnregisterPrefixSuccessCallback(),
377 UnregisterPrefixFailureCallback()); });
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800378}
379
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700380void
381Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
382{
Alexander Afanasyev787be412016-09-08 15:15:43 -0700383 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] { m_impl->asyncUnsetInterestFilter(interestFilterId); });
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700384}
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700385
386void
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700387Face::unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
388 const UnregisterPrefixSuccessCallback& onSuccess,
389 const UnregisterPrefixFailureCallback& onFailure)
390{
Alexander Afanasyev787be412016-09-08 15:15:43 -0700391 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure); });
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700392}
393
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800394void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700395Face::processEvents(const time::milliseconds& timeout/* = time::milliseconds::zero()*/,
396 bool keepThread/* = false*/)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800397{
Jeff Thompsonfb29cda2013-08-24 10:26:54 -0700398}
399
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800400void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700401Face::shutdown()
Jeff Thompson517ffa82013-08-05 16:04:34 -0700402{
Alexander Afanasyev787be412016-09-08 15:15:43 -0700403 m_impl->m_scheduler.scheduleEvent(time::seconds(0), [this] { this->asyncShutdown(); });
Alexander Afanasyev7dced462014-03-19 15:12:32 -0700404}
405
406void
407Face::asyncShutdown()
408{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700409 m_impl->m_pendingInterestTable.clear();
410 m_impl->m_registeredPrefixTable.clear();
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800411
Alexander Afanasyev984ad192014-05-02 19:11:15 -0700412 if (m_transport->isConnected())
413 m_transport->close();
Jeff Thompsonaa4e6db2013-07-15 17:25:23 -0700414}
415
Eric Newberry83872fd2015-08-06 17:01:24 -0700416/**
417 * @brief extract local fields from NDNLPv2 packet and tag onto a network layer packet
418 */
419template<typename NETPKT>
420static void
421extractLpLocalFields(NETPKT& netPacket, const lp::Packet& lpPacket)
422{
423 if (lpPacket.has<lp::IncomingFaceIdField>()) {
Junxiao Shi4b469982015-12-03 18:20:19 +0000424 netPacket.setTag(make_shared<lp::IncomingFaceIdTag>(lpPacket.get<lp::IncomingFaceIdField>()));
Eric Newberry83872fd2015-08-06 17:01:24 -0700425 }
426}
427
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800428void
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800429Face::onReceiveElement(const Block& blockFromDaemon)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800430{
Eric Newberry83872fd2015-08-06 17:01:24 -0700431 lp::Packet lpPacket(blockFromDaemon); // bare Interest/Data is a valid lp::Packet,
432 // no need to distinguish
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800433
Eric Newberry83872fd2015-08-06 17:01:24 -0700434 Buffer::const_iterator begin, end;
435 std::tie(begin, end) = lpPacket.get<lp::FragmentField>();
436 Block netPacket(&*begin, std::distance(begin, end));
437 switch (netPacket.type()) {
438 case tlv::Interest: {
439 shared_ptr<Interest> interest = make_shared<Interest>(netPacket);
440 if (lpPacket.has<lp::NackField>()) {
441 auto nack = make_shared<lp::Nack>(std::move(*interest));
442 nack->setHeader(lpPacket.get<lp::NackField>());
443 extractLpLocalFields(*nack, lpPacket);
444 m_impl->nackPendingInterests(*nack);
445 }
446 else {
447 extractLpLocalFields(*interest, lpPacket);
448 m_impl->processInterestFilters(*interest);
449 }
450 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800451 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700452 case tlv::Data: {
453 shared_ptr<Data> data = make_shared<Data>(netPacket);
454 extractLpLocalFields(*data, lpPacket);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700455 m_impl->satisfyPendingInterests(*data);
Eric Newberry83872fd2015-08-06 17:01:24 -0700456 break;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800457 }
Eric Newberry83872fd2015-08-06 17:01:24 -0700458 }
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800459}
460
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800461} // namespace ndn