blob: e5179d34655c4c0dd38c1878d18f666f3fc9200d [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 Afanasyev30f60e32012-07-10 14:21:16 -0700106 clear ();
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700107}
108
109void
110CcnxPitImpl::DoCleanExpired ()
111{
112 // NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
113 Time now = Simulator::Now ();
114
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700115 NS_LOG_ERROR ("Need to be repaired");
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700116 // // uint32_t count = 0;
117 // while (!empty ())
118 // {
119 // CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin ();
120 // if (entry->GetExpireTime () <= now) // is the record stale?
121 // {
122 // get<i_timestamp> ().erase (entry);
123 // // count ++;
124 // }
125 // else
126 // break; // nothing else to do. All later records will not be stale
127 // }
128}
129
130Ptr<CcnxPitEntry>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700131CcnxPitImpl::Lookup (const CcnxContentObjectHeader &header)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700132{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700133 /// @todo use predicate to search with exclude filters
134 super::iterator item = super::longest_prefix_match (header.GetName ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700135
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700136 if (item == super::end ())
137 return 0;
138 else
139 return item->payload (); // which could also be 0
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700140}
141
142Ptr<CcnxPitEntry>
143CcnxPitImpl::Lookup (const CcnxInterestHeader &header)
144{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700145 NS_LOG_FUNCTION (header.GetName ());
146 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700147
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700148 super::iterator foundItem, lastItem;
149 bool reachLast;
150 boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (header.GetName ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700151
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700152 if (!reachLast || lastItem == super::end ())
153 return 0;
154 else
155 return lastItem->payload (); // which could also be 0
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700156}
157
158Ptr<CcnxPitEntry>
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700159CcnxPitImpl::Create (Ptr<const CcnxInterestHeader> header)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700160{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700161 Ptr<CcnxFibEntry> fibEntry = m_fib->LongestPrefixMatch (*header);
162 NS_ASSERT_MSG (fibEntry != 0,
163 "There should be at least default route set" <<
164 " Prefix = "<< header->GetName() << "NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib);
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700165
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700166
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700167 Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700168 std::pair< super::iterator, bool > result = super::insert (header->GetName (), newEntry);
169 if (result.first != super::end ())
170 {
171 if (result.second)
172 {
173 newEntry->SetTrie (result.first);
174 return newEntry;
175 }
176 else
177 {
178 // should we do anything?
179 // update payload? add new payload?
180 return result.first->payload ();
181 }
182 }
183 else
184 return 0;
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700185}
186
187
188void
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700189CcnxPitImpl::MarkErased (Ptr<CcnxPitEntry> item)
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700190{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700191 // entry->SetExpireTime (Simulator::Now () + m_PitEntryPruningTimout);
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700192 super::erase (StaticCast< entry > (item)->to_iterator ());
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700193}
194
195
196void
197CcnxPitImpl::Print (std::ostream& os) const
198{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700199 // !!! unordered_set imposes "random" order of item in the same level !!!
200 super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0);
201 for (; item != end; item++)
202 {
203 if (item->payload () == 0) continue;
204
205 os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n";
206 }
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700207}
208
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700209Ptr<CcnxPitEntry>
210CcnxPitImpl::Begin ()
211{
212 super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
213 for (; item != end; item++)
214 {
215 if (item->payload () == 0) continue;
216 break;
217 }
218
219 if (item == end)
220 return End ();
221 else
222 return item->payload ();
223}
224
225Ptr<CcnxPitEntry>
226CcnxPitImpl::End ()
227{
228 return 0;
229}
230
231Ptr<CcnxPitEntry>
232CcnxPitImpl::Next (Ptr<CcnxPitEntry> from)
233{
234 if (from == 0) return 0;
235
236 super::parent_trie::recursive_iterator
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700237 item (*StaticCast< entry > (from)->to_iterator ()),
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700238 end (0);
239
240 for (item++; item != end; item++)
241 {
242 if (item->payload () == 0) continue;
243 break;
244 }
245
246 if (item == end)
247 return End ();
248 else
249 return item->payload ();
250}
251
252
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -0700253} // namespace ns3