blob: 836a94f6dcb2460fec080e6981ff373a95462967 [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
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070024#include "ns3/ndnSIM/model/ndn-common.hpp"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080025
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>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040class ContentStoreWithFreshness
41 : public ContentStoreImpl<ndnSIM::
42 multi_policy_traits<boost::mpl::
43 vector2<Policy,
44 ndnSIM::freshness_policy_traits>>> {
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080045public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 typedef ContentStoreImpl<ndnSIM::multi_policy_traits<boost::mpl::
47 vector2<Policy,
48 ndnSIM::freshness_policy_traits>>>
49 super;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080050
51 typedef typename super::policy_container::template index<1>::type freshness_policy_container;
52
53 static TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 GetTypeId();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080055
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070056 virtual inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 Print(std::ostream& os) const;
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070058
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080059 virtual inline bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070060 Add(shared_ptr<const Data> data);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080061
62private:
63 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064 CleanExpired();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080065
66 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 RescheduleCleaning();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080068
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080069private:
70 static LogComponent g_log; ///< @brief Logging variable
71
72 EventId m_cleanEvent;
73 Time m_scheduledCleaningTime;
74};
75
76//////////////////////////////////////////
77////////// Implementation ////////////////
78//////////////////////////////////////////
79
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080080template<class Policy>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081LogComponent ContentStoreWithFreshness<Policy>::g_log = LogComponent(("ndn.cs.Freshness."
82 + Policy::GetName()).c_str());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080083
84template<class Policy>
85TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086ContentStoreWithFreshness<Policy>::GetTypeId()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080087{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 static TypeId tid = TypeId(("ns3::ndn::cs::Freshness::" + Policy::GetName()).c_str())
89 .SetGroupName("Ndn")
90 .SetParent<super>()
91 .template AddConstructor<ContentStoreWithFreshness<Policy>>()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080092
93 // trace stuff here
94 ;
95
96 return tid;
97}
98
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080099template<class Policy>
100inline bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700101ContentStoreWithFreshness<Policy>::Add(shared_ptr<const Data> data)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800102{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 bool ok = super::Add(data);
104 if (!ok)
105 return false;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800106
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 NS_LOG_DEBUG(data->GetName() << " added to cache");
108 RescheduleCleaning();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800109 return true;
110}
111
112template<class Policy>
113inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114ContentStoreWithFreshness<Policy>::RescheduleCleaning()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800115{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800116 const freshness_policy_container& freshness =
117 this->getPolicy().template get<freshness_policy_container>();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800118
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 if (freshness.size() > 0) {
120 Time nextStateTime =
121 freshness_policy_container::policy_base::get_freshness(&(*freshness.begin()));
122
123 if (m_scheduledCleaningTime.IsZero() || // if not yet scheduled
124 m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800125 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800126 if (m_cleanEvent.IsRunning()) {
127 Simulator::Remove(m_cleanEvent); // just canceling would not clean up list of events
128 }
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800129
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800130 // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s");
131 m_cleanEvent = Simulator::Schedule(nextStateTime - Now(),
132 &ContentStoreWithFreshness<Policy>::CleanExpired, this);
133 m_scheduledCleaningTime = nextStateTime;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800134 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800135 }
136 else {
137 if (m_cleanEvent.IsRunning()) {
138 Simulator::Remove(m_cleanEvent); // just canceling would not clean up list of events
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800139 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800140 }
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800141}
142
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800143template<class Policy>
144inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145ContentStoreWithFreshness<Policy>::CleanExpired()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800146{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800147 freshness_policy_container& freshness =
148 this->getPolicy().template get<freshness_policy_container>();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800149
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800150 // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items
151 // with freshness: " << freshness.size ());
152 Time now = Simulator::Now();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800153
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 while (!freshness.empty()) {
155 typename freshness_policy_container::iterator entry = freshness.begin();
156
157 if (freshness_policy_container::policy_base::get_freshness(&(*entry))
158 <= now) // is the record stale?
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800159 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800160 super::erase(&(*entry));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800161 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800162 else
163 break; // nothing else to do. All later records will not be stale
164 }
165 // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items
166 // with freshness: " << freshness.size ());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800167
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800168 m_scheduledCleaningTime = Time();
169 RescheduleCleaning();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800170}
171
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700172template<class Policy>
173void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800174ContentStoreWithFreshness<Policy>::Print(std::ostream& os) const
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700175{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 // const freshness_policy_container &freshness = this->getPolicy ().template
177 // get<freshness_policy_container> ();
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700178
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179 for (typename super::policy_container::const_iterator item = this->getPolicy().begin();
180 item != this->getPolicy().end(); item++) {
181 Time ttl = freshness_policy_container::policy_base::get_freshness(&(*item)) - Simulator::Now();
182 os << item->payload()->GetName() << "(left: " << ttl.ToDouble(Time::S) << "s)" << std::endl;
183 }
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700184}
185
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800186} // namespace cs
187} // namespace ndn
188} // namespace ns3
189
190#endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_