blob: 5546d8480dde30a40e6c0e839abc59dd332bfb1b [file] [log] [blame]
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -07001/* -*- 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
29NS_LOG_COMPONENT_DEFINE ("CcnxContentStoreLru");
30
31namespace ns3
32{
33
34NS_OBJECT_ENSURE_REGISTERED (CcnxContentStoreLru);
35
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070036TypeId
37CcnxContentStoreLru::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
54void
55CcnxContentStoreLru::SetMaxSize (uint32_t maxSize)
56{
57 m_maxSize = maxSize;
Alexander Afanasyevb0c43892012-06-13 00:52:58 -070058 m_contentStore.getPolicy ().set_max_size (maxSize);
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070059}
60
61uint32_t
62CcnxContentStoreLru::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 Afanasyevd9fecdd2012-06-08 16:22:24 -070075
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070076CcnxContentStoreLru::CcnxContentStoreLru ()
77 : m_maxSize (100)
78{ } // this value shouldn't matter, NS-3 should call SetSize with default value specified in AddAttribute earlier
79
80CcnxContentStoreLru::~CcnxContentStoreLru ()
81{ }
82
83
84boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
85CcnxContentStoreLru::Lookup (Ptr<const CcnxInterestHeader> interest)
86{
87 NS_LOG_FUNCTION (this << interest->GetName ());
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070088
Alexander Afanasyevb0c43892012-06-13 00:52:58 -070089 /// @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 Afanasyevd9fecdd2012-06-08 16:22:24 -0700101 }
Alexander Afanasyevb0c43892012-06-13 00:52:58 -0700102 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 Afanasyevd9fecdd2012-06-08 16:22:24 -0700108}
109
110bool
111CcnxContentStoreLru::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
112{
113 NS_LOG_FUNCTION (this << header->GetName ());
Alexander Afanasyevb0c43892012-06-13 00:52:58 -0700114
115 return
116 m_contentStore
117 .insert (header->GetName (), Create<CcnxContentStoreEntry> (header, packet))
118 .second;
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700119}
120
121void
Alexander Afanasyevb0c43892012-06-13 00:52:58 -0700122CcnxContentStoreLru::Print () const
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700123{
Alexander Afanasyevb0c43892012-06-13 00:52:58 -0700124 BOOST_FOREACH (const CcnxContentStoreLruContainer::parent_trie &item, m_contentStore.getPolicy ())
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700125 {
Alexander Afanasyevb0c43892012-06-13 00:52:58 -0700126 NS_LOG_INFO (item.payload ()->GetName ());
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700127 }
128}
129
130} // namespace ns3