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-2018 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 "data.hpp"
26 #include "name.hpp"
27 #include "interest.hpp"
28 #include "interest-filter.hpp"
30 #include "lp/nack.hpp"
31 #include "net/asio-fwd.hpp"
32 #include "security/key-chain.hpp"
34 
35 namespace ndn {
36 
37 class Transport;
38 
39 class PendingInterestId;
40 class RegisteredPrefixId;
41 class InterestFilterId;
42 
43 namespace nfd {
44 class Controller;
45 } // namespace nfd
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(boost::asio::io_service& ioService);
164 
171  explicit
172  Face(const std::string& host, const std::string& port = "6363");
173 
186  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
187 
201  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
202 
217  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
218 
219  virtual
220  ~Face();
221 
222 public: // consumer
233  const PendingInterestId*
234  expressInterest(const Interest& interest,
235  const DataCallback& afterSatisfied,
236  const NackCallback& afterNacked,
237  const TimeoutCallback& afterTimeout);
238 
244  void
245  removePendingInterest(const PendingInterestId* pendingInterestId);
246 
250  void
251  removeAllPendingInterests();
252 
256  size_t
257  getNPendingInterests() const;
258 
259 public: // producer
280  const RegisteredPrefixId*
281  setInterestFilter(const InterestFilter& interestFilter,
282  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  const RegisteredPrefixId*
309  setInterestFilter(const InterestFilter& interestFilter,
310  const InterestCallback& onInterest,
311  const RegisterPrefixSuccessCallback& onSuccess,
312  const RegisterPrefixFailureCallback& onFailure,
313  const security::SigningInfo& signingInfo = security::SigningInfo(),
314  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
315 
328  const InterestFilterId*
329  setInterestFilter(const InterestFilter& interestFilter,
330  const InterestCallback& onInterest);
331 
349  const RegisteredPrefixId*
350  registerPrefix(const Name& prefix,
351  const RegisterPrefixSuccessCallback& onSuccess,
352  const RegisterPrefixFailureCallback& onFailure,
353  const security::SigningInfo& signingInfo = security::SigningInfo(),
354  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
355 
368  void
369  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
370 
379  void
380  unsetInterestFilter(const InterestFilterId* interestFilterId);
381 
395  void
396  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
397  const UnregisterPrefixSuccessCallback& onSuccess,
398  const UnregisterPrefixFailureCallback& onFailure);
399 
410  void
411  put(Data data);
412 
419  void
420  put(lp::Nack nack);
421 
422 public: // IO routine
452  void
453  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
454  bool keepThread = false)
455  {
456  this->doProcessEvents(timeout, keepThread);
457  }
458 
471  void
472  shutdown();
473 
477  boost::asio::io_service&
479  {
480  return m_ioService;
481  }
482 
487  shared_ptr<Transport>
488  getTransport();
489 
490 protected:
491  virtual void
492  doProcessEvents(time::milliseconds timeout, bool keepThread);
493 
494 private:
498  shared_ptr<Transport>
499  makeDefaultTransport();
500 
505  void
506  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
507 
508  void
509  onReceiveElement(const Block& blockFromDaemon);
510 
511  void
512  asyncShutdown();
513 
514 private:
516  unique_ptr<boost::asio::io_service> m_internalIoService;
518  boost::asio::io_service& m_ioService;
519 
520  shared_ptr<Transport> m_transport;
521 
529  unique_ptr<KeyChain> m_internalKeyChain;
530 
531  unique_ptr<nfd::Controller> m_nfdController;
532 
533  class Impl;
534  shared_ptr<Impl> m_impl;
535 };
536 
537 } // namespace ndn
538 
539 #endif // NDN_FACE_HPP
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:65
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:85
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:43
Signing parameters passed to KeyChain.
represents a Network Nack
Definition: nack.hpp:40
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:453
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:70
Represents an absolute name.
Definition: name.hpp:42
boost::asio::io_service & getIoService()
Definition: face.hpp:478
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:65
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:80
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:60
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:55
Represents a Data packet.
Definition: data.hpp:35
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:42
function< void(const Interest &, const Data &)> DataCallback
Callback invoked when expressed Interest gets satisfied with a Data packet.
Definition: face.hpp:50
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:102