NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
content-store-with-freshness.h
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 University of California, Los Angeles
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19  */
20 
21 #ifndef NDN_CONTENT_STORE_WITH_FRESHNESS_H_
22 #define NDN_CONTENT_STORE_WITH_FRESHNESS_H_
23 
24 #include "content-store-impl.h"
25 
26 #include "../../utils/trie/multi-policy.h"
27 #include "custom-policies/freshness-policy.h"
28 
29 namespace ns3 {
30 namespace ndn {
31 namespace cs {
32 
37 template<class Policy>
39  public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > >
40 {
41 public:
43 
44  typedef typename super::policy_container::template index<1>::type freshness_policy_container;
45 
46  static TypeId
47  GetTypeId ();
48 
49  virtual inline void
50  Print (std::ostream &os) const;
51 
52  virtual inline bool
53  Add (Ptr<const Data> data);
54 
55 private:
56  inline void
57  CleanExpired ();
58 
59  inline void
60  RescheduleCleaning ();
61 
62 private:
63  static LogComponent g_log;
64 
65  EventId m_cleanEvent;
66  Time m_scheduledCleaningTime;
67 };
68 
72 
73 
74 template<class Policy>
75 LogComponent
76 ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ());
77 
78 
79 template<class Policy>
80 TypeId
82 {
83  static TypeId tid = TypeId (("ns3::ndn::cs::Freshness::"+Policy::GetName ()).c_str ())
84  .SetGroupName ("Ndn")
85  .SetParent<super> ()
86  .template AddConstructor< ContentStoreWithFreshness< Policy > > ()
87 
88  // trace stuff here
89  ;
90 
91  return tid;
92 }
93 
94 
95 template<class Policy>
96 inline bool
98 {
99  bool ok = super::Add (data);
100  if (!ok) return false;
101 
102  NS_LOG_DEBUG (data->GetName () << " added to cache");
103  RescheduleCleaning ();
104  return true;
105 }
106 
107 template<class Policy>
108 inline void
110 {
111  const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
112 
113  if (freshness.size () > 0)
114  {
115  Time nextStateTime = freshness_policy_container::policy_base::get_freshness (&(*freshness.begin ()));
116 
117  if (m_scheduledCleaningTime.IsZero () || // if not yet scheduled
118  m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled
119  {
120  if (m_cleanEvent.IsRunning ())
121  {
122  Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events
123  }
124 
125  // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s");
126  m_cleanEvent = Simulator::Schedule (nextStateTime - Now (), &ContentStoreWithFreshness< Policy >::CleanExpired, this);
127  m_scheduledCleaningTime = nextStateTime;
128  }
129  }
130  else
131  {
132  if (m_cleanEvent.IsRunning ())
133  {
134  Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events
135  }
136  }
137 }
138 
139 
140 template<class Policy>
141 inline void
142 ContentStoreWithFreshness< Policy >::CleanExpired ()
143 {
144  freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
145 
146  // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ());
147  Time now = Simulator::Now ();
148 
149  while (!freshness.empty ())
150  {
151  typename freshness_policy_container::iterator entry = freshness.begin ();
152 
153  if (freshness_policy_container::policy_base::get_freshness (&(*entry)) <= now) // is the record stale?
154  {
155  super::erase (&(*entry));
156  }
157  else
158  break; // nothing else to do. All later records will not be stale
159  }
160  // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ());
161 
162  m_scheduledCleaningTime = Time ();
163  RescheduleCleaning ();
164 }
165 
166 template<class Policy>
167 void
169 {
170  // const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
171 
172  for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
173  item != this->getPolicy ().end ();
174  item++)
175  {
176  Time ttl = freshness_policy_container::policy_base::get_freshness (&(*item)) - Simulator::Now ();
177  os << item->payload ()->GetName () << "(left: " << ttl.ToDouble (Time::S) << "s)" << std::endl;
178  }
179 }
180 
181 
182 
183 } // namespace cs
184 } // namespace ndn
185 } // namespace ns3
186 
187 #endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_
Special content store realization that honors Freshness parameter in Data packets.
virtual bool Add(Ptr< const Data > data)
Add a new content to the content store.
Base implementation of NDN content store.
virtual void Print(std::ostream &os) const
Print out content store entries.