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 | |
| 26 | #include "../../utils/random-policy.h" |
| 27 | #include "../../utils/lru-policy.h" |
| 28 | #include "../../utils/fifo-policy.h" |
| 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 | |
| 141 | return |
| 142 | this->insert (header->GetName (), Create<Entry> (header, packet)) |
| 143 | .second; |
| 144 | } |
| 145 | |
| 146 | template<class Policy> |
| 147 | void |
| 148 | ContentStoreImpl<Policy>::Print (std::ostream &os) const |
| 149 | { |
| 150 | for (typename super::policy_container::const_iterator item = this->getPolicy ().begin (); |
| 151 | item != this->getPolicy ().end (); |
| 152 | item++) |
| 153 | // BOOST_FOREACH (const typename super::parent_trie &item, this->getPolicy ()) |
| 154 | { |
| 155 | os << item->payload ()->GetName () << std::endl; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | template<class Policy> |
| 160 | void |
| 161 | ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize) |
| 162 | { |
| 163 | this->getPolicy ().set_max_size (maxSize); |
| 164 | } |
| 165 | |
| 166 | template<class Policy> |
| 167 | uint32_t |
| 168 | ContentStoreImpl<Policy>::GetMaxSize () const |
| 169 | { |
| 170 | return this->getPolicy ().get_max_size (); |
| 171 | } |
| 172 | |
| 173 | //////////////////////////////////////////////////////////////////// |
| 174 | //////////////////////////////////////////////////////////////////// |
| 175 | |
| 176 | // explicit instantiation and registering |
| 177 | template class ContentStoreImpl<lru_policy_traits>; |
| 178 | template class ContentStoreImpl<random_policy_traits>; |
| 179 | template class ContentStoreImpl<fifo_policy_traits>; |
| 180 | |
| 181 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, lru_policy_traits); |
| 182 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, random_policy_traits); |
| 183 | NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, fifo_policy_traits); |
| 184 | |
| 185 | |
| 186 | } // namespace cs |
| 187 | } // namespace ndn |
| 188 | } // namespace ns3 |