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 | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 23 | #include "ns3/ndn-interest-header.h" |
| 24 | #include "ns3/ndn-content-object-header.h" |
| 25 | #include "ns3/ndn-forwarding-strategy.h" |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 27 | #include "../../utils/empty-policy.h" |
| 28 | #include "../../utils/persistent-policy.h" |
| 29 | #include "../../utils/random-policy.h" |
| 30 | #include "../../utils/lru-policy.h" |
| 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 | bd6f3f4 | 2012-07-26 17:50:17 -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"); |
| 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 | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 206 | m_forwardingStrategy->WillErasePendingInterest (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 | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 257 | Ptr<fib::Entry> fibEntry = m_fib->LongestPrefixMatch (*header); |
Alexander Afanasyev | 3c5b6a7 | 2012-07-20 15:35:48 -0700 | [diff] [blame] | 258 | if (fibEntry == 0) |
| 259 | return 0; |
| 260 | |
| 261 | // NS_ASSERT_MSG (fibEntry != 0, |
| 262 | // "There should be at least default route set" << |
| 263 | // " Prefix = "<< header->GetName() << ", NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 264 | |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 265 | Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 266 | std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 267 | if (result.first != super::end ()) |
| 268 | { |
| 269 | if (result.second) |
| 270 | { |
| 271 | newEntry->SetTrie (result.first); |
| 272 | return newEntry; |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | // should we do anything? |
| 277 | // update payload? add new payload? |
| 278 | return result.first->payload (); |
| 279 | } |
| 280 | } |
| 281 | else |
| 282 | return 0; |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 286 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 287 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 288 | PitImpl<Policy>::MarkErased (Ptr<Entry> item) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 289 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 290 | // entry->SetExpireTime (Simulator::Now () + m_PitEntryPruningTimout); |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 291 | super::erase (StaticCast< entry > (item)->to_iterator ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 295 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 296 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 297 | PitImpl<Policy>::Print (std::ostream& os) const |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 298 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 299 | // !!! unordered_set imposes "random" order of item in the same level !!! |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 300 | typename super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 301 | for (; item != end; item++) |
| 302 | { |
| 303 | if (item->payload () == 0) continue; |
| 304 | |
| 305 | os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n"; |
| 306 | } |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 309 | template<class Policy> |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 310 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 311 | PitImpl<Policy>::GetSize () const |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 312 | { |
| 313 | return super::getPolicy ().size (); |
| 314 | } |
| 315 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 316 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 317 | Ptr<Entry> |
| 318 | PitImpl<Policy>::Begin () |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 319 | { |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 320 | typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 321 | for (; item != end; item++) |
| 322 | { |
| 323 | if (item->payload () == 0) continue; |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | if (item == end) |
| 328 | return End (); |
| 329 | else |
| 330 | return item->payload (); |
| 331 | } |
| 332 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 333 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 334 | Ptr<Entry> |
| 335 | PitImpl<Policy>::End () |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 336 | { |
| 337 | return 0; |
| 338 | } |
| 339 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 340 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 341 | Ptr<Entry> |
| 342 | PitImpl<Policy>::Next (Ptr<Entry> from) |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 343 | { |
| 344 | if (from == 0) return 0; |
| 345 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 346 | typename super::parent_trie::recursive_iterator |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 347 | item (*StaticCast< entry > (from)->to_iterator ()), |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 348 | end (0); |
| 349 | |
| 350 | for (item++; item != end; item++) |
| 351 | { |
| 352 | if (item->payload () == 0) continue; |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | if (item == end) |
| 357 | return End (); |
| 358 | else |
| 359 | return item->payload (); |
| 360 | } |
| 361 | |
| 362 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 363 | // explicit instantiation and registering |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 364 | template class PitImpl<persistent_policy_traits>; |
| 365 | template class PitImpl<random_policy_traits>; |
| 366 | template class PitImpl<lru_policy_traits>; |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 367 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 368 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, persistent_policy_traits); |
| 369 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, random_policy_traits); |
| 370 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, lru_policy_traits); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 371 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 372 | } // namespace pit |
| 373 | } // namespace ndn |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 374 | } // namespace ns3 |