NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
face.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  */
21 
22 #ifndef NDN_FACE_HPP
23 #define NDN_FACE_HPP
24 
25 #include "ndn-cxx/data.hpp"
26 #include "ndn-cxx/interest.hpp"
31 #include "ndn-cxx/lp/nack.hpp"
34 
35 namespace ndn {
36 
37 class Transport;
38 
39 class PendingInterestId;
40 class PendingInterestHandle;
41 class RegisteredPrefixId;
42 class RegisteredPrefixHandle;
43 class InterestFilterId;
44 class InterestFilterHandle;
45 
49 typedef function<void(const Interest&, const Data&)> DataCallback;
50 
54 typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
55 
59 typedef function<void(const Interest&)> TimeoutCallback;
60 
64 typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
65 
69 typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
70 
74 typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
75 
79 typedef function<void()> UnregisterPrefixSuccessCallback;
80 
84 typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
85 
89 class Face : noncopyable
90 {
91 public:
92  class Error : public std::runtime_error
93  {
94  public:
95  using std::runtime_error::runtime_error;
96  };
97 
102  {
103  public:
110  OversizedPacketError(char pktType, const Name& name, size_t wireSize);
111 
112  public:
113  const char pktType;
114  const Name name;
115  const size_t wireSize;
116  };
117 
118 public: // constructors
130  explicit
131  Face(shared_ptr<Transport> transport = nullptr);
132 
161  explicit
162  Face(DummyIoService& ioService);
163 
176  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
177 
178  virtual
179  ~Face();
180 
181 public: // consumer
194  expressInterest(const Interest& interest,
195  const DataCallback& afterSatisfied,
196  const NackCallback& afterNacked,
197  const TimeoutCallback& afterTimeout);
198 
202  [[deprecated]]
203  void
204  removePendingInterest(const PendingInterestId* pendingInterestId)
205  {
206  cancelPendingInterest(pendingInterestId);
207  }
208 
212  void
214 
218  size_t
219  getNPendingInterests() const;
220 
221 public: // producer
242  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
243  const RegisterPrefixFailureCallback& onFailure,
244  const security::SigningInfo& signingInfo = security::SigningInfo(),
245  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
246 
268  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
269  const RegisterPrefixSuccessCallback& onSuccess,
270  const RegisterPrefixFailureCallback& onFailure,
271  const security::SigningInfo& signingInfo = security::SigningInfo(),
272  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
273 
287  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest);
288 
307  registerPrefix(const Name& prefix,
308  const RegisterPrefixSuccessCallback& onSuccess,
309  const RegisterPrefixFailureCallback& onFailure,
310  const security::SigningInfo& signingInfo = security::SigningInfo(),
311  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
312 
316  [[deprecated]]
317  void
318  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
319  {
320  unregisterPrefixImpl(registeredPrefixId, nullptr, nullptr);
321  }
322 
326  [[deprecated]]
327  void
328  unsetInterestFilter(const InterestFilterId* interestFilterId)
329  {
330  clearInterestFilter(interestFilterId);
331  }
332 
336  [[deprecated]]
337  void
338  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
339  const UnregisterPrefixSuccessCallback& onSuccess,
340  const UnregisterPrefixFailureCallback& onFailure)
341  {
342  unregisterPrefixImpl(registeredPrefixId, onSuccess, onFailure);
343  }
344 
355  void
356  put(Data data);
357 
364  void
365  put(lp::Nack nack);
366 
367 public: // IO routine
397  void
398  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
399  bool keepThread = false)
400  {
401  this->doProcessEvents(timeout, keepThread);
402  }
403 
416  void
417  shutdown();
418 
424  {
425  static DummyIoService io;
426  return io;
427  }
428 
433  shared_ptr<Transport>
434  getTransport() const
435  {
436  return m_transport;
437  }
438 
439 protected:
440  virtual void
441  doProcessEvents(time::milliseconds timeout, bool keepThread);
442 
443 private:
447  shared_ptr<Transport>
448  makeDefaultTransport();
449 
454  void
455  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
456 
457  void
458  onReceiveElement(const Block& blockFromDaemon);
459 
460  void
461  cancelPendingInterest(const PendingInterestId* pendingInterestId);
462 
463  void
464  clearInterestFilter(const InterestFilterId* interestFilterId);
465 
466  void
467  unregisterPrefixImpl(const RegisteredPrefixId* registeredPrefixId,
468  const UnregisterPrefixSuccessCallback& onSuccess,
469  const UnregisterPrefixFailureCallback& onFailure);
470 
471 private:
472  shared_ptr<Transport> m_transport;
473 
474  class Impl;
475  shared_ptr<Impl> m_impl;
476 
477  friend PendingInterestHandle;
478  friend RegisteredPrefixHandle;
479  friend InterestFilterHandle;
480 };
481 
493 {
494 public:
495  PendingInterestHandle() noexcept = default;
496 
497  PendingInterestHandle(Face& face, const PendingInterestId* id);
498 };
499 
515 using ScopedPendingInterestHandle = detail::ScopedCancelHandle<PendingInterestHandle>;
516 
519 class RegisteredPrefixHandle : public detail::CancelHandle
520 {
521 public:
523  {
524  // This could have been '= default', but there's compiler bug in Apple clang 9.0.0,
525  // see https://stackoverflow.com/a/44693603
526  }
527 
528  RegisteredPrefixHandle(Face& face, const RegisteredPrefixId* id);
529 
534  void
535  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
536  const UnregisterPrefixFailureCallback& onFailure = nullptr);
537 
538 private:
539  Face* m_face = nullptr;
540  const RegisteredPrefixId* m_id = nullptr;
541 };
542 
560 
572 {
573 public:
574  InterestFilterHandle() noexcept = default;
575 
576  InterestFilterHandle(Face& face, const InterestFilterId* id);
577 };
578 
594 using ScopedInterestFilterHandle = detail::ScopedCancelHandle<InterestFilterHandle>;
595 
596 } // namespace ndn
597 
598 #endif // NDN_FACE_HPP
ndn::RegisterPrefixFailureCallback
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:74
ndn::InterestFilterHandle::InterestFilterHandle
InterestFilterHandle() noexcept=default
ndn::RegisteredPrefixHandle
A handle of registered prefix.
Definition: face.hpp:520
ndn::Face::unsetInterestFilter
void unsetInterestFilter(const InterestFilterId *interestFilterId)
Definition: face.hpp:328
key-chain.hpp
ndn::Face::expressInterest
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:121
ndn::PendingInterestHandle
A handle of pending Interest.
Definition: face.hpp:493
ndn::security::SigningInfo
Signing parameters passed to KeyChain.
Definition: signing-info.hpp:42
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
ndn::detail::CancelHandle
Handle to cancel an operation.
Definition: cancel-handle.hpp:33
ndn::InterestFilterHandle
A handle of registered Interest filter.
Definition: face.hpp:572
ndn::Face::shutdown
void shutdown()
Shutdown face operations.
Definition: face.cpp:249
ndn::Face
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
ndn::Face::registerPrefix
RegisteredPrefixHandle registerPrefix(const Name &prefix, const RegisterPrefixSuccessCallback &onSuccess, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Register prefix with the connected NDN forwarder.
Definition: face.cpp:219
ndn::Face::getNPendingInterests
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:155
ndn::Face::OversizedPacketError::pktType
const char pktType
Definition: face.hpp:113
ndn::Face::put
void put(Data data)
Publish data packet.
Definition: face.cpp:161
ndn::Face::processEvents
void processEvents(time::milliseconds timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.hpp:398
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::UnregisterPrefixSuccessCallback
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:79
ndn::InterestFilter
declares the set of Interests a producer can serve, which starts with a name prefix,...
Definition: interest-filter.hpp:36
ndn::InterestCallback
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:64
ndn::security::v2::KeyChain
The interface of signing key management.
Definition: key-chain.hpp:47
ndn::Face::OversizedPacketError::wireSize
const size_t wireSize
Definition: face.hpp:115
ndn::DummyIoService
Definition: asio-fwd.hpp:28
ndn::Face::Face
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
Definition: face.cpp:67
ndn::Face::doProcessEvents
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:244
ndn::DataCallback
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:44
ndn::RegisteredPrefixHandle::RegisteredPrefixHandle
RegisteredPrefixHandle() noexcept
Definition: face.hpp:522
nack.hpp
ndn::Face::OversizedPacketError::name
const Name name
Definition: face.hpp:114
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::Face::removeAllPendingInterests
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:147
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::RegisterPrefixSuccessCallback
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:69
ndn::NackCallback
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:54
ndn::Face::~Face
virtual ~Face()
nfd::face::Transport
Transport
Definition: transport.cpp:32
interest.hpp
data.hpp
ndn::Face::removePendingInterest
void removePendingInterest(const PendingInterestId *pendingInterestId)
Definition: face.hpp:204
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Face::unsetInterestFilter
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Definition: face.hpp:318
ndn::PendingInterestHandle::PendingInterestHandle
PendingInterestHandle() noexcept=default
ndn::Face::OversizedPacketError
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:102
ndn::lp::Nack
represents a Network Nack
Definition: nack.hpp:39
ndn::name
Definition: name-component-types.hpp:33
ndn::detail::CancelHandle::CancelHandle
CancelHandle() noexcept
ndn::TimeoutCallback
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:59
interest-filter.hpp
ndn::Face::Error
Definition: face.hpp:93
ndn::Face::getIoService
DummyIoService & getIoService()
Definition: face.hpp:423
ndn::Face::unregisterPrefix
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Definition: face.hpp:338
ndn::UnregisterPrefixFailureCallback
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:84
nfd-constants.hpp
cancel-handle.hpp
signing-info.hpp
ndn::Face::OversizedPacketError::OversizedPacketError
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
Definition: face.cpp:52
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::detail::ScopedCancelHandle< PendingInterestHandle >
ndn::Face::setInterestFilter
RegisteredPrefixHandle setInterestFilter(const InterestFilter &filter, const InterestCallback &onInterest, const RegisterPrefixFailureCallback &onFailure, const security::SigningInfo &signingInfo=security::SigningInfo(), uint64_t flags=nfd::ROUTE_FLAG_CHILD_INHERIT)
Set InterestFilter to dispatch incoming matching interest to onInterest callback and register the fil...
Definition: face.cpp:177
asio-fwd.hpp
ndn::nfd::ROUTE_FLAG_CHILD_INHERIT
@ ROUTE_FLAG_CHILD_INHERIT
Definition: nfd-constants.hpp:125