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  // TODO: After we remove the registerPrefix with the deprecated OnInterest,
194  // we can remove the explicit cast to OnInterestCallback (needed for boost).
195  uint64_t registeredPrefixId = face_->registerPrefix
196  (prefix, (const OnInterestCallback&)func_lib::ref(*this), onRegisterFailed,
197  onRegisterSuccess, flags, wireFormat);
198  // Remember the registeredPrefixId so unregisterAll can remove it.
199  registeredPrefixIdList_.push_back(registeredPrefixId);
200  }
201 
224  void
226  (const InterestFilter& filter,
227  const OnInterestCallback& onDataNotFound = OnInterestCallback())
228  {
229  onDataNotFoundForPrefix_[filter.getPrefix().toUri()] = onDataNotFound;
230  uint64_t interestFilterId = face_->setInterestFilter
231  (filter, func_lib::ref(*this));
232  // Remember the interestFilterId so unregisterAll can remove it.
233  interestFilterIdList_.push_back(interestFilterId);
234  }
235 
256  void
258  (const Name& prefix,
259  const OnInterestCallback& onDataNotFound = OnInterestCallback())
260  {
261  onDataNotFoundForPrefix_[prefix.toUri()] = onDataNotFound;
262  uint64_t interestFilterId = face_->setInterestFilter
263  (prefix, func_lib::ref(*this));
264  // Remember the interestFilterId so unregisterAll can remove it.
265  interestFilterIdList_.push_back(interestFilterId);
266  }
267 
275  void
276  unregisterAll();
277 
294  void
295  add(const Data& data);
296 
309  void
311  (const ptr_lib::shared_ptr<const Interest>& interest, Face& face);
312 
320  const OnInterestCallback&
322  {
323  return storePendingInterestCallback_;
324  }
325 
337  void
339  (const Name& name,
340  std::vector<ptr_lib::shared_ptr<const PendingInterest> >& pendingInterests);
341 
351  void
352  operator()
353  (const ptr_lib::shared_ptr<const Name>& prefix,
354  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
355  uint64_t interestFilterId,
356  const ptr_lib::shared_ptr<const InterestFilter>& filter);
357 
358 private:
364  class Content {
365  public:
370  Content(const Data& data)
371  // wireEncode returns the cached encoding if available.
372  : name_(data.getName()), dataEncoding_(data.wireEncode())
373  {}
374 
375  const Name&
376  getName() const { return name_; }
377 
378  const Blob&
379  getDataEncoding() const { return dataEncoding_; }
380 
381  private:
382  Name name_;
383  Blob dataEncoding_;
384  };
385 
390  class StaleTimeContent : public Content {
391  public:
398  StaleTimeContent(const Data& data);
399 
406  bool
407  isStale(MillisecondsSince1970 nowMilliseconds) const
408  {
409  return staleTimeMilliseconds_ <= nowMilliseconds;
410  }
411 
415  class Compare {
416  public:
417  bool
418  operator()
419  (const ptr_lib::shared_ptr<const StaleTimeContent>& x,
420  const ptr_lib::shared_ptr<const StaleTimeContent>& y) const
421  {
422  return x->staleTimeMilliseconds_ < y->staleTimeMilliseconds_;
423  }
424  };
425 
426  private:
427  MillisecondsSince1970 staleTimeMilliseconds_;
429  };
430 
438  void
439  doCleanup();
440 
446  void
447  storePendingInterestCallback
448  (const ptr_lib::shared_ptr<const Name>& prefix,
449  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
450  uint64_t interestFilterId,
451  const ptr_lib::shared_ptr<const InterestFilter>& filter)
452  {
453  storePendingInterest(interest, face);
454  }
455 
456  Face* face_;
457  Milliseconds cleanupIntervalMilliseconds_;
458  MillisecondsSince1970 nextCleanupTime_;
459  std::map<std::string, OnInterestCallback> onDataNotFoundForPrefix_;
460  std::vector<uint64_t> interestFilterIdList_;
461  std::vector<uint64_t> registeredPrefixIdList_;
462  std::vector<ptr_lib::shared_ptr<const Content> > noStaleTimeCache_;
463  // Use a deque so we can efficiently remove from the front.
464  std::deque<ptr_lib::shared_ptr<const StaleTimeContent> > staleTimeCache_;
465  StaleTimeContent::Compare contentCompare_;
466  Name::Component emptyComponent_;
467  std::vector<ptr_lib::shared_ptr<const PendingInterest> > pendingInterestTable_;
468  OnInterestCallback storePendingInterestCallback_;
469 };
470 
471 }
472 
473 #endif
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:111
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:35
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:226
Definition: data.hpp:37
std::string toUri(bool includeScheme=false) const
Encode this name as a URI.
Definition: name.cpp:305
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
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
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
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:321
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:116
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:196
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
Compare shared_ptrs to Content based only on staleTimeMilliseconds_.
Definition: memory-content-cache.hpp:415
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
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
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