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