blob: f51aa33df8d9d6bf086ab0e5ca4bf9188b37e979 [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"
23#include "ns3/simulator.h"
24#include "ccnx-interest-header.h"
25#include "ccnx-content-object-header.h"
26
27NS_LOG_COMPONENT_DEFINE ("CcnxPit");
28
29namespace ns3 {
30
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080031NS_OBJECT_ENSURE_REGISTERED (CcnxPit);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070032
33using namespace __ccnx_private;
34
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070035TypeId
36CcnxPit::GetTypeId ()
37{
38 static TypeId tid = TypeId ("ns3::CcnxPit")
39 .SetGroupName ("Ccnx")
40 .SetParent<Object> ()
41 .AddConstructor<CcnxPit> ()
42 .AddAttribute ("CleanupTimeout",
43 "Timeout defining how frequent RIT should be cleaned up",
44 TimeValue (Seconds (1)),
45 MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout),
46 MakeTimeChecker ())
47 ;
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070048
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070049 return tid;
50}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070051
52CcnxPit::CcnxPit ()
53{
54}
55
Alexander Afanasyevd02a5d62011-11-21 11:01:51 -080056CcnxPit::~CcnxPit ()
57{
58 if (m_cleanupEvent.IsRunning ())
59 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
60
61 clear ();
62}
63
Alexander Afanasyev18252852011-11-21 13:35:31 -080064void
65CcnxPit::NotifyNewAggregate ()
66{
67}
68
69void
70CcnxPit::DoDispose ()
71{
72 if (m_cleanupEvent.IsRunning ())
73 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
74
75 clear ();
76}
77
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070078void
79CcnxPit::SetCleanupTimeout (const Time &timeout)
80{
81 m_cleanupTimeout = timeout;
82 if (m_cleanupEvent.IsRunning ())
83 m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events
84
85 // schedule even with new timeout
86 m_cleanupEvent = Simulator::Schedule (Simulator::Now () + m_cleanupTimeout,
87 &CcnxPit::CleanExpired, this);
88}
89
90Time
91CcnxPit::GetCleanupTimeout () const
92{
93 return m_cleanupTimeout;
94}
95
96void CcnxPit::CleanExpired ()
97{
98 NS_LOG_LOGIC ("Cleaning PIT");
99 Time now = Simulator::Now ();
100
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700101 while( !empty() )
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700102 {
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700103 if( get<i_timestamp> ().front ().GetExpireTime () <= now ) // is the record stale?
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700104 {
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700105 get<i_timestamp> ().pop_front( );
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700106 }
107 else
108 break; // nothing else to do. All later records will not be stale
109 }
110
111 // schedule next even
112 m_cleanupEvent = Simulator::Schedule (Simulator::Now () + m_cleanupTimeout,
113 &CcnxPit::CleanExpired, this);
114}
115
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700116void
117CcnxPit::SetFib (Ptr<CcnxFib> fib)
118{
119 m_fib = fib;
120}
121
Ilya Moiseenko60491402011-10-28 13:10:16 -0700122/*CcnxPitEntryContainer::type::iterator
123CcnxPit::Add (const CcnxInterestHeader &header, CcnxFibEntryContainer::type::iterator fibEntry, Ptr<CcnxFace> face)
124{
125 if( m_bucketsPerFace[face->GetId()]+1.0 >= maxBucketsPerFace[face->GetId()] )
126 {
127 // printf( "DEBUG: bucket overflow. Should not forward anything to interface %d\n", interest.interfaceIndex );
128 return end();
129 }
130
131 CcnxPitEntryContainer::type::iterator entry = insert (end (),
132 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
133 *fibEntry));
134 return entry;
135}*/
136
137
138
139bool
140CcnxPit::TryAddOutgoing(CcnxPitEntryContainer::type::iterator pitEntry, Ptr<CcnxFace> face)
141{
142 NS_LOG_INFO ("Face has " << m_bucketsPerFace[face->GetId()] << " packets with max allowance " << maxBucketsPerFace[face->GetId()]);
143
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800144 if((face->IsLocal() == false)
Ilya Moiseenko60491402011-10-28 13:10:16 -0700145 && (m_bucketsPerFace[face->GetId()]+1.0 >= maxBucketsPerFace[face->GetId()] ))
146 {
Ilya Moiseenkoaa1154b2011-11-16 16:30:11 -0800147 NS_LOG_INFO("********LIMIT**************");
Ilya Moiseenko60491402011-10-28 13:10:16 -0700148 return false;
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800149 }
Ilya Moiseenko60491402011-10-28 13:10:16 -0700150
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800151 m_bucketsPerFace[face->GetId()] = m_bucketsPerFace[face->GetId()] + 1.0;
Ilya Moiseenko60491402011-10-28 13:10:16 -0700152
153 NS_LOG_INFO(this->size());
154 NS_LOG_INFO("before modify");
155 NS_LOG_INFO(pitEntry->GetPrefix());
156 modify (pitEntry, CcnxPitEntry::AddOutgoing(face));
157 NS_LOG_INFO("after modify");
158 return true;
159}
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700160
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700161const CcnxPitEntry&
162CcnxPit::Lookup (const CcnxContentObjectHeader &header) const
163{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700164 NS_LOG_FUNCTION_NOARGS ();
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 Afanasyeva98cdd22011-08-29 17:32:37 -0700170 throw CcnxPitEntryNotFound();
171
172 return *entry;
173}
174
Ilya Moiseenko60491402011-10-28 13:10:16 -0700175CcnxPitEntryContainer::type::iterator
176CcnxPit::Lookup (const CcnxInterestHeader &header, CcnxFibEntryContainer::type::iterator &outFibEntry)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700177{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700178 NS_LOG_FUNCTION_NOARGS ();
179 NS_ASSERT_MSG (m_fib != 0, "FIB should be set");
180
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700181 CcnxPitEntryContainer::type::iterator entry =
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700182 get<i_prefix> ().find (header.GetName ());
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700183
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700184 CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header);
185 if (fibEntry == m_fib->end ())
186 {
187 NS_LOG_WARN ("FIB entry wasn't found. Creating an empty record");
188 fibEntry = m_fib->insert (m_fib->end (), CcnxFibEntry (header.GetName ()));
189 }
190
191 if (entry == end ())
Ilya Moiseenko60491402011-10-28 13:10:16 -0700192 {
193 NS_LOG_INFO("entry == end");
194 NS_LOG_INFO(this->size());
195 entry = insert (end (),
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700196 CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()),
197 *fibEntry));
Ilya Moiseenko60491402011-10-28 13:10:16 -0700198 NS_LOG_INFO(this->size());
199 }
200 outFibEntry = fibEntry;
201 return entry;
202}
203
204void
205CcnxPit::LeakBuckets( )
206{
207 for( PitBucketIterator it=m_bucketsPerFace.begin();
208 it != m_bucketsPerFace.end();
209 it++ )
210 {
211 it->second = std::max( 0.0, it->second - leakSize[it->first] );
212 }
213}
214
215void
216CcnxPit::LeakBucket(Ptr<CcnxFace> face, int amount )
217{
218 m_bucketsPerFace[face->GetId()] = std::max( 0.0, m_bucketsPerFace[face->GetId()] - amount );
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700219}
220
221
222} // namespace ns3