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 | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 47 | EntryImpl (Ptr<ContentStore> cs, Ptr<const ContentObject> header, Ptr<const Packet> packet) |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 48 | : Entry (cs, header, packet) |
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 | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 89 | virtual inline boost::tuple<Ptr<Packet>, Ptr<const ContentObject>, Ptr<const Packet> > |
| 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 | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 93 | Add (Ptr<const ContentObject> header, Ptr<const Packet> packet); |
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 | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 159 | boost::tuple<Ptr<Packet>, Ptr<const ContentObject>, Ptr<const Packet> > |
| 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 | { |
| 169 | this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ()); |
| 170 | |
| 171 | // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ()); |
| 172 | return boost::make_tuple (node->payload ()->GetFullyFormedNdnPacket (), |
| 173 | node->payload ()->GetHeader (), |
| 174 | node->payload ()->GetPacket ()); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | // NS_LOG_DEBUG ("cache miss for " << interest->GetName ()); |
| 179 | this->m_cacheMissesTrace (interest); |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 180 | return boost::tuple<Ptr<Packet>, Ptr<ContentObject>, Ptr<Packet> > (0, 0, 0); |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 181 | } |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 184 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 185 | bool |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 186 | ContentStoreImpl<Policy>::Add (Ptr<const ContentObject> header, Ptr<const Packet> packet) |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 187 | { |
| 188 | NS_LOG_FUNCTION (this << header->GetName ()); |
| 189 | |
Alexander Afanasyev | 0e4ae8e | 2013-03-12 15:59:18 -0700 | [diff] [blame] | 190 | Ptr< entry > newEntry = Create< entry > (this, header, packet); |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 191 | std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry); |
| 192 | |
| 193 | if (result.first != super::end ()) |
| 194 | { |
| 195 | if (result.second) |
| 196 | { |
| 197 | newEntry->SetTrie (result.first); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 198 | |
| 199 | m_didAddEntry (newEntry); |
| 200 | return true; |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 201 | } |
| 202 | else |
| 203 | { |
| 204 | // should we do anything? |
| 205 | // update payload? add new payload? |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | else |
| 210 | return false; // cannot insert entry |
| 211 | } |
| 212 | |
| 213 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 214 | void |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 215 | ContentStoreImpl<Policy>::Print (std::ostream &os) const |
| 216 | { |
| 217 | for (typename super::policy_container::const_iterator item = this->getPolicy ().begin (); |
| 218 | item != this->getPolicy ().end (); |
| 219 | item++) |
| 220 | { |
| 221 | os << item->payload ()->GetName () << std::endl; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | template<class Policy> |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 226 | void |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 227 | ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize) |
| 228 | { |
| 229 | this->getPolicy ().set_max_size (maxSize); |
| 230 | } |
| 231 | |
| 232 | template<class Policy> |
| 233 | uint32_t |
| 234 | ContentStoreImpl<Policy>::GetMaxSize () const |
| 235 | { |
| 236 | return this->getPolicy ().get_max_size (); |
| 237 | } |
| 238 | |
| 239 | template<class Policy> |
| 240 | uint32_t |
| 241 | ContentStoreImpl<Policy>::GetSize () const |
| 242 | { |
| 243 | return this->getPolicy ().size (); |
| 244 | } |
| 245 | |
| 246 | template<class Policy> |
| 247 | Ptr<Entry> |
| 248 | ContentStoreImpl<Policy>::Begin () |
| 249 | { |
| 250 | typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0); |
| 251 | for (; 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 | template<class Policy> |
| 264 | Ptr<Entry> |
| 265 | ContentStoreImpl<Policy>::End () |
| 266 | { |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | template<class Policy> |
| 271 | Ptr<Entry> |
| 272 | ContentStoreImpl<Policy>::Next (Ptr<Entry> from) |
| 273 | { |
| 274 | if (from == 0) return 0; |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 275 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 276 | typename super::parent_trie::recursive_iterator |
| 277 | item (*StaticCast< entry > (from)->to_iterator ()), |
| 278 | end (0); |
Alexander Afanasyev | 25093a3 | 2013-02-01 13:00:13 -0800 | [diff] [blame] | 279 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 280 | for (item++; item != end; item++) |
| 281 | { |
| 282 | if (item->payload () == 0) continue; |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | if (item == end) |
| 287 | return End (); |
| 288 | else |
| 289 | return item->payload (); |
| 290 | } |
| 291 | |
| 292 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 293 | } // namespace cs |
| 294 | } // namespace ndn |
| 295 | } // namespace ns3 |
| 296 | |
| 297 | #endif // NDN_CONTENT_STORE_IMPL_H_ |