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