blob: 2a1b7f01879473d6891ea249e20cc5ba7748b319 [file] [log] [blame]
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -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-rit.h"
22
23#include "ns3/log.h"
24#include "ns3/simulator.h"
25#include "ns3/ccnx-interest-header.h"
26#include "ns3/assert.h"
27
28#include <utility>
29
30NS_LOG_COMPONENT_DEFINE ("CcnxRit");
31
32namespace ns3 {
33
34using namespace __ccnx_private_rit;
35
36NS_OBJECT_ENSURE_REGISTERED (CcnxRit);
37
38TypeId
39CcnxRit::GetTypeId (void)
40{
41 static TypeId tid = TypeId ("ns3::CcnxRit")
42 .SetGroupName ("Ccnx")
43 .SetParent<Object> ()
44 .AddConstructor<CcnxRit> ()
45 .AddAttribute ("RitTimeout",
46 "Timeout defining how long records should be kept in RIT",
47 TimeValue (Seconds (1)),
48 MakeTimeAccessor (&CcnxRit::GetRitTimeout, &CcnxRit::SetRitTimeout),
49 MakeTimeChecker ())
50 .AddAttribute ("CleanupTimeout",
51 "Timeout defining how frequent RIT should be cleaned up",
52 TimeValue (Seconds (1)),
53 MakeTimeAccessor (&CcnxRit::GetCleanupTimeout, &CcnxRit::SetCleanupTimeout),
54 MakeTimeChecker ())
55 ;
56
57 return tid;
58}
Ilya Moiseenkof194f392011-10-28 13:13:55 -070059
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070060// /**
61// * \ingroup ccnx
62// * \brief Typedef for prefix hash index of RIT container
63// */
64// struct CcnxRitByTimestamp
65// {
66// typedef
67// CcnxRitContainer::type::index<timestamp>::type
68// type;
69// };
70
71//////////////////////////////////////////////////////////////////////
72
73
74CcnxRit::CcnxRit( )
75{
76}
77
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080078CcnxRit::~CcnxRit( )
79{
80 NS_LOG_FUNCTION_NOARGS ();
81
82 if (m_cleanupEvent.IsRunning ())
83 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
84
85 clear ();
86}
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -070087
88void
Alexander Afanasyev18252852011-11-21 13:35:31 -080089CcnxRit::NotifyNewAggregate ()
90{
91}
92
93void
94CcnxRit::DoDispose ()
95{
96 if (m_cleanupEvent.IsRunning ())
97 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
98
99 clear ();
100}
101
102void
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700103CcnxRit::SetRitTimeout (const Time &timeout)
104{
105 m_ritTimeout = timeout;
106}
107
108Time
109CcnxRit::GetRitTimeout () const
110{
111 return m_ritTimeout;
112}
113
114void
115CcnxRit::SetCleanupTimeout (const Time &timeout)
116{
117 m_cleanupTimeout = timeout;
118 if (m_cleanupEvent.IsRunning ())
119 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
120
121 // schedule even with new timeout
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800122 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700123 &CcnxRit::CleanExpired, this);
124}
125
126Time
127CcnxRit::GetCleanupTimeout () const
128{
129 return m_cleanupTimeout;
130}
131
132bool
133CcnxRit::WasRecentlySatisfied (const CcnxInterestHeader &header)
134{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800135 // NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -0800136 std::pair<CcnxRitByNonce::type::iterator,CcnxRitByNonce::type::iterator>
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700137 entries = get<nonce> ().equal_range (header.GetNonce ());
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700138
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700139 if (entries.first == end ())
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700140 return false;
141
142 // check all entries if the name of RIT entry matches the name of interest
143 for (CcnxRitByNonce::type::iterator it = entries.first; it != entries.second; it++)
144 {
145 // NS_LOG_DEBUG (it->m_prefix << " vs " << header.GetName () << " = " << (it->m_prefix == header.GetName ()));
146 if (it->m_prefix == header.GetName ())
147 return true;
148 }
149
150 return false;
151}
152
153void
154CcnxRit::SetRecentlySatisfied (const CcnxInterestHeader &header)
155{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800156 // NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700157 NS_ASSERT_MSG (!WasRecentlySatisfied (header), "Duplicate recent interest should not be added to RIT");
158
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700159 get<timestamp> ().push_back (
160 CcnxRitEntry(header.GetName (),
161 header.GetNonce (),
162 Simulator::Now ()+m_ritTimeout)
163 );
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700164}
165
166
167void CcnxRit::CleanExpired ()
168{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800169 NS_LOG_LOGIC ("Cleaning RIT, total: " << size ());
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700170 Time now = Simulator::Now ();
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700171
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700172 while( !empty() )
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700173 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700174 if( get<timestamp> ().front ().m_expireTime <= now ) // is the record stale?
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700175 {
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700176 get<timestamp> ().pop_front( );
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700177 }
178 else
179 break; // nothing else to do. All later records will not be stale
180 }
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700181
182 // schedule next even
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800183 m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout,
Alexander Afanasyevfccdb9e2011-08-22 19:27:14 -0700184 &CcnxRit::CleanExpired, this);
185}
186
187} //namespace ns3