Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -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 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #include "ndn-pit-impl.h" |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 23 | #include "ns3/ndn-interest.h" |
| 24 | #include "ns3/ndn-content-object.h" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 25 | #include "ns3/ndn-forwarding-strategy.h" |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 1a2df6a | 2012-08-17 13:21:51 -0700 | [diff] [blame] | 27 | #include "../../utils/trie/empty-policy.h" |
| 28 | #include "../../utils/trie/persistent-policy.h" |
| 29 | #include "../../utils/trie/random-policy.h" |
| 30 | #include "../../utils/trie/lru-policy.h" |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 32 | #include "ns3/log.h" |
| 33 | #include "ns3/string.h" |
| 34 | #include "ns3/uinteger.h" |
| 35 | #include "ns3/simulator.h" |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 36 | |
| 37 | #include <boost/lambda/bind.hpp> |
| 38 | #include <boost/lambda/lambda.hpp> |
| 39 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 40 | NS_LOG_COMPONENT_DEFINE ("ndn.pit.PitImpl"); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 41 | |
| 42 | using namespace boost::tuples; |
| 43 | using namespace boost; |
| 44 | namespace ll = boost::lambda; |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 45 | |
| 46 | #define NS_OBJECT_ENSURE_REGISTERED_TEMPL(type, templ) \ |
| 47 | static struct X ## type ## templ ## RegistrationClass \ |
| 48 | { \ |
| 49 | X ## type ## templ ## RegistrationClass () { \ |
| 50 | ns3::TypeId tid = type<templ>::GetTypeId (); \ |
| 51 | tid.GetParent (); \ |
| 52 | } \ |
| 53 | } x_ ## type ## templ ## RegistrationVariable |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 54 | |
| 55 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 56 | namespace ndn { |
| 57 | namespace pit { |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 59 | using namespace ndnSIM; |
| 60 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 61 | template<> |
| 62 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 63 | PitImpl<persistent_policy_traits>::GetTypeId () |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 64 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 65 | static TypeId tid = TypeId ("ns3::ndn::pit::Persistent") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 66 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 67 | .SetParent<Pit> () |
| 68 | .AddConstructor< PitImpl< persistent_policy_traits > > () |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 69 | .AddAttribute ("MaxSize", |
| 70 | "Set maximum number of entries in PIT. If 0, limit is not enforced", |
| 71 | StringValue ("0"), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 72 | MakeUintegerAccessor (&PitImpl< persistent_policy_traits >::GetMaxSize, |
| 73 | &PitImpl< persistent_policy_traits >::SetMaxSize), |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 74 | MakeUintegerChecker<uint32_t> ()) |
| 75 | ; |
| 76 | |
Alexander Afanasyev | 0a0dc39 | 2012-07-27 13:41:49 -0700 | [diff] [blame] | 77 | return tid; |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | template<> |
| 81 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 82 | PitImpl<random_policy_traits>::GetTypeId () |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 83 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 84 | static TypeId tid = TypeId ("ns3::ndn::pit::Random") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 85 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 86 | .SetParent<Pit> () |
| 87 | .AddConstructor< PitImpl< random_policy_traits > > () |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 88 | .AddAttribute ("MaxSize", |
| 89 | "Set maximum number of entries in PIT. If 0, limit is not enforced", |
| 90 | StringValue ("0"), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 91 | MakeUintegerAccessor (&PitImpl< random_policy_traits >::GetMaxSize, |
| 92 | &PitImpl< random_policy_traits >::SetMaxSize), |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 93 | MakeUintegerChecker<uint32_t> ()) |
| 94 | ; |
| 95 | |
Alexander Afanasyev | 0a0dc39 | 2012-07-27 13:41:49 -0700 | [diff] [blame] | 96 | return tid; |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | template<> |
| 100 | TypeId |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 101 | PitImpl<lru_policy_traits>::GetTypeId () |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 102 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 103 | static TypeId tid = TypeId ("ns3::ndn::pit::Lru") |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 104 | .SetGroupName ("Ndn") |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 105 | .SetParent<Pit> () |
| 106 | .AddConstructor< PitImpl< lru_policy_traits > > () |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 107 | .AddAttribute ("MaxSize", |
| 108 | "Set maximum number of entries in PIT. If 0, limit is not enforced", |
| 109 | StringValue ("0"), |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 110 | MakeUintegerAccessor (&PitImpl< lru_policy_traits >::GetMaxSize, |
| 111 | &PitImpl< lru_policy_traits >::SetMaxSize), |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 112 | MakeUintegerChecker<uint32_t> ()) |
| 113 | ; |
| 114 | |
Alexander Afanasyev | 0a0dc39 | 2012-07-27 13:41:49 -0700 | [diff] [blame] | 115 | return tid; |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 116 | } |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 118 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 119 | PitImpl<Policy>::PitImpl () |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 120 | { |
| 121 | } |
| 122 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 123 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 124 | PitImpl<Policy>::~PitImpl () |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 125 | { |
| 126 | } |
| 127 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 128 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 129 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 130 | PitImpl<Policy>::GetMaxSize () const |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 131 | { |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 132 | return super::getPolicy ().get_max_size (); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 135 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 136 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 137 | PitImpl<Policy>::SetMaxSize (uint32_t maxSize) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 138 | { |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 139 | super::getPolicy ().set_max_size (maxSize); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 142 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 143 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 144 | PitImpl<Policy>::NotifyNewAggregate () |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 145 | { |
| 146 | if (m_fib == 0) |
| 147 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 148 | m_fib = GetObject<Fib> (); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 149 | } |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 150 | if (m_forwardingStrategy == 0) |
| 151 | { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 152 | m_forwardingStrategy = GetObject<ForwardingStrategy> (); |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 155 | Pit::NotifyNewAggregate (); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 158 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 159 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 160 | PitImpl<Policy>::DoDispose () |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 161 | { |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 162 | super::clear (); |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 163 | |
| 164 | m_forwardingStrategy = 0; |
| 165 | m_fib = 0; |
| 166 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 167 | Pit::DoDispose (); |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 170 | template<class Policy> |
| 171 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 172 | PitImpl<Policy>::RescheduleCleaning () |
Alexander Afanasyev | b18ae02 | 2012-09-03 21:46:36 -0700 | [diff] [blame] | 173 | { |
| 174 | // m_cleanEvent.Cancel (); |
| 175 | Simulator::Remove (m_cleanEvent); // slower, but better for memory |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 176 | if (i_time.empty ()) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 177 | { |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 178 | // NS_LOG_DEBUG ("No items in PIT"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 179 | return; |
| 180 | } |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 181 | |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 182 | Time nextEvent = i_time.begin ()->GetExpireTime () - Simulator::Now (); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 183 | if (nextEvent <= 0) nextEvent = Seconds (0); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 184 | |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 185 | NS_LOG_DEBUG ("Schedule next cleaning in " << |
| 186 | nextEvent.ToDouble (Time::S) << "s (at " << |
| 187 | i_time.begin ()->GetExpireTime () << "s abs time"); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 188 | |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 189 | m_cleanEvent = Simulator::Schedule (nextEvent, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 190 | &PitImpl<Policy>::CleanExpired, this); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 193 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 194 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 195 | PitImpl<Policy>::CleanExpired () |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 196 | { |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 197 | NS_LOG_LOGIC ("Cleaning PIT. Total: " << i_time.size ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 198 | Time now = Simulator::Now (); |
| 199 | |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 200 | // uint32_t count = 0; |
| 201 | while (!i_time.empty ()) |
| 202 | { |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 203 | typename time_index::iterator entry = i_time.begin (); |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 204 | if (entry->GetExpireTime () <= now) // is the record stale? |
| 205 | { |
Alexander Afanasyev | ea9b3e6 | 2012-08-13 19:02:54 -0700 | [diff] [blame] | 206 | m_forwardingStrategy->WillEraseTimedOutPendingInterest (entry->to_iterator ()->payload ()); |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 207 | super::erase (entry->to_iterator ()); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 208 | // count ++; |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 209 | } |
| 210 | else |
| 211 | break; // nothing else to do. All later records will not be stale |
| 212 | } |
| 213 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 214 | if (super::getPolicy ().size ()) |
| 215 | { |
| 216 | NS_LOG_DEBUG ("Size: " << super::getPolicy ().size ()); |
Alexander Afanasyev | 6b51b08 | 2012-07-27 16:28:34 -0700 | [diff] [blame] | 217 | NS_LOG_DEBUG ("i_time size: " << i_time.size ()); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 218 | } |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 219 | RescheduleCleaning (); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 222 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 223 | Ptr<Entry> |
| 224 | PitImpl<Policy>::Lookup (const ContentObjectHeader &header) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 225 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 226 | /// @todo use predicate to search with exclude filters |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 227 | typename super::iterator item = super::longest_prefix_match (header.GetName ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 228 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 229 | if (item == super::end ()) |
| 230 | return 0; |
| 231 | else |
| 232 | return item->payload (); // which could also be 0 |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 235 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 236 | Ptr<Entry> |
| 237 | PitImpl<Policy>::Lookup (const InterestHeader &header) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 238 | { |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 239 | // NS_LOG_FUNCTION (header.GetName ()); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 240 | NS_ASSERT_MSG (m_fib != 0, "FIB should be set"); |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 241 | NS_ASSERT_MSG (m_forwardingStrategy != 0, "Forwarding strategy should be set"); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 242 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 243 | typename super::iterator foundItem, lastItem; |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 244 | bool reachLast; |
| 245 | boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (header.GetName ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 246 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 247 | if (!reachLast || lastItem == super::end ()) |
| 248 | return 0; |
| 249 | else |
| 250 | return lastItem->payload (); // which could also be 0 |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 253 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 254 | Ptr<Entry> |
| 255 | PitImpl<Policy>::Create (Ptr<const InterestHeader> header) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 256 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 257 | NS_LOG_DEBUG (header->GetName ()); |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 258 | Ptr<fib::Entry> fibEntry = m_fib->LongestPrefixMatch (*header); |
Alexander Afanasyev | 3c5b6a7 | 2012-07-20 15:35:48 -0700 | [diff] [blame] | 259 | if (fibEntry == 0) |
| 260 | return 0; |
| 261 | |
| 262 | // NS_ASSERT_MSG (fibEntry != 0, |
| 263 | // "There should be at least default route set" << |
| 264 | // " Prefix = "<< header->GetName() << ", NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 265 | |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 266 | Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 267 | std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 268 | if (result.first != super::end ()) |
| 269 | { |
| 270 | if (result.second) |
| 271 | { |
| 272 | newEntry->SetTrie (result.first); |
| 273 | return newEntry; |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | // should we do anything? |
| 278 | // update payload? add new payload? |
| 279 | return result.first->payload (); |
| 280 | } |
| 281 | } |
| 282 | else |
| 283 | return 0; |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 287 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 288 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 289 | PitImpl<Policy>::MarkErased (Ptr<Entry> item) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 290 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 291 | // entry->SetExpireTime (Simulator::Now () + m_PitEntryPruningTimout); |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 292 | super::erase (StaticCast< entry > (item)->to_iterator ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 296 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 297 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 298 | PitImpl<Policy>::Print (std::ostream& os) const |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 299 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 300 | // !!! unordered_set imposes "random" order of item in the same level !!! |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 301 | typename super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 302 | for (; item != end; item++) |
| 303 | { |
| 304 | if (item->payload () == 0) continue; |
| 305 | |
| 306 | os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n"; |
| 307 | } |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 310 | template<class Policy> |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 311 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 312 | PitImpl<Policy>::GetSize () const |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 313 | { |
| 314 | return super::getPolicy ().size (); |
| 315 | } |
| 316 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 317 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 318 | Ptr<Entry> |
| 319 | PitImpl<Policy>::Begin () |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 320 | { |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 321 | typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 322 | for (; item != end; item++) |
| 323 | { |
| 324 | if (item->payload () == 0) continue; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | if (item == end) |
| 329 | return End (); |
| 330 | else |
| 331 | return item->payload (); |
| 332 | } |
| 333 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 334 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 335 | Ptr<Entry> |
| 336 | PitImpl<Policy>::End () |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 337 | { |
| 338 | return 0; |
| 339 | } |
| 340 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 341 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 342 | Ptr<Entry> |
| 343 | PitImpl<Policy>::Next (Ptr<Entry> from) |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 344 | { |
| 345 | if (from == 0) return 0; |
| 346 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 347 | typename super::parent_trie::recursive_iterator |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 348 | item (*StaticCast< entry > (from)->to_iterator ()), |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 349 | end (0); |
| 350 | |
| 351 | for (item++; item != end; item++) |
| 352 | { |
| 353 | if (item->payload () == 0) continue; |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | if (item == end) |
| 358 | return End (); |
| 359 | else |
| 360 | return item->payload (); |
| 361 | } |
| 362 | |
| 363 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 364 | // explicit instantiation and registering |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 365 | template class PitImpl<persistent_policy_traits>; |
| 366 | template class PitImpl<random_policy_traits>; |
| 367 | template class PitImpl<lru_policy_traits>; |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 368 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 369 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, persistent_policy_traits); |
| 370 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, random_policy_traits); |
| 371 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, lru_policy_traits); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 372 | |
Alexander Afanasyev | e095f0f | 2012-11-21 17:43:32 -0800 | [diff] [blame^] | 373 | #ifdef DOXYGEN |
| 374 | // /** |
| 375 | // * \brief PIT in which new entries will be rejected if PIT size reached its limit |
| 376 | // */ |
| 377 | class Persistent : public PitImpl<persistent_policy_traits> { }; |
| 378 | |
| 379 | /** |
| 380 | * \brief PIT in which PIT reaches its limit, random entry (could be the newly created one) will be removed from PIT |
| 381 | */ |
| 382 | class Random : public PitImpl<random_policy_traits> { }; |
| 383 | |
| 384 | /** |
| 385 | * \brief PIT in which the least recently used entry (the oldest entry with minimum number of incoming faces) |
| 386 | * will be removed when PIT size reached its limit |
| 387 | */ |
| 388 | class Lru : public PitImpl<lru_policy_traits> { }; |
| 389 | #endif |
| 390 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 391 | } // namespace pit |
| 392 | } // namespace ndn |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 393 | } // namespace ns3 |