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