blob: 2bd74de61a505875287bac29ec01c58c3e43d9ec [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
Alexander Afanasyev0c395372014-12-20 15:54:02 -080026#include "content-store-impl.hpp"
27
28#include "../../utils/trie/multi-policy.hpp"
29#include "custom-policies/freshness-policy.hpp"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080030
31namespace ns3 {
32namespace ndn {
33namespace cs {
34
Alexander Afanasyev79206512013-07-27 16:49:12 -070035/**
36 * @ingroup ndn-cs
37 * @brief Special content store realization that honors Freshness parameter in Data packets
38 */
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080039template<class Policy>
40class ContentStoreWithFreshness :
41 public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > >
42{
43public:
44 typedef ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > > super;
45
46 typedef typename super::policy_container::template index<1>::type freshness_policy_container;
47
48 static TypeId
49 GetTypeId ();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080050
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070051 virtual inline void
52 Print (std::ostream &os) const;
53
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080054 virtual inline bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070055 Add (Ptr<const Data> data);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080056
57private:
58 inline void
59 CleanExpired ();
60
61 inline void
62 RescheduleCleaning ();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080063
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080064private:
65 static LogComponent g_log; ///< @brief Logging variable
66
67 EventId m_cleanEvent;
68 Time m_scheduledCleaningTime;
69};
70
71//////////////////////////////////////////
72////////// Implementation ////////////////
73//////////////////////////////////////////
74
75
76template<class Policy>
77LogComponent
78ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ());
79
80
81template<class Policy>
82TypeId
83ContentStoreWithFreshness< Policy >::GetTypeId ()
84{
85 static TypeId tid = TypeId (("ns3::ndn::cs::Freshness::"+Policy::GetName ()).c_str ())
86 .SetGroupName ("Ndn")
87 .SetParent<super> ()
88 .template AddConstructor< ContentStoreWithFreshness< Policy > > ()
89
90 // trace stuff here
91 ;
92
93 return tid;
94}
95
96
97template<class Policy>
98inline bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070099ContentStoreWithFreshness< Policy >::Add (Ptr<const Data> data)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800100{
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700101 bool ok = super::Add (data);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800102 if (!ok) return false;
103
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700104 NS_LOG_DEBUG (data->GetName () << " added to cache");
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800105 RescheduleCleaning ();
106 return true;
107}
108
109template<class Policy>
110inline void
111ContentStoreWithFreshness< Policy >::RescheduleCleaning ()
112{
113 const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
114
115 if (freshness.size () > 0)
116 {
Alexander Afanasyev69786112012-12-13 11:53:36 -0800117 Time nextStateTime = freshness_policy_container::policy_base::get_freshness (&(*freshness.begin ()));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800118
119 if (m_scheduledCleaningTime.IsZero () || // if not yet scheduled
120 m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled
121 {
122 if (m_cleanEvent.IsRunning ())
123 {
124 Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events
125 }
126
127 // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s");
128 m_cleanEvent = Simulator::Schedule (nextStateTime - Now (), &ContentStoreWithFreshness< Policy >::CleanExpired, this);
129 m_scheduledCleaningTime = nextStateTime;
130 }
131 }
132 else
133 {
134 if (m_cleanEvent.IsRunning ())
135 {
136 Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events
137 }
138 }
139}
140
141
142template<class Policy>
143inline void
144ContentStoreWithFreshness< Policy >::CleanExpired ()
145{
146 freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
147
148 // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ());
149 Time now = Simulator::Now ();
150
151 while (!freshness.empty ())
152 {
153 typename freshness_policy_container::iterator entry = freshness.begin ();
154
Alexander Afanasyev69786112012-12-13 11:53:36 -0800155 if (freshness_policy_container::policy_base::get_freshness (&(*entry)) <= now) // is the record stale?
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800156 {
157 super::erase (&(*entry));
158 }
159 else
160 break; // nothing else to do. All later records will not be stale
161 }
162 // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ());
163
164 m_scheduledCleaningTime = Time ();
165 RescheduleCleaning ();
166}
167
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700168template<class Policy>
169void
170ContentStoreWithFreshness< Policy >::Print (std::ostream &os) const
171{
172 // const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
173
174 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
175 item != this->getPolicy ().end ();
176 item++)
177 {
178 Time ttl = freshness_policy_container::policy_base::get_freshness (&(*item)) - Simulator::Now ();
179 os << item->payload ()->GetName () << "(left: " << ttl.ToDouble (Time::S) << "s)" << std::endl;
180 }
181}
182
183
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800184
185} // namespace cs
186} // namespace ndn
187} // namespace ns3
188
189#endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_