face.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_FACE_HPP
23 #define NDN_FACE_HPP
24 
25 #include "common.hpp"
26 
27 #include "name.hpp"
28 #include "interest.hpp"
29 #include "interest-filter.hpp"
30 #include "data.hpp"
32 #include "lp/nack.hpp"
34 #include "security/key-chain.hpp"
35 
36 #define NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
37 
38 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
40 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
41 
42 namespace boost {
43 namespace asio {
44 class io_service;
45 } // namespace asio
46 } // namespace boost
47 
48 namespace ndn {
49 
50 class Transport;
51 
52 class PendingInterestId;
53 class RegisteredPrefixId;
54 class InterestFilterId;
55 
56 namespace nfd {
57 class Controller;
58 } // namespace nfd
59 
63 typedef function<void(const Interest&, const Data&)> DataCallback;
64 
68 typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
69 
73 typedef function<void(const Interest&)> TimeoutCallback;
74 
79 typedef function<void(const Interest&, Data&)> OnData;
80 
85 typedef function<void(const Interest&)> OnTimeout;
86 
90 typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
91 
96 typedef function<void (const InterestFilter&, const Interest&)> OnInterest;
97 
101 typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
102 
106 typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
107 
111 typedef function<void()> UnregisterPrefixSuccessCallback;
112 
116 typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
117 
121 class Face : noncopyable
122 {
123 public:
124  class Error : public std::runtime_error
125  {
126  public:
127  explicit
128  Error(const std::string& what)
129  : std::runtime_error(what)
130  {
131  }
132  };
133 
134 public: // constructors
146  explicit
147  Face(shared_ptr<Transport> transport = nullptr);
148 
177  explicit
178  Face(boost::asio::io_service& ioService);
179 
186  explicit
187  Face(const std::string& host, const std::string& port = "6363");
188 
201  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
202 
216  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
217 
232  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
233 
234  virtual
235  ~Face();
236 
237 public: // consumer
247  const PendingInterestId*
248  expressInterest(const Interest& interest,
249  const DataCallback& afterSatisfied,
250  const NackCallback& afterNacked,
251  const TimeoutCallback& afterTimeout);
252 
266  const PendingInterestId*
267  expressInterest(const Interest& interest,
268  const OnData& onData,
269  const OnTimeout& onTimeout = nullptr);
270 
285  const PendingInterestId*
286  expressInterest(const Name& name,
287  const Interest& tmpl,
288  const OnData& onData,
289  const OnTimeout& onTimeout = nullptr);
290 
296  void
297  removePendingInterest(const PendingInterestId* pendingInterestId);
298 
302  void
304 
308  size_t
309  getNPendingInterests() const;
310 
311 public: // producer
332  const RegisteredPrefixId*
333  setInterestFilter(const InterestFilter& interestFilter,
334  const InterestCallback& onInterest,
335  const RegisterPrefixFailureCallback& onFailure,
336  const security::SigningInfo& signingInfo = security::SigningInfo(),
337  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
338 
360  const RegisteredPrefixId*
361  setInterestFilter(const InterestFilter& interestFilter,
362  const InterestCallback& onInterest,
363  const RegisterPrefixSuccessCallback& onSuccess,
364  const RegisterPrefixFailureCallback& onFailure,
365  const security::SigningInfo& signingInfo = security::SigningInfo(),
366  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
367 
380  const InterestFilterId*
381  setInterestFilter(const InterestFilter& interestFilter,
382  const InterestCallback& onInterest);
383 
384 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
385 
408  DEPRECATED(
409  const RegisteredPrefixId*
410  setInterestFilter(const InterestFilter& interestFilter,
411  const OnInterest& onInterest,
412  const RegisterPrefixSuccessCallback& onSuccess,
413  const RegisterPrefixFailureCallback& onFailure,
414  const security::v1::IdentityCertificate& certificate,
415  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
416 
439  DEPRECATED(
440  const RegisteredPrefixId*
441  setInterestFilter(const InterestFilter& interestFilter,
442  const OnInterest& onInterest,
443  const RegisterPrefixFailureCallback& onFailure,
444  const security::v1::IdentityCertificate& certificate,
445  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
446 
468  DEPRECATED(
469  const RegisteredPrefixId*
470  setInterestFilter(const InterestFilter& interestFilter,
471  const OnInterest& onInterest,
472  const RegisterPrefixSuccessCallback& onSuccess,
473  const RegisterPrefixFailureCallback& onFailure,
474  const Name& identity,
475  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
476 
497  DEPRECATED(
498  const RegisteredPrefixId*
499  setInterestFilter(const InterestFilter& interestFilter,
500  const OnInterest& onInterest,
501  const RegisterPrefixFailureCallback& onFailure,
502  const Name& identity,
503  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
504 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
505 
523  const RegisteredPrefixId*
524  registerPrefix(const Name& prefix,
525  const RegisterPrefixSuccessCallback& onSuccess,
526  const RegisterPrefixFailureCallback& onFailure,
527  const security::SigningInfo& signingInfo = security::SigningInfo(),
528  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
529 
530 #ifdef NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
531 
549  DEPRECATED(
550  const RegisteredPrefixId*
551  registerPrefix(const Name& prefix,
552  const RegisterPrefixSuccessCallback& onSuccess,
553  const RegisterPrefixFailureCallback& onFailure,
554  const security::v1::IdentityCertificate& certificate,
555  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
556 
575  DEPRECATED(
576  const RegisteredPrefixId*
577  registerPrefix(const Name& prefix,
578  const RegisterPrefixSuccessCallback& onSuccess,
579  const RegisterPrefixFailureCallback& onFailure,
580  const Name& identity,
581  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT));
582 #endif // NDN_FACE_KEEP_DEPRECATED_REGISTRATION_SIGNING
583 
596  void
597  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
598 
607  void
608  unsetInterestFilter(const InterestFilterId* interestFilterId);
609 
623  void
624  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
625  const UnregisterPrefixSuccessCallback& onSuccess,
626  const UnregisterPrefixFailureCallback& onFailure);
627 
641  void
642  put(const Data& data);
643 
649  void
650  put(const lp::Nack& nack);
651 
652 public: // IO routine
674  void
675  processEvents(const time::milliseconds& timeout = time::milliseconds::zero(),
676  bool keepThread = false)
677  {
678  this->doProcessEvents(timeout, keepThread);
679  }
680 
689  void
690  shutdown();
691 
695  boost::asio::io_service&
697  {
698  return m_ioService;
699  }
700 
705  shared_ptr<Transport>
706  getTransport();
707 
708 protected:
709  virtual void
710  doProcessEvents(const time::milliseconds& timeout, bool keepThread);
711 
712 private:
716  shared_ptr<Transport>
717  makeDefaultTransport();
718 
723  void
724  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
725 
726  void
727  onReceiveElement(const Block& blockFromDaemon);
728 
729  void
730  asyncShutdown();
731 
732 private:
734  unique_ptr<boost::asio::io_service> m_internalIoService;
736  boost::asio::io_service& m_ioService;
737 
738  shared_ptr<Transport> m_transport;
739 
747  unique_ptr<KeyChain> m_internalKeyChain;
748 
749  unique_ptr<nfd::Controller> m_nfdController;
750 
751  class Impl;
752  shared_ptr<Impl> m_impl;
753 };
754 
755 } // namespace ndn
756 
757 #endif // NDN_FACE_HPP
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
Error(const std::string &what)
Definition: face.hpp:128
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:116
virtual ~Face()
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:98
const RegisteredPrefixId * 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:294
Copyright (c) 2013-2017 Regents of the University of California.
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
STL namespace.
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
represents an Interest packet
Definition: interest.hpp:42
function< void(const Interest &)> OnTimeout
Callback invoked when expressed Interest times out.
Definition: face.hpp:85
function< void(const InterestFilter &, const Interest &)> OnInterest
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:96
virtual void doProcessEvents(const time::milliseconds &timeout, bool keepThread)
Definition: face.cpp:458
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Unregister prefix from RIB.
Definition: face.cpp:448
represents a Network Nack
Definition: nack.hpp:40
Copyright (c) 2013-2016 Regents of the University of California.
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:224
shared_ptr< Transport > getTransport()
Definition: face.cpp:160
void shutdown()
Shutdown face operations.
Definition: face.cpp:497
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:121
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:106
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:101
Name abstraction to represent an absolute name.
Definition: name.hpp:46
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId.
Definition: face.cpp:432
boost::asio::io_service & getIoService()
Definition: face.hpp:696
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:90
function< void(const Interest &, Data &)> OnData
Callback invoked when expressed Interest gets satisfied with Data packet.
Definition: face.hpp:79
#define DEPRECATED(func)
Definition: common.hpp:71
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:232
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:111
const PendingInterestId * expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:166
void processEvents(const time::milliseconds &timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.hpp:675
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:73
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:68
represents a Data packet
Definition: data.hpp:37
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:42
const RegisteredPrefixId * 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:392
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
Definition: face.cpp:47
ndn security v2 KeyChain
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:63
void removePendingInterest(const PendingInterestId *pendingInterestId)
Cancel previously expressed Interest.
Definition: face.cpp:216
void put(const Data &data)
Publish data packet.
Definition: face.cpp:238