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 
58  public:
68  (const ptr_lib::shared_ptr<const Interest>& interest, Face& face);
69 
74  const ptr_lib::shared_ptr<const Interest>&
75  getInterest() const { return interest_; }
76 
85  getTimeoutPeriodStart() const { return timeoutPeriodStart_; }
86 
90  Face&
91  getFace() const { return face_; }
92 
99  bool
100  isTimedOut(MillisecondsSince1970 nowMilliseconds) const
101  {
102  return timeoutTimeMilliseconds_ >= 0.0 &&
103  nowMilliseconds >= timeoutTimeMilliseconds_;
104  }
105 
106  private:
107  ptr_lib::shared_ptr<const Interest> interest_;
108  Face& face_;
109  MillisecondsSince1970 timeoutPeriodStart_;
110  MillisecondsSince1970 timeoutTimeMilliseconds_;
113  };
114 
144  void
146  (const Name& prefix, const OnRegisterFailed& onRegisterFailed,
147  const OnInterestCallback& onDataNotFound = OnInterestCallback(),
148  const ForwardingFlags& flags = ForwardingFlags(),
150  {
152  (prefix, onRegisterFailed, OnRegisterSuccess(), onDataNotFound, flags,
153  wireFormat);
154  }
155 
184  void
186  (const Name& prefix, const OnRegisterFailed& onRegisterFailed,
187  const OnRegisterSuccess& onRegisterSuccess,
188  const OnInterestCallback& onDataNotFound = OnInterestCallback(),
189  const ForwardingFlags& flags = ForwardingFlags(),
191  {
192  onDataNotFoundForPrefix_[prefix.toUri()] = onDataNotFound;
193  uint64_t registeredPrefixId = face_->registerPrefix
194  (prefix, func_lib::ref(*this), onRegisterFailed, onRegisterSuccess, flags,
195  wireFormat);
196  // Remember the registeredPrefixId so unregisterAll can remove it.
197  registeredPrefixIdList_.push_back(registeredPrefixId);
198  }
199 
222  void
224  (const InterestFilter& filter,
225  const OnInterestCallback& onDataNotFound = OnInterestCallback())
226  {
227  onDataNotFoundForPrefix_[filter.getPrefix().toUri()] = onDataNotFound;
228  uint64_t interestFilterId = face_->setInterestFilter
229  (filter, func_lib::ref(*this));
230  // Remember the interestFilterId so unregisterAll can remove it.
231  interestFilterIdList_.push_back(interestFilterId);
232  }
233 
254  void
256  (const Name& prefix,
257  const OnInterestCallback& onDataNotFound = OnInterestCallback())
258  {
259  onDataNotFoundForPrefix_[prefix.toUri()] = onDataNotFound;
260  uint64_t interestFilterId = face_->setInterestFilter
261  (prefix, func_lib::ref(*this));
262  // Remember the interestFilterId so unregisterAll can remove it.
263  interestFilterIdList_.push_back(interestFilterId);
264  }
265 
273  void
274  unregisterAll();
275 
292  void
293  add(const Data& data);
294 
307  void
309  (const ptr_lib::shared_ptr<const Interest>& interest, Face& face);
310 
318  const OnInterestCallback&
320  {
321  return storePendingInterestCallback_;
322  }
323 
335  void
337  (const Name& name,
338  std::vector<ptr_lib::shared_ptr<const PendingInterest> >& pendingInterests);
339 
349  void
350  operator()
351  (const ptr_lib::shared_ptr<const Name>& prefix,
352  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
353  uint64_t interestFilterId,
354  const ptr_lib::shared_ptr<const InterestFilter>& filter);
355 
356 private:
362  class Content {
363  public:
368  Content(const Data& data)
369  // wireEncode returns the cached encoding if available.
370  : name_(data.getName()), dataEncoding_(data.wireEncode())
371  {}
372 
373  const Name&
374  getName() const { return name_; }
375 
376  const Blob&
377  getDataEncoding() const { return dataEncoding_; }
378 
379  private:
380  Name name_;
381  Blob dataEncoding_;
382  };
383 
388  class StaleTimeContent : public Content {
389  public:
396  StaleTimeContent(const Data& data);
397 
404  bool
405  isStale(MillisecondsSince1970 nowMilliseconds) const
406  {
407  return staleTimeMilliseconds_ <= nowMilliseconds;
408  }
409 
413  class Compare {
414  public:
415  bool
416  operator()
417  (const ptr_lib::shared_ptr<const StaleTimeContent>& x,
418  const ptr_lib::shared_ptr<const StaleTimeContent>& y) const
419  {
420  return x->staleTimeMilliseconds_ < y->staleTimeMilliseconds_;
421  }
422  };
423 
424  private:
425  MillisecondsSince1970 staleTimeMilliseconds_;
427  };
428 
436  void
437  doCleanup();
438 
444  void
445  storePendingInterestCallback
446  (const ptr_lib::shared_ptr<const Name>& prefix,
447  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
448  uint64_t interestFilterId,
449  const ptr_lib::shared_ptr<const InterestFilter>& filter)
450  {
451  storePendingInterest(interest, face);
452  }
453 
454  Face* face_;
455  Milliseconds cleanupIntervalMilliseconds_;
456  MillisecondsSince1970 nextCleanupTime_;
457  std::map<std::string, OnInterestCallback> onDataNotFoundForPrefix_;
458  std::vector<uint64_t> interestFilterIdList_;
459  std::vector<uint64_t> registeredPrefixIdList_;
460  std::vector<ptr_lib::shared_ptr<const Content> > noStaleTimeCache_;
461  // Use a deque so we can efficiently remove from the front.
462  std::deque<ptr_lib::shared_ptr<const StaleTimeContent> > staleTimeCache_;
463  StaleTimeContent::Compare contentCompare_;
464  Name::Component emptyComponent_;
465  std::vector<ptr_lib::shared_ptr<const PendingInterest> > pendingInterestTable_;
466  OnInterestCallback storePendingInterestCallback_;
467 };
468 
469 }
470 
471 #endif
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:112
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:237
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
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:224
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
Definition: data.hpp:37
std::string toUri(bool includeScheme=false) const
Encode this name as a URI.
Definition: name.cpp:397
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.cpp:114
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.cpp:106
Face & getFace() const
Return the face given to the constructor.
Definition: memory-content-cache.hpp:91
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:100
const Name & getPrefix() const
Get the prefix given to the constructor.
Definition: interest-filter.hpp:154
const ptr_lib::shared_ptr< const Interest > & getInterest() const
Return the interest given to the constructor.
Definition: memory-content-cache.hpp:75
A PendingInterest holds an interest which onInterest received but could not satisfy.
Definition: memory-content-cache.hpp:57
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
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.cpp:46
MemoryContentCache(Face *face, Milliseconds cleanupIntervalMilliseconds=1000.0)
Create a new MemoryContentCache to use the given Face.
Definition: memory-content-cache.cpp:35
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
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:146
const OnInterestCallback & getStorePendingInterest()
Return a callback to use for onDataNotFound in registerPrefix which simply calls storePendingInterest...
Definition: memory-content-cache.hpp:319
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:117
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.cpp:61
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:169
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
Compare shared_ptrs to Content based only on staleTimeMilliseconds_.
Definition: memory-content-cache.hpp:413
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:85
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