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-2017 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  explicit
97  Error(const std::string& what)
98  : std::runtime_error(what)
99  {
100  }
101  };
102 
107  {
108  public:
115  OversizedPacketError(char pktType, const Name& name, size_t wireSize);
116 
117  public:
118  const char pktType;
119  const Name name;
120  const size_t wireSize;
121  };
122 
123 public: // constructors
135  explicit
136  Face(shared_ptr<Transport> transport = nullptr);
137 
166  explicit
167  Face(boost::asio::io_service& ioService);
168 
175  explicit
176  Face(const std::string& host, const std::string& port = "6363");
177 
190  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
191 
205  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
206 
221  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
222 
223  virtual
224  ~Face();
225 
226 public: // consumer
237  const PendingInterestId*
238  expressInterest(const Interest& interest,
239  const DataCallback& afterSatisfied,
240  const NackCallback& afterNacked,
241  const TimeoutCallback& afterTimeout);
242 
248  void
249  removePendingInterest(const PendingInterestId* pendingInterestId);
250 
254  void
255  removeAllPendingInterests();
256 
260  size_t
261  getNPendingInterests() const;
262 
263 public: // producer
284  const RegisteredPrefixId*
285  setInterestFilter(const InterestFilter& interestFilter,
286  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  const RegisteredPrefixId*
313  setInterestFilter(const InterestFilter& interestFilter,
314  const InterestCallback& onInterest,
315  const RegisterPrefixSuccessCallback& onSuccess,
316  const RegisterPrefixFailureCallback& onFailure,
317  const security::SigningInfo& signingInfo = security::SigningInfo(),
318  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
319 
332  const InterestFilterId*
333  setInterestFilter(const InterestFilter& interestFilter,
334  const InterestCallback& onInterest);
335 
353  const RegisteredPrefixId*
354  registerPrefix(const Name& prefix,
355  const RegisterPrefixSuccessCallback& onSuccess,
356  const RegisterPrefixFailureCallback& onFailure,
357  const security::SigningInfo& signingInfo = security::SigningInfo(),
358  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
359 
372  void
373  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
374 
383  void
384  unsetInterestFilter(const InterestFilterId* interestFilterId);
385 
399  void
400  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
401  const UnregisterPrefixSuccessCallback& onSuccess,
402  const UnregisterPrefixFailureCallback& onFailure);
403 
414  void
415  put(Data data);
416 
423  void
424  put(lp::Nack nack);
425 
426 public: // IO routine
450  void
451  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
452  bool keepThread = false)
453  {
454  this->doProcessEvents(timeout, keepThread);
455  }
456 
465  void
466  shutdown();
467 
471  boost::asio::io_service&
473  {
474  return m_ioService;
475  }
476 
481  shared_ptr<Transport>
482  getTransport();
483 
484 protected:
485  virtual void
486  doProcessEvents(time::milliseconds timeout, bool keepThread);
487 
488 private:
492  shared_ptr<Transport>
493  makeDefaultTransport();
494 
499  void
500  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
501 
502  void
503  onReceiveElement(const Block& blockFromDaemon);
504 
505  void
506  asyncShutdown();
507 
508 private:
510  unique_ptr<boost::asio::io_service> m_internalIoService;
512  boost::asio::io_service& m_ioService;
513 
514  shared_ptr<Transport> m_transport;
515 
523  unique_ptr<KeyChain> m_internalKeyChain;
524 
525  unique_ptr<nfd::Controller> m_nfdController;
526 
527  class Impl;
528  shared_ptr<Impl> m_impl;
529 };
530 
531 } // namespace ndn
532 
533 #endif // NDN_FACE_HPP
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
Error(const std::string &what)
Definition: face.hpp:97
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
STL namespace.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
represents an Interest packet
Definition: interest.hpp:42
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:451
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:472
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:106