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 <deque>
27 #include <ndnboost/atomic.hpp>
28 #include <ndn-cpp/common.hpp>
29 #include <ndn-cpp/interest.hpp>
30 #include <ndn-cpp/data.hpp>
31 #include <ndn-cpp/forwarding-flags.hpp>
32 #include <ndn-cpp/interest-filter.hpp>
33 #include <ndn-cpp/face.hpp>
34 #include "util/command-interest-generator.hpp"
35 #include "encoding/element-listener.hpp"
36 
37 struct ndn_Interest;
38 
39 namespace ndn {
40 
41 class KeyChain;
42 
43 class Node : public ElementListener {
44 public:
50  Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo);
51 
69  void
71  (uint64_t pendingInterestId,
72  const ptr_lib::shared_ptr<const Interest>& interestCopy,
73  const OnData& onData, const OnTimeout& onTimeout, WireFormat& wireFormat,
74  Face* face);
75 
82  void
83  removePendingInterest(uint64_t pendingInterestId);
84 
95  void
97  (Interest& interest, KeyChain& keyChain, const Name& certificateName,
98  WireFormat& wireFormat)
99  {
100  commandInterestGenerator_.generate
101  (interest, keyChain, certificateName, wireFormat);
102  }
103 
132  void
134  (uint64_t registeredPrefixId,
135  const ptr_lib::shared_ptr<const Name>& prefixCopy,
136  const OnInterestCallback& onInterest,
137  const OnRegisterFailed& onRegisterFailed,
138  const OnRegisterSuccess& onRegisterSuccess, const ForwardingFlags& flags,
139  WireFormat& wireFormat, KeyChain& commandKeyChain,
140  const Name& commandCertificateName, Face* face);
141 
146  void
148  (uint64_t registeredPrefixId,
149  const ptr_lib::shared_ptr<const Name>& prefixCopy,
150  const OnInterestCallback& onInterest,
151  const OnRegisterFailed& onRegisterFailed,
152  const OnRegisterSuccess& onRegisterSuccess, const ForwardingFlags& flags,
153  WireFormat& wireFormat, Face* face)
154  {
156  (registeredPrefixId, prefixCopy, onInterest, onRegisterFailed,
157  onRegisterSuccess, flags, wireFormat, *face->getCommandKeyChain(),
158  face->getCommandCertificateName(), face);
159  }
160 
169  void
170  removeRegisteredPrefix(uint64_t registeredPrefixId);
171 
189  void
191  (uint64_t interestFilterId,
192  const ptr_lib::shared_ptr<const InterestFilter>& filterCopy,
193  const OnInterestCallback& onInterest, Face* face);
194 
202  void
203  unsetInterestFilter(uint64_t interestFilterId);
204 
212  void
213  send(const uint8_t *encoding, size_t encodingLength);
214 
227  void
228  processEvents();
229 
230  const ptr_lib::shared_ptr<Transport>&
231  getTransport() { return transport_; }
232 
233  const ptr_lib::shared_ptr<const Transport::ConnectionInfo>&
234  getConnectionInfo() { return connectionInfo_; }
235 
236  void
237  onReceivedElement(const uint8_t *element, size_t elementLength);
238 
244  bool
245  isLocal() { return transport_->isLocal(*connectionInfo_); }
246 
247  void
248  shutdown();
249 
257  static size_t
258  getMaxNdnPacketSize() { return MAX_NDN_PACKET_SIZE; }
259 
266  void
267  callLater(Milliseconds delayMilliseconds, const Face::Callback& callback);
268 
277  uint64_t
278  getNextEntryId();
279 
280 private:
281  enum ConnectStatus {
282  ConnectStatus_UNCONNECTED = 1,
283  ConnectStatus_CONNECT_REQUESTED = 2,
284  ConnectStatus_CONNECT_COMPLETE = 3
285  };
286 
290  class DelayedCall {
291  public:
298  DelayedCall(Milliseconds delayMilliseconds, const Face::Callback& callback);
299 
305  getCallTime() const { return callTime_; }
306 
311  void
312  callCallback() const { callback_(); }
313 
317  class Compare {
318  public:
319  bool
320  operator()
321  (const ptr_lib::shared_ptr<const DelayedCall>& x,
322  const ptr_lib::shared_ptr<const DelayedCall>& y) const
323  {
324  return x->callTime_ < y->callTime_;
325  }
326  };
327 
328  private:
329  const Face::Callback callback_;
330  MillisecondsSince1970 callTime_;
331  };
332 
333  class PendingInterest {
334  public:
343  PendingInterest
344  (uint64_t pendingInterestId, const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
345  const OnTimeout& onTimeout);
346 
350  uint64_t
351  getPendingInterestId() { return pendingInterestId_; }
352 
353  const ptr_lib::shared_ptr<const Interest>&
354  getInterest() { return interest_; }
355 
356  const OnData&
357  getOnData() { return onData_; }
358 
362  void
363  setIsRemoved() { isRemoved_ = true; }
364 
369  bool
370  getIsRemoved() { return isRemoved_; }
371 
375  void
376  callTimeout();
377 
378  private:
379  ptr_lib::shared_ptr<const Interest> interest_;
380  uint64_t pendingInterestId_;
381  const OnData onData_;
382  const OnTimeout onTimeout_;
383  bool isRemoved_;
384  };
385 
392  class RegisteredPrefix {
393  public:
403  RegisteredPrefix
404  (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix,
405  uint64_t relatedInterestFilterId)
406  : registeredPrefixId_(registeredPrefixId), prefix_(prefix),
407  relatedInterestFilterId_(relatedInterestFilterId)
408  {
409  }
410 
415  uint64_t
416  getRegisteredPrefixId() { return registeredPrefixId_; }
417 
422  const ptr_lib::shared_ptr<const Name>&
423  getPrefix() { return prefix_; }
424 
429  uint64_t
430  getRelatedInterestFilterId() { return relatedInterestFilterId_; }
431 
432  private:
433  uint64_t registeredPrefixId_;
434  ptr_lib::shared_ptr<const Name> prefix_;
435  uint64_t relatedInterestFilterId_;
436  };
437 
442  class InterestFilterEntry {
443  public:
453  InterestFilterEntry
454  (uint64_t interestFilterId,
455  const ptr_lib::shared_ptr<const InterestFilter>& filter,
456  const OnInterestCallback& onInterest, Face* face)
457  : interestFilterId_(interestFilterId), filter_(filter),
458  prefix_(new Name(filter->getPrefix())), onInterest_(onInterest), face_(face)
459  {
460  }
461 
466  uint64_t
467  getInterestFilterId() { return interestFilterId_; }
468 
473  const ptr_lib::shared_ptr<const InterestFilter>&
474  getFilter() { return filter_; }
475 
482  const ptr_lib::shared_ptr<const Name>&
483  getPrefix() { return prefix_; }
484 
489  const OnInterestCallback&
490  getOnInterest() { return onInterest_; }
491 
496  Face&
497  getFace() { return *face_; }
498 
499  private:
500  uint64_t interestFilterId_;
501  ptr_lib::shared_ptr<const InterestFilter> filter_;
502  ptr_lib::shared_ptr<const Name> prefix_;
503  const OnInterestCallback onInterest_;
504  Face* face_;
505  };
506 
513  class RegisterResponse {
514  public:
515  class Info;
516  RegisterResponse(ptr_lib::shared_ptr<RegisterResponse::Info> info)
517  : info_(info)
518  {
519  }
520 
526  void
527  operator()(const ptr_lib::shared_ptr<const Interest>& interest,
528  const ptr_lib::shared_ptr<Data>& responseData);
529 
534  void
535  operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest);
536 
537  class Info {
538  public:
539  Info(const ptr_lib::shared_ptr<const Name>& prefix,
540  const OnRegisterFailed& onRegisterFailed,
541  const OnRegisterSuccess& onRegisterSuccess,
542  uint64_t registeredPrefixId)
543  : prefix_(prefix), onRegisterFailed_(onRegisterFailed),
544  onRegisterSuccess_(onRegisterSuccess),
545  registeredPrefixId_(registeredPrefixId)
546  {
547  }
548 
549  ptr_lib::shared_ptr<const Name> prefix_;
550  const OnRegisterFailed onRegisterFailed_;
551  const OnRegisterSuccess onRegisterSuccess_;
552  uint64_t registeredPrefixId_;
553  };
554 
555  private:
556  ptr_lib::shared_ptr<Info> info_;
557  };
558 
576  void
577  expressInterestHelper
578  (uint64_t pendingInterestId,
579  const ptr_lib::shared_ptr<const Interest>& interestCopy,
580  const OnData& onData, const OnTimeout& onTimeout, WireFormat* wireFormat,
581  Face* face);
582 
589  void
590  processInterestTimeout(ptr_lib::shared_ptr<PendingInterest> pendingInterest);
591 
600  void
601  extractEntriesForExpressedInterest
602  (const Name& name,
603  std::vector<ptr_lib::shared_ptr<PendingInterest> > &entries);
604 
619  void
620  nfdRegisterPrefix
621  (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix,
622  const OnInterestCallback& onInterest,
623  const OnRegisterFailed& onRegisterFailed,
624  const OnRegisterSuccess& onRegisterSuccess, const ForwardingFlags& flags,
625  KeyChain& commandKeyChain, const Name& commandCertificateName,
626  WireFormat& wireFormat, Face* face);
627 
631  void
632  onConnected();
633 
634  ptr_lib::shared_ptr<Transport> transport_;
635  ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
636  std::vector<ptr_lib::shared_ptr<PendingInterest> > pendingInterestTable_;
637  std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > registeredPrefixTable_;
638  std::vector<ptr_lib::shared_ptr<InterestFilterEntry> > interestFilterTable_;
639  // Use a deque so we can efficiently remove from the front.
640  std::deque<ptr_lib::shared_ptr<DelayedCall> > delayedCallTable_;
641  DelayedCall::Compare delayedCallCompare_;
642  std::vector<Face::Callback> onConnectedCallbacks_;
643  CommandInterestGenerator commandInterestGenerator_;
644  Name timeoutPrefix_;
645  // lastEntryId_ is used to get the next unique ID. Use atomic_uint64_t to be
646  // thread safe. This is not exposed in the public API or shared with the
647  // application, so use ndnboost. */
648  ndnboost::atomic_uint64_t lastEntryId_;
649  ConnectStatus connectStatus_;
650 };
651 
652 }
653 
654 #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:44
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:111
Definition: node.hpp:537
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
void expressInterest(uint64_t pendingInterestId, const ptr_lib::shared_ptr< const Interest > &interestCopy, const OnData &onData, const OnTimeout &onTimeout, WireFormat &wireFormat, Face *face)
Send the Interest through the transport, read the entire response and call onData(interest, data).
Definition: node.cpp:52
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.cpp:163
void callLater(Milliseconds delayMilliseconds, const Face::Callback &callback)
Call callback() after the given delay.
Definition: node.cpp:456
void processEvents()
Process any packets to receive and call callbacks such as onData, onInterest or onTimeout.
Definition: node.cpp:320
The Face class provides the main methods for NDN communication.
Definition: face.hpp:78
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:70
static size_t getMaxNdnPacketSize()
Get the practical limit of the size of a network-layer packet.
Definition: node.hpp:258
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:43
void send(const uint8_t *encoding, size_t encodingLength)
Send the encoded packet out through the face.
Definition: node.cpp:189
void unsetInterestFilter(uint64_t interestFilterId)
Remove the interest filter entry which has the interestFilterId from the interest filter table...
Definition: node.cpp:172
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:340
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:38
func_lib::function< void()> Callback
Face::Callback is used internally in callLater.
Definition: face.hpp:461
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:97
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:116
void removeRegisteredPrefix(uint64_t registeredPrefixId)
Remove the registered prefix entry with the registeredPrefixId from the registered prefix table...
Definition: node.cpp:138
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:59
Definition: node.hpp:43
uint64_t getNextEntryId()
Get the next unique entry ID for the pending interest table, interest filter table, etc.
Definition: node.cpp:199
Definition: wire-format.hpp:36
An CommandInterestGenerator keeps track of a timestamp and generates command interests according to t...
Definition: command-interest-generator.hpp:35
bool isLocal()
Check if the face is local based on the current connection through the Transport; some Transport may ...
Definition: node.hpp:245
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:64
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:123
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.cpp:102
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:34
Compare shared_ptrs to DelayedCall based only on callTime_.
Definition: node.hpp:317