Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -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: Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "ccnx-content-store-lru.h" |
| 23 | #include "ns3/log.h" |
| 24 | #include "ns3/packet.h" |
| 25 | #include "ns3/ccnx-interest-header.h" |
| 26 | #include "ns3/ccnx-content-object-header.h" |
| 27 | #include "ns3/uinteger.h" |
| 28 | |
| 29 | NS_LOG_COMPONENT_DEFINE ("CcnxContentStoreLru"); |
| 30 | |
| 31 | namespace ns3 |
| 32 | { |
| 33 | |
| 34 | NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreLru); |
| 35 | |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 36 | TypeId |
| 37 | CcnxContentStoreLru::GetTypeId (void) |
| 38 | { |
| 39 | static TypeId tid = TypeId ("ns3::CcnxContentStoreLru") |
| 40 | .SetGroupName ("Ccnx") |
| 41 | .SetParent<CcnxContentStore> () |
| 42 | .AddConstructor<CcnxContentStoreLru> () |
| 43 | .AddAttribute ("Size", |
| 44 | "Maximum number of packets that content storage can hold", |
| 45 | UintegerValue (100), |
| 46 | MakeUintegerAccessor (&CcnxContentStoreLru::SetMaxSize, |
| 47 | &CcnxContentStoreLru::GetMaxSize), |
| 48 | MakeUintegerChecker<uint32_t> ()) |
| 49 | ; |
| 50 | |
| 51 | return tid; |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | CcnxContentStoreLru::SetMaxSize (uint32_t maxSize) |
| 56 | { |
| 57 | m_maxSize = maxSize; |
Alexander Afanasyev | b0c4389 | 2012-06-13 00:52:58 -0700 | [diff] [blame] | 58 | m_contentStore.getPolicy ().set_max_size (maxSize); |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | uint32_t |
| 62 | CcnxContentStoreLru::GetMaxSize () const |
| 63 | { |
| 64 | return m_maxSize; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | ////////////////////////////////////////////////////////////////////// |
| 69 | // Helper classes |
| 70 | ////////////////////////////////////////////////////////////////////// |
| 71 | /** |
| 72 | * \ingroup ccnx |
| 73 | * \brief Typedef for hash index of content store container |
| 74 | */ |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 76 | CcnxContentStoreLru::CcnxContentStoreLru () |
| 77 | : m_maxSize (100) |
| 78 | { } // this value shouldn't matter, NS-3 should call SetSize with default value specified in AddAttribute earlier |
| 79 | |
| 80 | CcnxContentStoreLru::~CcnxContentStoreLru () |
| 81 | { } |
| 82 | |
| 83 | |
| 84 | boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> > |
| 85 | CcnxContentStoreLru::Lookup (Ptr<const CcnxInterestHeader> interest) |
| 86 | { |
| 87 | NS_LOG_FUNCTION (this << interest->GetName ()); |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | b0c4389 | 2012-06-13 00:52:58 -0700 | [diff] [blame] | 89 | /// @todo Change to search with predicate |
| 90 | CcnxContentStoreLruContainer::iterator node = |
| 91 | m_contentStore.deepest_prefix_match (interest->GetName ()); |
| 92 | |
| 93 | if (node != m_contentStore.end ()) |
| 94 | { |
| 95 | m_cacheHitsTrace (interest, node->payload ()->GetHeader ()); |
| 96 | |
| 97 | NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ()); |
| 98 | return boost::make_tuple (node->payload ()->GetFullyFormedCcnxPacket (), |
| 99 | node->payload ()->GetHeader (), |
| 100 | node->payload ()->GetPacket ()); |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 101 | } |
Alexander Afanasyev | b0c4389 | 2012-06-13 00:52:58 -0700 | [diff] [blame] | 102 | else |
| 103 | { |
| 104 | NS_LOG_DEBUG ("cache miss for " << interest->GetName ()); |
| 105 | m_cacheMissesTrace (interest); |
| 106 | return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0); |
| 107 | } |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool |
| 111 | CcnxContentStoreLru::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) |
| 112 | { |
| 113 | NS_LOG_FUNCTION (this << header->GetName ()); |
Alexander Afanasyev | b0c4389 | 2012-06-13 00:52:58 -0700 | [diff] [blame] | 114 | |
| 115 | return |
| 116 | m_contentStore |
| 117 | .insert (header->GetName (), Create<CcnxContentStoreEntry> (header, packet)) |
| 118 | .second; |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Alexander Afanasyev | b0c4389 | 2012-06-13 00:52:58 -0700 | [diff] [blame] | 122 | CcnxContentStoreLru::Print () const |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 123 | { |
Alexander Afanasyev | b0c4389 | 2012-06-13 00:52:58 -0700 | [diff] [blame] | 124 | BOOST_FOREACH (const CcnxContentStoreLruContainer::parent_trie &item, m_contentStore.getPolicy ()) |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 125 | { |
Alexander Afanasyev | b0c4389 | 2012-06-13 00:52:58 -0700 | [diff] [blame] | 126 | NS_LOG_INFO (item.payload ()->GetName ()); |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | } // namespace ns3 |