NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
content-store-with-freshness.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #ifndef NDN_CONTENT_STORE_WITH_FRESHNESS_H_
21 #define NDN_CONTENT_STORE_WITH_FRESHNESS_H_
22 
23 #include "ns3/ndnSIM/model/ndn-common.hpp"
24 
25 #include "content-store-impl.hpp"
26 
27 #include "../../utils/trie/multi-policy.hpp"
29 
30 namespace ns3 {
31 namespace ndn {
32 namespace cs {
33 
38 template<class Policy>
40  : public ContentStoreImpl<ndnSIM::
41  multi_policy_traits<boost::mpl::
42  vector2<Policy,
43  ndnSIM::freshness_policy_traits>>> {
44 public:
45  typedef ContentStoreImpl<ndnSIM::multi_policy_traits<boost::mpl::
46  vector2<Policy,
47  ndnSIM::freshness_policy_traits>>>
49 
50  typedef typename super::policy_container::template index<1>::type freshness_policy_container;
51 
52  static TypeId
53  GetTypeId();
54 
55  virtual inline void
56  Print(std::ostream& os) const;
57 
58  virtual inline bool
59  Add(shared_ptr<const Data> data);
60 
61 private:
62  inline void
63  CleanExpired();
64 
65  inline void
66  RescheduleCleaning();
67 
68 private:
69  static LogComponent g_log;
70 
71  EventId m_cleanEvent;
72  Time m_scheduledCleaningTime;
73 };
74 
78 
79 template<class Policy>
80 LogComponent ContentStoreWithFreshness<Policy>::g_log = LogComponent(("ndn.cs.Freshness."
81  + Policy::GetName()).c_str(), __FILE__);
82 
83 template<class Policy>
84 TypeId
86 {
87  static TypeId tid = TypeId(("ns3::ndn::cs::Freshness::" + Policy::GetName()).c_str())
88  .SetGroupName("Ndn")
89  .SetParent<super>()
90  .template AddConstructor<ContentStoreWithFreshness<Policy>>()
91 
92  // trace stuff here
93  ;
94 
95  return tid;
96 }
97 
98 template<class Policy>
99 inline bool
100 ContentStoreWithFreshness<Policy>::Add(shared_ptr<const Data> data)
101 {
102  bool ok = super::Add(data);
103  if (!ok)
104  return false;
105 
106  NS_LOG_DEBUG(data->getName() << " added to cache");
107  RescheduleCleaning();
108  return true;
109 }
110 
111 template<class Policy>
112 inline void
114 {
115  const freshness_policy_container& freshness =
116  this->getPolicy().template get<freshness_policy_container>();
117 
118  if (freshness.size() > 0) {
119  Time nextStateTime =
120  freshness_policy_container::policy_base::get_freshness(&(*freshness.begin()));
121 
122  if (m_scheduledCleaningTime.IsZero() || // if not yet scheduled
123  m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled
124  {
125  if (m_cleanEvent.IsRunning()) {
126  Simulator::Remove(m_cleanEvent); // just canceling would not clean up list of events
127  }
128 
129  // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s");
130  m_cleanEvent = Simulator::Schedule(nextStateTime - Now(),
131  &ContentStoreWithFreshness<Policy>::CleanExpired, this);
132  m_scheduledCleaningTime = nextStateTime;
133  }
134  }
135  else {
136  if (m_cleanEvent.IsRunning()) {
137  Simulator::Remove(m_cleanEvent); // just canceling would not clean up list of events
138  }
139  }
140 }
141 
142 template<class Policy>
143 inline void
144 ContentStoreWithFreshness<Policy>::CleanExpired()
145 {
146  freshness_policy_container& freshness =
147  this->getPolicy().template get<freshness_policy_container>();
148 
149  // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items
150  // with freshness: " << freshness.size ());
151  Time now = Simulator::Now();
152 
153  while (!freshness.empty()) {
154  typename freshness_policy_container::iterator entry = freshness.begin();
155 
156  if (freshness_policy_container::policy_base::get_freshness(&(*entry))
157  <= now) // is the record stale?
158  {
159  super::erase(&(*entry));
160  }
161  else
162  break; // nothing else to do. All later records will not be stale
163  }
164  // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items
165  // with freshness: " << freshness.size ());
166 
167  m_scheduledCleaningTime = Time();
168  RescheduleCleaning();
169 }
170 
171 template<class Policy>
172 void
174 {
175  // const freshness_policy_container &freshness = this->getPolicy ().template
176  // get<freshness_policy_container> ();
177 
178  for (typename super::policy_container::const_iterator item = this->getPolicy().begin();
179  item != this->getPolicy().end(); item++) {
180  Time ttl = freshness_policy_container::policy_base::get_freshness(&(*item)) - Simulator::Now();
181  os << item->payload()->GetName() << "(left: " << ttl.ToDouble(Time::S) << "s)" << std::endl;
182  }
183 }
184 
185 } // namespace cs
186 } // namespace ndn
187 } // namespace ns3
188 
189 #endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_
Copyright (c) 2011-2015 Regents of the University of California.
virtual void Print(std::ostream &os) const
Print out content store entries.
ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > > super
ndnSIM::trie_with_policy< Name, ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< lru_policy_traits, ndnSIM::freshness_policy_traits > > > >, Entry >, ndnSIM::multi_policy_traits< boost::mpl::vector2< lru_policy_traits, ndnSIM::freshness_policy_traits > > > super
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Opaque type (shared_ptr) representing ID of a scheduled event.
Special content store realization that honors Freshness parameter in Data packets.
Copyright (c) 2011-2015 Regents of the University of California.
super::policy_container::template index< 1 >::type freshness_policy_container
Base implementation of NDN content store.
virtual bool Add(shared_ptr< const Data > data)
Add a new content to the content store.