blob: ead991cb4b55ba69efa464ac6134517278baf5da [file] [log] [blame]
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -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.h"
22#include "ns3/log.h"
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080023#include "ns3/string.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070024#include "ns3/simulator.h"
25#include "ccnx-interest-header.h"
26#include "ccnx-content-object-header.h"
27
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080028#include <boost/bind.hpp>
29#include <boost/lambda/lambda.hpp>
30
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070031NS_LOG_COMPONENT_DEFINE ("CcnxPit");
32
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080033using namespace boost::tuples;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080034using namespace boost;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080035
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070036namespace ns3 {
37
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080038NS_OBJECT_ENSURE_REGISTERED (CcnxPit);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070039
40using namespace __ccnx_private;
41
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080042TypeId
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070043CcnxPit::GetTypeId ()
44{
45 static TypeId tid = TypeId ("ns3::CcnxPit")
46 .SetGroupName ("Ccnx")
47 .SetParent<Object> ()
48 .AddConstructor<CcnxPit> ()
49 .AddAttribute ("CleanupTimeout",
50 "Timeout defining how frequent RIT should be cleaned up",
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080051 StringValue ("1s"),
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070052 MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout),
53 MakeTimeChecker ())
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080054
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080055 .AddAttribute ("PitEntryPruningTimout",
56 "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again",
57 StringValue ("100ms"),
58 MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout),
59 MakeTimeChecker ())
Alexander Afanasyevf9f4eb02011-12-16 01:51:14 -080060
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080061 .AddAttribute ("PitEntryDefaultLifetime",
62 "Default lifetime of PIT entry (aka default Interest lifetime)",
63 StringValue("4s"),
64 MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime),
65 MakeTimeChecker ())
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070066 ;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070067
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070068 return tid;
69}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070070
71CcnxPit::CcnxPit ()
72{
73}
74
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080075CcnxPit::~CcnxPit ()
76{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080077 DoDispose ();
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080078}
79
Alexander Afanasyev18252852011-11-21 13:35:31 -080080void
81CcnxPit::NotifyNewAggregate ()
82{
83}
84
85void
86CcnxPit::DoDispose ()
87{
88 if (m_cleanupEvent.IsRunning ())
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080089 m_cleanupEvent.Cancel ();
90
Alexander Afanasyev18252852011-11-21 13:35:31 -080091 clear ();
92}
93
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070094void
95CcnxPit::SetCleanupTimeout (const Time &timeout)
96{
97 m_cleanupTimeout = timeout;
98 if (m_cleanupEvent.IsRunning ())
99 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
100
101 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800102 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700103 &CcnxPit::CleanExpired, this);
104}
105
106Time
107CcnxPit::GetCleanupTimeout () const
108{
109 return m_cleanupTimeout;
110}
111
112void CcnxPit::CleanExpired ()
113{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800114 NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700115 Time now = Simulator::Now ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800116
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800117 // uint32_t count = 0;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 while (!empty ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700119 {
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800120 CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin ();
121 if (entry->GetExpireTime () <= now) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700122 {
Alexander Afanasyevd76f4aa2011-12-15 21:30:16 -0800123 get<i_timestamp> ().erase (entry);
124 // count ++;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700125 }
126 else
127 break; // nothing else to do. All later records will not be stale
128 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800129
130 // NS_LOG_LOGIC ("Cleaned " << count << " records. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700131 // schedule next even
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800132
133 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700134 &CcnxPit::CleanExpired, this);
135}
136
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700137void
138CcnxPit::SetFib (Ptr<CcnxFib> fib)
139{
140 m_fib = fib;
141}
142
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700143const CcnxPitEntry&
144CcnxPit::Lookup (const CcnxContentObjectHeader &header) const
145{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800146 // NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700147
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700148 CcnxPitEntryContainer::type::iterator entry = end ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700149
Alexander Afanasyevee217df2012-04-09 15:01:06 -0700150 // do the longest prefix match
151 const CcnxNameComponents &name = header.GetName ();
152 for (size_t componentsCount = name.GetComponents ().size ()+1;
153 componentsCount > 0;
154 componentsCount--)
155 {
156 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount-1));
157
158 entry = get<i_prefix> ().find (subPrefix);
159 if (entry != end())
160 return *entry;
161 }
162
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700163 if (entry == end ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700164 throw CcnxPitEntryNotFound();
165
166 return *entry;
167}
168
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800169boost::tuple<const CcnxPitEntry&, bool, bool>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800170CcnxPit::Lookup (const CcnxInterestHeader &header)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700171{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700172 NS_LOG_FUNCTION_NOARGS ();
173 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
174
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800175 bool isDuplicate = false;
176 bool isNew = true;
177
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700178 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700179 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700180
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700181 if (entry == end ())
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800182 {
183 CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header);
Alexander Afanasyev07827182011-12-13 01:07:32 -0800184 NS_ASSERT_MSG (fibEntry != m_fib->m_fib.end (),
Ilya Moiseenkoad9e8ab2012-01-11 19:58:34 -0800185 "There should be at least default route set" << " Prefix = "<<header.GetName() << "NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib);
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800186
187 entry = insert (end (),
188 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800189 header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime
190 : header.GetInterestLifetime (),
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800191 *fibEntry));
192
193 // isDuplicate = false; // redundant
194 // isNew = true; // also redundant
195 }
196 else
197 {
198 isNew = false;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800199 isDuplicate = entry->IsNonceSeen (header.GetNonce ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800200 }
201
202 if (!isDuplicate)
203 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800204 modify (entry,
205 boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800206 }
207
208 return make_tuple (cref(*entry), isNew, isDuplicate);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700209}
210
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800211std::ostream& operator<< (std::ostream& os, const CcnxPit &pit)
212{
213 BOOST_FOREACH (const CcnxPitEntry &entry, pit)
214 {
215 os << entry;
216 }
217
218 return os;
219}
220
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700221} // namespace ns3