blob: 3fac6fee8a0afeea56af0c8c251db460fa591d50 [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}
59
60
61//////////////////////////////////////////////////////////////////////
62// Helper classes
63//////////////////////////////////////////////////////////////////////
64/**
65 * \ingroup ccnx
66 * \brief Typedef for nonce hash index of RIT container
67 */
68struct CcnxRitByNonce
69{
70 typedef
71 CcnxRitContainer::type::index<nonce>::type
72 type;
73};
74
75// /**
76// * \ingroup ccnx
77// * \brief Typedef for prefix hash index of RIT container
78// */
79// struct CcnxRitByTimestamp
80// {
81// typedef
82// CcnxRitContainer::type::index<timestamp>::type
83// type;
84// };
85
86//////////////////////////////////////////////////////////////////////
87
88
89CcnxRit::CcnxRit( )
90{
91}
92
93CcnxRit::~CcnxRit( ) { }
94
95void
96CcnxRit::SetRitTimeout (const Time &timeout)
97{
98 m_ritTimeout = timeout;
99}
100
101Time
102CcnxRit::GetRitTimeout () const
103{
104 return m_ritTimeout;
105}
106
107void
108CcnxRit::SetCleanupTimeout (const Time &timeout)
109{
110 m_cleanupTimeout = timeout;
111 if (m_cleanupEvent.IsRunning ())
112 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
113
114 // schedule even with new timeout
115 m_cleanupEvent = Simulator::Schedule (Simulator::Now () + m_cleanupTimeout,
116 &CcnxRit::CleanExpired, this);
117}
118
119Time
120CcnxRit::GetCleanupTimeout () const
121{
122 return m_cleanupTimeout;
123}
124
125bool
126CcnxRit::WasRecentlySatisfied (const CcnxInterestHeader &header)
127{
128 std::pair<CcnxRitByNonce::type::iterator,CcnxRitByNonce::type::iterator>
129 entries = m_rit.get<nonce> ().equal_range (header.GetNonce ());
130
131 if (entries.first == m_rit.end ())
132 return false;
133
134 // check all entries if the name of RIT entry matches the name of interest
135 for (CcnxRitByNonce::type::iterator it = entries.first; it != entries.second; it++)
136 {
137 // NS_LOG_DEBUG (it->m_prefix << " vs " << header.GetName () << " = " << (it->m_prefix == header.GetName ()));
138 if (it->m_prefix == header.GetName ())
139 return true;
140 }
141
142 return false;
143}
144
145void
146CcnxRit::SetRecentlySatisfied (const CcnxInterestHeader &header)
147{
148 NS_ASSERT_MSG (!WasRecentlySatisfied (header), "Duplicate recent interest should not be added to RIT");
149
150 m_rit.get<timestamp> ().push_back (
151 CcnxRitEntry(header.GetName (),
152 header.GetNonce (),
153 Simulator::Now ()+m_ritTimeout)
154 );
155}
156
157
158void CcnxRit::CleanExpired ()
159{
160 NS_LOG_LOGIC ("Cleaning RIT");
161 Time now = Simulator::Now ();
162#ifdef _DEBUG
163 uint32_t count = 0;
164#endif
165
166 while( !m_rit.empty() )
167 {
168 if( m_rit.get<timestamp> ().front ().m_expireTime <= now ) // is the record stale?
169 {
170 m_rit.get<timestamp> ().pop_front( );
171#ifdef _DEBUG
172 count++;
173#endif
174 }
175 else
176 break; // nothing else to do. All later records will not be stale
177 }
178#ifdef _DEBUG
179 NS_LOG_DEBUG (count << " records cleaned");
180#endif
181
182 // schedule next even
183 m_cleanupEvent = Simulator::Schedule (Simulator::Now () + m_cleanupTimeout,
184 &CcnxRit::CleanExpired, this);
185}
186
187} //namespace ns3