Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -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-pit.h" |
| 22 | #include "ns3/log.h" |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 23 | #include "ns3/string.h" |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 24 | #include "ns3/simulator.h" |
| 25 | #include "ccnx-interest-header.h" |
| 26 | #include "ccnx-content-object-header.h" |
| 27 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 28 | #include <boost/bind.hpp> |
| 29 | #include <boost/lambda/lambda.hpp> |
| 30 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 31 | NS_LOG_COMPONENT_DEFINE ("CcnxPit"); |
| 32 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 33 | using namespace boost::tuples; |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 34 | using namespace boost; |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 36 | namespace ns3 { |
| 37 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 38 | NS_OBJECT_ENSURE_REGISTERED (CcnxPit); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 39 | |
| 40 | using namespace __ccnx_private; |
| 41 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 42 | TypeId |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 43 | CcnxPit::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 Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 51 | StringValue ("1s"), |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 52 | MakeTimeAccessor (&CcnxPit::GetCleanupTimeout, &CcnxPit::SetCleanupTimeout), |
| 53 | MakeTimeChecker ()) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 54 | .AddAttribute ("PitEntryPruningTimout", |
| 55 | "Timeout for PIT entry to live after being satisfied. To make sure recently satisfied interest will not be satisfied again", |
| 56 | StringValue ("100ms"), |
| 57 | MakeTimeAccessor (&CcnxPit::m_PitEntryPruningTimout), |
| 58 | MakeTimeChecker ()) |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 59 | .AddAttribute ("PitEntryDefaultLifetime", |
| 60 | "Default lifetime of PIT entry (aka default Interest lifetime)", |
| 61 | StringValue("4s"), |
| 62 | MakeTimeAccessor (&CcnxPit::m_PitEntryDefaultLifetime), |
| 63 | MakeTimeChecker ()) |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 64 | ; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 66 | return tid; |
| 67 | } |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 68 | |
| 69 | CcnxPit::CcnxPit () |
| 70 | { |
| 71 | } |
| 72 | |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 73 | CcnxPit::~CcnxPit () |
| 74 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 75 | DoDispose (); |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 78 | void |
| 79 | CcnxPit::NotifyNewAggregate () |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | CcnxPit::DoDispose () |
| 85 | { |
| 86 | if (m_cleanupEvent.IsRunning ()) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 87 | m_cleanupEvent.Cancel (); |
| 88 | |
Alexander Afanasyev | 1825285 | 2011-11-21 13:35:31 -0800 | [diff] [blame] | 89 | clear (); |
| 90 | } |
| 91 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 92 | void |
| 93 | CcnxPit::SetCleanupTimeout (const Time &timeout) |
| 94 | { |
| 95 | m_cleanupTimeout = timeout; |
| 96 | if (m_cleanupEvent.IsRunning ()) |
| 97 | m_cleanupEvent.Cancel (); // cancel any scheduled cleanup events |
| 98 | |
| 99 | // schedule even with new timeout |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 100 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 101 | &CcnxPit::CleanExpired, this); |
| 102 | } |
| 103 | |
| 104 | Time |
| 105 | CcnxPit::GetCleanupTimeout () const |
| 106 | { |
| 107 | return m_cleanupTimeout; |
| 108 | } |
| 109 | |
| 110 | void CcnxPit::CleanExpired () |
| 111 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 112 | NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 113 | Time now = Simulator::Now (); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 114 | |
| 115 | uint32_t count = 0; |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 116 | while (!empty ()) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 117 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 118 | if (get<i_timestamp> ().front ().GetExpireTime () <= now) // is the record stale? |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 119 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 120 | get<i_timestamp> ().pop_front (); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 121 | count ++; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | break; // nothing else to do. All later records will not be stale |
| 125 | } |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 126 | |
| 127 | // NS_LOG_LOGIC ("Cleaned " << count << " records. Total: " << size ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 128 | // schedule next even |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 129 | |
| 130 | m_cleanupEvent = Simulator::Schedule (m_cleanupTimeout, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 131 | &CcnxPit::CleanExpired, this); |
| 132 | } |
| 133 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 134 | void |
| 135 | CcnxPit::SetFib (Ptr<CcnxFib> fib) |
| 136 | { |
| 137 | m_fib = fib; |
| 138 | } |
| 139 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 140 | const CcnxPitEntry& |
| 141 | CcnxPit::Lookup (const CcnxContentObjectHeader &header) const |
| 142 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 143 | // NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 145 | CcnxPitEntryContainer::type::iterator entry = |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 146 | get<i_prefix> ().find (header.GetName ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 148 | if (entry == end ()) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 149 | throw CcnxPitEntryNotFound(); |
| 150 | |
| 151 | return *entry; |
| 152 | } |
| 153 | |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 154 | boost::tuple<const CcnxPitEntry&, bool, bool> |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 155 | CcnxPit::Lookup (const CcnxInterestHeader &header) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 156 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 157 | NS_LOG_FUNCTION_NOARGS (); |
| 158 | NS_ASSERT_MSG (m_fib != 0, "FIB should be set"); |
| 159 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 160 | bool isDuplicate = false; |
| 161 | bool isNew = true; |
| 162 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 163 | CcnxPitEntryContainer::type::iterator entry = |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 164 | get<i_prefix> ().find (header.GetName ()); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 166 | if (entry == end ()) |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 167 | { |
| 168 | CcnxFibEntryContainer::type::iterator fibEntry = m_fib->LongestPrefixMatch (header); |
| 169 | NS_ASSERT_MSG (fibEntry != m_fib->end (), |
| 170 | "There should be at least default route set"); |
| 171 | |
| 172 | entry = insert (end (), |
| 173 | CcnxPitEntry (Create<CcnxNameComponents> (header.GetName ()), |
Alexander Afanasyev | 0a61c34 | 2011-12-06 12:48:55 -0800 | [diff] [blame] | 174 | header.GetInterestLifetime ().IsZero ()?m_PitEntryDefaultLifetime |
| 175 | : header.GetInterestLifetime (), |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 176 | *fibEntry)); |
| 177 | |
| 178 | // isDuplicate = false; // redundant |
| 179 | // isNew = true; // also redundant |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | isNew = false; |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 184 | isDuplicate = entry->IsNonceSeen (header.GetNonce ()); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (!isDuplicate) |
| 188 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 189 | modify (entry, |
| 190 | boost::bind(&CcnxPitEntry::AddSeenNonce, boost::lambda::_1, header.GetNonce ())); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return make_tuple (cref(*entry), isNew, isDuplicate); |
Ilya Moiseenko | 6049140 | 2011-10-28 13:10:16 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 196 | } // namespace ns3 |