blob: e9c1f1bc98ac0ebfe7d0c3d91abbbde5a6d783dd [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
Alexander Afanasyev18252852011-11-21 13:35:31 -080089 clear ();
90}
91
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070092void
93CcnxPit::SetCleanupTimeout (const Time &timeout)
94{
95 m_cleanupTimeout = timeout;
96 if (m_cleanupEvent.IsRunning ())
97 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
98
99 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800100 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700101 &CcnxPit::CleanExpired, this);
102}
103
104Time
105CcnxPit::GetCleanupTimeout () const
106{
107 return m_cleanupTimeout;
108}
109
110void CcnxPit::CleanExpired ()
111{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800112 NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700113 Time now = Simulator::Now ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800114
115 uint32_t count = 0;
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116 while (!empty ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700117 {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 if (get<i_timestamp> ().front ().GetExpireTime () <= now) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700119 {
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800120 get<i_timestamp> ().pop_front ();
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800121 count ++;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700122 }
123 else
124 break; // nothing else to do. All later records will not be stale
125 }
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800126
127 // NS_LOG_LOGIC ("Cleaned " << count << " records. Total: " << size ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700128 // schedule next even
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800129
130 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700131 &CcnxPit::CleanExpired, this);
132}
133
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700134void
135CcnxPit::SetFib (Ptr<CcnxFib> fib)
136{
137 m_fib = fib;
138}
139
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700140const CcnxPitEntry&
141CcnxPit::Lookup (const CcnxContentObjectHeader &header) const
142{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800143 // NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700144
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700145 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700146 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700147
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700148 if (entry == end ())
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700149 throw CcnxPitEntryNotFound();
150
151 return *entry;
152}
153
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800154boost::tuple<const CcnxPitEntry&, bool, bool>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800155CcnxPit::Lookup (const CcnxInterestHeader &header)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700156{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700157 NS_LOG_FUNCTION_NOARGS ();
158 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
159
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800160 bool isDuplicate = false;
161 bool isNew = true;
162
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700163 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700164 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700165
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700166 if (entry == end ())
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800167 {
168 CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header);
Alexander Afanasyev07827182011-12-13 01:07:32 -0800169 NS_ASSERT_MSG (fibEntry != m_fib->m_fib.end (),
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800170 "There should be at least default route set");
171
172 entry = insert (end (),
173 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800174 header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime
175 : header.GetInterestLifetime (),
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800176 *fibEntry));
177
178 // isDuplicate = false; // redundant
179 // isNew = true; // also redundant
180 }
181 else
182 {
183 isNew = false;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800184 isDuplicate = entry->IsNonceSeen (header.GetNonce ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800185 }
186
187 if (!isDuplicate)
188 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800189 modify (entry,
190 boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800191 }
192
193 return make_tuple (cref(*entry), isNew, isDuplicate);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700194}
195
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700196} // namespace ns3