face.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_FACE_HPP
23 #define NDN_FACE_HPP
24 
25 #include "interest.hpp"
26 #include "data.hpp"
27 #include "network-nack.hpp"
28 #include "forwarding-flags.hpp"
29 #include "encoding/wire-format.hpp"
30 #include "interest-filter.hpp"
31 #include "transport/transport.hpp"
32 
33 namespace ndn {
34 
35 class Face;
36 
40 typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>&,
41  const ptr_lib::shared_ptr<Data>&)> OnData;
42 
46 typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>&)> OnTimeout;
47 
51 typedef func_lib::function<void(const ptr_lib::shared_ptr<const Interest>&,
52  const ptr_lib::shared_ptr<NetworkNack>&)> OnNetworkNack;
53 
57 typedef func_lib::function<void
58  (const ptr_lib::shared_ptr<const Name>&, const ptr_lib::shared_ptr<const Interest>&, Transport&, uint64_t)> OnInterest;
59 
64 typedef func_lib::function<void
65  (const ptr_lib::shared_ptr<const Name>&,
66  const ptr_lib::shared_ptr<const Interest>&, Face&, uint64_t,
67  const ptr_lib::shared_ptr<const InterestFilter>&)> OnInterestCallback;
68 
72 typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name>&)> OnRegisterFailed;
73 
77 typedef func_lib::function<void(const ptr_lib::shared_ptr<const Name>&, uint64_t)>
79 
80 class Node;
81 class KeyChain;
82 
86 class Face {
87 public:
93  Face(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo);
94 
101  Face(const char *host, unsigned short port = 6363);
102 
109  Face();
110 
111  virtual ~Face();
112 
147  virtual uint64_t
149  (const Interest& interest, const OnData& onData,
150  const OnTimeout& onTimeout, const OnNetworkNack& onNetworkNack,
152 
190  virtual uint64_t
192  (const Name& name, const Interest *interestTemplate, const OnData& onData,
193  const OnTimeout& onTimeout, const OnNetworkNack& onNetworkNack,
195 
230  uint64_t
232  (const Name& name, const OnData& onData,
233  const OnTimeout& onTimeout, const OnNetworkNack& onNetworkNack,
235  {
236  return expressInterest(name, 0, onData, onTimeout, onNetworkNack, wireFormat);
237  }
238 
264  uint64_t
266  (const Interest& interest, const OnData& onData,
267  const OnTimeout& onTimeout = OnTimeout(),
269  {
270  return expressInterest
271  (interest, onData, onTimeout, OnNetworkNack(), wireFormat);
272  }
273 
301  uint64_t
303  (const Name& name, const Interest *interestTemplate, const OnData& onData,
304  const OnTimeout& onTimeout = OnTimeout(),
306  {
307  return expressInterest
308  (name, interestTemplate, onData, onTimeout, OnNetworkNack(), wireFormat);
309  }
310 
337  uint64_t
339  (const Name& name, const OnData& onData,
340  const OnTimeout& onTimeout = OnTimeout(),
342  {
343  return expressInterest
344  (name, 0, onData, onTimeout, OnNetworkNack(), wireFormat);
345  }
346 
353  virtual void
354  removePendingInterest(uint64_t pendingInterestId);
355 
367  void
368  setCommandSigningInfo(KeyChain& keyChain, const Name& certificateName)
369  {
370  commandKeyChain_ = &keyChain;
371  commandCertificateName_ = certificateName;
372  }
373 
380  void
381  setCommandCertificateName(const Name& certificateName)
382  {
383  commandCertificateName_ = certificateName;
384  }
385 
386  KeyChain*
387  getCommandKeyChain() { return commandKeyChain_; }
388 
389  const Name&
390  getCommandCertificateName() { return commandCertificateName_; }
391 
403  virtual void
405  (Interest& interest,
406  WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
407 
445  virtual uint64_t
447  (const Name& prefix, const OnInterestCallback& onInterest,
448  const OnRegisterFailed& onRegisterFailed,
449  const OnRegisterSuccess& onRegisterSuccess,
450  const ForwardingFlags& flags = ForwardingFlags(),
451  WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
452 
479  uint64_t
481  (const Name& prefix, const OnInterestCallback& onInterest,
482  const OnRegisterFailed& onRegisterFailed,
483  const ForwardingFlags& flags,
485  {
486  return registerPrefix
487  (prefix, onInterest, onRegisterFailed, OnRegisterSuccess(), flags,
488  wireFormat);
489  }
490 
513  uint64_t
515  (const Name& prefix, const OnInterestCallback& onInterest,
516  const OnRegisterFailed& onRegisterFailed)
517  {
518  return registerPrefix
519  (prefix, onInterest, onRegisterFailed, OnRegisterSuccess(),
521  }
522 
528  uint64_t
529  DEPRECATED_IN_NDN_CPP registerPrefix
530  (const Name& prefix, const OnInterest& onInterest,
531  const OnRegisterFailed& onRegisterFailed,
532  const ForwardingFlags& flags = ForwardingFlags(),
534 
543  virtual void
544  removeRegisteredPrefix(uint64_t registeredPrefixId);
545 
562  virtual uint64_t
564  (const InterestFilter& filter, const OnInterestCallback& onInterest);
565 
582  virtual uint64_t
583  setInterestFilter(const Name& prefix, const OnInterestCallback& onInterest);
584 
592  virtual void
593  unsetInterestFilter(uint64_t interestFilterId);
594 
604  void
605  putData
606  (const Data& data,
608 
615  void
616  send(const Blob& encoding)
617  {
618  send(encoding.buf(), encoding.size());
619  }
620 
628  virtual void
629  send(const uint8_t *encoding, size_t encodingLength);
630 
643  void
644  processEvents();
645 
652  virtual bool
653  isLocal();
654 
658  void
659  shutdown();
660 
668  static size_t
669  getMaxNdnPacketSize() { return MAX_NDN_PACKET_SIZE; }
670 
674  typedef func_lib::function<void()> Callback;
675 
683  virtual void
684  callLater(Milliseconds delayMilliseconds, const Callback& callback);
685 
686 protected:
692  static std::string
694 
703  static ptr_lib::shared_ptr<const Interest>
704  getInterestCopy(const Name& name, const Interest *interestTemplate)
705  {
706  if (interestTemplate) {
707  // Copy the interestTemplate.
708  ptr_lib::shared_ptr<Interest> interestCopy(new Interest(*interestTemplate));
709  interestCopy->setName(name);
710  return interestCopy;
711  }
712  else
713  return ptr_lib::make_shared<Interest>(name, 4000.0);
714  }
715 
716  Node *node_;
717  KeyChain* commandKeyChain_;
718  Name commandCertificateName_;
719 
720 private:
726  static void
727  onInterestWrapper
728  (const ptr_lib::shared_ptr<const Name>& prefix,
729  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
730  uint64_t interestFilterId,
731  const ptr_lib::shared_ptr<const InterestFilter>& filter,
732  const OnInterest callerOnInterest);
733 
734  static ptr_lib::shared_ptr<Transport>
735  getDefaultTransport();
736 
737  static ptr_lib::shared_ptr<Transport::ConnectionInfo>
738  getDefaultConnectionInfo();
739 };
740 
741 }
742 
743 #endif
func_lib::function< void(const ptr_lib::shared_ptr< const Interest > &)> OnTimeout
An OnTimeout function object is used to pass a callback to expressInterest.
Definition: face.hpp:46
Definition: transport.hpp:32
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:111
void setCommandCertificateName(const Name &certificateName)
Set the certificate name used to sign command interest (e.g.
Definition: face.hpp:381
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &, const ptr_lib::shared_ptr< const Interest > &, Transport &, uint64_t)> OnInterest
Definition: face.hpp:58
Definition: data.hpp:37
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
virtual void removeRegisteredPrefix(uint64_t registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId from the registered prefix table...
Definition: face.cpp:189
A ForwardingFlags object holds the flags which specify how the forwarding daemon should forward an in...
Definition: forwarding-flags.hpp:35
static ptr_lib::shared_ptr< const Interest > getInterestCopy(const Name &name, const Interest *interestTemplate)
Do the work of expressInterest to make an Interest based on name and interestTemplate.
Definition: face.hpp:704
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &, uint64_t)> OnRegisterSuccess
An OnRegisterSuccess function object is used to report when registerPrefix succeeds.
Definition: face.hpp:78
virtual uint64_t expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout, const OnNetworkNack &onNetworkNack, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Send the Interest through the transport, read the entire response and call onData, onTimeout or onNetworkNack as described below.
Definition: face.cpp:101
virtual void removePendingInterest(uint64_t pendingInterestId)
Remove the pending interest entry with the pendingInterestId from the pending interest table...
Definition: face.cpp:131
KeyChain is the main class of the security library.
Definition: key-chain.hpp:45
void putData(const Data &data, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
The OnInterestCallback calls this to put a Data packet which satisfies an Interest.
Definition: face.cpp:230
virtual void unsetInterestFilter(uint64_t interestFilterId)
Remove the interest filter entry which has the interestFilterId from the interest filter table...
Definition: face.cpp:224
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:41
const uint8_t * buf() const
Return a const pointer to the first byte of the immutable byte array, or 0 if the pointer is null...
Definition: blob.hpp:159
static size_t getMaxNdnPacketSize()
Get the practical limit of the size of a network-layer packet.
Definition: face.hpp:669
Face()
Create a new Face for communication with an NDN hub using a default connection as follows...
func_lib::function< void()> Callback
Face::Callback is used internally in callLater.
Definition: face.hpp:674
virtual void makeCommandInterest(Interest &interest, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Append a timestamp component and a random value component to interest's name.
Definition: face.cpp:137
size_t size() const
Return the length of the immutable byte array.
Definition: blob.hpp:147
virtual uint64_t setInterestFilter(const InterestFilter &filter, const OnInterestCallback &onInterest)
Add an entry to the local interest filter table to call the onInterest callback for a matching incomi...
Definition: face.cpp:196
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &, const ptr_lib::shared_ptr< const Interest > &, Face &, uint64_t, const ptr_lib::shared_ptr< const InterestFilter > &)> OnInterestCallback
An OnInterestCallback function object is used to pass a callback to setInterestFilter and optionally ...
Definition: face.hpp:67
Definition: node.hpp:49
virtual bool isLocal()
Check if the face is local based on the current connection through the Transport; some Transport may ...
Definition: face.cpp:256
static std::string getUnixSocketFilePathForLocalhost()
If the forwarder's Unix socket file path exists, then return the file path.
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:34
void shutdown()
Shut down and disconnect this Face.
Definition: face.cpp:262
void processEvents()
Process any packets to receive and call callbacks such as onData, onInterest or onTimeout.
Definition: face.cpp:249
Definition: wire-format.hpp:39
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &)> OnRegisterFailed
An OnRegisterFailed function object is used to report when registerPrefix fails.
Definition: face.hpp:72
virtual void callLater(Milliseconds delayMilliseconds, const Callback &callback)
Call callback() after the given delay.
Definition: face.cpp:268
An InterestFilter holds a Name prefix and optional regex match expression for use in Face::setInteres...
Definition: interest-filter.hpp:33
void send(const Blob &encoding)
Send the encoded packet out through the face.
Definition: face.hpp:616
virtual uint64_t registerPrefix(const Name &prefix, const OnInterestCallback &onInterest, const OnRegisterFailed &onRegisterFailed, const OnRegisterSuccess &onRegisterSuccess, const ForwardingFlags &flags=ForwardingFlags(), WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Register prefix with the connected NDN hub and call onInterest when a matching interest is received...
Definition: face.cpp:145
void setCommandSigningInfo(KeyChain &keyChain, const Name &certificateName)
Set the KeyChain and certificate name used to sign command interests (e.g.
Definition: face.hpp:368
func_lib::function< void(const ptr_lib::shared_ptr< const Interest > &, const ptr_lib::shared_ptr< Data > &)> OnData
An OnData function object is used to pass a callback to expressInterest.
Definition: face.hpp:35
func_lib::function< void(const ptr_lib::shared_ptr< const Interest > &, const ptr_lib::shared_ptr< NetworkNack > &)> OnNetworkNack
An OnNetworkNack function object is used to pass a callback to expressInterest.
Definition: face.hpp:52