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 
246  [[deprecated]]
247  void
248  removePendingInterest(const PendingInterestId* pendingInterestId)
249  {
250  cancelPendingInterest(pendingInterestId);
251  }
252 
256  void
257  removeAllPendingInterests();
258 
262  size_t
263  getNPendingInterests() const;
264 
265 public: // producer
286  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
287  const RegisterPrefixFailureCallback& onFailure,
288  const security::SigningInfo& signingInfo = security::SigningInfo(),
289  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
290 
312  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
313  const RegisterPrefixSuccessCallback& onSuccess,
314  const RegisterPrefixFailureCallback& onFailure,
315  const security::SigningInfo& signingInfo = security::SigningInfo(),
316  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
317 
331  setInterestFilter(const InterestFilter& filter, 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 
360  [[deprecated]]
361  void
362  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
363  {
364  unregisterPrefixImpl(registeredPrefixId, nullptr, nullptr);
365  }
366 
370  [[deprecated]]
371  void
372  unsetInterestFilter(const InterestFilterId* interestFilterId)
373  {
374  clearInterestFilter(interestFilterId);
375  }
376 
380  [[deprecated]]
381  void
382  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
383  const UnregisterPrefixSuccessCallback& onSuccess,
384  const UnregisterPrefixFailureCallback& onFailure)
385  {
386  unregisterPrefixImpl(registeredPrefixId, onSuccess, onFailure);
387  }
388 
399  void
400  put(Data data);
401 
408  void
409  put(lp::Nack nack);
410 
411 public: // IO routine
441  void
442  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
443  bool keepThread = false)
444  {
445  this->doProcessEvents(timeout, keepThread);
446  }
447 
460  void
461  shutdown();
462 
466  boost::asio::io_service&
468  {
469  return m_ioService;
470  }
471 
476  shared_ptr<Transport>
477  getTransport();
478 
479 protected:
480  virtual void
481  doProcessEvents(time::milliseconds timeout, bool keepThread);
482 
483 private:
487  shared_ptr<Transport>
488  makeDefaultTransport();
489 
494  void
495  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
496 
497  void
498  onReceiveElement(const Block& blockFromDaemon);
499 
500  void
501  cancelPendingInterest(const PendingInterestId* pendingInterestId);
502 
503  void
504  clearInterestFilter(const InterestFilterId* interestFilterId);
505 
506  void
507  unregisterPrefixImpl(const RegisteredPrefixId* registeredPrefixId,
508  const UnregisterPrefixSuccessCallback& onSuccess,
509  const UnregisterPrefixFailureCallback& onFailure);
510 
511 private:
513  unique_ptr<boost::asio::io_service> m_internalIoService;
515  boost::asio::io_service& m_ioService;
516 
517  shared_ptr<Transport> m_transport;
518 
526  unique_ptr<KeyChain> m_internalKeyChain;
527 
528  unique_ptr<nfd::Controller> m_nfdController;
529 
530  class Impl;
531  shared_ptr<Impl> m_impl;
532 
533  friend PendingInterestHandle;
534  friend RegisteredPrefixHandle;
535  friend InterestFilterHandle;
536 };
537 
549 {
550 public:
551  PendingInterestHandle() noexcept = default;
552 
553  PendingInterestHandle(Face& face, const PendingInterestId* id);
554 
555  [[deprecated]]
556  operator const PendingInterestId*() const noexcept
557  {
558  return m_id;
559  }
560 
561 private:
562  const PendingInterestId* m_id = nullptr;
563 };
564 
581 
585 {
586 public:
588  {
589  // This could have been '= default', but there's compiler bug in Apple clang 9.0.0,
590  // see https://stackoverflow.com/a/44693603
591  }
592 
593  RegisteredPrefixHandle(Face& face, const RegisteredPrefixId* id);
594 
595  [[deprecated]]
596  operator const RegisteredPrefixId*() const noexcept
597  {
598  return m_id;
599  }
600 
605  void
606  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
607  const UnregisterPrefixFailureCallback& onFailure = nullptr);
608 
609 private:
610  Face* m_face = nullptr;
611  const RegisteredPrefixId* m_id = nullptr;
612 };
613 
631 
643 {
644 public:
645  InterestFilterHandle() noexcept = default;
646 
647  InterestFilterHandle(Face& face, const InterestFilterId* id);
648 
649  [[deprecated]]
650  operator const InterestFilterId*() const noexcept
651  {
652  return m_id;
653  }
654 
655 private:
656  const InterestFilterId* m_id = nullptr;
657 };
658 
675 
676 } // namespace ndn
677 
678 #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
void unsetInterestFilter(const InterestFilterId *interestFilterId)
Definition: face.hpp:372
Signing parameters passed to KeyChain.
void unregisterPrefix(const RegisteredPrefixId *registeredPrefixId, const UnregisterPrefixSuccessCallback &onSuccess, const UnregisterPrefixFailureCallback &onFailure)
Definition: face.hpp:382
represents a Network Nack
Definition: nack.hpp:38
A handle of pending Interest.
Definition: face.hpp:548
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:442
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
void unsetInterestFilter(const RegisteredPrefixId *registeredPrefixId)
Definition: face.hpp:362
boost::asio::io_service & getIoService()
Definition: face.hpp:467
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:587
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:642
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
util::scheduler::EventId deprecated
Definition: scheduler.hpp:228
A handle of registered prefix.
Definition: face.hpp:584
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:53
void removePendingInterest(const PendingInterestId *pendingInterestId)
Definition: face.hpp:248
Cancels an operation automatically upon destruction.
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:105