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;
45 
49 typedef function<void(const Interest&, const Data&)> DataCallback;
50 
54 typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
55 
59 typedef function<void(const Interest&)> TimeoutCallback;
60 
64 typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
65 
69 typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
70 
74 typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
75 
79 typedef function<void()> UnregisterPrefixSuccessCallback;
80 
84 typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
85 
89 class Face : noncopyable
90 {
91 public:
92  class Error : public std::runtime_error
93  {
94  public:
95  using std::runtime_error::runtime_error;
96  };
97 
102  {
103  public:
110  OversizedPacketError(char pktType, const Name& name, size_t wireSize);
111 
112  public:
113  const char pktType;
114  const Name name;
115  const size_t wireSize;
116  };
117 
118 public: // constructors
130  explicit
131  Face(shared_ptr<Transport> transport = nullptr);
132 
161  explicit
162  Face(boost::asio::io_service& ioService);
163 
170  explicit
171  Face(const std::string& host, const std::string& port = "6363");
172 
185  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
186 
200  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
201 
216  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
217 
218  virtual
219  ~Face();
220 
221 public: // consumer
234  expressInterest(const Interest& interest,
235  const DataCallback& afterSatisfied,
236  const NackCallback& afterNacked,
237  const TimeoutCallback& afterTimeout);
238 
242  [[deprecated]]
243  void
244  removePendingInterest(const PendingInterestId* pendingInterestId)
245  {
246  cancelPendingInterest(pendingInterestId);
247  }
248 
252  void
254 
258  size_t
259  getNPendingInterests() const;
260 
261 public: // producer
282  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
283  const RegisterPrefixFailureCallback& onFailure,
284  const security::SigningInfo& signingInfo = security::SigningInfo(),
285  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
286 
308  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
309  const RegisterPrefixSuccessCallback& onSuccess,
310  const RegisterPrefixFailureCallback& onFailure,
311  const security::SigningInfo& signingInfo = security::SigningInfo(),
312  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
313 
327  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest);
328 
347  registerPrefix(const Name& prefix,
348  const RegisterPrefixSuccessCallback& onSuccess,
349  const RegisterPrefixFailureCallback& onFailure,
350  const security::SigningInfo& signingInfo = security::SigningInfo(),
351  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
352 
356  [[deprecated]]
357  void
358  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
359  {
360  unregisterPrefixImpl(registeredPrefixId, nullptr, nullptr);
361  }
362 
366  [[deprecated]]
367  void
368  unsetInterestFilter(const InterestFilterId* interestFilterId)
369  {
370  clearInterestFilter(interestFilterId);
371  }
372 
376  [[deprecated]]
377  void
378  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
379  const UnregisterPrefixSuccessCallback& onSuccess,
380  const UnregisterPrefixFailureCallback& onFailure)
381  {
382  unregisterPrefixImpl(registeredPrefixId, onSuccess, onFailure);
383  }
384 
395  void
396  put(Data data);
397 
404  void
405  put(lp::Nack nack);
406 
407 public: // IO routine
437  void
438  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
439  bool keepThread = false)
440  {
441  this->doProcessEvents(timeout, keepThread);
442  }
443 
456  void
457  shutdown();
458 
462  boost::asio::io_service&
464  {
465  return m_ioService;
466  }
467 
472  shared_ptr<Transport>
473  getTransport() const
474  {
475  return m_transport;
476  }
477 
478 protected:
479  virtual void
480  doProcessEvents(time::milliseconds timeout, bool keepThread);
481 
482 private:
486  shared_ptr<Transport>
487  makeDefaultTransport();
488 
493  void
494  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
495 
496  void
497  onReceiveElement(const Block& blockFromDaemon);
498 
499  void
500  cancelPendingInterest(const PendingInterestId* pendingInterestId);
501 
502  void
503  clearInterestFilter(const InterestFilterId* interestFilterId);
504 
505  void
506  unregisterPrefixImpl(const RegisteredPrefixId* registeredPrefixId,
507  const UnregisterPrefixSuccessCallback& onSuccess,
508  const UnregisterPrefixFailureCallback& onFailure);
509 
510 private:
512  unique_ptr<boost::asio::io_service> m_internalIoService;
514  boost::asio::io_service& m_ioService;
515 
516  shared_ptr<Transport> m_transport;
517 
525  unique_ptr<KeyChain> m_internalKeyChain;
526 
527  class Impl;
528  shared_ptr<Impl> m_impl;
529 
530  friend PendingInterestHandle;
531  friend RegisteredPrefixHandle;
532  friend InterestFilterHandle;
533 };
534 
546 {
547 public:
548  PendingInterestHandle() noexcept = default;
549 
550  PendingInterestHandle(Face& face, const PendingInterestId* id);
551 };
552 
569 
573 {
574 public:
576  {
577  // This could have been '= default', but there's compiler bug in Apple clang 9.0.0,
578  // see https://stackoverflow.com/a/44693603
579  }
580 
581  RegisteredPrefixHandle(Face& face, const RegisteredPrefixId* id);
582 
587  void
588  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
589  const UnregisterPrefixFailureCallback& onFailure = nullptr);
590 
591 private:
592  Face* m_face = nullptr;
593  const RegisteredPrefixId* m_id = nullptr;
594 };
595 
613 
625 {
626 public:
627  InterestFilterHandle() noexcept = default;
628 
629  InterestFilterHandle(Face& face, const InterestFilterId* id);
630 };
631 
648 
649 } // namespace ndn
650 
651 #endif // NDN_FACE_HPP
Definition: data.cpp:26
virtual void doProcessEvents(time::milliseconds timeout, bool keepThread)
Definition: face.cpp:288
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:84
virtual ~Face()
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:473
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:43
void unsetInterestFilter(const InterestFilterId *interestFilterId)
Definition: face.hpp:368
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Definition: face.hpp:378
represents a Network Nack
Definition: nack.hpp:38
void removeAllPendingInterests()
Cancel all previously expressed Interests.
Definition: face.cpp:191
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:263
A handle of pending Interest.
Definition: face.hpp:545
size_t getNPendingInterests() const
Get number of pending Interests.
Definition: face.cpp:199
void shutdown()
Shutdown face operations.
Definition: face.cpp:323
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:89
#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:74
void processEvents(time::milliseconds timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.hpp:438
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:69
Represents an absolute name.
Definition: name.hpp:43
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Definition: face.hpp:358
boost::asio::io_service & getIoService()
Returns a reference to the io_service used by this face.
Definition: face.hpp:463
PendingInterestHandle expressInterest(const Interest &interest, const DataCallback &afterSatisfied, const NackCallback &afterNacked, const TimeoutCallback &afterTimeout)
Express Interest.
Definition: face.cpp:165
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:64
RegisteredPrefixHandle() noexcept
Definition: face.hpp:575
Handle to cancel an operation.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:79
void put(Data data)
Publish data packet.
Definition: face.cpp:205
A handle of registered Interest filter.
Definition: face.hpp:624
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:59
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:54
Represents a Data packet.
Definition: data.hpp:35
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:221
Face(shared_ptr< Transport > transport=nullptr)
Create Face using given transport (or default transport if omitted)
Definition: face.cpp:57
A handle of registered prefix.
Definition: face.hpp:572
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:44
void removePendingInterest(const PendingInterestId *pendingInterestId)
Definition: face.hpp:244
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:101