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 
46 namespace nfd {
47 class Controller;
48 } // namespace nfd
49 
53 typedef function<void(const Interest&, const Data&)> DataCallback;
54 
58 typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
59 
63 typedef function<void(const Interest&)> TimeoutCallback;
64 
68 typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
69 
73 typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
74 
78 typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
79 
83 typedef function<void()> UnregisterPrefixSuccessCallback;
84 
88 typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
89 
93 class Face : noncopyable
94 {
95 public:
96  class Error : public std::runtime_error
97  {
98  public:
99  using std::runtime_error::runtime_error;
100  };
101 
106  {
107  public:
114  OversizedPacketError(char pktType, const Name& name, size_t wireSize);
115 
116  public:
117  const char pktType;
118  const Name name;
119  const size_t wireSize;
120  };
121 
122 public: // constructors
134  explicit
135  Face(shared_ptr<Transport> transport = nullptr);
136 
165  explicit
166  Face(DummyIoService& ioService);
167 
180  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
181 
182  virtual
183  ~Face();
184 
185 public: // consumer
198  expressInterest(const Interest& interest,
199  const DataCallback& afterSatisfied,
200  const NackCallback& afterNacked,
201  const TimeoutCallback& afterTimeout);
202 
207  void
208  removePendingInterest(const PendingInterestId* pendingInterestId);
209 
213  void
215 
219  size_t
220  getNPendingInterests() const;
221 
222 public: // producer
243  setInterestFilter(const InterestFilter& interestFilter,
244  const InterestCallback& onInterest,
245  const RegisterPrefixFailureCallback& onFailure,
246  const security::SigningInfo& signingInfo = security::SigningInfo(),
247  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
248 
270  setInterestFilter(const InterestFilter& interestFilter,
271  const InterestCallback& onInterest,
272  const RegisterPrefixSuccessCallback& onSuccess,
273  const RegisterPrefixFailureCallback& onFailure,
274  const security::SigningInfo& signingInfo = security::SigningInfo(),
275  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
276 
290  setInterestFilter(const InterestFilter& interestFilter,
291  const InterestCallback& onInterest);
292 
311  registerPrefix(const Name& prefix,
312  const RegisterPrefixSuccessCallback& onSuccess,
313  const RegisterPrefixFailureCallback& onFailure,
314  const security::SigningInfo& signingInfo = security::SigningInfo(),
315  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
316 
329  void
330  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
331 
340  void
341  unsetInterestFilter(const InterestFilterId* interestFilterId);
342 
356  void
357  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
358  const UnregisterPrefixSuccessCallback& onSuccess,
359  const UnregisterPrefixFailureCallback& onFailure);
360 
371  void
372  put(Data data);
373 
380  void
381  put(lp::Nack nack);
382 
383 public: // IO routine
413  void
414  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
415  bool keepThread = false)
416  {
417  this->doProcessEvents(timeout, keepThread);
418  }
419 
432  void
433  shutdown();
434 
440  {
441  static DummyIoService io;
442  return io;
443  }
444 
449  shared_ptr<Transport>
450  getTransport();
451 
452 protected:
453  virtual void
454  doProcessEvents(time::milliseconds timeout, bool keepThread);
455 
456 private:
460  shared_ptr<Transport>
461  makeDefaultTransport();
462 
467  void
468  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
469 
470  void
471  onReceiveElement(const Block& blockFromDaemon);
472 
473  void
474  asyncShutdown();
475 
476 private:
477  shared_ptr<Transport> m_transport;
478 
479  unique_ptr<nfd::Controller> m_nfdController;
480 
481  class Impl;
482  shared_ptr<Impl> m_impl;
483 };
484 
498 {
499 public:
500  PendingInterestHandle() noexcept = default;
501 
502  PendingInterestHandle(Face& face, const PendingInterestId* id);
503 
504  operator const PendingInterestId*() const noexcept
505  {
506  return m_id;
507  }
508 
509 private:
510  const PendingInterestId* m_id = nullptr;
511 };
512 
531 
535 {
536 public:
538  {
539  // This could have been '= default', but there's compiler bug in Apple clang 9.0.0,
540  // see https://stackoverflow.com/a/44693603
541  }
542 
543  RegisteredPrefixHandle(Face& face, const RegisteredPrefixId* id);
544 
545  operator const RegisteredPrefixId*() const noexcept
546  {
547  return m_id;
548  }
549 
557  void
558  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
559  const UnregisterPrefixFailureCallback& onFailure = nullptr);
560 
561 private:
562  Face* m_face = nullptr;
563  const RegisteredPrefixId* m_id = nullptr;
564 };
565 
586 
600 {
601 public:
602  InterestFilterHandle() noexcept = default;
603 
604  InterestFilterHandle(Face& face, const InterestFilterId* id);
605 
606  operator const InterestFilterId*() const noexcept
607  {
608  return m_id;
609  }
610 
611 private:
612  const InterestFilterId* m_id = nullptr;
613 };
614 
633 
634 } // namespace ndn
635 
636 #endif // NDN_FACE_HPP
Copyright (c) 2011-2015 Regents of the University of California.
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:267
The interface of signing key management.
Definition: key-chain.hpp:46
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:88
virtual ~Face()
void unregister(const UnregisterPrefixSuccessCallback &onSuccess=nullptr, const UnregisterPrefixFailureCallback &onFailure=nullptr)
Unregister the prefix.
Definition: face.cpp:356
implementation detail of Face
Definition: face-impl.hpp:60
declares the set of Interests a producer can serve, which starts with a name prefix,...
DummyIoService & getIoService()
Definition: face.hpp:439
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:44
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
Definition: face.cpp:257
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:42
represents a Network Nack
Definition: nack.hpp:38
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:155
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:227
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
A handle of pending Interest.
Definition: face.hpp:497
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:163
void shutdown()
Shutdown face operations.
Definition: face.cpp:272
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:93
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:78
void processEvents(time::milliseconds timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.hpp:414
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:73
Represents an absolute name.
Definition: name.hpp:43
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
Definition: face.cpp:241
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:131
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:68
RegisteredPrefixHandle() noexcept
Definition: face.hpp:537
InterestFilterHandle() noexcept=default
Handle to cancel an operation.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:83
void put(Data data)
Publish data packet.
Definition: face.cpp:169
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
Definition: face.cpp:54
A handle of registered Interest filter.
Definition: face.hpp:599
PendingInterestHandle() noexcept=default
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:63
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:58
Represents a Data packet.
Definition: data.hpp:35
RegisteredPrefixHandle setInterestFilter(const InterestFilter &interestFilter, 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:185
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
Definition: face.cpp:70
A handle of registered prefix.
Definition: face.hpp:534
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:53
void removePendingInterest(const PendingInterestId *pendingInterestId)
Cancel previously expressed Interest.
Definition: face.cpp:147
Cancels an operation automatically upon destruction.
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:105