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 | f21bfc1 | 2013-11-17 11:58:33 -0800 | [diff] [blame] | 121 | const typename super::policy_container & |
| 122 | GetPolicy () const { return super::getPolicy (); } |
| 123 | |
| 124 | typename super::policy_container & |
| 125 | GetPolicy () { return super::getPolicy (); } |
| 126 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 127 | private: |
| 128 | void |
| 129 | SetMaxSize (uint32_t maxSize); |
| 130 | |
| 131 | uint32_t |
| 132 | GetMaxSize () const; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 133 | |
| 134 | private: |
| 135 | static LogComponent g_log; ///< @brief Logging variable |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 136 | |
| 137 | /// @brief trace of for entry additions (fired every time entry is successfully added to the cache): first parameter is pointer to the CS entry |
| 138 | TracedCallback< Ptr<const Entry> > m_didAddEntry; |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 141 | ////////////////////////////////////////// |
| 142 | ////////// Implementation //////////////// |
| 143 | ////////////////////////////////////////// |
| 144 | |
| 145 | |
| 146 | template<class Policy> |
| 147 | LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ()); |
| 148 | |
| 149 | |
| 150 | template<class Policy> |
| 151 | TypeId |
| 152 | ContentStoreImpl< Policy >::GetTypeId () |
| 153 | { |
| 154 | static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ()) |
| 155 | .SetGroupName ("Ndn") |
| 156 | .SetParent<ContentStore> () |
| 157 | .AddConstructor< ContentStoreImpl< Policy > > () |
| 158 | .AddAttribute ("MaxSize", |
| 159 | "Set maximum number of entries in ContentStore. If 0, limit is not enforced", |
| 160 | StringValue ("100"), |
| 161 | MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize, |
| 162 | &ContentStoreImpl< Policy >::SetMaxSize), |
| 163 | MakeUintegerChecker<uint32_t> ()) |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 164 | |
| 165 | .AddTraceSource ("DidAddEntry", "Trace fired every time entry is successfully added to the cache", |
| 166 | MakeTraceSourceAccessor (&ContentStoreImpl< Policy >::m_didAddEntry)) |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 167 | ; |
| 168 | |
| 169 | return tid; |
| 170 | } |
| 171 | |
Alexander Afanasyev | eec89ba | 2013-07-19 16:34:30 -0700 | [diff] [blame] | 172 | struct isNotExcluded |
| 173 | { |
| 174 | inline |
| 175 | isNotExcluded (const Exclude &exclude) |
| 176 | : m_exclude (exclude) |
| 177 | { |
| 178 | } |
| 179 | |
| 180 | bool |
| 181 | operator () (const name::Component &comp) const |
| 182 | { |
| 183 | return !m_exclude.isExcluded (comp); |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | const Exclude &m_exclude; |
| 188 | }; |
| 189 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 190 | template<class Policy> |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 191 | Ptr<Data> |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 192 | ContentStoreImpl<Policy>::Lookup (Ptr<const Interest> interest) |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 193 | { |
| 194 | NS_LOG_FUNCTION (this << interest->GetName ()); |
| 195 | |
Alexander Afanasyev | eec89ba | 2013-07-19 16:34:30 -0700 | [diff] [blame] | 196 | typename super::const_iterator node; |
| 197 | if (interest->GetExclude () == 0) |
| 198 | { |
| 199 | node = this->deepest_prefix_match (interest->GetName ()); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | node = this->deepest_prefix_match_if_next_level (interest->GetName (), |
| 204 | isNotExcluded (*interest->GetExclude ())); |
| 205 | } |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 207 | if (node != this->end ()) |
| 208 | { |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 209 | this->m_cacheHitsTrace (interest, node->payload ()->GetData ()); |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 210 | |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 211 | Ptr<Data> copy = Create<Data> (*node->payload ()->GetData ()); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 212 | ConstCast<Packet> (copy->GetPayload ())->RemoveAllPacketTags (); |
| 213 | return copy; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 214 | } |
| 215 | else |
| 216 | { |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 217 | this->m_cacheMissesTrace (interest); |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 218 | return 0; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 219 | } |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 222 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 223 | bool |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 224 | ContentStoreImpl<Policy>::Add (Ptr<const Data> data) |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 225 | { |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 226 | NS_LOG_FUNCTION (this << data->GetName ()); |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 227 | |
Alexander Afanasyev | b989b12 | 2013-07-10 17:15:46 -0700 | [diff] [blame] | 228 | Ptr< entry > newEntry = Create< entry > (this, data); |
| 229 | std::pair< typename super::iterator, bool > result = super::insert (data->GetName (), newEntry); |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 230 | |
| 231 | if (result.first != super::end ()) |
| 232 | { |
| 233 | if (result.second) |
| 234 | { |
| 235 | newEntry->SetTrie (result.first); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 236 | |
| 237 | m_didAddEntry (newEntry); |
| 238 | return true; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | { |
| 242 | // should we do anything? |
| 243 | // update payload? add new payload? |
| 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | else |
| 248 | return false; // cannot insert entry |
| 249 | } |
| 250 | |
| 251 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 252 | void |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 253 | ContentStoreImpl<Policy>::Print (std::ostream &os) const |
| 254 | { |
| 255 | for (typename super::policy_container::const_iterator item = this->getPolicy ().begin (); |
| 256 | item != this->getPolicy ().end (); |
| 257 | item++) |
| 258 | { |
| 259 | os << item->payload ()->GetName () << std::endl; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 264 | void |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 265 | ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize) |
| 266 | { |
| 267 | this->getPolicy ().set_max_size (maxSize); |
| 268 | } |
| 269 | |
| 270 | template<class Policy> |
| 271 | uint32_t |
| 272 | ContentStoreImpl<Policy>::GetMaxSize () const |
| 273 | { |
| 274 | return this->getPolicy ().get_max_size (); |
| 275 | } |
| 276 | |
| 277 | template<class Policy> |
| 278 | uint32_t |
| 279 | ContentStoreImpl<Policy>::GetSize () const |
| 280 | { |
| 281 | return this->getPolicy ().size (); |
| 282 | } |
| 283 | |
| 284 | template<class Policy> |
| 285 | Ptr<Entry> |
| 286 | ContentStoreImpl<Policy>::Begin () |
| 287 | { |
| 288 | typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0); |
| 289 | for (; item != end; item++) |
| 290 | { |
| 291 | if (item->payload () == 0) continue; |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | if (item == end) |
| 296 | return End (); |
| 297 | else |
| 298 | return item->payload (); |
| 299 | } |
| 300 | |
| 301 | template<class Policy> |
| 302 | Ptr<Entry> |
| 303 | ContentStoreImpl<Policy>::End () |
| 304 | { |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | template<class Policy> |
| 309 | Ptr<Entry> |
| 310 | ContentStoreImpl<Policy>::Next (Ptr<Entry> from) |
| 311 | { |
| 312 | if (from == 0) return 0; |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 313 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 314 | typename super::parent_trie::recursive_iterator |
| 315 | item (*StaticCast< entry > (from)->to_iterator ()), |
| 316 | end (0); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 317 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 318 | for (item++; item != end; item++) |
| 319 | { |
| 320 | if (item->payload () == 0) continue; |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | if (item == end) |
| 325 | return End (); |
| 326 | else |
| 327 | return item->payload (); |
| 328 | } |
| 329 | |
| 330 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 331 | } // namespace cs |
| 332 | } // namespace ndn |
| 333 | } // namespace ns3 |
| 334 | |
| 335 | #endif // NDN_CONTENT_STORE_IMPL_H_ |