blob: 63e1db8b8ff7a3d4bf5629c9c5b4ac9424d09ea7 [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 Afanasyeva46844b2011-11-21 19:13:26 -080054 .AddAttribute ("PitEntryPruningTimout",
55 "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again",
56 StringValue ("100ms"),
57 MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout),
58 MakeTimeChecker ())
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080059 .AddAttribute ("PitEntryDefaultLifetime",
60 "Default lifetime of PIT entry (aka default Interest lifetime)",
61 StringValue("4s"),
62 MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime),
63 MakeTimeChecker ())
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070064 ;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070065
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070066 return tid;
67}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070068
69CcnxPit::CcnxPit ()
70{
71}
72
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080073CcnxPit::~CcnxPit ()
74{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080075 DoDispose ();
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080076}
77
Alexander Afanasyev18252852011-11-21 13:35:31 -080078void
79CcnxPit::NotifyNewAggregate ()
80{
81}
82
83void
84CcnxPit::DoDispose ()
85{
86 if (m_cleanupEvent.IsRunning ())
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080087 m_cleanupEvent.Cancel ();
88
89 if (m_PitBucketLeakEvent.IsRunning ())
90 m_PitBucketLeakEvent.Cancel ();
Alexander Afanasyev18252852011-11-21 13:35:31 -080091
92 clear ();
93}
94
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070095void
96CcnxPit::SetCleanupTimeout (const Time &timeout)
97{
98 m_cleanupTimeout = timeout;
99 if (m_cleanupEvent.IsRunning ())
100 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
101
102 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800103 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700104 &CcnxPit::CleanExpired, this);
105}
106
107Time
108CcnxPit::GetCleanupTimeout () const
109{
110 return m_cleanupTimeout;
111}
112
113void CcnxPit::CleanExpired ()
114{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800115 NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700116 Time now = Simulator::Now ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800117
118 uint32_t count = 0;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800119 while (!empty ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700120 {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800121 if (get<i_timestamp> ().front ().GetExpireTime () <= now) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700122 {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800123 get<i_timestamp> ().pop_front ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800124 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 Afanasyeva98cdd22011-08-29 17:32:37 -0700148 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700149 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700150
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700151 if (entry == end ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700152 throw CcnxPitEntryNotFound();
153
154 return *entry;
155}
156
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800157boost::tuple<const CcnxPitEntry&, bool, bool>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800158CcnxPit::Lookup (const CcnxInterestHeader &header)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700159{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700160 NS_LOG_FUNCTION_NOARGS ();
161 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
162
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800163 bool isDuplicate = false;
164 bool isNew = true;
165
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700166 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700167 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700168
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700169 if (entry == end ())
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800170 {
171 CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header);
172 NS_ASSERT_MSG (fibEntry != m_fib->end (),
173 "There should be at least default route set");
174
175 entry = insert (end (),
176 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
177 Simulator::Now () +
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800178 (header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime
179 : header.GetInterestLifetime ()),
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800180 *fibEntry));
181
182 // isDuplicate = false; // redundant
183 // isNew = true; // also redundant
184 }
185 else
186 {
187 isNew = false;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800188 isDuplicate = entry->IsNonceSeen (header.GetNonce ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800189 }
190
191 if (!isDuplicate)
192 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800193 modify (entry,
194 boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800195 }
196
197 return make_tuple (cref(*entry), isNew, isDuplicate);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700198}
199
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700200} // namespace ns3