NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2022 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_CXX_FACE_HPP
23 #define NDN_CXX_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(DummyIoService& ioService);
164 
177  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
178 
179  virtual
180  ~Face();
181 
182 public: // consumer
195  expressInterest(const Interest& interest,
196  const DataCallback& afterSatisfied,
197  const NackCallback& afterNacked,
198  const TimeoutCallback& afterTimeout);
199 
203  void
204  removeAllPendingInterests();
205 
209  size_t
210  getNPendingInterests() const;
211 
212 public: // producer
233  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
234  const RegisterPrefixFailureCallback& onFailure,
235  const security::SigningInfo& signingInfo = security::SigningInfo(),
236  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
237 
259  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest,
260  const RegisterPrefixSuccessCallback& onSuccess,
261  const RegisterPrefixFailureCallback& onFailure,
262  const security::SigningInfo& signingInfo = security::SigningInfo(),
263  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
264 
278  setInterestFilter(const InterestFilter& filter, const InterestCallback& onInterest);
279 
298  registerPrefix(const Name& prefix,
299  const RegisterPrefixSuccessCallback& onSuccess,
300  const RegisterPrefixFailureCallback& onFailure,
301  const security::SigningInfo& signingInfo = security::SigningInfo(),
302  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
303 
314  void
315  put(Data data);
316 
323  void
324  put(lp::Nack nack);
325 
326 public: // IO routine
356  void
357  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
358  bool keepThread = false)
359  {
360  this->doProcessEvents(timeout, keepThread);
361  }
362 
376  void
377  shutdown();
378 
384  {
385  static DummyIoService io;
386  return io;
387  }
388 
393  shared_ptr<Transport>
394  getTransport() const
395  {
396  return m_transport;
397  }
398 
399 protected:
400  virtual void
401  doProcessEvents(time::milliseconds timeout, bool keepThread);
402 
403 private:
407  shared_ptr<Transport>
408  makeDefaultTransport();
409 
413  void
414  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
415 
416  void
417  onReceiveElement(const Block& blockFromDaemon);
418 
419 private:
420  shared_ptr<Transport> m_transport;
421 
422  class Impl;
423  shared_ptr<Impl> m_impl;
424 
425  friend PendingInterestHandle;
426  friend RegisteredPrefixHandle;
427  friend InterestFilterHandle;
428 };
429 
438 {
439 public:
440  PendingInterestHandle() noexcept = default;
441 
442 private:
443  PendingInterestHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
444 
445  friend Face;
446 };
447 
461 
465 {
466 public:
467  RegisteredPrefixHandle() noexcept = default;
468 
471  void
472  unregister(const UnregisterPrefixSuccessCallback& onSuccess = nullptr,
473  const UnregisterPrefixFailureCallback& onFailure = nullptr);
474 
475 private:
476  RegisteredPrefixHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
477 
478  static void
479  unregister(const weak_ptr<Face::Impl>& impl, detail::RecordId id,
480  const UnregisterPrefixSuccessCallback& onSuccess,
481  const UnregisterPrefixFailureCallback& onFailure);
482 
483 private:
484  weak_ptr<Face::Impl> m_weakImpl;
485  detail::RecordId m_id = 0;
486 
487  friend Face;
488 };
489 
504 
513 {
514 public:
515  InterestFilterHandle() noexcept = default;
516 
517 private:
518  InterestFilterHandle(weak_ptr<Face::Impl> impl, detail::RecordId id);
519 
520  friend Face;
521 };
522 
536 
537 } // namespace ndn
538 
539 #endif // NDN_CXX_FACE_HPP
Copyright (c) 2011-2015 Regents of the University of California.
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregistering a prefix fails.
Definition: face.hpp:85
implementation detail of Face
Definition: face-impl.hpp:60
ndn security KeyChain
Definition: key-chain.cpp:70
declares the set of Interests a producer can serve, which starts with a name prefix, plus an optional regular expression
DummyIoService & getIoService()
Definition: face.hpp:383
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
Represents an Interest packet.
Definition: interest.hpp:48
Signing parameters passed to KeyChain.
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
represents a Network Nack
Definition: nack.hpp:38
ndn Face
Definition: face-impl.hpp:42
Handle for a pending Interest.
Definition: face.hpp:437
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
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:357
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:70
Represents an absolute name.
Definition: name.hpp:41
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when an incoming Interest matches the specified InterestFilter.
Definition: face.hpp:65
Handle to cancel an operation.
uint64_t RecordId
Definition: face.hpp:44
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregistering a prefix succeeds.
Definition: face.hpp:80
Handle for a registered Interest filter.
Definition: face.hpp:512
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:37
Handle for a registered prefix.
Definition: face.hpp:464
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
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48