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 | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 106 | clear (); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void |
| 110 | CcnxPitImpl::DoCleanExpired () |
| 111 | { |
| 112 | // NS_LOG_LOGIC ("Cleaning PIT. Total: " << size ()); |
| 113 | Time now = Simulator::Now (); |
| 114 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 115 | NS_LOG_ERROR ("Need to be repaired"); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 116 | // // uint32_t count = 0; |
| 117 | // while (!empty ()) |
| 118 | // { |
| 119 | // CcnxPit::index<i_timestamp>::type::iterator entry = get<i_timestamp> ().begin (); |
| 120 | // if (entry->GetExpireTime () <= now) // is the record stale? |
| 121 | // { |
| 122 | // get<i_timestamp> ().erase (entry); |
| 123 | // // count ++; |
| 124 | // } |
| 125 | // else |
| 126 | // break; // nothing else to do. All later records will not be stale |
| 127 | // } |
| 128 | } |
| 129 | |
| 130 | Ptr<CcnxPitEntry> |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 131 | CcnxPitImpl::Lookup (const CcnxContentObjectHeader &header) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 132 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 133 | /// @todo use predicate to search with exclude filters |
| 134 | super::iterator item = super::longest_prefix_match (header.GetName ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 136 | if (item == super::end ()) |
| 137 | return 0; |
| 138 | else |
| 139 | return item->payload (); // which could also be 0 |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | Ptr<CcnxPitEntry> |
| 143 | CcnxPitImpl::Lookup (const CcnxInterestHeader &header) |
| 144 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 145 | NS_LOG_FUNCTION (header.GetName ()); |
| 146 | NS_ASSERT_MSG (m_fib != 0, "FIB should be set"); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 147 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 148 | super::iterator foundItem, lastItem; |
| 149 | bool reachLast; |
| 150 | boost::tie (foundItem, reachLast, lastItem) = super::getTrie ().find (header.GetName ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 151 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 152 | if (!reachLast || lastItem == super::end ()) |
| 153 | return 0; |
| 154 | else |
| 155 | return lastItem->payload (); // which could also be 0 |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | Ptr<CcnxPitEntry> |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 159 | CcnxPitImpl::Create (Ptr<const CcnxInterestHeader> header) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 160 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 161 | Ptr<CcnxFibEntry> fibEntry = m_fib->LongestPrefixMatch (*header); |
| 162 | NS_ASSERT_MSG (fibEntry != 0, |
| 163 | "There should be at least default route set" << |
| 164 | " Prefix = "<< header->GetName() << "NodeID == " << m_fib->GetObject<Node>()->GetId() << "\n" << *m_fib); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 166 | |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame^] | 167 | Ptr< entry > newEntry = ns3::Create< entry > (boost::ref (*this), header, fibEntry); |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 168 | std::pair< super::iterator, bool > result = super::insert (header->GetName (), newEntry); |
| 169 | if (result.first != super::end ()) |
| 170 | { |
| 171 | if (result.second) |
| 172 | { |
| 173 | newEntry->SetTrie (result.first); |
| 174 | return newEntry; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | // should we do anything? |
| 179 | // update payload? add new payload? |
| 180 | return result.first->payload (); |
| 181 | } |
| 182 | } |
| 183 | else |
| 184 | return 0; |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | void |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame^] | 189 | CcnxPitImpl::MarkErased (Ptr<CcnxPitEntry> item) |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 190 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 191 | // entry->SetExpireTime (Simulator::Now () + m_PitEntryPruningTimout); |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame^] | 192 | super::erase (StaticCast< entry > (item)->to_iterator ()); |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
| 196 | void |
| 197 | CcnxPitImpl::Print (std::ostream& os) const |
| 198 | { |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 199 | // !!! unordered_set imposes "random" order of item in the same level !!! |
| 200 | super::parent_trie::const_recursive_iterator item (super::getTrie ()), end (0); |
| 201 | for (; item != end; item++) |
| 202 | { |
| 203 | if (item->payload () == 0) continue; |
| 204 | |
| 205 | os << item->payload ()->GetPrefix () << "\t" << *item->payload () << "\n"; |
| 206 | } |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 209 | Ptr<CcnxPitEntry> |
| 210 | CcnxPitImpl::Begin () |
| 211 | { |
| 212 | super::parent_trie::recursive_iterator item (super::getTrie ()), end (0); |
| 213 | for (; item != end; item++) |
| 214 | { |
| 215 | if (item->payload () == 0) continue; |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | if (item == end) |
| 220 | return End (); |
| 221 | else |
| 222 | return item->payload (); |
| 223 | } |
| 224 | |
| 225 | Ptr<CcnxPitEntry> |
| 226 | CcnxPitImpl::End () |
| 227 | { |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | Ptr<CcnxPitEntry> |
| 232 | CcnxPitImpl::Next (Ptr<CcnxPitEntry> from) |
| 233 | { |
| 234 | if (from == 0) return 0; |
| 235 | |
| 236 | super::parent_trie::recursive_iterator |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame^] | 237 | item (*StaticCast< entry > (from)->to_iterator ()), |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 238 | end (0); |
| 239 | |
| 240 | for (item++; item != end; item++) |
| 241 | { |
| 242 | if (item->payload () == 0) continue; |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | if (item == end) |
| 247 | return End (); |
| 248 | else |
| 249 | return item->payload (); |
| 250 | } |
| 251 | |
| 252 | |
Alexander Afanasyev | 11f7bb4 | 2012-07-09 17:06:30 -0700 | [diff] [blame] | 253 | } // namespace ns3 |