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" |
| 27 | #include "../../utils/trie/freshness-policy.h" |
| 28 | |
| 29 | namespace ns3 { |
| 30 | namespace ndn { |
| 31 | namespace cs { |
| 32 | |
| 33 | template<class Policy> |
| 34 | class ContentStoreWithFreshness : |
| 35 | public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > > |
| 36 | { |
| 37 | public: |
| 38 | typedef ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > > super; |
| 39 | |
| 40 | typedef typename super::policy_container::template index<1>::type freshness_policy_container; |
| 41 | |
| 42 | static TypeId |
| 43 | GetTypeId (); |
| 44 | |
| 45 | virtual inline bool |
| 46 | Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet); |
| 47 | |
| 48 | private: |
| 49 | inline void |
| 50 | CleanExpired (); |
| 51 | |
| 52 | inline void |
| 53 | RescheduleCleaning (); |
| 54 | |
| 55 | private: |
| 56 | static LogComponent g_log; ///< @brief Logging variable |
| 57 | |
| 58 | EventId m_cleanEvent; |
| 59 | Time m_scheduledCleaningTime; |
| 60 | }; |
| 61 | |
| 62 | ////////////////////////////////////////// |
| 63 | ////////// Implementation //////////////// |
| 64 | ////////////////////////////////////////// |
| 65 | |
| 66 | |
| 67 | template<class Policy> |
| 68 | LogComponent |
| 69 | ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ()); |
| 70 | |
| 71 | |
| 72 | template<class Policy> |
| 73 | TypeId |
| 74 | ContentStoreWithFreshness< Policy >::GetTypeId () |
| 75 | { |
| 76 | static TypeId tid = TypeId (("ns3::ndn::cs::Freshness::"+Policy::GetName ()).c_str ()) |
| 77 | .SetGroupName ("Ndn") |
| 78 | .SetParent<super> () |
| 79 | .template AddConstructor< ContentStoreWithFreshness< Policy > > () |
| 80 | |
| 81 | // trace stuff here |
| 82 | ; |
| 83 | |
| 84 | return tid; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | template<class Policy> |
| 89 | inline bool |
| 90 | ContentStoreWithFreshness< Policy >::Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet) |
| 91 | { |
| 92 | bool ok = super::Add (header, packet); |
| 93 | if (!ok) return false; |
| 94 | |
| 95 | NS_LOG_DEBUG (header->GetName () << " added to cache"); |
| 96 | RescheduleCleaning (); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | template<class Policy> |
| 101 | inline void |
| 102 | ContentStoreWithFreshness< Policy >::RescheduleCleaning () |
| 103 | { |
| 104 | const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> (); |
| 105 | |
| 106 | if (freshness.size () > 0) |
| 107 | { |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 108 | Time nextStateTime = freshness_policy_container::policy_base::get_freshness (&(*freshness.begin ())); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 109 | |
| 110 | if (m_scheduledCleaningTime.IsZero () || // if not yet scheduled |
| 111 | m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled |
| 112 | { |
| 113 | if (m_cleanEvent.IsRunning ()) |
| 114 | { |
| 115 | Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events |
| 116 | } |
| 117 | |
| 118 | // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s"); |
| 119 | m_cleanEvent = Simulator::Schedule (nextStateTime - Now (), &ContentStoreWithFreshness< Policy >::CleanExpired, this); |
| 120 | m_scheduledCleaningTime = nextStateTime; |
| 121 | } |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | if (m_cleanEvent.IsRunning ()) |
| 126 | { |
| 127 | Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | template<class Policy> |
| 134 | inline void |
| 135 | ContentStoreWithFreshness< Policy >::CleanExpired () |
| 136 | { |
| 137 | freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> (); |
| 138 | |
| 139 | // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ()); |
| 140 | Time now = Simulator::Now (); |
| 141 | |
| 142 | while (!freshness.empty ()) |
| 143 | { |
| 144 | typename freshness_policy_container::iterator entry = freshness.begin (); |
| 145 | |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 146 | 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] | 147 | { |
| 148 | super::erase (&(*entry)); |
| 149 | } |
| 150 | else |
| 151 | break; // nothing else to do. All later records will not be stale |
| 152 | } |
| 153 | // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ()); |
| 154 | |
| 155 | m_scheduledCleaningTime = Time (); |
| 156 | RescheduleCleaning (); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | } // namespace cs |
| 161 | } // namespace ndn |
| 162 | } // namespace ns3 |
| 163 | |
| 164 | #endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_ |