blob: e53cbbacb033038c6315f17b726c2b97e61a1811 [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
Alexander Afanasyev79206512013-07-27 16:49:12 -070033/**
34 * @ingroup ndn-cs
35 * @brief Special content store realization that honors Freshness parameter in Data packets
36 */
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080037template<class Policy>
38class ContentStoreWithFreshness :
39 public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > >
40{
41public:
42 typedef ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > > super;
43
44 typedef typename super::policy_container::template index<1>::type freshness_policy_container;
45
46 static TypeId
47 GetTypeId ();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080048
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070049 virtual inline void
50 Print (std::ostream &os) const;
51
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080052 virtual inline bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070053 Add (Ptr<const Data> data);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080054
55private:
56 inline void
57 CleanExpired ();
58
59 inline void
60 RescheduleCleaning ();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080061
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080062private:
63 static LogComponent g_log; ///< @brief Logging variable
64
65 EventId m_cleanEvent;
66 Time m_scheduledCleaningTime;
67};
68
69//////////////////////////////////////////
70////////// Implementation ////////////////
71//////////////////////////////////////////
72
73
74template<class Policy>
75LogComponent
76ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ());
77
78
79template<class Policy>
80TypeId
81ContentStoreWithFreshness< Policy >::GetTypeId ()
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
95template<class Policy>
96inline bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070097ContentStoreWithFreshness< Policy >::Add (Ptr<const Data> data)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080098{
Alexander Afanasyevb989b122013-07-10 17:15:46 -070099 bool ok = super::Add (data);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800100 if (!ok) return false;
101
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700102 NS_LOG_DEBUG (data->GetName () << " added to cache");
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800103 RescheduleCleaning ();
104 return true;
105}
106
107template<class Policy>
108inline void
109ContentStoreWithFreshness< Policy >::RescheduleCleaning ()
110{
111 const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
112
113 if (freshness.size () > 0)
114 {
Alexander Afanasyev69786112012-12-13 11:53:36 -0800115 Time nextStateTime = freshness_policy_container::policy_base::get_freshness (&(*freshness.begin ()));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800116
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
140template<class Policy>
141inline void
142ContentStoreWithFreshness< 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
Alexander Afanasyev69786112012-12-13 11:53:36 -0800153 if (freshness_policy_container::policy_base::get_freshness (&(*entry)) <= now) // is the record stale?
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800154 {
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
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700166template<class Policy>
167void
168ContentStoreWithFreshness< Policy >::Print (std::ostream &os) const
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
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800182
183} // namespace cs
184} // namespace ndn
185} // namespace ns3
186
187#endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_