node.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_NODE_HPP
23 #define NDN_NODE_HPP
24 
25 #include <map>
26 #include <ndn-cpp/ndn-cpp-config.h>
27 #ifdef NDN_CPP_HAVE_BOOST_ASIO
28 #include <boost/atomic.hpp>
29 #endif
30 #include <ndn-cpp/common.hpp>
31 #include <ndn-cpp/interest.hpp>
32 #include <ndn-cpp/data.hpp>
33 #include <ndn-cpp/forwarding-flags.hpp>
34 #include <ndn-cpp/interest-filter.hpp>
35 #include <ndn-cpp/face.hpp>
36 #include "util/command-interest-generator.hpp"
37 #include "impl/delayed-call-table.hpp"
38 #include "impl/interest-filter-table.hpp"
39 #include "impl/pending-interest-table.hpp"
40 #include "impl/registered-prefix-table.hpp"
41 #include "encoding/element-listener.hpp"
42 
43 struct ndn_Interest;
44 
45 namespace ndn {
46 
47 class KeyChain;
48 
49 class Node : public ElementListener {
50 public:
56  Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo);
57 
87  void
89  (uint64_t pendingInterestId,
90  const ptr_lib::shared_ptr<const Interest>& interestCopy,
91  const OnData& onData, const OnTimeout& onTimeout,
92  const OnNetworkNack& onNetworkNack, WireFormat& wireFormat, Face* face);
93 
100  void
101  removePendingInterest(uint64_t pendingInterestId)
102  {
103  pendingInterestTable_.removePendingInterest(pendingInterestId);
104  }
105 
116  void
118  (Interest& interest, KeyChain& keyChain, const Name& certificateName,
119  WireFormat& wireFormat)
120  {
121  commandInterestGenerator_.generate
122  (interest, keyChain, certificateName, wireFormat);
123  }
124 
153  void
155  (uint64_t registeredPrefixId,
156  const ptr_lib::shared_ptr<const Name>& prefixCopy,
157  const OnInterestCallback& onInterest,
158  const OnRegisterFailed& onRegisterFailed,
159  const OnRegisterSuccess& onRegisterSuccess, const ForwardingFlags& flags,
160  WireFormat& wireFormat, KeyChain& commandKeyChain,
161  const Name& commandCertificateName, Face* face);
162 
167  void
169  (uint64_t registeredPrefixId,
170  const ptr_lib::shared_ptr<const Name>& prefixCopy,
171  const OnInterestCallback& onInterest,
172  const OnRegisterFailed& onRegisterFailed,
173  const OnRegisterSuccess& onRegisterSuccess, const ForwardingFlags& flags,
174  WireFormat& wireFormat, Face* face)
175  {
177  (registeredPrefixId, prefixCopy, onInterest, onRegisterFailed,
178  onRegisterSuccess, flags, wireFormat, *face->getCommandKeyChain(),
179  face->getCommandCertificateName(), face);
180  }
181 
190  void
191  removeRegisteredPrefix(uint64_t registeredPrefixId)
192  {
193  registeredPrefixTable_.removeRegisteredPrefix(registeredPrefixId);
194  }
195 
213  void
215  (uint64_t interestFilterId,
216  const ptr_lib::shared_ptr<const InterestFilter>& filterCopy,
217  const OnInterestCallback& onInterest, Face* face)
218  {
219  interestFilterTable_.setInterestFilter
220  (interestFilterId, filterCopy, onInterest, face);
221  }
222 
230  void
231  unsetInterestFilter(uint64_t interestFilterId)
232  {
233  interestFilterTable_.unsetInterestFilter(interestFilterId);
234  }
235 
243  void
244  send(const uint8_t *encoding, size_t encodingLength);
245 
258  void
259  processEvents();
260 
261  const ptr_lib::shared_ptr<Transport>&
262  getTransport() { return transport_; }
263 
264  const ptr_lib::shared_ptr<const Transport::ConnectionInfo>&
265  getConnectionInfo() { return connectionInfo_; }
266 
267  void
268  onReceivedElement(const uint8_t *element, size_t elementLength);
269 
275  bool
276  isLocal() { return transport_->isLocal(*connectionInfo_); }
277 
278  void
279  shutdown();
280 
288  static size_t
289  getMaxNdnPacketSize() { return MAX_NDN_PACKET_SIZE; }
290 
297  void
298  callLater(Milliseconds delayMilliseconds, const Face::Callback& callback)
299  {
300  delayedCallTable_.callLater(delayMilliseconds, callback);
301  }
302 
311  uint64_t
312  getNextEntryId();
313 
314 private:
315  enum ConnectStatus {
316  ConnectStatus_UNCONNECTED = 1,
317  ConnectStatus_CONNECT_REQUESTED = 2,
318  ConnectStatus_CONNECT_COMPLETE = 3
319  };
320 
327  class RegisterResponse {
328  public:
329  class Info;
330  RegisterResponse
331  (ptr_lib::shared_ptr<RegisterResponse::Info> info, Node& parent)
332  : info_(info), parent_(parent)
333  {
334  }
335 
341  void
342  operator()(const ptr_lib::shared_ptr<const Interest>& interest,
343  const ptr_lib::shared_ptr<Data>& responseData);
344 
349  void
350  operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest);
351 
352  class Info {
353  public:
354  Info(const ptr_lib::shared_ptr<const Name>& prefix,
355  const OnRegisterFailed& onRegisterFailed,
356  const OnRegisterSuccess& onRegisterSuccess,
357  uint64_t registeredPrefixId, const OnInterestCallback& onInterest,
358  Face* face)
359  : prefix_(prefix), onRegisterFailed_(onRegisterFailed),
360  onRegisterSuccess_(onRegisterSuccess),
361  registeredPrefixId_(registeredPrefixId), onInterest_(onInterest),
362  face_(face)
363  {
364  }
365 
366  ptr_lib::shared_ptr<const Name> prefix_;
367  const OnRegisterFailed onRegisterFailed_;
368  const OnRegisterSuccess onRegisterSuccess_;
369  uint64_t registeredPrefixId_;
370  const OnInterestCallback onInterest_;
371  Face* face_;
372  };
373 
374  private:
375  ptr_lib::shared_ptr<Info> info_;
376  Node& parent_;
377  };
378 
407  void
408  expressInterestHelper
409  (uint64_t pendingInterestId,
410  const ptr_lib::shared_ptr<const Interest>& interestCopy,
411  const OnData& onData, const OnTimeout& onTimeout,
412  const OnNetworkNack& onNetworkNack, WireFormat* wireFormat, Face* face);
413 
420  void
421  processInterestTimeout(ptr_lib::shared_ptr<PendingInterestTable::Entry> pendingInterest);
422 
437  void
438  nfdRegisterPrefix
439  (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix,
440  const OnInterestCallback& onInterest,
441  const OnRegisterFailed& onRegisterFailed,
442  const OnRegisterSuccess& onRegisterSuccess, const ForwardingFlags& flags,
443  KeyChain& commandKeyChain, const Name& commandCertificateName,
444  WireFormat& wireFormat, Face* face);
445 
449  void
450  onConnected();
451 
452  ptr_lib::shared_ptr<Transport> transport_;
453  ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
454  PendingInterestTable pendingInterestTable_;
455  RegisteredPrefixTable registeredPrefixTable_;
456  InterestFilterTable interestFilterTable_;
457  DelayedCallTable delayedCallTable_;
458  std::vector<Face::Callback> onConnectedCallbacks_;
459  CommandInterestGenerator commandInterestGenerator_;
460  Name timeoutPrefix_;
461  ConnectStatus connectStatus_;
462  Blob nonceTemplate_;
463 #ifdef NDN_CPP_HAVE_BOOST_ASIO
464  // ThreadsafeFace accesses lastEntryId_ outside of a thread safe dispatch, so
465  // use atomic_uint64_t to be thread safe.
466  boost::atomic_uint64_t lastEntryId_;
467 #else
468  // Not using Boost asio to dispatch, so we can use a normal uint64_t.
469  uint64_t lastEntryId_;
470 #endif
471 };
472 
473 }
474 
475 #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
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:111
Definition: node.hpp:352
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
void setInterestFilter(uint64_t interestFilterId, const ptr_lib::shared_ptr< const InterestFilter > &filterCopy, const OnInterestCallback &onInterest, Face *face)
Add an entry to the local interest filter table to call the onInterest callback for a matching incomi...
Definition: node.hpp:215
void callLater(Milliseconds delayMilliseconds, const Face::Callback &callback)
Call callback() after the given delay.
Definition: node.hpp:298
void processEvents()
Process any packets to receive and call callbacks such as onData, onInterest or onTimeout.
Definition: node.cpp:291
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
An ElementListener extends an ndn_ElementListener struct to proved an abstract virtual onReceivedElem...
Definition: element-listener.hpp:33
A ForwardingFlags object holds the flags which specify how the forwarding daemon should forward an in...
Definition: forwarding-flags.hpp:35
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
A RegisteredPrefixTable is an internal class to hold a list of registered prefixes with information n...
Definition: registered-prefix-table.hpp:34
static size_t getMaxNdnPacketSize()
Get the practical limit of the size of a network-layer packet.
Definition: node.hpp:289
An ndn_Interest holds an ndn_Name and other fields for an interest.
Definition: interest-types.h:61
Node(const ptr_lib::shared_ptr< Transport > &transport, const ptr_lib::shared_ptr< const Transport::ConnectionInfo > &connectionInfo)
Create a new Node for communication with an NDN hub with the given Transport object and connectionInf...
Definition: node.cpp:41
void send(const uint8_t *encoding, size_t encodingLength)
Send the encoded packet out through the face.
Definition: node.cpp:137
Definition: delayed-call-table.hpp:30
void unsetInterestFilter(uint64_t interestFilterId)
Remove the interest filter entry which has the interestFilterId from the interest filter table...
Definition: node.hpp:231
KeyChain is the main class of the security library.
Definition: key-chain.hpp:45
void onReceivedElement(const uint8_t *element, size_t elementLength)
This is called when an entire element is received.
Definition: node.cpp:301
A PendingInterestTable is an internal class to hold a list of pending interests with their callbacks...
Definition: pending-interest-table.hpp:33
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
void expressInterest(uint64_t pendingInterestId, const ptr_lib::shared_ptr< const Interest > &interestCopy, const OnData &onData, const OnTimeout &onTimeout, const OnNetworkNack &onNetworkNack, WireFormat &wireFormat, Face *face)
Send the Interest through the transport, read the entire response and call onData, onTimeout or onNetworkNack as described below.
Definition: node.cpp:52
func_lib::function< void()> Callback
Face::Callback is used internally in callLater.
Definition: face.hpp:674
void makeCommandInterest(Interest &interest, KeyChain &keyChain, const Name &certificateName, WireFormat &wireFormat)
Append a timestamp component and a random value component to interest's name.
Definition: node.hpp:118
void removePendingInterest(uint64_t pendingInterestId)
Remove the pending interest entry with the pendingInterestId from the pending interest table and set ...
Definition: pending-interest-table.cpp:110
void removeRegisteredPrefix(uint64_t registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId from the registered prefix table...
Definition: node.hpp:191
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
void callLater(Milliseconds delayMilliseconds, const Face::Callback &callback)
Call callback() after the given delay.
Definition: delayed-call-table.cpp:32
uint64_t getNextEntryId()
Get the next unique entry ID for the pending interest table, interest filter table, etc.
Definition: node.cpp:147
An InterestFilterTable is an internal class to hold a list of entries with an interest Filter and its...
Definition: interest-filter-table.hpp:33
Definition: wire-format.hpp:39
An CommandInterestGenerator keeps track of a timestamp and generates command interests according to t...
Definition: command-interest-generator.hpp:35
void setInterestFilter(uint64_t interestFilterId, const ptr_lib::shared_ptr< const InterestFilter > &filterCopy, const OnInterestCallback &onInterest, Face *face)
Add a new entry to the table.
Definition: interest-filter-table.hpp:114
bool isLocal()
Check if the face is local based on the current connection through the Transport; some Transport may ...
Definition: node.hpp:276
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
void registerPrefix(uint64_t registeredPrefixId, const ptr_lib::shared_ptr< const Name > &prefixCopy, const OnInterestCallback &onInterest, const OnRegisterFailed &onRegisterFailed, const OnRegisterSuccess &onRegisterSuccess, const ForwardingFlags &flags, WireFormat &wireFormat, KeyChain &commandKeyChain, const Name &commandCertificateName, Face *face)
Register prefix with the connected NDN hub and call onInterest when a matching interest is received...
Definition: node.cpp:122
void unsetInterestFilter(uint64_t interestFilterId)
Remove the interest filter entry which has the interestFilterId from the interest filter table...
Definition: interest-filter-table.cpp:44
void removeRegisteredPrefix(uint64_t registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId from the registered prefix table...
Definition: registered-prefix-table.cpp:53
void generate(Interest &interest, KeyChain &keyChain, const Name &certificateName, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Append a timestamp component and a random value component to interest's name.
Definition: command-interest-generator.cpp:40
void removePendingInterest(uint64_t pendingInterestId)
Remove the pending interest entry with the pendingInterestId from the pending interest table...
Definition: node.hpp:101
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