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 | |
| 24 | #include "content-store-impl.h" |
| 25 | |
| 26 | #include "../../utils/trie/multi-policy.h" |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 27 | #include "custom-policies/freshness-policy.h" |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ns3 { |
| 30 | namespace ndn { |
| 31 | namespace cs { |
| 32 | |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 33 | /** |
| 34 | * @ingroup ndn-cs |
| 35 | * @brief Special content store realization that honors Freshness parameter in Data packets |
| 36 | */ |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 37 | template<class Policy> |
| 38 | class ContentStoreWithFreshness : |
| 39 | public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > > |
| 40 | { |
| 41 | public: |
| 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 Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 49 | virtual inline void |
| 50 | Print (std::ostream &os) const; |
| 51 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 52 | virtual inline bool |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 53 | Add (Ptr<const Data> data); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 54 | |
| 55 | private: |
| 56 | inline void |
| 57 | CleanExpired (); |
| 58 | |
| 59 | inline void |
| 60 | RescheduleCleaning (); |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 62 | private: |
| 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 | |
| 74 | template<class Policy> |
| 75 | LogComponent |
| 76 | ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ()); |
| 77 | |
| 78 | |
| 79 | template<class Policy> |
| 80 | TypeId |
| 81 | ContentStoreWithFreshness< 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 | |
| 95 | template<class Policy> |
| 96 | inline bool |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 97 | ContentStoreWithFreshness< Policy >::Add (Ptr<const Data> data) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 98 | { |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 99 | bool ok = super::Add (data); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 100 | if (!ok) return false; |
| 101 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 102 | NS_LOG_DEBUG (data->GetName () << " added to cache"); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 103 | RescheduleCleaning (); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | template<class Policy> |
| 108 | inline void |
| 109 | ContentStoreWithFreshness< Policy >::RescheduleCleaning () |
| 110 | { |
| 111 | const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> (); |
| 112 | |
| 113 | if (freshness.size () > 0) |
| 114 | { |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 115 | Time nextStateTime = freshness_policy_container::policy_base::get_freshness (&(*freshness.begin ())); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 116 | |
| 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 | |
| 140 | template<class Policy> |
| 141 | inline void |
| 142 | ContentStoreWithFreshness< 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 Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 153 | if (freshness_policy_container::policy_base::get_freshness (&(*entry)) <= now) // is the record stale? |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 154 | { |
| 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 Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 166 | template<class Policy> |
| 167 | void |
| 168 | ContentStoreWithFreshness< 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 Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 182 | |
| 183 | } // namespace cs |
| 184 | } // namespace ndn |
| 185 | } // namespace ns3 |
| 186 | |
| 187 | #endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_ |