blob: 0dcfef2e921d984854e1ff6623ff5fc933406ab1 [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 ())
113 return;
114
115 m_cleanEvent = Simulator::Schedule (i_time.begin ()->GetExpireTime (),
116 &CcnxPitImpl::CleanExpired, this);
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700117}
118
119void
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700120CcnxPitImpl::CleanExpired ()
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700121{
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700122 NS_LOG_LOGIC ("Cleaning PIT. Total: " << i_time.size ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700123 Time now = Simulator::Now ();
124
Alexander Afanasyev413c7f12012-07-10 17:35:16 -0700125 // uint32_t count = 0;
126 while (!i_time.empty ())
127 {
128 time_index::iterator entry = i_time.begin ();
129 if (entry->GetExpireTime () <= now) // is the record stale?
130 {
131 super::erase (entry->to_iterator ());
132 // // count ++;
133 }
134 else
135 break; // nothing else to do. All later records will not be stale
136 }
137
138 RescheduleCleaning ();
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700139}
140
141Ptr<CcnxPitEntry>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700142CcnxPitImpl::Lookup (const CcnxContentObjectHeader &header)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700143{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700144 /// @todo use predicate to search with exclude filters
145 super::iterator item = super::longest_prefix_match (header.GetName ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700146
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700147 if (item == super::end ())
148 return 0;
149 else
150 return item->payload (); // which could also be 0
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700151}
152
153Ptr<CcnxPitEntry>
154CcnxPitImpl::Lookup (const CcnxInterestHeader &header)
155{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700156 NS_LOG_FUNCTION (header.GetName ());
157 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700158
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700159 super::iterator foundItem, lastItem;
160 bool reachLast;
161 boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (header.GetName ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700162
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700163 if (!reachLast || lastItem == super::end ())
164 return 0;
165 else
166 return lastItem->payload (); // which could also be 0
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700167}
168
169Ptr<CcnxPitEntry>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700170CcnxPitImpl::Create (Ptr<const CcnxInterestHeader> header)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700171{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700172 Ptr<CcnxFibEntry> fibEntry = m_fib->LongestPrefixMatch (*header);
173 NS_ASSERT_MSG (fibEntry != 0,
174 "There should be at least default route set" <<
175 " Prefix = "<< header->GetName() << "NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib);
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700176
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700177
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700178 Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700179 std::pair< super::iterator, bool > result = super::insert (header->GetName (), newEntry);
180 if (result.first != super::end ())
181 {
182 if (result.second)
183 {
184 newEntry->SetTrie (result.first);
185 return newEntry;
186 }
187 else
188 {
189 // should we do anything?
190 // update payload? add new payload?
191 return result.first->payload ();
192 }
193 }
194 else
195 return 0;
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700196}
197
198
199void
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700200CcnxPitImpl::MarkErased (Ptr<CcnxPitEntry> item)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700201{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700202 // entry->SetExpireTime (Simulator::Now () + m_PitEntryPruningTimout);
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700203 super::erase (StaticCast< entry > (item)->to_iterator ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700204}
205
206
207void
208CcnxPitImpl::Print (std::ostream& os) const
209{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700210 // !!! unordered_set imposes "random" order of item in the same level !!!
211 super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0);
212 for (; item != end; item++)
213 {
214 if (item->payload () == 0) continue;
215
216 os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n";
217 }
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700218}
219
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700220Ptr<CcnxPitEntry>
221CcnxPitImpl::Begin ()
222{
223 super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
224 for (; item != end; item++)
225 {
226 if (item->payload () == 0) continue;
227 break;
228 }
229
230 if (item == end)
231 return End ();
232 else
233 return item->payload ();
234}
235
236Ptr<CcnxPitEntry>
237CcnxPitImpl::End ()
238{
239 return 0;
240}
241
242Ptr<CcnxPitEntry>
243CcnxPitImpl::Next (Ptr<CcnxPitEntry> from)
244{
245 if (from == 0) return 0;
246
247 super::parent_trie::recursive_iterator
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700248 item (*StaticCast< entry > (from)->to_iterator ()),
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700249 end (0);
250
251 for (item++; item != end; item++)
252 {
253 if (item->payload () == 0) continue;
254 break;
255 }
256
257 if (item == end)
258 return End ();
259 else
260 return item->payload ();
261}
262
263
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700264} // namespace ns3