blob: f68f9ddf39a81b7d1add0af2cb51d0ef2f54cdbe [file] [log] [blame]
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 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#include "ccnx-pit-impl.h"
22#include "ns3/log.h"
23#include "ns3/string.h"
24#include "ns3/uinteger.h"
25#include "ns3/simulator.h"
26#include "ccnx-interest-header.h"
27#include "ccnx-content-object-header.h"
28
29#include <boost/lambda/bind.hpp>
30#include <boost/lambda/lambda.hpp>
31
32NS_LOG_COMPONENT_DEFINE ("CcnxPitImpl");
33
34using namespace boost::tuples;
35using namespace boost;
36namespace ll = boost::lambda;
37
38namespace ns3 {
39
40NS_OBJECT_ENSURE_REGISTERED (CcnxPitImpl);
41
Alexander Afanasyev36b45772012-07-10 16:57:42 -070042
43// CcnxPitEntryImpl::CcnxPitEntryImpl (CcnxPit &pit,
44// Ptr<const CcnxInterestHeader> header,
45// Ptr<CcnxFibEntry> fibEntry)
46// : CcnxPitEntry (pit, header, fibEntry)
47// , item_ (0)
48// {
49// static_cast<CcnxPitImpl&> (m_container).i_time.insert (*this);
50// }
51
52// CcnxPitEntryImpl::~CcnxPitEntryImpl ()
53// {
54// static_cast<CcnxPitImpl&> (m_container).i_time.erase (*this);
55// }
56
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070057TypeId
58CcnxPitImpl::GetTypeId ()
59{
60 static TypeId tid = TypeId ("ns3::CcnxPit")
61 .SetGroupName ("Ccnx")
62 .SetParent<CcnxPit> ()
63 .AddConstructor<CcnxPitImpl> ()
64 .AddAttribute ("MaxSize",
65 "Set maximum number of entries in PIT. If 0, limit is not enforced",
66 StringValue ("0"),
67 MakeUintegerAccessor (&CcnxPitImpl::GetMaxSize, &CcnxPitImpl::SetMaxSize),
68 MakeUintegerChecker<uint32_t> ())
69 ;
70
71 return tid;
72}
73
74CcnxPitImpl::CcnxPitImpl ()
75{
76}
77
78CcnxPitImpl::~CcnxPitImpl ()
79{
80}
81
82uint32_t
83CcnxPitImpl::GetMaxSize () const
84{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070085 return getPolicy ().get_max_size ();
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070086}
87
88void
89CcnxPitImpl::SetMaxSize (uint32_t maxSize)
90{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070091 getPolicy ().set_max_size (maxSize);
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070092}
93
94void
95CcnxPitImpl::NotifyNewAggregate ()
96{
97 if (m_fib == 0)
98 {
99 m_fib = GetObject<CcnxFib> ();
100 }
101}
102
103void
104CcnxPitImpl::DoDispose ()
105{
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700106 super::clear ();
107}
108
109void CcnxPitImpl::RescheduleCleaning ()
110{
111 m_cleanEvent.Cancel ();
112 if (i_time.empty ())
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700113 {
114 NS_LOG_DEBUG ("No items in PIT");
115 return;
116 }
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700117
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700118 Time nextEvent = i_time.begin ()->GetExpireTime () - Simulator::Now ();
119 if (nextEvent < 0) nextEvent = Seconds (0);
120
121 NS_LOG_DEBUG ("Schedule next cleaning in " <<
122 nextEvent.ToDouble (Time::S) << "s (at " <<
123 i_time.begin ()->GetExpireTime () << "s abs time");
124 m_cleanEvent = Simulator::Schedule (nextEvent,
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700125 &CcnxPitImpl::CleanExpired, this);
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700126}
127
128void
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700129CcnxPitImpl::CleanExpired ()
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700130{
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700131 NS_LOG_LOGIC ("Cleaning PIT. Total: " << i_time.size ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700132 Time now = Simulator::Now ();
133
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700134 // uint32_t count = 0;
135 while (!i_time.empty ())
136 {
137 time_index::iterator entry = i_time.begin ();
138 if (entry->GetExpireTime () <= now) // is the record stale?
139 {
140 super::erase (entry->to_iterator ());
141 // // count ++;
142 }
143 else
144 break; // nothing else to do. All later records will not be stale
145 }
146
147 RescheduleCleaning ();
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700148}
149
150Ptr<CcnxPitEntry>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700151CcnxPitImpl::Lookup (const CcnxContentObjectHeader &header)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700152{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700153 /// @todo use predicate to search with exclude filters
154 super::iterator item = super::longest_prefix_match (header.GetName ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700155
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700156 if (item == super::end ())
157 return 0;
158 else
159 return item->payload (); // which could also be 0
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700160}
161
162Ptr<CcnxPitEntry>
163CcnxPitImpl::Lookup (const CcnxInterestHeader &header)
164{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700165 NS_LOG_FUNCTION (header.GetName ());
166 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700167
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700168 super::iterator foundItem, lastItem;
169 bool reachLast;
170 boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (header.GetName ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700171
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700172 if (!reachLast || lastItem == super::end ())
173 return 0;
174 else
175 return lastItem->payload (); // which could also be 0
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700176}
177
178Ptr<CcnxPitEntry>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700179CcnxPitImpl::Create (Ptr<const CcnxInterestHeader> header)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700180{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700181 Ptr<CcnxFibEntry> fibEntry = m_fib->LongestPrefixMatch (*header);
182 NS_ASSERT_MSG (fibEntry != 0,
183 "There should be at least default route set" <<
184 " Prefix = "<< header->GetName() << "NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib);
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700185
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700186
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700187 Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700188 std::pair< super::iterator, bool > result = super::insert (header->GetName (), newEntry);
189 if (result.first != super::end ())
190 {
191 if (result.second)
192 {
193 newEntry->SetTrie (result.first);
194 return newEntry;
195 }
196 else
197 {
198 // should we do anything?
199 // update payload? add new payload?
200 return result.first->payload ();
201 }
202 }
203 else
204 return 0;
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700205}
206
207
208void
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700209CcnxPitImpl::MarkErased (Ptr<CcnxPitEntry> item)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700210{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700211 // entry->SetExpireTime (Simulator::Now () + m_PitEntryPruningTimout);
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700212 super::erase (StaticCast< entry > (item)->to_iterator ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700213}
214
215
216void
217CcnxPitImpl::Print (std::ostream& os) const
218{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700219 // !!! unordered_set imposes "random" order of item in the same level !!!
220 super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0);
221 for (; item != end; item++)
222 {
223 if (item->payload () == 0) continue;
224
225 os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n";
226 }
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700227}
228
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700229uint32_t
230CcnxPitImpl::GetSize () const
231{
232 return super::getPolicy ().size ();
233}
234
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700235Ptr<CcnxPitEntry>
236CcnxPitImpl::Begin ()
237{
238 super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
239 for (; item != end; item++)
240 {
241 if (item->payload () == 0) continue;
242 break;
243 }
244
245 if (item == end)
246 return End ();
247 else
248 return item->payload ();
249}
250
251Ptr<CcnxPitEntry>
252CcnxPitImpl::End ()
253{
254 return 0;
255}
256
257Ptr<CcnxPitEntry>
258CcnxPitImpl::Next (Ptr<CcnxPitEntry> from)
259{
260 if (from == 0) return 0;
261
262 super::parent_trie::recursive_iterator
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700263 item (*StaticCast< entry > (from)->to_iterator ()),
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700264 end (0);
265
266 for (item++; item != end; item++)
267 {
268 if (item->payload () == 0) continue;
269 break;
270 }
271
272 if (item == end)
273 return End ();
274 else
275 return item->payload ();
276}
277
278
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700279} // namespace ns3