All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
memory-content-cache.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_MEMORY_CONTENT_CACHE_HPP
23 #define NDN_MEMORY_CONTENT_CACHE_HPP
24 
25 #include <map>
26 #include <deque>
27 #include "../face.hpp"
28 
29 namespace ndn {
30 
39 public:
50  (Face* face, Milliseconds cleanupIntervalMilliseconds = 1000.0)
51  : impl_(new Impl(face, cleanupIntervalMilliseconds))
52  {
53  impl_->initialize();
54  }
55 
62  public:
72  (const ptr_lib::shared_ptr<const Interest>& interest, Face& face);
73 
78  const ptr_lib::shared_ptr<const Interest>&
79  getInterest() const { return interest_; }
80 
89  getTimeoutPeriodStart() const { return timeoutPeriodStart_; }
90 
94  Face&
95  getFace() const { return face_; }
96 
103  bool
104  isTimedOut(MillisecondsSince1970 nowMilliseconds) const
105  {
106  return nowMilliseconds >= timeoutTimeMilliseconds_;
107  }
108 
109  private:
110  ptr_lib::shared_ptr<const Interest> interest_;
111  Face& face_;
112  MillisecondsSince1970 timeoutPeriodStart_;
113  MillisecondsSince1970 timeoutTimeMilliseconds_;
116  };
117 
122  class Content {
123  public:
128  Content(const Data& data)
129  // wireEncode returns the cached encoding if available.
130  : name_(data.getName()), dataEncoding_(data.wireEncode())
131  {}
132 
137  const Name&
138  getName() const { return name_; }
139 
144  const Blob&
145  getDataEncoding() const { return dataEncoding_; }
146 
147  private:
148  Name name_;
149  Blob dataEncoding_;
150  };
151 
152  typedef std::vector<ptr_lib::shared_ptr<const Content> > ContentList;
153 
158  typedef func_lib::function<void
159  (const ptr_lib::shared_ptr<ContentList>& contentList)> OnContentRemoved;
160 
190  void
192  (const Name& prefix, const OnRegisterFailed& onRegisterFailed,
193  const OnInterestCallback& onDataNotFound = OnInterestCallback(),
194  const ForwardingFlags& flags = ForwardingFlags(),
196  {
198  (prefix, onRegisterFailed, OnRegisterSuccess(), onDataNotFound, flags,
199  wireFormat);
200  }
201 
230  void
232  (const Name& prefix, const OnRegisterFailed& onRegisterFailed,
233  const OnRegisterSuccess& onRegisterSuccess,
234  const OnInterestCallback& onDataNotFound = OnInterestCallback(),
235  const ForwardingFlags& flags = ForwardingFlags(),
237  {
238  impl_->registerPrefix
239  (prefix, onRegisterFailed, onRegisterSuccess, onDataNotFound, flags,
240  wireFormat);
241  }
242 
265  void
267  (const InterestFilter& filter,
268  const OnInterestCallback& onDataNotFound = OnInterestCallback())
269  {
270  impl_->setInterestFilter(filter, onDataNotFound);
271  }
272 
293  void
295  (const Name& prefix,
296  const OnInterestCallback& onDataNotFound = OnInterestCallback())
297  {
298  impl_->setInterestFilter(prefix, onDataNotFound);
299  }
300 
308  void
309  unregisterAll() { impl_->unregisterAll(); }
310 
328  void
329  add(const Data& data) { impl_->add(data); }
330 
343  void
345  (const ptr_lib::shared_ptr<const Interest>& interest, Face& face)
346  {
347  impl_->storePendingInterest(interest, face);
348  }
349 
357  const OnInterestCallback&
358  getStorePendingInterest() { return impl_->getStorePendingInterest(); }
359 
372  void
374  (const Name& name,
375  std::vector<ptr_lib::shared_ptr<const PendingInterest> >& pendingInterests)
376  {
377  impl_->getPendingInterestsForName(name, pendingInterests);
378  }
379 
393  void
395  (const Name& prefix,
396  std::vector<ptr_lib::shared_ptr<const PendingInterest> >& pendingInterests)
397  {
398  impl_->getPendingInterestsWithPrefix(prefix, pendingInterests);
399  }
400 
411  void
412  setOnContentRemoved(const OnContentRemoved& onContentRemoved)
413  {
414  impl_->setOnContentRemoved(onContentRemoved);
415  }
416 
422  getMinimumCacheLifetime() { return impl_->getMinimumCacheLifetime(); }
423 
432  void
433  setMinimumCacheLifetime(Milliseconds minimumCacheLifetime)
434  {
435  impl_->setMinimumCacheLifetime(minimumCacheLifetime);
436  }
437 
438 private:
444  class Impl : public ptr_lib::enable_shared_from_this<Impl> {
445  public:
451  Impl(Face* face, Milliseconds cleanupIntervalMilliseconds);
452 
457  void
458  initialize();
459 
460  void
462  (const Name& prefix, const OnRegisterFailed& onRegisterFailed,
463  const OnRegisterSuccess& onRegisterSuccess,
464  const OnInterestCallback& onDataNotFound,
465  const ForwardingFlags& flags, WireFormat& wireFormat);
466 
467  void
469  (const InterestFilter& filter, const OnInterestCallback& onDataNotFound);
470 
471  void
473  (const Name& prefix, const OnInterestCallback& onDataNotFound);
474 
475  void
476  unregisterAll();
477 
478  void
479  add(const Data& data);
480 
481  void
483  (const ptr_lib::shared_ptr<const Interest>& interest, Face& face);
484 
485  const OnInterestCallback&
486  getStorePendingInterest() { return storePendingInterestCallback_; }
487 
488  void
490  (const Name& name,
491  std::vector<ptr_lib::shared_ptr<const PendingInterest> >& pendingInterests);
492 
493  void
495  (const Name& prefix,
496  std::vector<ptr_lib::shared_ptr<const PendingInterest> >& pendingInterests);
497 
498  void
499  setOnContentRemoved(const OnContentRemoved& onContentRemoved)
500  {
501  onContentRemoved_ = onContentRemoved;
502  }
503 
505  getMinimumCacheLifetime() { return minimumCacheLifetime_; }
506 
507  void
508  setMinimumCacheLifetime(Milliseconds minimumCacheLifetime)
509  {
510  minimumCacheLifetime_ = minimumCacheLifetime;
511  }
512 
522  void
523  onInterest
524  (const ptr_lib::shared_ptr<const Name>& prefix,
525  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
526  uint64_t interestFilterId,
527  const ptr_lib::shared_ptr<const InterestFilter>& filter);
528 
529  private:
534  class StaleTimeContent : public Content {
535  public:
546  StaleTimeContent
547  (const Data& data, MillisecondsSince1970 nowMilliseconds,
548  Milliseconds minimumCacheLifetime);
549 
557  bool
558  isPastRemovalTime(MillisecondsSince1970 nowMilliseconds) const
559  {
560  return cacheRemovalTimeMilliseconds_ <= nowMilliseconds;
561  }
562 
570  bool
571  isFresh(MillisecondsSince1970 nowMilliseconds) const
572  {
573  return freshnessExpiryTimeMilliseconds_ > nowMilliseconds;
574  }
575 
579  class Compare {
580  public:
581  bool
582  operator()
583  (const ptr_lib::shared_ptr<const StaleTimeContent>& x,
584  const ptr_lib::shared_ptr<const StaleTimeContent>& y) const
585  {
586  return x->cacheRemovalTimeMilliseconds_ < y->cacheRemovalTimeMilliseconds_;
587  }
588  };
589 
590  private:
591  MillisecondsSince1970 cacheRemovalTimeMilliseconds_;
594  MillisecondsSince1970 freshnessExpiryTimeMilliseconds_;
597  };
598 
609  void
610  doCleanup(MillisecondsSince1970 nowMilliseconds);
611 
617  void
618  storePendingInterestCallback
619  (const ptr_lib::shared_ptr<const Name>& prefix,
620  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
621  uint64_t interestFilterId,
622  const ptr_lib::shared_ptr<const InterestFilter>& filter)
623  {
624  storePendingInterest(interest, face);
625  }
626 
627  Face* face_;
628  Milliseconds cleanupIntervalMilliseconds_;
629  MillisecondsSince1970 nextCleanupTime_;
630  std::map<std::string, OnInterestCallback> onDataNotFoundForPrefix_;
631  std::vector<uint64_t> interestFilterIdList_;
632  std::vector<uint64_t> registeredPrefixIdList_;
633  std::vector<ptr_lib::shared_ptr<const Content> > noStaleTimeCache_;
634  // Use a deque so we can efficiently remove from the front.
635  std::deque<ptr_lib::shared_ptr<const StaleTimeContent> > staleTimeCache_;
636  StaleTimeContent::Compare contentCompare_;
637  Name::Component emptyComponent_;
638  std::vector<ptr_lib::shared_ptr<const PendingInterest> > pendingInterestTable_;
639  OnInterestCallback storePendingInterestCallback_;
640  OnContentRemoved onContentRemoved_;
641  bool isDoingCleanup_;
642  Milliseconds minimumCacheLifetime_;
643  };
644 
645  ndn::ptr_lib::shared_ptr<Impl> impl_;
646 };
647 
648 }
649 
650 #endif
void getPendingInterestsWithPrefix(const Name &prefix, std::vector< ptr_lib::shared_ptr< const PendingInterest > > &pendingInterests)
Remove timed-out pending interests, then for each pending interest which matches according to prefix...
Definition: memory-content-cache.hpp:395
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:114
PendingInterest(const ptr_lib::shared_ptr< const Interest > &interest, Face &face)
Create a new PendingInterest and set the timeoutTime_ based on the current time and the interest life...
Definition: memory-content-cache.cpp:344
A MemoryContentCache holds a set of Data packets and answers an Interest to return the correct Data p...
Definition: memory-content-cache.hpp:38
void setInterestFilter(const InterestFilter &filter, const OnInterestCallback &onDataNotFound=OnInterestCallback())
Call setInterestFilter on the Face given to the constructor so that this MemoryContentCache will answ...
Definition: memory-content-cache.hpp:267
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &prefix, const ptr_lib::shared_ptr< const Interest > &interest, Face &face, uint64_t interestFilterId, const ptr_lib::shared_ptr< const InterestFilter > &filter)> OnInterestCallback
An OnInterestCallback function object is used to pass a callback to setInterestFilter and optionally ...
Definition: face.hpp:65
void setOnContentRemoved(const OnContentRemoved &onContentRemoved)
Set the OnContentRemoved callback to call when stale content is removed from the cache during cleanup...
Definition: memory-content-cache.hpp:412
Definition: data.hpp:37
void getPendingInterestsForName(const Name &name, std::vector< ptr_lib::shared_ptr< const PendingInterest > > &pendingInterests)
Remove timed-out pending interests, then for each pending interest which matches according to Interes...
Definition: memory-content-cache.hpp:374
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
void storePendingInterest(const ptr_lib::shared_ptr< const Interest > &interest, Face &face)
Store an interest from an OnInterest callback in the internal pending interest table (normally becaus...
Definition: memory-content-cache.hpp:345
Face & getFace() const
Return the face given to the constructor.
Definition: memory-content-cache.hpp:95
Compare shared_ptrs to Content based only on cacheRemovalTimeMilliseconds_.
Definition: memory-content-cache.hpp:579
A ForwardingFlags object holds the flags which specify how the forwarding daemon should forward an in...
Definition: forwarding-flags.hpp:35
bool isTimedOut(MillisecondsSince1970 nowMilliseconds) const
Check if this interest is timed out.
Definition: memory-content-cache.hpp:104
const ptr_lib::shared_ptr< const Interest > & getInterest() const
Return the interest given to the constructor.
Definition: memory-content-cache.hpp:79
const Name & getName() const
Get the content Name.
Definition: memory-content-cache.hpp:138
A PendingInterest holds an interest which onInterest received but could not satisfy.
Definition: memory-content-cache.hpp:61
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &prefix, uint64_t registeredPrefixId)> OnRegisterSuccess
An OnRegisterSuccess function object is used to report when registerPrefix succeeds.
Definition: face.hpp:78
const Blob & getDataEncoding() const
Get the encoded content Data packet.
Definition: memory-content-cache.hpp:145
A Name::Component holds a read-only name component value.
Definition: name.hpp:45
void unregisterAll()
Call Face.unsetInterestFilter and Face.removeRegisteredPrefix for all the prefixes given to the setIn...
Definition: memory-content-cache.hpp:309
MemoryContentCache(Face *face, Milliseconds cleanupIntervalMilliseconds=1000.0)
Create a new MemoryContentCache to use the given Face.
Definition: memory-content-cache.hpp:50
Milliseconds getMinimumCacheLifetime()
Get the minimum lifetime before removing stale content from the cache.
Definition: memory-content-cache.hpp:422
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<uint8_t>.
Definition: blob.hpp:42
void registerPrefix(const Name &prefix, const OnRegisterFailed &onRegisterFailed, const OnInterestCallback &onDataNotFound=OnInterestCallback(), const ForwardingFlags &flags=ForwardingFlags(), WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Call registerPrefix on the Face given to the constructor so that this MemoryContentCache will answer ...
Definition: memory-content-cache.hpp:192
const OnInterestCallback & getStorePendingInterest()
Return a callback to use for onDataNotFound in registerPrefix which simply calls storePendingInterest...
Definition: memory-content-cache.hpp:358
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:119
void add(const Data &data)
Add the Data packet to the cache so that it is available to use to answer interests.
Definition: memory-content-cache.hpp:329
func_lib::function< void(const ptr_lib::shared_ptr< const Name > &prefix)> OnRegisterFailed
An OnRegisterFailed function object is used to report when registerPrefix fails.
Definition: face.hpp:71
Content(const Data &data)
Create a new Content entry to hold data's name and wire encoding.
Definition: memory-content-cache.hpp:128
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:34
Definition: wire-format.hpp:39
An InterestFilter holds a Name prefix and optional regex match expression for use in Face::setInteres...
Definition: interest-filter.hpp:33
MillisecondsSince1970 getTimeoutPeriodStart() const
Return the time when this pending interest entry was created (the time when the unsatisfied interest ...
Definition: memory-content-cache.hpp:89
Content is a private class to hold the name and encoding for each entry in the cache.
Definition: memory-content-cache.hpp:122
func_lib::function< void(const ptr_lib::shared_ptr< ContentList > &contentList)> OnContentRemoved
An OnContentRemoved function object is called when content is removed from the cache.
Definition: memory-content-cache.hpp:159
void setMinimumCacheLifetime(Milliseconds minimumCacheLifetime)
Set the minimum lifetime before removing stale content from the cache which can keep content in the c...
Definition: memory-content-cache.hpp:433