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-2022 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 typedef function<void(const Interest&, const Data&)> DataCallback;
51 
55 typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
56 
60 typedef function<void(const Interest&)> TimeoutCallback;
61 
65 typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
66 
70 typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
71 
75 typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
76 
80 typedef function<void()> UnregisterPrefixSuccessCallback;
81 
85 typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
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 
162  explicit
163  Face(boost::asio::io_service& ioService);
164 
171  explicit
172  Face(const std::string& host, const std::string& port = "6363");
173 
186  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
187 
201  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
202 
217  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
218 
219  virtual
220  ~Face();
221 
222 public: // consumer
235  expressInterest(const Interest& interest,
236  const DataCallback& afterSatisfied,
237  const NackCallback& afterNacked,
238  const TimeoutCallback& afterTimeout);
239 
243  void
245 
249  size_t
250  getNPendingInterests() const;
251 
252 public: // producer
273  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
274  const RegisterPrefixFailureCallback& onFailure,
275  const security::SigningInfo& signingInfo = security::SigningInfo(),
276  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
277 
299  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
300  const RegisterPrefixSuccessCallback& onSuccess,
301  const RegisterPrefixFailureCallback& onFailure,
302  const security::SigningInfo& signingInfo = security::SigningInfo(),
303  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
304 
318  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest);
319 
338  registerPrefix(const Name& prefix,
339  const RegisterPrefixSuccessCallback& onSuccess,
340  const RegisterPrefixFailureCallback& onFailure,
341  const security::SigningInfo& signingInfo = security::SigningInfo(),
342  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
343 
354  void
355  put(Data data);
356 
363  void
364  put(lp::Nack nack);
365 
366 public: // IO routine
396  void
397  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
398  bool keepThread = false)
399  {
400  this->doProcessEvents(timeout, keepThread);
401  }
402 
416  void
417  shutdown();
418 
422  boost::asio::io_service&
424  {
425  return m_ioService;
426  }
427 
432  shared_ptr<Transport>
433  getTransport() const
434  {
435  return m_transport;
436  }
437 
438 protected:
439  virtual void
440  doProcessEvents(time::milliseconds timeout, bool keepThread);
441 
442 private:
446  shared_ptr<Transport>
447  makeDefaultTransport();
448 
452  void
453  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
454 
455  void
456  onReceiveElement(const Block& blockFromDaemon);
457 
458 private:
460  unique_ptr<boost::asio::io_service> m_internalIoService;
462  boost::asio::io_service& m_ioService;
463 
464  shared_ptr<Transport> m_transport;
465 
473  unique_ptr<KeyChain> m_internalKeyChain;
474 
475  class Impl;
476  shared_ptr<Impl> m_impl;
477 
478  friend PendingInterestHandle;
479  friend RegisteredPrefixHandle;
480  friend InterestFilterHandle;
481 };
482 
491 {
492 public:
493  PendingInterestHandle() noexcept = default;
494 
495 private:
496  PendingInterestHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
497 
498  friend Face;
499 };
500 
513 using ScopedPendingInterestHandle = detail::ScopedCancelHandle<PendingInterestHandle>;
514 
517 class RegisteredPrefixHandle : public detail::CancelHandle
518 {
519 public:
520  RegisteredPrefixHandle() noexcept = default;
521 
524  void
525  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
526  const UnregisterPrefixFailureCallback& onFailure = nullptr);
527 
528 private:
529  RegisteredPrefixHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
530 
531  static void
532  unregister(const weak_ptr<Face::Impl>& impl, detail::RecordId id,
533  const UnregisterPrefixSuccessCallback& onSuccess,
534  const UnregisterPrefixFailureCallback& onFailure);
535 
536 private:
537  weak_ptr<Face::Impl> m_weakImpl;
538  detail::RecordId m_id = 0;
539 
540  friend Face;
541 };
542 
556 using ScopedRegisteredPrefixHandle = detail::ScopedCancelHandle<RegisteredPrefixHandle>;
557 
565 class InterestFilterHandle : public detail::CancelHandle
566 {
567 public:
568  InterestFilterHandle() noexcept = default;
569 
570 private:
571  InterestFilterHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
572 
573  friend Face;
574 };
575 
588 using ScopedInterestFilterHandle = detail::ScopedCancelHandle<InterestFilterHandle>;
589 
590 } // namespace ndn
591 
592 #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:49
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:91
void processEvents(time::milliseconds timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.hpp:397
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:260
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:246
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:212
boost::asio::io_service & getIoService()
Returns a reference to the io_service used by this face.
Definition: face.hpp:423
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:182
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
Definition: face.cpp:59
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express an Interest.
Definition: face.cpp:164
shared_ptr< Transport > getTransport() const
Returns the underlying transport.
Definition: face.hpp:433
void shutdown()
Shutdown face operations.
Definition: face.cpp:291
void put(Data data)
Publish a Data packet.
Definition: face.cpp:196
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:190
Handle for a registered Interest filter.
Definition: face.hpp:566
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:44
Handle for a pending Interest.
Definition: face.hpp:491
PendingInterestHandle() noexcept=default
Handle for a registered prefix.
Definition: face.hpp:518
RegisteredPrefixHandle() noexcept=default
Handle to cancel an operation.
Represents a Network Nack.
Definition: nack.hpp:40
Signing parameters passed to KeyChain.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
@ ROUTE_FLAG_CHILD_INHERIT
uint64_t RecordId
Definition: face.hpp:44
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
Definition: data.cpp:25
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregistering a prefix fails.
Definition: face.hpp:85
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when an incoming Interest matches the specified InterestFilter.
Definition: face.hpp:65
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
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregistering a prefix succeeds.
Definition: face.hpp:80
function< void(const Interest &)> TimeoutCallback
Callback invoked when an expressed Interest times out.
Definition: face.hpp:60
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:75
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:70
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when an expressed Interest is satisfied by a Data packet.
Definition: face.hpp:50