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 <ndn-cpp/common.hpp>
26 #include <ndn-cpp/interest.hpp>
27 #include <ndn-cpp/data.hpp>
28 #include <ndn-cpp/forwarding-flags.hpp>
29 #include <ndn-cpp/interest-filter.hpp>
30 #include <ndn-cpp/face.hpp>
31 #include "util/command-interest-generator.hpp"
32 #include "encoding/element-listener.hpp"
33 
34 struct ndn_Interest;
35 
36 namespace ndn {
37 
38 class KeyChain;
39 
40 class Node : public ElementListener {
41 public:
47  Node(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo);
48 
61  uint64_t
62  expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout, WireFormat& wireFormat);
63 
70  void
71  removePendingInterest(uint64_t pendingInterestId);
72 
83  void
85  (Interest& interest, KeyChain& keyChain, const Name& certificateName,
86  WireFormat& wireFormat)
87  {
88  commandInterestGenerator_.generate
89  (interest, keyChain, certificateName, wireFormat);
90  }
91 
113  uint64_t
115  (const Name& prefix, const OnInterestCallback& onInterest,
116  const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags,
117  WireFormat& wireFormat, KeyChain& commandKeyChain,
118  const Name& commandCertificateName, Face* face);
119 
128  void
129  removeRegisteredPrefix(uint64_t registeredPrefixId);
130 
145  uint64_t
147  (const InterestFilter& filter, const OnInterestCallback& onInterest,
148  Face* face)
149  {
150  uint64_t interestFilterId = InterestFilterEntry::getNextInterestFilterId();
151  interestFilterTable_.push_back(ptr_lib::make_shared<InterestFilterEntry>
152  (interestFilterId, ptr_lib::make_shared<const InterestFilter>(filter),
153  onInterest, face));
154 
155  return interestFilterId;
156  }
157 
165  void
166  unsetInterestFilter(uint64_t interestFilterId);
167 
176  void
177  putData(const Data& data, WireFormat& wireFormat);
178 
186  void
187  send(const uint8_t *encoding, size_t encodingLength);
188 
201  void
202  processEvents();
203 
204  const ptr_lib::shared_ptr<Transport>&
205  getTransport() { return transport_; }
206 
207  const ptr_lib::shared_ptr<const Transport::ConnectionInfo>&
208  getConnectionInfo() { return connectionInfo_; }
209 
210  void
211  onReceivedElement(const uint8_t *element, size_t elementLength);
212 
218  bool
219  isLocal() { return transport_->isLocal(*connectionInfo_); }
220 
221  void
222  shutdown();
223 
231  static size_t
232  getMaxNdnPacketSize() { return MAX_NDN_PACKET_SIZE; }
233 
234 private:
235  class PendingInterest {
236  public:
244  PendingInterest
245  (uint64_t pendingInterestId, const ptr_lib::shared_ptr<const Interest>& interest, const OnData& onData,
246  const OnTimeout& onTimeout);
247 
251  static uint64_t
252  getNextPendingInterestId()
253  {
254  return ++lastPendingInterestId_;
255  }
256 
260  uint64_t
261  getPendingInterestId() { return pendingInterestId_; }
262 
263  const ptr_lib::shared_ptr<const Interest>&
264  getInterest() { return interest_; }
265 
266  const OnData&
267  getOnData() { return onData_; }
268 
274  bool
275  isTimedOut(MillisecondsSince1970 nowMilliseconds)
276  {
277  return timeoutTimeMilliseconds_ >= 0.0 && nowMilliseconds >= timeoutTimeMilliseconds_;
278  }
279 
283  void
284  callTimeout();
285 
286  private:
287  ptr_lib::shared_ptr<const Interest> interest_;
288  static uint64_t lastPendingInterestId_;
289  uint64_t pendingInterestId_;
290  const OnData onData_;
291  const OnTimeout onTimeout_;
292  MillisecondsSince1970 timeoutTimeMilliseconds_;
293  };
294 
301  class RegisteredPrefix {
302  public:
311  RegisteredPrefix
312  (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix,
313  uint64_t relatedInterestFilterId)
314  : registeredPrefixId_(registeredPrefixId), prefix_(prefix),
315  relatedInterestFilterId_(relatedInterestFilterId)
316  {
317  }
318 
323  static uint64_t
324  getNextRegisteredPrefixId()
325  {
326  return ++lastRegisteredPrefixId_;
327  }
328 
333  uint64_t
334  getRegisteredPrefixId() { return registeredPrefixId_; }
335 
340  const ptr_lib::shared_ptr<const Name>&
341  getPrefix() { return prefix_; }
342 
347  uint64_t
348  getRelatedInterestFilterId() { return relatedInterestFilterId_; }
349 
350  private:
351  static uint64_t lastRegisteredPrefixId_;
352  uint64_t registeredPrefixId_;
353  ptr_lib::shared_ptr<const Name> prefix_;
354  uint64_t relatedInterestFilterId_;
355  };
356 
361  class InterestFilterEntry {
362  public:
372  InterestFilterEntry
373  (uint64_t interestFilterId,
374  const ptr_lib::shared_ptr<const InterestFilter>& filter,
375  const OnInterestCallback& onInterest, Face* face)
376  : interestFilterId_(interestFilterId), filter_(filter),
377  prefix_(new Name(filter->getPrefix())), onInterest_(onInterest), face_(face)
378  {
379  }
380 
387  static uint64_t
388  getNextInterestFilterId()
389  {
390  return RegisteredPrefix::getNextRegisteredPrefixId();
391  }
392 
397  uint64_t
398  getInterestFilterId() { return interestFilterId_; }
399 
404  const ptr_lib::shared_ptr<const InterestFilter>&
405  getFilter() { return filter_; }
406 
413  const ptr_lib::shared_ptr<const Name>&
414  getPrefix() { return prefix_; }
415 
420  const OnInterestCallback&
421  getOnInterest() { return onInterest_; }
422 
427  Face&
428  getFace() { return *face_; }
429 
430  private:
431  uint64_t interestFilterId_;
432  ptr_lib::shared_ptr<const InterestFilter> filter_;
433  ptr_lib::shared_ptr<const Name> prefix_;
434  const OnInterestCallback onInterest_;
435  Face* face_;
436  };
437 
442  class NdndIdFetcher {
443  public:
444  class Info;
445  NdndIdFetcher(ptr_lib::shared_ptr<NdndIdFetcher::Info> info)
446  : info_(info)
447  {
448  }
449 
455  void
456  operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData);
457 
462  void
463  operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest);
464 
465  class Info {
466  public:
478  Info(Node *node, uint64_t registeredPrefixId, const Name& prefix,
479  const OnInterestCallback& onInterest,
480  const OnRegisterFailed& onRegisterFailed, const ForwardingFlags& flags,
481  WireFormat& wireFormat, Face* face)
482  : node_(*node), registeredPrefixId_(registeredPrefixId),
483  prefix_(new Name(prefix)), onInterest_(onInterest),
484  onRegisterFailed_(onRegisterFailed), flags_(flags),
485  wireFormat_(wireFormat), face_(face)
486  {
487  }
488 
489  Node& node_;
490  uint64_t registeredPrefixId_;
491  ptr_lib::shared_ptr<const Name> prefix_;
492  const OnInterestCallback onInterest_;
493  const OnRegisterFailed onRegisterFailed_;
494  ForwardingFlags flags_;
495  WireFormat& wireFormat_;
496  Face* face_;
497  };
498 
499  private:
500  ptr_lib::shared_ptr<Info> info_;
501  };
502 
503 
510  class RegisterResponse {
511  public:
512  class Info;
513  RegisterResponse(ptr_lib::shared_ptr<RegisterResponse::Info> info)
514  : info_(info)
515  {
516  }
517 
523  void
524  operator()(const ptr_lib::shared_ptr<const Interest>& interest,
525  const ptr_lib::shared_ptr<Data>& responseData);
526 
531  void
532  operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest);
533 
534  class Info {
535  public:
536  Info(Node* node, const ptr_lib::shared_ptr<const Name>& prefix,
537  const OnInterestCallback& onInterest,
538  const OnRegisterFailed& onRegisterFailed,
539  const ForwardingFlags& flags, WireFormat& wireFormat,
540  bool isNfdCommand, Face* face)
541  : node_(*node), prefix_(prefix), onInterest_(onInterest),
542  onRegisterFailed_(onRegisterFailed), flags_(flags),
543  wireFormat_(wireFormat), isNfdCommand_(isNfdCommand), face_(face)
544  {
545  }
546 
547  Node& node_;
548  ptr_lib::shared_ptr<const Name> prefix_;
549  const OnInterestCallback onInterest_;
550  const OnRegisterFailed onRegisterFailed_;
551  ForwardingFlags flags_;
552  WireFormat& wireFormat_;
553  bool isNfdCommand_;
554  Face* face_;
555  };
556 
557  private:
558  ptr_lib::shared_ptr<Info> info_;
559  };
560 
569  void
570  extractEntriesForExpressedInterest
571  (const Name& name,
572  std::vector<ptr_lib::shared_ptr<PendingInterest> > &entries);
573 
587  void
588  registerPrefixHelper
589  (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix,
590  const OnInterestCallback& onInterest, const OnRegisterFailed& onRegisterFailed,
591  const ForwardingFlags& flags, WireFormat& wireFormat, Face* face);
592 
607  void
608  nfdRegisterPrefix
609  (uint64_t registeredPrefixId, const ptr_lib::shared_ptr<const Name>& prefix,
610  const OnInterestCallback& onInterest, const OnRegisterFailed& onRegisterFailed,
611  const ForwardingFlags& flags, KeyChain& commandKeyChain,
612  const Name& commandCertificateName, WireFormat& wireFormat, Face* face);
613 
614  ptr_lib::shared_ptr<Transport> transport_;
615  ptr_lib::shared_ptr<const Transport::ConnectionInfo> connectionInfo_;
616  std::vector<ptr_lib::shared_ptr<PendingInterest> > pendingInterestTable_;
617  std::vector<ptr_lib::shared_ptr<RegisteredPrefix> > registeredPrefixTable_;
618  std::vector<ptr_lib::shared_ptr<InterestFilterEntry> > interestFilterTable_;
619  Interest ndndIdFetcherInterest_;
620  Blob ndndId_;
621  CommandInterestGenerator commandInterestGenerator_;
622  Name timeoutPrefix_;
623 };
624 
625 }
626 
627 #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
Definition: node.hpp:534
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
void processEvents()
Process any packets to receive and call callbacks such as onData, onInterest or onTimeout.
Definition: node.cpp:592
Definition: data.hpp:36
The Face class provides the main methods for NDN communication.
Definition: face.hpp:72
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:34
uint64_t registerPrefix(const Name &prefix, const OnInterestCallback &onInterest, const OnRegisterFailed &onRegisterFailed, 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:241
static size_t getMaxNdnPacketSize()
Get the practical limit of the size of a network-layer packet.
Definition: node.hpp:232
An ndn_Interest holds an ndn_Name and other fields for an interest.
Definition: interest-types.h:70
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:192
void send(const uint8_t *encoding, size_t encodingLength)
Send the encoded packet out through the face.
Definition: node.cpp:334
void unsetInterestFilter(uint64_t interestFilterId)
Remove the interest filter entry which has the interestFilterId from the interest filter table...
Definition: node.cpp:306
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 binary XML element is received.
Definition: node.cpp:612
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:42
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
uint64_t expressInterest(const Interest &interest, const OnData &onData, const OnTimeout &onTimeout, WireFormat &wireFormat)
Send the Interest through the transport, read the entire response and call onData(interest, data).
Definition: node.cpp:200
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:85
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:282
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:40
void putData(const Data &data, WireFormat &wireFormat)
The OnInterestCallback calls this to put a Data packet which satisfies an Interest.
Definition: node.cpp:323
Definition: wire-format.hpp:37
An CommandInterestGenerator keeps track of a timestamp and generates command interests according to t...
Definition: command-interest-generator.hpp:35
Info(Node *node, uint64_t registeredPrefixId, const Name &prefix, const OnInterestCallback &onInterest, const OnRegisterFailed &onRegisterFailed, const ForwardingFlags &flags, WireFormat &wireFormat, Face *face)
Definition: node.hpp:478
bool isLocal()
Check if the face is local based on the current connection through the Transport; some Transport may ...
Definition: node.hpp:219
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
An InterestFilter holds a Name prefix and optional regex match expression for use in Face::setInteres...
Definition: interest-filter.hpp:33
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
Definition: node.hpp:465
void removePendingInterest(uint64_t pendingInterestId)
Remove the pending interest entry with the pendingInterestId from the pending interest table...
Definition: node.cpp:223
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
uint64_t setInterestFilter(const InterestFilter &filter, 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:147