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 | |
| 36 | using namespace __ccnx_private; |
| 37 | |
| 38 | TypeId |
| 39 | CcnxContentStoreLru::GetTypeId (void) |
| 40 | { |
| 41 | static TypeId tid = TypeId ("ns3::CcnxContentStoreLru") |
| 42 | .SetGroupName ("Ccnx") |
| 43 | .SetParent<CcnxContentStore> () |
| 44 | .AddConstructor<CcnxContentStoreLru> () |
| 45 | .AddAttribute ("Size", |
| 46 | "Maximum number of packets that content storage can hold", |
| 47 | UintegerValue (100), |
| 48 | MakeUintegerAccessor (&CcnxContentStoreLru::SetMaxSize, |
| 49 | &CcnxContentStoreLru::GetMaxSize), |
| 50 | MakeUintegerChecker<uint32_t> ()) |
| 51 | ; |
| 52 | |
| 53 | return tid; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | CcnxContentStoreLru::SetMaxSize (uint32_t maxSize) |
| 58 | { |
| 59 | m_maxSize = maxSize; |
| 60 | } |
| 61 | |
| 62 | uint32_t |
| 63 | CcnxContentStoreLru::GetMaxSize () const |
| 64 | { |
| 65 | return m_maxSize; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | ////////////////////////////////////////////////////////////////////// |
| 70 | // Helper classes |
| 71 | ////////////////////////////////////////////////////////////////////// |
| 72 | /** |
| 73 | * \ingroup ccnx |
| 74 | * \brief Typedef for hash index of content store container |
| 75 | */ |
| 76 | struct CcnxContentStoreByPrefix |
| 77 | { |
| 78 | typedef |
| 79 | CcnxContentStoreLruContainer::type::index<i_prefix>::type |
| 80 | type; |
| 81 | }; |
| 82 | |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 83 | CcnxContentStoreLru::CcnxContentStoreLru () |
| 84 | : m_maxSize (100) |
| 85 | { } // this value shouldn't matter, NS-3 should call SetSize with default value specified in AddAttribute earlier |
| 86 | |
| 87 | CcnxContentStoreLru::~CcnxContentStoreLru () |
| 88 | { } |
| 89 | |
| 90 | |
| 91 | boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> > |
| 92 | CcnxContentStoreLru::Lookup (Ptr<const CcnxInterestHeader> interest) |
| 93 | { |
| 94 | NS_LOG_FUNCTION (this << interest->GetName ()); |
| 95 | CcnxContentStoreLruContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (interest->GetName ()); |
| 96 | if (it != m_contentStore.end ()) |
| 97 | { |
| 98 | // promote entry to the top |
| 99 | m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (), |
| 100 | m_contentStore.project<i_mru> (it)); |
| 101 | |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame^] | 102 | m_cacheHitsTrace (interest, it->GetHeader ()); |
| 103 | |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 104 | // return fully formed CCNx packet |
| 105 | return boost::make_tuple (it->GetFullyFormedCcnxPacket (), it->GetHeader (), it->GetPacket ()); |
| 106 | } |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame^] | 107 | m_cacheMissesTrace (interest); |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 108 | return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0); |
| 109 | } |
| 110 | |
| 111 | bool |
| 112 | CcnxContentStoreLru::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet) |
| 113 | { |
| 114 | NS_LOG_FUNCTION (this << header->GetName ()); |
| 115 | CcnxContentStoreLruContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (header->GetName ()); |
| 116 | if (it == m_contentStore.end ()) |
| 117 | { // add entry to the top |
| 118 | m_contentStore.get<i_mru> ().push_front (CcnxContentStoreEntry (header, packet)); |
| 119 | if (m_contentStore.size () > m_maxSize) |
| 120 | m_contentStore.get<i_mru> ().pop_back (); |
| 121 | return false; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | /// @todo Wrong!!! Record should be updated and relocated, not just relocated |
| 126 | // promote entry to the top |
| 127 | m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (), |
| 128 | m_contentStore.project<i_mru> (it)); |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | CcnxContentStoreLru::Print() const |
| 135 | { |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame^] | 136 | BOOST_FOREACH (const CcnxContentStoreEntry &entry, m_contentStore.get<i_mru> ()) |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 137 | { |
Alexander Afanasyev | 39485d8 | 2012-06-08 17:51:47 -0700 | [diff] [blame^] | 138 | NS_LOG_INFO (entry.GetName ()); |
Alexander Afanasyev | d9fecdd | 2012-06-08 16:22:24 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | } // namespace ns3 |