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 | |
| 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 (); |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 45 | virtual inline void |
| 46 | Print (std::ostream &os) const; |
| 47 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 48 | virtual inline bool |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 49 | Add (Ptr<const ContentObject> header, Ptr<const Packet> packet); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 50 | |
| 51 | private: |
| 52 | inline void |
| 53 | CleanExpired (); |
| 54 | |
| 55 | inline void |
| 56 | RescheduleCleaning (); |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 58 | private: |
| 59 | static LogComponent g_log; ///< @brief Logging variable |
| 60 | |
| 61 | EventId m_cleanEvent; |
| 62 | Time m_scheduledCleaningTime; |
| 63 | }; |
| 64 | |
| 65 | ////////////////////////////////////////// |
| 66 | ////////// Implementation //////////////// |
| 67 | ////////////////////////////////////////// |
| 68 | |
| 69 | |
| 70 | template<class Policy> |
| 71 | LogComponent |
| 72 | ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ()); |
| 73 | |
| 74 | |
| 75 | template<class Policy> |
| 76 | TypeId |
| 77 | ContentStoreWithFreshness< Policy >::GetTypeId () |
| 78 | { |
| 79 | static TypeId tid = TypeId (("ns3::ndn::cs::Freshness::"+Policy::GetName ()).c_str ()) |
| 80 | .SetGroupName ("Ndn") |
| 81 | .SetParent<super> () |
| 82 | .template AddConstructor< ContentStoreWithFreshness< Policy > > () |
| 83 | |
| 84 | // trace stuff here |
| 85 | ; |
| 86 | |
| 87 | return tid; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | template<class Policy> |
| 92 | inline bool |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 93 | ContentStoreWithFreshness< Policy >::Add (Ptr<const ContentObject> header, Ptr<const Packet> packet) |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 94 | { |
| 95 | bool ok = super::Add (header, packet); |
| 96 | if (!ok) return false; |
| 97 | |
| 98 | NS_LOG_DEBUG (header->GetName () << " added to cache"); |
| 99 | RescheduleCleaning (); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | template<class Policy> |
| 104 | inline void |
| 105 | ContentStoreWithFreshness< Policy >::RescheduleCleaning () |
| 106 | { |
| 107 | const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> (); |
| 108 | |
| 109 | if (freshness.size () > 0) |
| 110 | { |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 111 | Time nextStateTime = freshness_policy_container::policy_base::get_freshness (&(*freshness.begin ())); |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 112 | |
| 113 | if (m_scheduledCleaningTime.IsZero () || // if not yet scheduled |
| 114 | m_scheduledCleaningTime > nextStateTime) // if new item expire sooner than already scheduled |
| 115 | { |
| 116 | if (m_cleanEvent.IsRunning ()) |
| 117 | { |
| 118 | Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events |
| 119 | } |
| 120 | |
| 121 | // NS_LOG_DEBUG ("Next event in: " << (nextStateTime - Now ()).ToDouble (Time::S) << "s"); |
| 122 | m_cleanEvent = Simulator::Schedule (nextStateTime - Now (), &ContentStoreWithFreshness< Policy >::CleanExpired, this); |
| 123 | m_scheduledCleaningTime = nextStateTime; |
| 124 | } |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | if (m_cleanEvent.IsRunning ()) |
| 129 | { |
| 130 | Simulator::Remove (m_cleanEvent); // just canceling would not clean up list of events |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | template<class Policy> |
| 137 | inline void |
| 138 | ContentStoreWithFreshness< Policy >::CleanExpired () |
| 139 | { |
| 140 | freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> (); |
| 141 | |
| 142 | // NS_LOG_LOGIC (">> Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ()); |
| 143 | Time now = Simulator::Now (); |
| 144 | |
| 145 | while (!freshness.empty ()) |
| 146 | { |
| 147 | typename freshness_policy_container::iterator entry = freshness.begin (); |
| 148 | |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 149 | 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] | 150 | { |
| 151 | super::erase (&(*entry)); |
| 152 | } |
| 153 | else |
| 154 | break; // nothing else to do. All later records will not be stale |
| 155 | } |
| 156 | // NS_LOG_LOGIC ("<< Cleaning: Total number of items:" << this->getPolicy ().size () << ", items with freshness: " << freshness.size ()); |
| 157 | |
| 158 | m_scheduledCleaningTime = Time (); |
| 159 | RescheduleCleaning (); |
| 160 | } |
| 161 | |
Alexander Afanasyev | be82da5 | 2013-04-09 05:43:14 -0700 | [diff] [blame] | 162 | template<class Policy> |
| 163 | void |
| 164 | ContentStoreWithFreshness< Policy >::Print (std::ostream &os) const |
| 165 | { |
| 166 | // const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> (); |
| 167 | |
| 168 | for (typename super::policy_container::const_iterator item = this->getPolicy ().begin (); |
| 169 | item != this->getPolicy ().end (); |
| 170 | item++) |
| 171 | { |
| 172 | Time ttl = freshness_policy_container::policy_base::get_freshness (&(*item)) - Simulator::Now (); |
| 173 | os << item->payload ()->GetName () << "(left: " << ttl.ToDouble (Time::S) << "s)" << std::endl; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |
Alexander Afanasyev | c3cc0b3 | 2012-12-12 18:41:20 -0800 | [diff] [blame] | 178 | |
| 179 | } // namespace cs |
| 180 | } // namespace ndn |
| 181 | } // namespace ns3 |
| 182 | |
| 183 | #endif // NDN_CONTENT_STORE_WITH_FRESHNESS_H_ |