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