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-2020 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 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
244  removeAllPendingInterests();
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 
514 
518 {
519 public:
521  {
522  // This could have been '= default', but there's compiler bug in Apple clang 9.0.0,
523  // see https://stackoverflow.com/a/44693603
524  }
525 
528  void
529  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
530  const UnregisterPrefixFailureCallback& onFailure = nullptr);
531 
532 private:
533  RegisteredPrefixHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
534 
535  static void
536  unregister(const weak_ptr<Face::Impl>& impl, detail::RecordId id,
537  const UnregisterPrefixSuccessCallback& onSuccess,
538  const UnregisterPrefixFailureCallback& onFailure);
539 
540 private:
541  weak_ptr<Face::Impl> m_weakImpl;
542  detail::RecordId m_id = 0;
543 
544  friend Face;
545 };
546 
561 
570 {
571 public:
572  InterestFilterHandle() noexcept = default;
573 
574 private:
575  InterestFilterHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
576 
577  friend Face;
578 };
579 
593 
594 } // namespace ndn
595 
596 #endif // NDN_FACE_HPP
Definition: data.cpp:26
The interface of signing key management.
Definition: key-chain.hpp:45
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregistering a prefix fails.
Definition: face.hpp:85
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
shared_ptr< Transport > getTransport() const
Returns the underlying transport.
Definition: face.hpp:433
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:50
Signing parameters passed to KeyChain.
represents a Network Nack
Definition: nack.hpp:38
uint64_t RecordId
Definition: face.hpp:44
Handle for a pending Interest.
Definition: face.hpp:490
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:75
void processEvents(time::milliseconds timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.hpp:397
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:70
Represents an absolute name.
Definition: name.hpp:44
boost::asio::io_service & getIoService()
Returns a reference to the io_service used by this face.
Definition: face.hpp:423
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when an incoming Interest matches the specified InterestFilter.
Definition: face.hpp:65
RegisteredPrefixHandle() noexcept
Definition: face.hpp:520
Handle to cancel an operation.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregistering a prefix succeeds.
Definition: face.hpp:80
Handle for a registered Interest filter.
Definition: face.hpp:569
function< void(const Interest &)> TimeoutCallback
Callback invoked when an expressed Interest times out.
Definition: face.hpp:60
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
Represents a Data packet.
Definition: data.hpp:39
Handle for a registered prefix.
Definition: face.hpp:517
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when an expressed Interest is satisfied by a Data packet.
Definition: face.hpp:50
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:102