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 
84  getTimeoutPeriodStart() const { return timeoutPeriodStart_; }
85 
89  Face&
90  getFace() const { return face_; }
91 
98  bool
99  isTimedOut(MillisecondsSince1970 nowMilliseconds) const
100  {
101  return timeoutTimeMilliseconds_ >= 0.0 &&
102  nowMilliseconds >= timeoutTimeMilliseconds_;
103  }
104 
105  private:
106  ptr_lib::shared_ptr<const Interest> interest_;
107  Face& face_;
108  MillisecondsSince1970 timeoutPeriodStart_;
109  MillisecondsSince1970 timeoutTimeMilliseconds_;
112  };
113 
135  void
137  (const Name& prefix, const OnRegisterFailed& onRegisterFailed,
138  const OnInterestCallback& onDataNotFound = OnInterestCallback(),
139  const ForwardingFlags& flags = ForwardingFlags(),
141  {
142  onDataNotFoundForPrefix_[prefix.toUri()] = onDataNotFound;
143  // TODO: After we remove the registerPrefix with the deprecated OnInterest,
144  // we can remove the explicit cast to OnInterestCallback (needed for boost).
145  uint64_t registeredPrefixId = face_->registerPrefix
146  (prefix, (const OnInterestCallback&)func_lib::ref(*this), onRegisterFailed, flags, wireFormat);
147  // Remember the registeredPrefixId so unregisterAll can remove it.
148  registeredPrefixIdList_.push_back(registeredPrefixId);
149  }
150 
157  void
158  unregisterAll();
159 
176  void
177  add(const Data& data);
178 
191  void
193  (const ptr_lib::shared_ptr<const Interest>& interest, Face& face);
194 
202  const OnInterestCallback&
204  {
205  return storePendingInterestCallback_;
206  }
207 
219  void
221  (const Name& name,
222  std::vector<ptr_lib::shared_ptr<const PendingInterest> >& pendingInterests);
223 
233  void
234  operator()
235  (const ptr_lib::shared_ptr<const Name>& prefix,
236  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
237  uint64_t interestFilterId,
238  const ptr_lib::shared_ptr<const InterestFilter>& filter);
239 
240 private:
246  class Content {
247  public:
252  Content(const Data& data)
253  // wireEncode returns the cached encoding if available.
254  : name_(data.getName()), dataEncoding_(data.wireEncode())
255  {}
256 
257  const Name&
258  getName() const { return name_; }
259 
260  const Blob&
261  getDataEncoding() const { return dataEncoding_; }
262 
263  private:
264  Name name_;
265  Blob dataEncoding_;
266  };
267 
272  class StaleTimeContent : public Content {
273  public:
280  StaleTimeContent(const Data& data);
281 
288  bool
289  isStale(MillisecondsSince1970 nowMilliseconds) const
290  {
291  return staleTimeMilliseconds_ <= nowMilliseconds;
292  }
293 
297  class Compare {
298  public:
299  bool
300  operator()
301  (const ptr_lib::shared_ptr<const StaleTimeContent>& x,
302  const ptr_lib::shared_ptr<const StaleTimeContent>& y) const
303  {
304  return x->staleTimeMilliseconds_ < y->staleTimeMilliseconds_;
305  }
306  };
307 
308  private:
309  MillisecondsSince1970 staleTimeMilliseconds_;
311  };
312 
320  void
321  doCleanup();
322 
328  void
329  storePendingInterestCallback
330  (const ptr_lib::shared_ptr<const Name>& prefix,
331  const ptr_lib::shared_ptr<const Interest>& interest, Face& face,
332  uint64_t interestFilterId,
333  const ptr_lib::shared_ptr<const InterestFilter>& filter)
334  {
335  storePendingInterest(interest, face);
336  }
337 
338  Face* face_;
339  Milliseconds cleanupIntervalMilliseconds_;
340  MillisecondsSince1970 nextCleanupTime_;
341  std::map<std::string, OnInterestCallback> onDataNotFoundForPrefix_;
342  std::vector<uint64_t> registeredPrefixIdList_;
343  std::vector<ptr_lib::shared_ptr<const Content> > noStaleTimeCache_;
344  // Use a deque so we can efficiently remove from the front.
345  std::deque<ptr_lib::shared_ptr<const StaleTimeContent> > staleTimeCache_;
346  StaleTimeContent::Compare contentCompare_;
347  Name::Component emptyComponent_;
348  std::vector<ptr_lib::shared_ptr<const PendingInterest> > pendingInterestTable_;
349  OnInterestCallback storePendingInterestCallback_;
350 };
351 
352 }
353 
354 #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:224
Copyright (C) 2013-2015 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
Definition: data.hpp:36
std::string toUri(bool includeScheme=false) const
Encode this name as a URI.
Definition: name.cpp:324
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:108
The Face class provides the main methods for NDN communication.
Definition: face.hpp:72
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:100
Face & getFace() const
Return the face given to the constructor.
Definition: memory-content-cache.hpp:90
A ForwardingFlags object holds the flags which specify how the forwarding daemon should forward an in...
Definition: forwarding-flags.hpp:34
bool isTimedOut(MillisecondsSince1970 nowMilliseconds) const
Check if this interest is timed out.
Definition: memory-content-cache.hpp:99
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:47
void unregisterAll()
Call Face.removeRegisteredPrefix for all the prefixes given to the registerPrefix method on this Memo...
Definition: memory-content-cache.cpp:44
MemoryContentCache(Face *face, Milliseconds cleanupIntervalMilliseconds=1000.0)
Create a new MemoryContentCache to use the given Face.
Definition: memory-content-cache.cpp:33
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.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:137
const OnInterestCallback & getStorePendingInterest()
Return a callback to use for onDataNotFound in registerPrefix which simply calls storePendingInterest...
Definition: memory-content-cache.hpp:203
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:55
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
uint64_t registerPrefix(const Name &prefix, const OnInterestCallback &onInterest, const OnRegisterFailed &onRegisterFailed, 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:132
Compare shared_ptrs to Content based only on staleTimeMilliseconds_.
Definition: memory-content-cache.hpp:297
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:36
Definition: wire-format.hpp:37
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
MillisecondsSince1970 getTimeoutPeriodStart() const
Return the time when this pending interest entry was created (the time when the unsatisfied interest ...
Definition: memory-content-cache.hpp:84