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-2023 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_CXX_FACE_HPP
23 #define NDN_CXX_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 PendingInterestHandle;
40 class RegisteredPrefixHandle;
41 class InterestFilterHandle;
42 
43 namespace detail {
44 using RecordId = uint64_t;
45 } // namespace detail
46 
50 using DataCallback = std::function<void(const Interest&, const Data&)>;
51 
55 using NackCallback = std::function<void(const Interest&, const lp::Nack&)>;
56 
60 using TimeoutCallback = std::function<void(const Interest&)>;
61 
65 using InterestCallback = std::function<void(const InterestFilter&, const Interest&)>;
66 
70 using RegisterPrefixSuccessCallback = std::function<void(const Name&)>;
71 
75 using RegisterPrefixFailureCallback = std::function<void(const Name&, const std::string&)>;
76 
80 using UnregisterPrefixSuccessCallback = std::function<void()>;
81 
85 using UnregisterPrefixFailureCallback = std::function<void(const std::string&)>;
86 
90 class Face : noncopyable
91 {
92 public:
93  class Error : public std::runtime_error
94  {
95  public:
96  using std::runtime_error::runtime_error;
97  };
98 
103  {
104  public:
111  OversizedPacketError(char pktType, const Name& name, size_t wireSize);
112 
113  public:
114  const char pktType;
115  const Name name;
116  const size_t wireSize;
117  };
118 
119 public: // constructors
131  explicit
132  Face(shared_ptr<Transport> transport = nullptr);
133 
160  explicit
161  Face(boost::asio::io_context& ioCtx);
162 
169  explicit
170  Face(const std::string& host, const std::string& port = "6363");
171 
184  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
185 
199  Face(shared_ptr<Transport> transport, boost::asio::io_context& ioCtx);
200 
215  Face(shared_ptr<Transport> transport, boost::asio::io_context& ioCtx, KeyChain& keyChain);
216 
217  virtual
218  ~Face();
219 
220 public: // consumer
233  expressInterest(const Interest& interest,
234  const DataCallback& afterSatisfied,
235  const NackCallback& afterNacked,
236  const TimeoutCallback& afterTimeout);
237 
241  void
243 
247  size_t
248  getNPendingInterests() const;
249 
250 public: // producer
271  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
272  const RegisterPrefixFailureCallback& onFailure,
273  const security::SigningInfo& signingInfo = security::SigningInfo(),
274  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
275 
297  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
298  const RegisterPrefixSuccessCallback& onSuccess,
299  const RegisterPrefixFailureCallback& onFailure,
300  const security::SigningInfo& signingInfo = security::SigningInfo(),
301  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
302 
316  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest);
317 
336  registerPrefix(const Name& prefix,
337  const RegisterPrefixSuccessCallback& onSuccess,
338  const RegisterPrefixFailureCallback& onFailure,
339  const security::SigningInfo& signingInfo = security::SigningInfo(),
340  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
341 
352  void
353  put(const Data& data);
354 
361  void
362  put(const lp::Nack& nack);
363 
364 public: // event loop routines
396  void
397  processEvents(time::milliseconds timeout = 0_ms, bool keepRunning = false)
398  {
399  this->doProcessEvents(timeout, keepRunning);
400  }
401 
415  void
416  shutdown();
417 
421  boost::asio::io_context&
422  getIoContext() const noexcept
423  {
424  return m_ioCtx;
425  }
426 
430  [[deprecated("use getIoContext")]]
431  boost::asio::io_context&
432  getIoService() const noexcept
433  {
434  return m_ioCtx;
435  }
436 
441  Transport&
442  getTransport() const
443  {
444  return *m_transport;
445  }
446 
447 protected:
448  virtual void
449  doProcessEvents(time::milliseconds timeout, bool keepRunning);
450 
451 private:
455  void
456  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
457 
458  void
459  onReceiveElement(const Block& blockFromDaemon);
460 
461 private:
463  unique_ptr<boost::asio::io_context> m_internalIoCtx;
465  boost::asio::io_context& m_ioCtx;
466 
467  shared_ptr<Transport> m_transport;
468 
476  unique_ptr<KeyChain> m_internalKeyChain;
477 
478  class Impl;
479  shared_ptr<Impl> m_impl;
480 
481  friend PendingInterestHandle;
482  friend RegisteredPrefixHandle;
483  friend InterestFilterHandle;
484 };
485 
494 {
495 public:
496  PendingInterestHandle() noexcept = default;
497 
498 private:
499  PendingInterestHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
500 
501  friend Face;
502 };
503 
516 using ScopedPendingInterestHandle = detail::ScopedCancelHandle<PendingInterestHandle>;
517 
520 class RegisteredPrefixHandle : public detail::CancelHandle
521 {
522 public:
523  RegisteredPrefixHandle() noexcept = default;
524 
527  void
528  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
529  const UnregisterPrefixFailureCallback& onFailure = nullptr);
530 
531 private:
532  RegisteredPrefixHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
533 
534  static void
535  unregister(const weak_ptr<Face::Impl>& impl, detail::RecordId id,
536  const UnregisterPrefixSuccessCallback& onSuccess,
537  const UnregisterPrefixFailureCallback& onFailure);
538 
539 private:
540  weak_ptr<Face::Impl> m_weakImpl;
541  detail::RecordId m_id = 0;
542 
543  friend Face;
544 };
545 
559 using ScopedRegisteredPrefixHandle = detail::ScopedCancelHandle<RegisteredPrefixHandle>;
560 
568 class InterestFilterHandle : public detail::CancelHandle
569 {
570 public:
571  InterestFilterHandle() noexcept = default;
572 
573 private:
574  InterestFilterHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
575 
576  friend Face;
577 };
578 
591 using ScopedInterestFilterHandle = detail::ScopedCancelHandle<InterestFilterHandle>;
592 
593 } // namespace ndn
594 
595 #endif // NDN_CXX_FACE_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Represents a Data packet.
Definition: data.hpp:39
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:103
OversizedPacketError(char pktType, const Name &name, size_t wireSize)
Constructor.
Definition: face.cpp:37
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:91
boost::asio::io_context & getIoContext() const noexcept
Returns a reference to the io_context used by this face.
Definition: face.hpp:422
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:242
virtual ~Face()
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:206
Transport & getTransport() const
Returns the underlying transport.
Definition: face.hpp:442
virtual void doProcessEvents(time::milliseconds timeout, bool keepRunning)
Definition: face.cpp:256
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:170
Face(shared_ptr< Transport > transport=nullptr)
Create Face using the given Transport (or default transport if omitted).
Definition: face.cpp:47
void put(const Data &data)
Publish a Data packet.
Definition: face.cpp:186
boost::asio::io_context & getIoService() const noexcept
Definition: face.hpp:432
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express an Interest.
Definition: face.cpp:151
void shutdown()
Shutdown face operations.
Definition: face.cpp:288
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:180
void processEvents(time::milliseconds timeout=0_ms, bool keepRunning=false)
Run the event loop to process any pending work and execute completion handlers.
Definition: face.hpp:397
Handle for a registered Interest filter.
Definition: face.hpp:569
InterestFilterHandle() noexcept=default
Declares the set of Interests a producer can serve.
Represents an Interest packet.
Definition: interest.hpp:50
Represents an absolute name.
Definition: name.hpp:45
Handle for a pending Interest.
Definition: face.hpp:494
PendingInterestHandle() noexcept=default
Handle for a registered prefix.
Definition: face.hpp:521
RegisteredPrefixHandle() noexcept=default
Provides a "TLV-oriented" delivery service.
Definition: transport.hpp:37
Handle to cancel an operation.
Represents a Network Nack.
Definition: nack.hpp:39
The main interface for signing key management.
Definition: key-chain.hpp:87
Signing parameters passed to KeyChain.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:48
@ ROUTE_FLAG_CHILD_INHERIT
uint64_t RecordId
Definition: face.hpp:44
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
Definition: data.cpp:25
std::function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:70
std::function< void(const Interest &)> TimeoutCallback
Callback invoked when an expressed Interest times out.
Definition: face.hpp:60
std::function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregistering a prefix fails.
Definition: face.hpp:85
std::function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when a Nack is received in response to an expressed Interest.
Definition: face.hpp:55
std::function< void(const Interest &, const Data &)> DataCallback
Callback invoked when an expressed Interest is satisfied by a Data packet.
Definition: face.hpp:50
std::function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregistering a prefix succeeds.
Definition: face.hpp:80
std::function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:75
std::function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when an incoming Interest matches the specified InterestFilter.
Definition: face.hpp:65