blob: 4cfa70050613c8438ff4342cf63e8a1df2bcbac9 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080019
20#ifndef NDN_CONTENT_STORE_WITH_FRESHNESS_H_
21#define NDN_CONTENT_STORE_WITH_FRESHNESS_H_
22
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070023#include "ns3/ndnSIM/model/ndn-common.hpp"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080024
Alexander Afanasyev0c395372014-12-20 15:54:02 -080025#include "content-store-impl.hpp"
26
27#include "../../utils/trie/multi-policy.hpp"
28#include "custom-policies/freshness-policy.hpp"
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080029
30namespace ns3 {
31namespace ndn {
32namespace cs {
33
Alexander Afanasyev79206512013-07-27 16:49:12 -070034/**
35 * @ingroup ndn-cs
36 * @brief Special content store realization that honors Freshness parameter in Data packets
37 */
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080038template<class Policy>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039class ContentStoreWithFreshness
40 : public ContentStoreImpl<ndnSIM::
41 multi_policy_traits<boost::mpl::
42 vector2<Policy,
43 ndnSIM::freshness_policy_traits>>> {
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080044public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 typedef ContentStoreImpl<ndnSIM::multi_policy_traits<boost::mpl::
46 vector2<Policy,
47 ndnSIM::freshness_policy_traits>>>
48 super;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080049
50 typedef typename super::policy_container::template index<1>::type freshness_policy_container;
51
52 static TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080053 GetTypeId();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080054
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070055 virtual inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056 Print(std::ostream& os) const;
Alexander Afanasyevbe82da52013-04-09 05:43:14 -070057
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080058 virtual inline bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070059 Add(shared_ptr<const Data> data);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080060
61private:
62 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 CleanExpired();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080064
65 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 RescheduleCleaning();
Alexander Afanasyev7456b702013-02-01 22:41:48 -080067
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080068private:
69 static LogComponent g_log; ///< @brief Logging variable
70
71 EventId m_cleanEvent;
72 Time m_scheduledCleaningTime;
73};
74
75//////////////////////////////////////////
76////////// Implementation ////////////////
77//////////////////////////////////////////
78
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080079template<class Policy>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080LogComponent ContentStoreWithFreshness<Policy>::g_log = LogComponent(("ndn.cs.Freshness."
Alexander Afanasyevd6453cd2015-08-20 21:45:36 -070081 + Policy::GetName()).c_str(), __FILE__);
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080082
83template<class Policy>
84TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085ContentStoreWithFreshness<Policy>::GetTypeId()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080086{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 static TypeId tid = TypeId(("ns3::ndn::cs::Freshness::" + Policy::GetName()).c_str())
88 .SetGroupName("Ndn")
89 .SetParent<super>()
90 .template AddConstructor<ContentStoreWithFreshness<Policy>>()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080091
92 // trace stuff here
93 ;
94
95 return tid;
96}
97
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -080098template<class Policy>
99inline bool
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -0700100ContentStoreWithFreshness<Policy>::Add(shared_ptr<const Data> data)
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800101{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 bool ok = super::Add(data);
103 if (!ok)
104 return false;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800105
Spyridon Mastorakis1f1cd5e2014-12-04 11:12:40 -0800106 NS_LOG_DEBUG(data->getName() << " added to cache");
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 RescheduleCleaning();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800108 return true;
109}
110
111template<class Policy>
112inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113ContentStoreWithFreshness<Policy>::RescheduleCleaning()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800114{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 const freshness_policy_container& freshness =
116 this->getPolicy().template get<freshness_policy_container>();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800117
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 if (freshness.size() > 0) {
119 Time nextStateTime =
120 freshness_policy_container::policy_base::get_freshness(&(*freshness.begin()));
121
122 if (m_scheduledCleaningTime.IsZero() || // if not yet scheduled
123 m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800124 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 if (m_cleanEvent.IsRunning()) {
126 Simulator::Remove(m_cleanEvent); // just canceling would not clean up list of events
127 }
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800128
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s");
130 m_cleanEvent = Simulator::Schedule(nextStateTime - Now(),
131 &ContentStoreWithFreshness<Policy>::CleanExpired, this);
132 m_scheduledCleaningTime = nextStateTime;
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800133 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 }
135 else {
136 if (m_cleanEvent.IsRunning()) {
137 Simulator::Remove(m_cleanEvent); // just canceling would not clean up list of events
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800138 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800139 }
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800140}
141
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800142template<class Policy>
143inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800144ContentStoreWithFreshness<Policy>::CleanExpired()
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800145{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 freshness_policy_container& freshness =
147 this->getPolicy().template get<freshness_policy_container>();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800148
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items
150 // with freshness: " << freshness.size ());
151 Time now = Simulator::Now();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800152
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153 while (!freshness.empty()) {
154 typename freshness_policy_container::iterator entry = freshness.begin();
155
156 if (freshness_policy_container::policy_base::get_freshness(&(*entry))
157 <= now) // is the record stale?
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800158 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800159 super::erase(&(*entry));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800160 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800161 else
162 break; // nothing else to do. All later records will not be stale
163 }
164 // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items
165 // with freshness: " << freshness.size ());
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800166
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167 m_scheduledCleaningTime = Time();
168 RescheduleCleaning();
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800169}
170
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700171template<class Policy>
172void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800173ContentStoreWithFreshness<Policy>::Print(std::ostream& os) const
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700174{
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800175 // const freshness_policy_container &freshness = this->getPolicy ().template
176 // get<freshness_policy_container> ();
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700177
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800178 for (typename super::policy_container::const_iterator item = this->getPolicy().begin();
179 item != this->getPolicy().end(); item++) {
180 Time ttl = freshness_policy_container::policy_base::get_freshness(&(*item)) - Simulator::Now();
181 os << item->payload()->GetName() << "(left: " << ttl.ToDouble(Time::S) << "s)" << std::endl;
182 }
Alexander Afanasyevbe82da52013-04-09 05:43:14 -0700183}
184
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800185} // namespace cs
186} // namespace ndn
187} // namespace ns3
188
189#endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_