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 | |
| 21 | #include "ccnx-pit-impl.h" |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 22 | |
| 23 | #include "ns3/ccnx-interest-header.h" |
| 24 | #include "ns3/ccnx-content-object-header.h" |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 25 | #include "ns3/ccnx-forwarding-strategy.h" |
Alexander Afanasyev | e3d126f | 2012-07-16 17:07:31 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 27 | #include "ns3/log.h" |
| 28 | #include "ns3/string.h" |
| 29 | #include "ns3/uinteger.h" |
| 30 | #include "ns3/simulator.h" |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 31 | |
| 32 | #include <boost/lambda/bind.hpp> |
| 33 | #include <boost/lambda/lambda.hpp> |
| 34 | |
| 35 | NS_LOG_COMPONENT_DEFINE ("CcnxPitImpl"); |
| 36 | |
| 37 | using namespace boost::tuples; |
| 38 | using namespace boost; |
| 39 | namespace ll = boost::lambda; |
| 40 | |
| 41 | namespace ns3 { |
| 42 | |
| 43 | NS_OBJECT_ENSURE_REGISTERED (CcnxPitImpl); |
| 44 | |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 45 | |
| 46 | // CcnxPitEntryImpl::CcnxPitEntryImpl (CcnxPit &pit, |
| 47 | // Ptr<const CcnxInterestHeader> header, |
| 48 | // Ptr<CcnxFibEntry> fibEntry) |
| 49 | // : CcnxPitEntry (pit, header, fibEntry) |
| 50 | // , item_ (0) |
| 51 | // { |
| 52 | // static_cast<CcnxPitImpl&> (m_container).i_time.insert (*this); |
| 53 | // } |
| 54 | |
| 55 | // CcnxPitEntryImpl::~CcnxPitEntryImpl () |
| 56 | // { |
| 57 | // static_cast<CcnxPitImpl&> (m_container).i_time.erase (*this); |
| 58 | // } |
| 59 | |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 60 | TypeId |
| 61 | CcnxPitImpl::GetTypeId () |
| 62 | { |
| 63 | static TypeId tid = TypeId ("ns3::CcnxPit") |
| 64 | .SetGroupName ("Ccnx") |
| 65 | .SetParent<CcnxPit> () |
| 66 | .AddConstructor<CcnxPitImpl> () |
| 67 | .AddAttribute ("MaxSize", |
| 68 | "Set maximum number of entries in PIT. If 0, limit is not enforced", |
| 69 | StringValue ("0"), |
| 70 | MakeUintegerAccessor (&CcnxPitImpl::GetMaxSize, &CcnxPitImpl::SetMaxSize), |
| 71 | MakeUintegerChecker<uint32_t> ()) |
| 72 | ; |
| 73 | |
| 74 | return tid; |
| 75 | } |
| 76 | |
| 77 | CcnxPitImpl::CcnxPitImpl () |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | CcnxPitImpl::~CcnxPitImpl () |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | uint32_t |
| 86 | CcnxPitImpl::GetMaxSize () const |
| 87 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 88 | return getPolicy ().get_max_size (); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
| 92 | CcnxPitImpl::SetMaxSize (uint32_t maxSize) |
| 93 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 94 | getPolicy ().set_max_size (maxSize); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
| 98 | CcnxPitImpl::NotifyNewAggregate () |
| 99 | { |
| 100 | if (m_fib == 0) |
| 101 | { |
| 102 | m_fib = GetObject<CcnxFib> (); |
| 103 | } |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 104 | if (m_forwardingStrategy == 0) |
| 105 | { |
| 106 | m_forwardingStrategy = GetObject<CcnxForwardingStrategy> (); |
| 107 | } |
| 108 | |
| 109 | CcnxPit::NotifyNewAggregate (); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
| 113 | CcnxPitImpl::DoDispose () |
| 114 | { |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 115 | super::clear (); |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 116 | |
| 117 | m_forwardingStrategy = 0; |
| 118 | m_fib = 0; |
| 119 | |
| 120 | CcnxPit::DoDispose (); |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void CcnxPitImpl::RescheduleCleaning () |
| 124 | { |
| 125 | m_cleanEvent.Cancel (); |
| 126 | if (i_time.empty ()) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 127 | { |
| 128 | NS_LOG_DEBUG ("No items in PIT"); |
| 129 | return; |
| 130 | } |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 132 | Time nextEvent = i_time.begin ()->GetExpireTime () - Simulator::Now (); |
| 133 | if (nextEvent < 0) nextEvent = Seconds (0); |
| 134 | |
| 135 | NS_LOG_DEBUG ("Schedule next cleaning in " << |
| 136 | nextEvent.ToDouble (Time::S) << "s (at " << |
| 137 | i_time.begin ()->GetExpireTime () << "s abs time"); |
| 138 | m_cleanEvent = Simulator::Schedule (nextEvent, |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 139 | &CcnxPitImpl::CleanExpired, this); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 143 | CcnxPitImpl::CleanExpired () |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 144 | { |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 145 | NS_LOG_LOGIC ("Cleaning PIT. Total: " << i_time.size ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 146 | Time now = Simulator::Now (); |
| 147 | |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 148 | // uint32_t count = 0; |
| 149 | while (!i_time.empty ()) |
| 150 | { |
| 151 | time_index::iterator entry = i_time.begin (); |
| 152 | if (entry->GetExpireTime () <= now) // is the record stale? |
| 153 | { |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 154 | m_forwardingStrategy->WillErasePendingInterest (entry->to_iterator ()->payload ()); |
Alexander Afanasyev | 413c7f1 | 2012-07-10 17:35:16 -0700 | [diff] [blame] | 155 | super::erase (entry->to_iterator ()); |
| 156 | // // count ++; |
| 157 | } |
| 158 | else |
| 159 | break; // nothing else to do. All later records will not be stale |
| 160 | } |
| 161 | |
| 162 | RescheduleCleaning (); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | Ptr<CcnxPitEntry> |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 166 | CcnxPitImpl::Lookup (const CcnxContentObjectHeader &header) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 167 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 168 | /// @todo use predicate to search with exclude filters |
| 169 | super::iterator item = super::longest_prefix_match (header.GetName ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 171 | if (item == super::end ()) |
| 172 | return 0; |
| 173 | else |
| 174 | return item->payload (); // which could also be 0 |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | Ptr<CcnxPitEntry> |
| 178 | CcnxPitImpl::Lookup (const CcnxInterestHeader &header) |
| 179 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 180 | NS_LOG_FUNCTION (header.GetName ()); |
| 181 | NS_ASSERT_MSG (m_fib != 0, "FIB should be set"); |
Alexander Afanasyev | f249a19 | 2012-07-18 16:52:51 -0700 | [diff] [blame] | 182 | NS_ASSERT_MSG (m_forwardingStrategy != 0, "Forwarding strategy should be set"); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 183 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 184 | super::iterator foundItem, lastItem; |
| 185 | bool reachLast; |
| 186 | boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (header.GetName ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 187 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 188 | if (!reachLast || lastItem == super::end ()) |
| 189 | return 0; |
| 190 | else |
| 191 | return lastItem->payload (); // which could also be 0 |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | Ptr<CcnxPitEntry> |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 195 | CcnxPitImpl::Create (Ptr<const CcnxInterestHeader> header) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 196 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 197 | Ptr<CcnxFibEntry> fibEntry = m_fib->LongestPrefixMatch (*header); |
Alexander Afanasyev | 3c5b6a7 | 2012-07-20 15:35:48 -0700 | [diff] [blame] | 198 | if (fibEntry == 0) |
| 199 | return 0; |
| 200 | |
| 201 | // NS_ASSERT_MSG (fibEntry != 0, |
| 202 | // "There should be at least default route set" << |
| 203 | // " Prefix = "<< header->GetName() << ", NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 204 | |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 205 | Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 206 | std::pair< super::iterator, bool > result = super::insert (header->GetName (), newEntry); |
| 207 | if (result.first != super::end ()) |
| 208 | { |
| 209 | if (result.second) |
| 210 | { |
| 211 | newEntry->SetTrie (result.first); |
| 212 | return newEntry; |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | // should we do anything? |
| 217 | // update payload? add new payload? |
| 218 | return result.first->payload (); |
| 219 | } |
| 220 | } |
| 221 | else |
| 222 | return 0; |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
| 226 | void |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 227 | CcnxPitImpl::MarkErased (Ptr<CcnxPitEntry> item) |
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 | // entry->SetExpireTime (Simulator::Now () + m_PitEntryPruningTimout); |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 230 | super::erase (StaticCast< entry > (item)->to_iterator ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | void |
| 235 | CcnxPitImpl::Print (std::ostream& os) const |
| 236 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 237 | // !!! unordered_set imposes "random" order of item in the same level !!! |
| 238 | super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0); |
| 239 | for (; item != end; item++) |
| 240 | { |
| 241 | if (item->payload () == 0) continue; |
| 242 | |
| 243 | os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n"; |
| 244 | } |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 247 | uint32_t |
| 248 | CcnxPitImpl::GetSize () const |
| 249 | { |
| 250 | return super::getPolicy ().size (); |
| 251 | } |
| 252 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 253 | Ptr<CcnxPitEntry> |
| 254 | CcnxPitImpl::Begin () |
| 255 | { |
| 256 | super::parent_trie::recursive_iterator item (super::getTrie ()), end (0); |
| 257 | for (; item != end; item++) |
| 258 | { |
| 259 | if (item->payload () == 0) continue; |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | if (item == end) |
| 264 | return End (); |
| 265 | else |
| 266 | return item->payload (); |
| 267 | } |
| 268 | |
| 269 | Ptr<CcnxPitEntry> |
| 270 | CcnxPitImpl::End () |
| 271 | { |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | Ptr<CcnxPitEntry> |
| 276 | CcnxPitImpl::Next (Ptr<CcnxPitEntry> from) |
| 277 | { |
| 278 | if (from == 0) return 0; |
| 279 | |
| 280 | super::parent_trie::recursive_iterator |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 281 | item (*StaticCast< entry > (from)->to_iterator ()), |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 282 | end (0); |
| 283 | |
| 284 | for (item++; item != end; item++) |
| 285 | { |
| 286 | if (item->payload () == 0) continue; |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | if (item == end) |
| 291 | return End (); |
| 292 | else |
| 293 | return item->payload (); |
| 294 | } |
| 295 | |
| 296 | |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 297 | } // namespace ns3 |