blob: 22242b9cdeeeffecdffcec32bc9111a484d0f9b0 [file] [log] [blame]
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08001/* -*- 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"
Alexander Afanasyev7456b702013-02-01 22:41:48 -080027#include "custom-policies/freshness-policy.h"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080028
29namespace ns3 {
30namespace ndn {
31namespace cs {
32
33template<class Policy>
34class ContentStoreWithFreshness :
35 public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > >
36{
37public:
38 typedef ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > > super;
39
40 typedef typename super::policy_container::template index<1>::type freshness_policy_container;
41
42 static TypeId
43 GetTypeId ();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080044
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070045 virtual inline void
46 Print (std::ostream &os) const;
47
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080048 virtual inline bool
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070049 Add (Ptr<const ContentObject> data);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080050
51private:
52 inline void
53 CleanExpired ();
54
55 inline void
56 RescheduleCleaning ();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080057
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080058private:
59 static LogComponent g_log; ///< @brief Logging variable
60
61 EventId m_cleanEvent;
62 Time m_scheduledCleaningTime;
63};
64
65//////////////////////////////////////////
66////////// Implementation ////////////////
67//////////////////////////////////////////
68
69
70template<class Policy>
71LogComponent
72ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ());
73
74
75template<class Policy>
76TypeId
77ContentStoreWithFreshness< Policy >::GetTypeId ()
78{
79 static TypeId tid = TypeId (("ns3::ndn::cs::Freshness::"+Policy::GetName ()).c_str ())
80 .SetGroupName ("Ndn")
81 .SetParent<super> ()
82 .template AddConstructor< ContentStoreWithFreshness< Policy > > ()
83
84 // trace stuff here
85 ;
86
87 return tid;
88}
89
90
91template<class Policy>
92inline bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -070093ContentStoreWithFreshness< Policy >::Add (Ptr<const ContentObject> data)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080094{
Alexander Afanasyevb989b122013-07-10 17:15:46 -070095 bool ok = super::Add (data);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080096 if (!ok) return false;
97
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070098 NS_LOG_DEBUG (data->GetName () << " added to cache");
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080099 RescheduleCleaning ();
100 return true;
101}
102
103template<class Policy>
104inline void
105ContentStoreWithFreshness< Policy >::RescheduleCleaning ()
106{
107 const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
108
109 if (freshness.size () > 0)
110 {
Alexander Afanasyev69786112012-12-13 11:53:36 -0800111 Time nextStateTime = freshness_policy_container::policy_base::get_freshness (&(*freshness.begin ()));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800112
113 if (m_scheduledCleaningTime.IsZero () || // if not yet scheduled
114 m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled
115 {
116 if (m_cleanEvent.IsRunning ())
117 {
118 Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events
119 }
120
121 // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s");
122 m_cleanEvent = Simulator::Schedule (nextStateTime - Now (), &ContentStoreWithFreshness< Policy >::CleanExpired, this);
123 m_scheduledCleaningTime = nextStateTime;
124 }
125 }
126 else
127 {
128 if (m_cleanEvent.IsRunning ())
129 {
130 Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events
131 }
132 }
133}
134
135
136template<class Policy>
137inline void
138ContentStoreWithFreshness< Policy >::CleanExpired ()
139{
140 freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
141
142 // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ());
143 Time now = Simulator::Now ();
144
145 while (!freshness.empty ())
146 {
147 typename freshness_policy_container::iterator entry = freshness.begin ();
148
Alexander Afanasyev69786112012-12-13 11:53:36 -0800149 if (freshness_policy_container::policy_base::get_freshness (&(*entry)) <= now) // is the record stale?
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800150 {
151 super::erase (&(*entry));
152 }
153 else
154 break; // nothing else to do. All later records will not be stale
155 }
156 // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ());
157
158 m_scheduledCleaningTime = Time ();
159 RescheduleCleaning ();
160}
161
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700162template<class Policy>
163void
164ContentStoreWithFreshness< Policy >::Print (std::ostream &os) const
165{
166 // const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
167
168 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
169 item != this->getPolicy ().end ();
170 item++)
171 {
172 Time ttl = freshness_policy_container::policy_base::get_freshness (&(*item)) - Simulator::Now ();
173 os << item->payload ()->GetName () << "(left: " << ttl.ToDouble (Time::S) << "s)" << std::endl;
174 }
175}
176
177
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800178
179} // namespace cs
180} // namespace ndn
181} // namespace ns3
182
183#endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_