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