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;
44 class InterestFilterHandle;
45 
46 namespace nfd {
47 class Controller;
48 } // namespace nfd
49 
53 typedef function<void(const Interest&, const Data&)> DataCallback;
54 
58 typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
59 
63 typedef function<void(const Interest&)> TimeoutCallback;
64 
68 typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
69 
73 typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
74 
78 typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
79 
83 typedef function<void()> UnregisterPrefixSuccessCallback;
84 
88 typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
89 
93 class Face : noncopyable
94 {
95 public:
96  class Error : public std::runtime_error
97  {
98  public:
99  using std::runtime_error::runtime_error;
100  };
101 
106  {
107  public:
114  OversizedPacketError(char pktType, const Name& name, size_t wireSize);
115 
116  public:
117  const char pktType;
118  const Name name;
119  const size_t wireSize;
120  };
121 
122 public: // constructors
134  explicit
135  Face(shared_ptr<Transport> transport = nullptr);
136 
165  explicit
166  Face(boost::asio::io_service& ioService);
167 
174  explicit
175  Face(const std::string& host, const std::string& port = "6363");
176 
189  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
190 
204  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
205 
220  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
221 
222  virtual
223  ~Face();
224 
225 public: // consumer
238  expressInterest(const Interest& interest,
239  const DataCallback& afterSatisfied,
240  const NackCallback& afterNacked,
241  const TimeoutCallback& afterTimeout);
242 
247  void
248  removePendingInterest(const PendingInterestId* pendingInterestId);
249 
253  void
254  removeAllPendingInterests();
255 
259  size_t
260  getNPendingInterests() const;
261 
262 public: // producer
283  setInterestFilter(const InterestFilter& interestFilter,
284  const InterestCallback& onInterest,
285  const RegisterPrefixFailureCallback& onFailure,
286  const security::SigningInfo& signingInfo = security::SigningInfo(),
287  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
288 
310  setInterestFilter(const InterestFilter& interestFilter,
311  const InterestCallback& onInterest,
312  const RegisterPrefixSuccessCallback& onSuccess,
313  const RegisterPrefixFailureCallback& onFailure,
314  const security::SigningInfo& signingInfo = security::SigningInfo(),
315  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
316 
330  setInterestFilter(const InterestFilter& interestFilter,
331  const InterestCallback& onInterest);
332 
351  registerPrefix(const Name& prefix,
352  const RegisterPrefixSuccessCallback& onSuccess,
353  const RegisterPrefixFailureCallback& onFailure,
354  const security::SigningInfo& signingInfo = security::SigningInfo(),
355  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
356 
369  void
370  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
371 
380  void
381  unsetInterestFilter(const InterestFilterId* interestFilterId);
382 
396  void
397  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
398  const UnregisterPrefixSuccessCallback& onSuccess,
399  const UnregisterPrefixFailureCallback& onFailure);
400 
411  void
412  put(Data data);
413 
420  void
421  put(lp::Nack nack);
422 
423 public: // IO routine
453  void
454  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
455  bool keepThread = false)
456  {
457  this->doProcessEvents(timeout, keepThread);
458  }
459 
472  void
473  shutdown();
474 
478  boost::asio::io_service&
480  {
481  return m_ioService;
482  }
483 
488  shared_ptr<Transport>
489  getTransport();
490 
491 protected:
492  virtual void
493  doProcessEvents(time::milliseconds timeout, bool keepThread);
494 
495 private:
499  shared_ptr<Transport>
500  makeDefaultTransport();
501 
506  void
507  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
508 
509  void
510  onReceiveElement(const Block& blockFromDaemon);
511 
512  void
513  asyncShutdown();
514 
515 private:
517  unique_ptr<boost::asio::io_service> m_internalIoService;
519  boost::asio::io_service& m_ioService;
520 
521  shared_ptr<Transport> m_transport;
522 
530  unique_ptr<KeyChain> m_internalKeyChain;
531 
532  unique_ptr<nfd::Controller> m_nfdController;
533 
534  class Impl;
535  shared_ptr<Impl> m_impl;
536 };
537 
551 {
552 public:
553  PendingInterestHandle() noexcept = default;
554 
555  PendingInterestHandle(Face& face, const PendingInterestId* id);
556 
557  operator const PendingInterestId*() const noexcept
558  {
559  return m_id;
560  }
561 
562 private:
563  const PendingInterestId* m_id = nullptr;
564 };
565 
584 
588 {
589 public:
591  {
592  // This could have been '= default', but there's compiler bug in Apple clang 9.0.0,
593  // see https://stackoverflow.com/a/44693603
594  }
595 
596  RegisteredPrefixHandle(Face& face, const RegisteredPrefixId* id);
597 
598  operator const RegisteredPrefixId*() const noexcept
599  {
600  return m_id;
601  }
602 
610  void
611  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
612  const UnregisterPrefixFailureCallback& onFailure = nullptr);
613 
614 private:
615  Face* m_face = nullptr;
616  const RegisteredPrefixId* m_id = nullptr;
617 };
618 
639 
653 {
654 public:
655  InterestFilterHandle() noexcept = default;
656 
657  InterestFilterHandle(Face& face, const InterestFilterId* id);
658 
659  operator const InterestFilterId*() const noexcept
660  {
661  return m_id;
662  }
663 
664 private:
665  const InterestFilterId* m_id = nullptr;
666 };
667 
686 
687 } // namespace ndn
688 
689 #endif // NDN_FACE_HPP
Definition: data.cpp:26
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:88
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:44
Signing parameters passed to KeyChain.
represents a Network Nack
Definition: nack.hpp:38
A handle of pending Interest.
Definition: face.hpp:550
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:93
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:42
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:78
void processEvents(time::milliseconds timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.hpp:454
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:73
Represents an absolute name.
Definition: name.hpp:43
boost::asio::io_service & getIoService()
Definition: face.hpp:479
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:68
RegisteredPrefixHandle() noexcept
Definition: face.hpp:590
Handle to cancel an operation.
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:83
A handle of registered Interest filter.
Definition: face.hpp:652
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:63
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:58
Represents a Data packet.
Definition: data.hpp:35
A handle of registered prefix.
Definition: face.hpp:587
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:53
Cancels an operation automatically upon destruction.
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:105