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 | e5a8b5a | 2013-03-15 15:15:26 -0700 | [diff] [blame] | 216 | PitImpl<Policy>::Find (const Name &prefix) |
| 217 | { |
| 218 | typename super::iterator item = super::find_exact (prefix); |
| 219 | |
| 220 | if (item == super::end ()) |
| 221 | return 0; |
| 222 | else |
| 223 | return item->payload (); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | template<class Policy> |
| 228 | Ptr<Entry> |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 229 | PitImpl<Policy>::Create (Ptr<const Interest> header) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 230 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 231 | NS_LOG_DEBUG (header->GetName ()); |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 232 | Ptr<fib::Entry> fibEntry = m_fib->LongestPrefixMatch (*header); |
Alexander Afanasyev | 3c5b6a7 | 2012-07-20 15:35:48 -0700 | [diff] [blame] | 233 | if (fibEntry == 0) |
| 234 | return 0; |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 235 | |
Alexander Afanasyev | 3c5b6a7 | 2012-07-20 15:35:48 -0700 | [diff] [blame] | 236 | // NS_ASSERT_MSG (fibEntry != 0, |
| 237 | // "There should be at least default route set" << |
| 238 | // " Prefix = "<< header->GetName() << ", NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 239 | |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 240 | Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 241 | std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 242 | if (result.first != super::end ()) |
| 243 | { |
| 244 | if (result.second) |
| 245 | { |
| 246 | newEntry->SetTrie (result.first); |
| 247 | return newEntry; |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | // should we do anything? |
| 252 | // update payload? add new payload? |
| 253 | return result.first->payload (); |
| 254 | } |
| 255 | } |
| 256 | else |
| 257 | return 0; |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 261 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 262 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 263 | PitImpl<Policy>::MarkErased (Ptr<Entry> item) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 264 | { |
Alexander Afanasyev | eec6629 | 2013-02-06 16:23:21 -0800 | [diff] [blame] | 265 | if (this->m_PitEntryPruningTimout.IsZero ()) |
| 266 | { |
| 267 | super::erase (StaticCast< entry > (item)->to_iterator ()); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | item->OffsetLifetime (this->m_PitEntryPruningTimout - item->GetExpireTime () + Simulator::Now ()); |
| 272 | } |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 276 | template<class Policy> |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 277 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 278 | PitImpl<Policy>::Print (std::ostream& os) const |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 279 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 280 | // !!! unordered_set imposes "random" order of item in the same level !!! |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 281 | typename super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 282 | for (; item != end; item++) |
| 283 | { |
| 284 | if (item->payload () == 0) continue; |
| 285 | |
| 286 | os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n"; |
| 287 | } |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 290 | template<class Policy> |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 291 | uint32_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 292 | PitImpl<Policy>::GetSize () const |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 293 | { |
| 294 | return super::getPolicy ().size (); |
| 295 | } |
| 296 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 297 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 298 | Ptr<Entry> |
| 299 | PitImpl<Policy>::Begin () |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 300 | { |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 301 | typename super::parent_trie::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 | break; |
| 306 | } |
| 307 | |
| 308 | if (item == end) |
| 309 | return End (); |
| 310 | else |
| 311 | return item->payload (); |
| 312 | } |
| 313 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 314 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 315 | Ptr<Entry> |
| 316 | PitImpl<Policy>::End () |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 317 | { |
| 318 | return 0; |
| 319 | } |
| 320 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 321 | template<class Policy> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 322 | Ptr<Entry> |
| 323 | PitImpl<Policy>::Next (Ptr<Entry> from) |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 324 | { |
| 325 | if (from == 0) return 0; |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 326 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 327 | typename super::parent_trie::recursive_iterator |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 328 | item (*StaticCast< entry > (from)->to_iterator ()), |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 329 | end (0); |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 330 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 331 | for (item++; item != end; item++) |
| 332 | { |
| 333 | if (item->payload () == 0) continue; |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | if (item == end) |
| 338 | return End (); |
| 339 | else |
| 340 | return item->payload (); |
| 341 | } |
| 342 | |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 343 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 344 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 345 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 346 | |
| 347 | template<> |
| 348 | TypeId |
| 349 | PitImpl<persistent_policy_traits>::GetTypeId () |
| 350 | { |
| 351 | static TypeId tid = TypeId ("ns3::ndn::pit::Persistent") |
| 352 | .SetGroupName ("Ndn") |
| 353 | .SetParent<Pit> () |
| 354 | .AddConstructor< PitImpl< persistent_policy_traits > > () |
| 355 | .AddAttribute ("MaxSize", |
| 356 | "Set maximum number of entries in PIT. If 0, limit is not enforced", |
| 357 | StringValue ("0"), |
| 358 | MakeUintegerAccessor (&PitImpl< persistent_policy_traits >::GetMaxSize, |
| 359 | &PitImpl< persistent_policy_traits >::SetMaxSize), |
| 360 | MakeUintegerChecker<uint32_t> ()) |
| 361 | ; |
| 362 | |
| 363 | return tid; |
| 364 | } |
| 365 | |
| 366 | template<> |
| 367 | TypeId |
| 368 | PitImpl<random_policy_traits>::GetTypeId () |
| 369 | { |
| 370 | static TypeId tid = TypeId ("ns3::ndn::pit::Random") |
| 371 | .SetGroupName ("Ndn") |
| 372 | .SetParent<Pit> () |
| 373 | .AddConstructor< PitImpl< random_policy_traits > > () |
| 374 | .AddAttribute ("MaxSize", |
| 375 | "Set maximum number of entries in PIT. If 0, limit is not enforced", |
| 376 | StringValue ("0"), |
| 377 | MakeUintegerAccessor (&PitImpl< random_policy_traits >::GetMaxSize, |
| 378 | &PitImpl< random_policy_traits >::SetMaxSize), |
| 379 | MakeUintegerChecker<uint32_t> ()) |
| 380 | ; |
| 381 | |
| 382 | return tid; |
| 383 | } |
| 384 | |
| 385 | template<> |
| 386 | TypeId |
| 387 | PitImpl<lru_policy_traits>::GetTypeId () |
| 388 | { |
| 389 | static TypeId tid = TypeId ("ns3::ndn::pit::Lru") |
| 390 | .SetGroupName ("Ndn") |
| 391 | .SetParent<Pit> () |
| 392 | .AddConstructor< PitImpl< lru_policy_traits > > () |
| 393 | .AddAttribute ("MaxSize", |
| 394 | "Set maximum number of entries in PIT. If 0, limit is not enforced", |
| 395 | StringValue ("0"), |
| 396 | MakeUintegerAccessor (&PitImpl< lru_policy_traits >::GetMaxSize, |
| 397 | &PitImpl< lru_policy_traits >::SetMaxSize), |
| 398 | MakeUintegerChecker<uint32_t> ()) |
| 399 | ; |
| 400 | |
| 401 | return tid; |
| 402 | } |
| 403 | |
| 404 | template<> |
| 405 | TypeId |
| 406 | PitImpl<serialized_size_policy_traits>::GetTypeId () |
| 407 | { |
| 408 | static TypeId tid = TypeId ("ns3::ndn::pit::SerializedSize") |
| 409 | .SetGroupName ("Ndn") |
| 410 | .SetParent<Pit> () |
| 411 | .AddConstructor< PitImpl< serialized_size_policy_traits > > () |
| 412 | .AddAttribute ("MaxSize", |
| 413 | "Set maximum size of PIT in bytes. If 0, limit is not enforced", |
| 414 | UintegerValue (0), |
| 415 | MakeUintegerAccessor (&PitImpl< serialized_size_policy_traits >::GetMaxSize, |
| 416 | &PitImpl< serialized_size_policy_traits >::SetMaxSize), |
| 417 | MakeUintegerChecker<uint32_t> ()) |
| 418 | |
| 419 | .AddAttribute ("CurrentSize", "Get current size of PIT in bytes", |
| 420 | TypeId::ATTR_GET, |
| 421 | UintegerValue (0), |
| 422 | MakeUintegerAccessor (&PitImpl< serialized_size_policy_traits >::GetCurrentSize), |
| 423 | MakeUintegerChecker<uint32_t> ()) |
| 424 | |
| 425 | ; |
| 426 | |
| 427 | return tid; |
| 428 | } |
| 429 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 430 | |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 431 | // explicit instantiation and registering |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 432 | template class PitImpl<persistent_policy_traits>; |
| 433 | template class PitImpl<random_policy_traits>; |
| 434 | template class PitImpl<lru_policy_traits>; |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 435 | template class PitImpl<serialized_size_policy_traits>; |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 436 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 437 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, persistent_policy_traits); |
| 438 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, random_policy_traits); |
| 439 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, lru_policy_traits); |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 440 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(PitImpl, serialized_size_policy_traits); |
Alexander Afanasyev | bd6f3f4 | 2012-07-26 17:50:17 -0700 | [diff] [blame] | 441 | |
Alexander Afanasyev | e095f0f | 2012-11-21 17:43:32 -0800 | [diff] [blame] | 442 | #ifdef DOXYGEN |
| 443 | // /** |
| 444 | // * \brief PIT in which new entries will be rejected if PIT size reached its limit |
| 445 | // */ |
| 446 | class Persistent : public PitImpl<persistent_policy_traits> { }; |
| 447 | |
| 448 | /** |
| 449 | * \brief PIT in which PIT reaches its limit, random entry (could be the newly created one) will be removed from PIT |
| 450 | */ |
| 451 | class Random : public PitImpl<random_policy_traits> { }; |
| 452 | |
| 453 | /** |
| 454 | * \brief PIT in which the least recently used entry (the oldest entry with minimum number of incoming faces) |
| 455 | * will be removed when PIT size reached its limit |
| 456 | */ |
| 457 | class Lru : public PitImpl<lru_policy_traits> { }; |
Alexander Afanasyev | 7456b70 | 2013-02-01 22:41:48 -0800 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * @brief A variant of persistent PIT implementation where size of PIT is based on size of interests in bytes (MaxSize parameter) |
| 461 | */ |
| 462 | class SerializedSize : public PitImpl<serialized_size_policy_traits> { }; |
| 463 | |
Alexander Afanasyev | e095f0f | 2012-11-21 17:43:32 -0800 | [diff] [blame] | 464 | #endif |
| 465 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 466 | } // namespace pit |
| 467 | } // namespace ndn |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 468 | } // namespace ns3 |