Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -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 | #ifndef NDN_CONTENT_STORE_IMPL_H_ |
| 22 | #define NDN_CONTENT_STORE_IMPL_H_ |
| 23 | |
| 24 | #include "ndn-content-store.h" |
| 25 | #include "ns3/packet.h" |
Alexander Afanasyev | bd9c18e | 2012-11-19 15:23:41 -0800 | [diff] [blame] | 26 | #include "ns3/ndn-interest.h" |
Alexander Afanasyev | 6eba36f | 2013-08-07 17:42:54 -0700 | [diff] [blame] | 27 | #include "ns3/ndn-data.h" |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 28 | #include <boost/foreach.hpp> |
| 29 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 30 | #include "ns3/log.h" |
| 31 | #include "ns3/uinteger.h" |
| 32 | #include "ns3/string.h" |
| 33 | |
Alexander Afanasyev | 1a2df6a | 2012-08-17 13:21:51 -0700 | [diff] [blame] | 34 | #include "../../utils/trie/trie-with-policy.h" |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 35 | |
| 36 | namespace ns3 { |
| 37 | namespace ndn { |
| 38 | namespace cs { |
| 39 | |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 40 | /** |
| 41 | * @ingroup ndn-cs |
| 42 | * @brief Cache entry implementation with additional references to the base container |
| 43 | */ |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 44 | template<class CS> |
| 45 | class EntryImpl : public Entry |
| 46 | { |
| 47 | public: |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 48 | typedef Entry base_type; |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 50 | public: |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 51 | EntryImpl (Ptr<ContentStore> cs, Ptr<const Data> data) |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 52 | : Entry (cs, data) |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 53 | , item_ (0) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | SetTrie (typename CS::super::iterator item) |
| 59 | { |
| 60 | item_ = item; |
| 61 | } |
| 62 | |
| 63 | typename CS::super::iterator to_iterator () { return item_; } |
| 64 | typename CS::super::const_iterator to_iterator () const { return item_; } |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 66 | private: |
| 67 | typename CS::super::iterator item_; |
| 68 | }; |
| 69 | |
| 70 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 71 | |
Alexander Afanasyev | 7920651 | 2013-07-27 16:49:12 -0700 | [diff] [blame] | 72 | /** |
| 73 | * @ingroup ndn-cs |
| 74 | * @brief Base implementation of NDN content store |
| 75 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 76 | template<class Policy> |
| 77 | class ContentStoreImpl : public ContentStore, |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 78 | protected ndnSIM::trie_with_policy< Name, |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 79 | ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 80 | Policy > |
| 81 | { |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 82 | public: |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 83 | typedef ndnSIM::trie_with_policy< Name, |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 84 | ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >, |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 85 | Policy > super; |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 87 | typedef EntryImpl< ContentStoreImpl< Policy > > entry; |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 89 | static TypeId |
| 90 | GetTypeId (); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 92 | ContentStoreImpl () { }; |
| 93 | virtual ~ContentStoreImpl () { }; |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 95 | // from ContentStore |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 97 | virtual inline Ptr<Data> |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 98 | Lookup (Ptr<const Interest> interest); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 100 | virtual inline bool |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 101 | Add (Ptr<const Data> data); |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 102 | |
| 103 | // virtual bool |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 104 | // Remove (Ptr<Interest> header); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 106 | virtual inline void |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 107 | Print (std::ostream &os) const; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 109 | virtual uint32_t |
| 110 | GetSize () const; |
| 111 | |
| 112 | virtual Ptr<Entry> |
| 113 | Begin (); |
| 114 | |
| 115 | virtual Ptr<Entry> |
| 116 | End (); |
| 117 | |
| 118 | virtual Ptr<Entry> |
| 119 | Next (Ptr<Entry>); |
| 120 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 121 | private: |
| 122 | void |
| 123 | SetMaxSize (uint32_t maxSize); |
| 124 | |
| 125 | uint32_t |
| 126 | GetMaxSize () const; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | static LogComponent g_log; ///< @brief Logging variable |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 130 | |
| 131 | /// @brief trace of for entry additions (fired every time entry is successfully added to the cache): first parameter is pointer to the CS entry |
| 132 | TracedCallback< Ptr<const Entry> > m_didAddEntry; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 135 | ////////////////////////////////////////// |
| 136 | ////////// Implementation //////////////// |
| 137 | ////////////////////////////////////////// |
| 138 | |
| 139 | |
| 140 | template<class Policy> |
| 141 | LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ()); |
| 142 | |
| 143 | |
| 144 | template<class Policy> |
| 145 | TypeId |
| 146 | ContentStoreImpl< Policy >::GetTypeId () |
| 147 | { |
| 148 | static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ()) |
| 149 | .SetGroupName ("Ndn") |
| 150 | .SetParent<ContentStore> () |
| 151 | .AddConstructor< ContentStoreImpl< Policy > > () |
| 152 | .AddAttribute ("MaxSize", |
| 153 | "Set maximum number of entries in ContentStore. If 0, limit is not enforced", |
| 154 | StringValue ("100"), |
| 155 | MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize, |
| 156 | &ContentStoreImpl< Policy >::SetMaxSize), |
| 157 | MakeUintegerChecker<uint32_t> ()) |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 158 | |
| 159 | .AddTraceSource ("DidAddEntry", "Trace fired every time entry is successfully added to the cache", |
| 160 | MakeTraceSourceAccessor (&ContentStoreImpl< Policy >::m_didAddEntry)) |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 161 | ; |
| 162 | |
| 163 | return tid; |
| 164 | } |
| 165 | |
Alexander Afanasyev | eec89ba | 2013-07-19 16:34:30 -0700 | [diff] [blame] | 166 | struct isNotExcluded |
| 167 | { |
| 168 | inline |
| 169 | isNotExcluded (const Exclude &exclude) |
| 170 | : m_exclude (exclude) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | bool |
| 175 | operator () (const name::Component &comp) const |
| 176 | { |
| 177 | return !m_exclude.isExcluded (comp); |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | const Exclude &m_exclude; |
| 182 | }; |
| 183 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 184 | template<class Policy> |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 185 | Ptr<Data> |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 186 | ContentStoreImpl<Policy>::Lookup (Ptr<const Interest> interest) |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 187 | { |
| 188 | NS_LOG_FUNCTION (this << interest->GetName ()); |
| 189 | |
Alexander Afanasyev | eec89ba | 2013-07-19 16:34:30 -0700 | [diff] [blame] | 190 | typename super::const_iterator node; |
| 191 | if (interest->GetExclude () == 0) |
| 192 | { |
| 193 | node = this->deepest_prefix_match (interest->GetName ()); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | node = this->deepest_prefix_match_if_next_level (interest->GetName (), |
| 198 | isNotExcluded (*interest->GetExclude ())); |
| 199 | } |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 200 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 201 | if (node != this->end ()) |
| 202 | { |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 203 | this->m_cacheHitsTrace (interest, node->payload ()->GetData ()); |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 205 | Ptr<Data> copy = Create<Data> (*node->payload ()->GetData ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 206 | ConstCast<Packet> (copy->GetPayload ())->RemoveAllPacketTags (); |
| 207 | return copy; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 208 | } |
| 209 | else |
| 210 | { |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 211 | this->m_cacheMissesTrace (interest); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 212 | return 0; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 213 | } |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 216 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 217 | bool |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 218 | ContentStoreImpl<Policy>::Add (Ptr<const Data> data) |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 219 | { |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 220 | NS_LOG_FUNCTION (this << data->GetName ()); |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 221 | |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 222 | Ptr< entry > newEntry = Create< entry > (this, data); |
| 223 | std::pair< typename super::iterator, bool > result = super::insert (data->GetName (), newEntry); |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 224 | |
| 225 | if (result.first != super::end ()) |
| 226 | { |
| 227 | if (result.second) |
| 228 | { |
| 229 | newEntry->SetTrie (result.first); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 230 | |
| 231 | m_didAddEntry (newEntry); |
| 232 | return true; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | { |
| 236 | // should we do anything? |
| 237 | // update payload? add new payload? |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | else |
| 242 | return false; // cannot insert entry |
| 243 | } |
| 244 | |
| 245 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 246 | void |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 247 | ContentStoreImpl<Policy>::Print (std::ostream &os) const |
| 248 | { |
| 249 | for (typename super::policy_container::const_iterator item = this->getPolicy ().begin (); |
| 250 | item != this->getPolicy ().end (); |
| 251 | item++) |
| 252 | { |
| 253 | os << item->payload ()->GetName () << std::endl; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 258 | void |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 259 | ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize) |
| 260 | { |
| 261 | this->getPolicy ().set_max_size (maxSize); |
| 262 | } |
| 263 | |
| 264 | template<class Policy> |
| 265 | uint32_t |
| 266 | ContentStoreImpl<Policy>::GetMaxSize () const |
| 267 | { |
| 268 | return this->getPolicy ().get_max_size (); |
| 269 | } |
| 270 | |
| 271 | template<class Policy> |
| 272 | uint32_t |
| 273 | ContentStoreImpl<Policy>::GetSize () const |
| 274 | { |
| 275 | return this->getPolicy ().size (); |
| 276 | } |
| 277 | |
| 278 | template<class Policy> |
| 279 | Ptr<Entry> |
| 280 | ContentStoreImpl<Policy>::Begin () |
| 281 | { |
| 282 | typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0); |
| 283 | for (; item != end; item++) |
| 284 | { |
| 285 | if (item->payload () == 0) continue; |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | if (item == end) |
| 290 | return End (); |
| 291 | else |
| 292 | return item->payload (); |
| 293 | } |
| 294 | |
| 295 | template<class Policy> |
| 296 | Ptr<Entry> |
| 297 | ContentStoreImpl<Policy>::End () |
| 298 | { |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | template<class Policy> |
| 303 | Ptr<Entry> |
| 304 | ContentStoreImpl<Policy>::Next (Ptr<Entry> from) |
| 305 | { |
| 306 | if (from == 0) return 0; |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 307 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 308 | typename super::parent_trie::recursive_iterator |
| 309 | item (*StaticCast< entry > (from)->to_iterator ()), |
| 310 | end (0); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 311 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 312 | for (item++; item != end; item++) |
| 313 | { |
| 314 | if (item->payload () == 0) continue; |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | if (item == end) |
| 319 | return End (); |
| 320 | else |
| 321 | return item->payload (); |
| 322 | } |
| 323 | |
| 324 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 325 | } // namespace cs |
| 326 | } // namespace ndn |
| 327 | } // namespace ns3 |
| 328 | |
| 329 | #endif // NDN_CONTENT_STORE_IMPL_H_ |