blob: 9f394e363412826d79bf3ccc2cd9ccbfe4b54596 [file] [log] [blame]
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -08001/* -*- 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
29namespace ns3 {
30namespace ndn {
31namespace cs {
32
33template<class Policy>
34class ContentStoreWithFreshness :
35 public ContentStoreImpl< ndnSIM::multi_policy_traits< boost::mpl::vector2< Policy, ndnSIM::freshness_policy_traits > > >
36{
37public:
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
48private:
49 inline void
50 CleanExpired ();
51
52 inline void
53 RescheduleCleaning ();
54
55private:
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
67template<class Policy>
68LogComponent
69ContentStoreWithFreshness< Policy >::g_log = LogComponent (("ndn.cs.Freshness." + Policy::GetName ()).c_str ());
70
71
72template<class Policy>
73TypeId
74ContentStoreWithFreshness< 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
88template<class Policy>
89inline bool
90ContentStoreWithFreshness< 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
100template<class Policy>
101inline void
102ContentStoreWithFreshness< Policy >::RescheduleCleaning ()
103{
104 const freshness_policy_container &freshness = this->getPolicy ().template get<freshness_policy_container> ();
105
106 if (freshness.size () > 0)
107 {
Alexander Afanasyev69786112012-12-13 11:53:36 -0800108 Time nextStateTime = freshness_policy_container::policy_base::get_freshness (&(*freshness.begin ()));
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800109
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
133template<class Policy>
134inline void
135ContentStoreWithFreshness< 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 Afanasyev69786112012-12-13 11:53:36 -0800146 if (freshness_policy_container::policy_base::get_freshness (&(*entry)) <= now) // is the record stale?
Alexander Afanasyevc3cc0b32012-12-12 18:41:20 -0800147 {
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_