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 "security/key-chain.hpp"
33 
34 namespace boost {
35 namespace asio {
36 class io_service;
37 } // namespace asio
38 } // namespace boost
39 
40 namespace ndn {
41 
42 class Transport;
43 
44 class PendingInterestId;
45 class RegisteredPrefixId;
46 class InterestFilterId;
47 
48 namespace nfd {
49 class Controller;
50 } // namespace nfd
51 
55 typedef function<void(const Interest&, const Data&)> DataCallback;
56 
60 typedef function<void(const Interest&, const lp::Nack&)> NackCallback;
61 
65 typedef function<void(const Interest&)> TimeoutCallback;
66 
70 typedef function<void(const InterestFilter&, const Interest&)> InterestCallback;
71 
75 typedef function<void(const Name&)> RegisterPrefixSuccessCallback;
76 
80 typedef function<void(const Name&, const std::string&)> RegisterPrefixFailureCallback;
81 
85 typedef function<void()> UnregisterPrefixSuccessCallback;
86 
90 typedef function<void(const std::string&)> UnregisterPrefixFailureCallback;
91 
95 class Face : noncopyable
96 {
97 public:
98  class Error : public std::runtime_error
99  {
100  public:
101  explicit
102  Error(const std::string& what)
103  : std::runtime_error(what)
104  {
105  }
106  };
107 
112  {
113  public:
120  OversizedPacketError(char pktType, const Name& name, size_t wireSize);
121 
122  public:
123  const char pktType;
124  const Name name;
125  const size_t wireSize;
126  };
127 
128 public: // constructors
140  explicit
141  Face(shared_ptr<Transport> transport = nullptr);
142 
171  explicit
172  Face(boost::asio::io_service& ioService);
173 
180  explicit
181  Face(const std::string& host, const std::string& port = "6363");
182 
195  Face(shared_ptr<Transport> transport, KeyChain& keyChain);
196 
210  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService);
211 
226  Face(shared_ptr<Transport> transport, boost::asio::io_service& ioService, KeyChain& keyChain);
227 
228  virtual
229  ~Face();
230 
231 public: // consumer
242  const PendingInterestId*
243  expressInterest(const Interest& interest,
244  const DataCallback& afterSatisfied,
245  const NackCallback& afterNacked,
246  const TimeoutCallback& afterTimeout);
247 
253  void
254  removePendingInterest(const PendingInterestId* pendingInterestId);
255 
259  void
260  removeAllPendingInterests();
261 
265  size_t
266  getNPendingInterests() const;
267 
268 public: // producer
289  const RegisteredPrefixId*
290  setInterestFilter(const InterestFilter& interestFilter,
291  const InterestCallback& onInterest,
292  const RegisterPrefixFailureCallback& onFailure,
293  const security::SigningInfo& signingInfo = security::SigningInfo(),
294  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
295 
317  const RegisteredPrefixId*
318  setInterestFilter(const InterestFilter& interestFilter,
319  const InterestCallback& onInterest,
320  const RegisterPrefixSuccessCallback& onSuccess,
321  const RegisterPrefixFailureCallback& onFailure,
322  const security::SigningInfo& signingInfo = security::SigningInfo(),
323  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
324 
337  const InterestFilterId*
338  setInterestFilter(const InterestFilter& interestFilter,
339  const InterestCallback& onInterest);
340 
358  const RegisteredPrefixId*
359  registerPrefix(const Name& prefix,
360  const RegisterPrefixSuccessCallback& onSuccess,
361  const RegisterPrefixFailureCallback& onFailure,
362  const security::SigningInfo& signingInfo = security::SigningInfo(),
363  uint64_t flags = nfd::ROUTE_FLAG_CHILD_INHERIT);
364 
377  void
378  unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId);
379 
388  void
389  unsetInterestFilter(const InterestFilterId* interestFilterId);
390 
404  void
405  unregisterPrefix(const RegisteredPrefixId* registeredPrefixId,
406  const UnregisterPrefixSuccessCallback& onSuccess,
407  const UnregisterPrefixFailureCallback& onFailure);
408 
419  void
420  put(Data data);
421 
428  void
429  put(lp::Nack nack);
430 
431 public: // IO routine
455  void
456  processEvents(time::milliseconds timeout = time::milliseconds::zero(),
457  bool keepThread = false)
458  {
459  this->doProcessEvents(timeout, keepThread);
460  }
461 
470  void
471  shutdown();
472 
476  boost::asio::io_service&
478  {
479  return m_ioService;
480  }
481 
486  shared_ptr<Transport>
487  getTransport();
488 
489 protected:
490  virtual void
491  doProcessEvents(time::milliseconds timeout, bool keepThread);
492 
493 private:
497  shared_ptr<Transport>
498  makeDefaultTransport();
499 
504  void
505  construct(shared_ptr<Transport> transport, KeyChain& keyChain);
506 
507  void
508  onReceiveElement(const Block& blockFromDaemon);
509 
510  void
511  asyncShutdown();
512 
513 private:
515  unique_ptr<boost::asio::io_service> m_internalIoService;
517  boost::asio::io_service& m_ioService;
518 
519  shared_ptr<Transport> m_transport;
520 
528  unique_ptr<KeyChain> m_internalKeyChain;
529 
530  unique_ptr<nfd::Controller> m_nfdController;
531 
532  class Impl;
533  shared_ptr<Impl> m_impl;
534 };
535 
536 } // namespace ndn
537 
538 #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:102
function< void(const std::string &)> UnregisterPrefixFailureCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command fails.
Definition: face.hpp:90
Copyright (c) 2013-2017 Regents of the University of California.
Definition: block.hpp:31
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:95
function< void(const Name &, const std::string &)> RegisterPrefixFailureCallback
Callback invoked when registerPrefix or setInterestFilter command fails.
Definition: face.hpp:80
void processEvents(time::milliseconds timeout=time::milliseconds::zero(), bool keepThread=false)
Process any data to receive or call timeout callbacks.
Definition: face.hpp:456
function< void(const Name &)> RegisterPrefixSuccessCallback
Callback invoked when registerPrefix or setInterestFilter command succeeds.
Definition: face.hpp:75
Represents an absolute name.
Definition: name.hpp:42
boost::asio::io_service & getIoService()
Definition: face.hpp:477
function< void(const InterestFilter &, const Interest &)> InterestCallback
Callback invoked when incoming Interest matches the specified InterestFilter.
Definition: face.hpp:70
function< void()> UnregisterPrefixSuccessCallback
Callback invoked when unregisterPrefix or unsetInterestFilter command succeeds.
Definition: face.hpp:85
function< void(const Interest &)> TimeoutCallback
Callback invoked when expressed Interest times out.
Definition: face.hpp:65
function< void(const Interest &, const lp::Nack &)> NackCallback
Callback invoked when Nack is sent in response to expressed Interest.
Definition: face.hpp:60
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:55
Exception thrown when attempting to send a packet over size limit.
Definition: face.hpp:111