Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 30 | NS_LOG_COMPONENT_DEFINE ("CcnxRit"); |
| 31 | |
| 32 | namespace ns3 { |
| 33 | |
| 34 | using namespace __ccnx_private_rit; |
| 35 | |
| 36 | NS_OBJECT_ENSURE_REGISTERED (CcnxRit); |
| 37 | |
| 38 | TypeId |
| 39 | CcnxRit::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 Moiseenko | f194f39 | 2011-10-28 13:13:55 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 60 | // /** |
| 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 | |
| 74 | CcnxRit::CcnxRit( ) |
| 75 | { |
| 76 | } |
| 77 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame^] | 78 | CcnxRit::~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 Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 87 | |
| 88 | void |
| 89 | CcnxRit::SetRitTimeout (const Time &timeout) |
| 90 | { |
| 91 | m_ritTimeout = timeout; |
| 92 | } |
| 93 | |
| 94 | Time |
| 95 | CcnxRit::GetRitTimeout () const |
| 96 | { |
| 97 | return m_ritTimeout; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | CcnxRit::SetCleanupTimeout (const Time &timeout) |
| 102 | { |
| 103 | m_cleanupTimeout = timeout; |
| 104 | if (m_cleanupEvent.IsRunning ()) |
| 105 | m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events |
| 106 | |
| 107 | // schedule even with new timeout |
| 108 | m_cleanupEvent = Simulator::Schedule (Simulator::Now () + m_cleanupTimeout, |
| 109 | &CcnxRit::CleanExpired, this); |
| 110 | } |
| 111 | |
| 112 | Time |
| 113 | CcnxRit::GetCleanupTimeout () const |
| 114 | { |
| 115 | return m_cleanupTimeout; |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | CcnxRit::WasRecentlySatisfied (const CcnxInterestHeader &header) |
| 120 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 121 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame^] | 122 | std::pair<CcnxRitByNonce::type::iterator,CcnxRitByNonce::type::iterator> |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 123 | entries = get<nonce> ().equal_range (header.GetNonce ()); |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 125 | if (entries.first == end ()) |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 126 | return false; |
| 127 | |
| 128 | // check all entries if the name of RIT entry matches the name of interest |
| 129 | for (CcnxRitByNonce::type::iterator it = entries.first; it != entries.second; it++) |
| 130 | { |
| 131 | // NS_LOG_DEBUG (it->m_prefix << " vs " << header.GetName () << " = " << (it->m_prefix == header.GetName ())); |
| 132 | if (it->m_prefix == header.GetName ()) |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | CcnxRit::SetRecentlySatisfied (const CcnxInterestHeader &header) |
| 141 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 142 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 143 | NS_ASSERT_MSG (!WasRecentlySatisfied (header), "Duplicate recent interest should not be added to RIT"); |
| 144 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 145 | get<timestamp> ().push_back ( |
| 146 | CcnxRitEntry(header.GetName (), |
| 147 | header.GetNonce (), |
| 148 | Simulator::Now ()+m_ritTimeout) |
| 149 | ); |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | void CcnxRit::CleanExpired () |
| 154 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 155 | // NS_LOG_LOGIC ("Cleaning RIT"); |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 156 | Time now = Simulator::Now (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 157 | // #ifdef _DEBUG |
| 158 | // uint32_t count = 0; |
| 159 | // #endif |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 161 | while( !empty() ) |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 162 | { |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 163 | if( get<timestamp> ().front ().m_expireTime <= now ) // is the record stale? |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 164 | { |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 165 | get<timestamp> ().pop_front( ); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 166 | // #ifdef _DEBUG |
| 167 | // count++; |
| 168 | // #endif |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 169 | } |
| 170 | else |
| 171 | break; // nothing else to do. All later records will not be stale |
| 172 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 173 | // #ifdef _DEBUG |
| 174 | // NS_LOG_DEBUG (count << " records cleaned"); |
| 175 | // #endif |
Alexander Afanasyev | fccdb9e | 2011-08-22 19:27:14 -0700 | [diff] [blame] | 176 | |
| 177 | // schedule next even |
| 178 | m_cleanupEvent = Simulator::Schedule (Simulator::Now () + m_cleanupTimeout, |
| 179 | &CcnxRit::CleanExpired, this); |
| 180 | } |
| 181 | |
| 182 | } //namespace ns3 |