blob: ca2c3f322d5431529d05548cbaf3e12c462c69cc [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 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);
Alexander Afanasyev07827182011-12-13 01:07:32 -0800172 NS_ASSERT_MSG (fibEntry != m_fib->m_fib.end (),
Ilya Moiseenkoad9e8ab2012-01-11 19:58:34 -0800173 "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 -0800174
175 entry = insert (end (),
176 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800177 header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime
178 : header.GetInterestLifetime (),
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800179 *fibEntry));
180
181 // isDuplicate = false; // redundant
182 // isNew = true; // also redundant
183 }
184 else
185 {
186 isNew = false;
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800187 isDuplicate = entry->IsNonceSeen (header.GetNonce ());
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800188 }
189
190 if (!isDuplicate)
191 {
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800192 modify (entry,
193 boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ()));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800194 }
195
196 return make_tuple (cref(*entry), isNew, isDuplicate);
Ilya Moiseenko60491402011-10-28 13:10:16 -0700197}
198
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700199} // namespace ns3