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