blob: bb35c1baafc39e697f59c3b18a366ba4f0d8b11b [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
83/**
84 * \ingroup ccnx
85 * \brief Typedef for MRU index of content store container
86 */
87struct CcnxContentStoreByMru
88{
89 typedef
90 CcnxContentStoreLruContainer::type::index<i_mru>::type
91 type;
92};
93
94#ifdef _DEBUG
95#define DUMP_INDEX_TAG i_ordered
96#define DUMP_INDEX CcnxContentStoreOrderedPrefix
97/**
98 * \ingroup ccnx
99 * \brief Typedef for ordered index of content store container
100 */
101struct CcnxContentStoreOrderedPrefix
102{
103 typedef
104 CcnxContentStoreLruContainer::type::index<i_ordered>::type
105 type;
106};
107#else
108#define DUMP_INDEX_TAG i_mru
109#define DUMP_INDEX CcnxContentStoreByMru
110#endif
111
112
113
114CcnxContentStoreLru::CcnxContentStoreLru ()
115 : m_maxSize (100)
116{ } // this value shouldn't matter, NS-3 should call SetSize with default value specified in AddAttribute earlier
117
118CcnxContentStoreLru::~CcnxContentStoreLru ()
119{ }
120
121
122boost::tuple<Ptr<Packet>, Ptr<const CcnxContentObjectHeader>, Ptr<const Packet> >
123CcnxContentStoreLru::Lookup (Ptr<const CcnxInterestHeader> interest)
124{
125 NS_LOG_FUNCTION (this << interest->GetName ());
126 CcnxContentStoreLruContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (interest->GetName ());
127 if (it != m_contentStore.end ())
128 {
129 // promote entry to the top
130 m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
131 m_contentStore.project<i_mru> (it));
132
133 // return fully formed CCNx packet
134 return boost::make_tuple (it->GetFullyFormedCcnxPacket (), it->GetHeader (), it->GetPacket ());
135 }
136 return boost::tuple<Ptr<Packet>, Ptr<CcnxContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
137}
138
139bool
140CcnxContentStoreLru::Add (Ptr<CcnxContentObjectHeader> header, Ptr<const Packet> packet)
141{
142 NS_LOG_FUNCTION (this << header->GetName ());
143 CcnxContentStoreLruContainer::type::iterator it = m_contentStore.get<i_prefix> ().find (header->GetName ());
144 if (it == m_contentStore.end ())
145 { // add entry to the top
146 m_contentStore.get<i_mru> ().push_front (CcnxContentStoreEntry (header, packet));
147 if (m_contentStore.size () > m_maxSize)
148 m_contentStore.get<i_mru> ().pop_back ();
149 return false;
150 }
151 else
152 {
153 /// @todo Wrong!!! Record should be updated and relocated, not just relocated
154 // promote entry to the top
155 m_contentStore.get<i_mru> ().relocate (m_contentStore.get<i_mru> ().begin (),
156 m_contentStore.project<i_mru> (it));
157 return true;
158 }
159}
160
161void
162CcnxContentStoreLru::Print() const
163{
164 for( DUMP_INDEX::type::iterator it=m_contentStore.get<DUMP_INDEX_TAG> ().begin ();
165 it != m_contentStore.get<DUMP_INDEX_TAG> ().end ();
166 it++
167 )
168 {
169 NS_LOG_INFO (it->GetName ());
170 }
171}
172
173} // namespace ns3