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; |
| 45 | |
| 46 | public: |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 47 | EntryImpl (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet) |
| 48 | : Entry (header, packet) |
| 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_; } |
| 61 | |
| 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, |
| 70 | protected ndnSIM::trie_with_policy< NameComponents, |
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 | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 75 | typedef ndnSIM::trie_with_policy< NameComponents, |
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; |
| 78 | |
Alexander Afanasyev | 29c19b9 | 2012-09-03 23:46:41 -0700 | [diff] [blame] | 79 | typedef EntryImpl< ContentStoreImpl< Policy > > entry; |
| 80 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 81 | static TypeId |
| 82 | GetTypeId (); |
| 83 | |
| 84 | ContentStoreImpl () { }; |
| 85 | virtual ~ContentStoreImpl () { }; |
| 86 | |
| 87 | // from ContentStore |
| 88 | |
| 89 | virtual inline boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> > |
| 90 | Lookup (Ptr<const InterestHeader> interest); |
| 91 | |
| 92 | virtual inline bool |
| 93 | Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet); |
| 94 | |
| 95 | // virtual bool |
| 96 | // Remove (Ptr<InterestHeader> header); |
| 97 | |
| 98 | virtual inline void |
| 99 | Print (std::ostream &os) const; |
| 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 | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Alexander Afanasyev | 991a0cc | 2012-12-10 11:38:19 -0800 | [diff] [blame] | 124 | ////////////////////////////////////////// |
| 125 | ////////// Implementation //////////////// |
| 126 | ////////////////////////////////////////// |
| 127 | |
| 128 | |
| 129 | template<class Policy> |
| 130 | LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ()); |
| 131 | |
| 132 | |
| 133 | template<class Policy> |
| 134 | TypeId |
| 135 | ContentStoreImpl< Policy >::GetTypeId () |
| 136 | { |
| 137 | static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ()) |
| 138 | .SetGroupName ("Ndn") |
| 139 | .SetParent<ContentStore> () |
| 140 | .AddConstructor< ContentStoreImpl< Policy > > () |
| 141 | .AddAttribute ("MaxSize", |
| 142 | "Set maximum number of entries in ContentStore. If 0, limit is not enforced", |
| 143 | StringValue ("100"), |
| 144 | MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize, |
| 145 | &ContentStoreImpl< Policy >::SetMaxSize), |
| 146 | MakeUintegerChecker<uint32_t> ()) |
| 147 | ; |
| 148 | |
| 149 | return tid; |
| 150 | } |
| 151 | |
| 152 | template<class Policy> |
| 153 | boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> > |
| 154 | ContentStoreImpl<Policy>::Lookup (Ptr<const InterestHeader> interest) |
| 155 | { |
| 156 | NS_LOG_FUNCTION (this << interest->GetName ()); |
| 157 | |
| 158 | /// @todo Change to search with predicate |
| 159 | typename super::const_iterator node = this->deepest_prefix_match (interest->GetName ()); |
| 160 | |
| 161 | if (node != this->end ()) |
| 162 | { |
| 163 | this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ()); |
| 164 | |
| 165 | // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ()); |
| 166 | return boost::make_tuple (node->payload ()->GetFullyFormedNdnPacket (), |
| 167 | node->payload ()->GetHeader (), |
| 168 | node->payload ()->GetPacket ()); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | // NS_LOG_DEBUG ("cache miss for " << interest->GetName ()); |
| 173 | this->m_cacheMissesTrace (interest); |
| 174 | return boost::tuple<Ptr<Packet>, Ptr<ContentObjectHeader>, Ptr<Packet> > (0, 0, 0); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | template<class Policy> |
| 179 | bool |
| 180 | ContentStoreImpl<Policy>::Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet) |
| 181 | { |
| 182 | NS_LOG_FUNCTION (this << header->GetName ()); |
| 183 | |
| 184 | Ptr< entry > newEntry = Create< entry > (header, packet); |
| 185 | std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry); |
| 186 | |
| 187 | if (result.first != super::end ()) |
| 188 | { |
| 189 | if (result.second) |
| 190 | { |
| 191 | newEntry->SetTrie (result.first); |
| 192 | return newEntry; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | // should we do anything? |
| 197 | // update payload? add new payload? |
| 198 | return false; |
| 199 | } |
| 200 | } |
| 201 | else |
| 202 | return false; // cannot insert entry |
| 203 | } |
| 204 | |
| 205 | template<class Policy> |
| 206 | void |
| 207 | ContentStoreImpl<Policy>::Print (std::ostream &os) const |
| 208 | { |
| 209 | for (typename super::policy_container::const_iterator item = this->getPolicy ().begin (); |
| 210 | item != this->getPolicy ().end (); |
| 211 | item++) |
| 212 | { |
| 213 | os << item->payload ()->GetName () << std::endl; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | template<class Policy> |
| 218 | void |
| 219 | ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize) |
| 220 | { |
| 221 | this->getPolicy ().set_max_size (maxSize); |
| 222 | } |
| 223 | |
| 224 | template<class Policy> |
| 225 | uint32_t |
| 226 | ContentStoreImpl<Policy>::GetMaxSize () const |
| 227 | { |
| 228 | return this->getPolicy ().get_max_size (); |
| 229 | } |
| 230 | |
| 231 | template<class Policy> |
| 232 | uint32_t |
| 233 | ContentStoreImpl<Policy>::GetSize () const |
| 234 | { |
| 235 | return this->getPolicy ().size (); |
| 236 | } |
| 237 | |
| 238 | template<class Policy> |
| 239 | Ptr<Entry> |
| 240 | ContentStoreImpl<Policy>::Begin () |
| 241 | { |
| 242 | typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0); |
| 243 | for (; item != end; item++) |
| 244 | { |
| 245 | if (item->payload () == 0) continue; |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | if (item == end) |
| 250 | return End (); |
| 251 | else |
| 252 | return item->payload (); |
| 253 | } |
| 254 | |
| 255 | template<class Policy> |
| 256 | Ptr<Entry> |
| 257 | ContentStoreImpl<Policy>::End () |
| 258 | { |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | template<class Policy> |
| 263 | Ptr<Entry> |
| 264 | ContentStoreImpl<Policy>::Next (Ptr<Entry> from) |
| 265 | { |
| 266 | if (from == 0) return 0; |
| 267 | |
| 268 | typename super::parent_trie::recursive_iterator |
| 269 | item (*StaticCast< entry > (from)->to_iterator ()), |
| 270 | end (0); |
| 271 | |
| 272 | for (item++; item != end; item++) |
| 273 | { |
| 274 | if (item->payload () == 0) continue; |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | if (item == end) |
| 279 | return End (); |
| 280 | else |
| 281 | return item->payload (); |
| 282 | } |
| 283 | |
| 284 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 285 | } // namespace cs |
| 286 | } // namespace ndn |
| 287 | } // namespace ns3 |
| 288 | |
| 289 | #endif // NDN_CONTENT_STORE_IMPL_H_ |