blob: 8c0d7770f521f544d7d90a38f42e3fe9ca80a842 [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
36using namespace __ccnx_private;
37
38TypeId
39CcnxContentStoreLru::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
56void
57CcnxContentStoreLru::SetMaxSize (uint32_t maxSize)
58{
59 m_maxSize = maxSize;
60}
61
62uint32_t
63CcnxContentStoreLru::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 */
76struct CcnxContentStoreByPrefix
77{
78 typedef
79 CcnxContentStoreLruContainer::type::index<i_prefix>::type
80 type;
81};
82
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -070083CcnxContentStoreLru::CcnxContentStoreLru ()
84 : m_maxSize (100)
85{ } // this value shouldn't matter, NS-3 should call SetSize with default value specified in AddAttribute earlier
86
87CcnxContentStoreLru::~CcnxContentStoreLru ()
88{ }
89
90
91boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
92CcnxContentStoreLru::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 Afanasyev39485d82012-06-08 17:51:47 -0700102 m_cacheHitsTrace (interest, it->GetHeader ());
103
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700104 // return fully formed CCNx packet
105 return boost::make_tuple (it->GetFullyFormedCcnxPacket (), it->GetHeader (), it->GetPacket ());
106 }
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700107 m_cacheMissesTrace (interest);
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700108 return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
109}
110
111bool
112CcnxContentStoreLru::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
133void
134CcnxContentStoreLru::Print() const
135{
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700136 BOOST_FOREACH (const CcnxContentStoreEntry &entry, m_contentStore.get<i_mru> ())
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700137 {
Alexander Afanasyev39485d82012-06-08 17:51:47 -0700138 NS_LOG_INFO (entry.GetName ());
Alexander Afanasyevd9fecdd2012-06-08 16:22:24 -0700139 }
140}
141
142} // namespace ns3