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