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