Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 1 | /* -*- 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 Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame^] | 24 | #include "ns3/ndnSIM/model/ndn-common.hpp" |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 26 | #include "content-store-impl.hpp" |
| 27 | |
| 28 | #include "../../utils/trie/multi-policy.hpp" |
| 29 | #include "custom-policies/freshness-policy.hpp" |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 30 | |
| 31 | namespace ns3 { |
| 32 | namespace ndn { |
| 33 | namespace cs { |
| 34 | |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 35 | /** |
| 36 | * @ingroup ndn-cs |
| 37 | * @brief Special content store realization that honors Freshness parameter in Data packets |
| 38 | */ |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 39 | template<class Policy> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 40 | class ContentStoreWithFreshness |
| 41 | : public ContentStoreImpl<ndnSIM:: |
| 42 | multi_policy_traits<boost::mpl:: |
| 43 | vector2<Policy, |
| 44 | ndnSIM::freshness_policy_traits>>> { |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 45 | public: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 46 | typedef ContentStoreImpl<ndnSIM::multi_policy_traits<boost::mpl:: |
| 47 | vector2<Policy, |
| 48 | ndnSIM::freshness_policy_traits>>> |
| 49 | super; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 50 | |
| 51 | typedef typename super::policy_container::template index<1>::type freshness_policy_container; |
| 52 | |
| 53 | static TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 54 | GetTypeId(); |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 56 | virtual inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | Print(std::ostream& os) const; |
Alexander Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 59 | virtual inline bool |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame^] | 60 | Add(shared_ptr<const Data> data); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | CleanExpired(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 65 | |
| 66 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 67 | RescheduleCleaning(); |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 69 | private: |
| 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 Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 80 | template<class Policy> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 81 | LogComponent ContentStoreWithFreshness<Policy>::g_log = LogComponent(("ndn.cs.Freshness." |
| 82 | + Policy::GetName()).c_str()); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 83 | |
| 84 | template<class Policy> |
| 85 | TypeId |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 86 | ContentStoreWithFreshness<Policy>::GetTypeId() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 87 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | static TypeId tid = TypeId(("ns3::ndn::cs::Freshness::" + Policy::GetName()).c_str()) |
| 89 | .SetGroupName("Ndn") |
| 90 | .SetParent<super>() |
| 91 | .template AddConstructor<ContentStoreWithFreshness<Policy>>() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 92 | |
| 93 | // trace stuff here |
| 94 | ; |
| 95 | |
| 96 | return tid; |
| 97 | } |
| 98 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 99 | template<class Policy> |
| 100 | inline bool |
Spyridon Mastorakis | 53e922f | 2014-10-17 17:29:26 -0700 | [diff] [blame^] | 101 | ContentStoreWithFreshness<Policy>::Add(shared_ptr<const Data> data) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | bool ok = super::Add(data); |
| 104 | if (!ok) |
| 105 | return false; |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 107 | NS_LOG_DEBUG(data->GetName() << " added to cache"); |
| 108 | RescheduleCleaning(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
| 112 | template<class Policy> |
| 113 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 114 | ContentStoreWithFreshness<Policy>::RescheduleCleaning() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 115 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 116 | const freshness_policy_container& freshness = |
| 117 | this->getPolicy().template get<freshness_policy_container>(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 119 | 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 Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 125 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 126 | if (m_cleanEvent.IsRunning()) { |
| 127 | Simulator::Remove(m_cleanEvent); // just canceling would not clean up list of events |
| 128 | } |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 130 | // 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 Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 134 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 135 | } |
| 136 | else { |
| 137 | if (m_cleanEvent.IsRunning()) { |
| 138 | Simulator::Remove(m_cleanEvent); // just canceling would not clean up list of events |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 139 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 140 | } |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 143 | template<class Policy> |
| 144 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 145 | ContentStoreWithFreshness<Policy>::CleanExpired() |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 146 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 147 | freshness_policy_container& freshness = |
| 148 | this->getPolicy().template get<freshness_policy_container>(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 150 | // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items |
| 151 | // with freshness: " << freshness.size ()); |
| 152 | Time now = Simulator::Now(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 153 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 154 | 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 Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 159 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 160 | super::erase(&(*entry)); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 161 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 162 | 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 Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 167 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 168 | m_scheduledCleaningTime = Time(); |
| 169 | RescheduleCleaning(); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Alexander Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 172 | template<class Policy> |
| 173 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 174 | ContentStoreWithFreshness<Policy>::Print(std::ostream& os) const |
Alexander Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 175 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 176 | // const freshness_policy_container &freshness = this->getPolicy ().template |
| 177 | // get<freshness_policy_container> (); |
Alexander Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 178 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 179 | 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 Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 186 | } // namespace cs |
| 187 | } // namespace ndn |
| 188 | } // namespace ns3 |
| 189 | |
| 190 | #endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_ |