blob: 31cd66e4f6c885eb51c1a9bd329916239bd0002c [file] [log] [blame]
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "content-store-impl.h"
22#include "ns3/log.h"
23#include "ns3/uinteger.h"
24#include "ns3/string.h"
25
Alexander Afanasyev1a2df6a2012-08-17 13:21:51 -070026#include "../../utils/trie/random-policy.h"
27#include "../../utils/trie/lru-policy.h"
28#include "../../utils/trie/fifo-policy.h"
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070029
30NS_LOG_COMPONENT_DEFINE ("ndn.cs.ContentStoreImpl");
31
32#define NS_OBJECT_ENSURE_REGISTERED_TEMPL(type, templ) \
33 static struct X ## type ## templ ## RegistrationClass \
34 { \
35 X ## type ## templ ## RegistrationClass () { \
36 ns3::TypeId tid = type<templ>::GetTypeId (); \
37 tid.GetParent (); \
38 } \
39 } x_ ## type ## templ ## RegistrationVariable
40
41namespace ns3 {
42namespace ndn {
43
44using namespace ndnSIM;
45
46namespace cs {
47
48
49template<>
50TypeId
51ContentStoreImpl< lru_policy_traits >::GetTypeId ()
52{
53 static TypeId tid = TypeId ("ns3::ndn::cs::Lru")
54 .SetGroupName ("Ndn")
55 .SetParent<ContentStore> ()
56 .AddConstructor< ContentStoreImpl< lru_policy_traits > > ()
57 .AddAttribute ("MaxSize",
58 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
59 StringValue ("100"),
60 MakeUintegerAccessor (&ContentStoreImpl< lru_policy_traits >::GetMaxSize,
61 &ContentStoreImpl< lru_policy_traits >::SetMaxSize),
62 MakeUintegerChecker<uint32_t> ())
63 ;
64
65 return tid;
66}
67
68template<>
69TypeId
70ContentStoreImpl< random_policy_traits >::GetTypeId ()
71{
72 static TypeId tid = TypeId ("ns3::ndn::cs::Random")
73 .SetGroupName ("Ndn")
74 .SetParent<ContentStore> ()
75 .AddConstructor< ContentStoreImpl< random_policy_traits > > ()
76 .AddAttribute ("MaxSize",
77 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
78 StringValue ("100"),
79 MakeUintegerAccessor (&ContentStoreImpl< random_policy_traits >::GetMaxSize,
80 &ContentStoreImpl< random_policy_traits >::SetMaxSize),
81 MakeUintegerChecker<uint32_t> ())
82 ;
83
84 return tid;
85}
86
87template<>
88TypeId
89ContentStoreImpl< fifo_policy_traits >::GetTypeId ()
90{
91 static TypeId tid = TypeId ("ns3::ndn::cs::Fifo")
92 .SetGroupName ("Ndn")
93 .SetParent<ContentStore> ()
94 .AddConstructor< ContentStoreImpl< fifo_policy_traits > > ()
95 .AddAttribute ("MaxSize",
96 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
97 StringValue ("100"),
98 MakeUintegerAccessor (&ContentStoreImpl< fifo_policy_traits >::GetMaxSize,
99 &ContentStoreImpl< fifo_policy_traits >::SetMaxSize),
100 MakeUintegerChecker<uint32_t> ())
101 ;
102
103 return tid;
104}
105
106/////////////////////////////////////////////////////////////////////////////////////////////////
107/////////////////////////////////////////////////////////////////////////////////////////////////
108
109template<class Policy>
110boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> >
111ContentStoreImpl<Policy>::Lookup (Ptr<const InterestHeader> interest)
112{
113 // NS_LOG_FUNCTION (this << interest->GetName ());
114
115 /// @todo Change to search with predicate
116 typename super::const_iterator node = this->deepest_prefix_match (interest->GetName ());
117
118 if (node != this->end ())
119 {
120 this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
121
122 // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
123 return boost::make_tuple (node->payload ()->GetFullyFormedNdnPacket (),
124 node->payload ()->GetHeader (),
125 node->payload ()->GetPacket ());
126 }
127 else
128 {
129 // NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
130 this->m_cacheMissesTrace (interest);
131 return boost::tuple<Ptr<Packet>, Ptr<ContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
132 }
133}
134
135template<class Policy>
136bool
137ContentStoreImpl<Policy>::Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet)
138{
139 // NS_LOG_FUNCTION (this << header->GetName ());
140
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700141 Ptr< entry > newEntry = Create< entry > (header, packet);
142 std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry);
143
144 if (result.first != super::end ())
145 {
146 if (result.second)
147 {
148 newEntry->SetTrie (result.first);
149 return newEntry;
150 }
151 else
152 {
153 // should we do anything?
154 // update payload? add new payload?
155 return false;
156 }
157 }
158 else
159 return false; // cannot insert entry
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700160}
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700161
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700162template<class Policy>
163void
164ContentStoreImpl<Policy>::Print (std::ostream &os) const
165{
166 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
167 item != this->getPolicy ().end ();
168 item++)
169 // BOOST_FOREACH (const typename super::parent_trie &item, this->getPolicy ())
170 {
171 os << item->payload ()->GetName () << std::endl;
172 }
173}
174
175template<class Policy>
176void
177ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
178{
179 this->getPolicy ().set_max_size (maxSize);
180}
181
182template<class Policy>
183uint32_t
184ContentStoreImpl<Policy>::GetMaxSize () const
185{
186 return this->getPolicy ().get_max_size ();
187}
188
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700189template<class Policy>
190uint32_t
191ContentStoreImpl<Policy>::GetSize () const
192{
193 return this->getPolicy ().size ();
194}
195
196template<class Policy>
197Ptr<Entry>
198ContentStoreImpl<Policy>::Begin ()
199{
200 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
201 for (; item != end; item++)
202 {
203 if (item->payload () == 0) continue;
204 break;
205 }
206
207 if (item == end)
208 return End ();
209 else
210 return item->payload ();
211}
212
213template<class Policy>
214Ptr<Entry>
215ContentStoreImpl<Policy>::End ()
216{
217 return 0;
218}
219
220template<class Policy>
221Ptr<Entry>
222ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
223{
224 if (from == 0) return 0;
225
226 typename super::parent_trie::recursive_iterator
227 item (*StaticCast< entry > (from)->to_iterator ()),
228 end (0);
229
230 for (item++; item != end; item++)
231 {
232 if (item->payload () == 0) continue;
233 break;
234 }
235
236 if (item == end)
237 return End ();
238 else
239 return item->payload ();
240}
241
242
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700243////////////////////////////////////////////////////////////////////
244////////////////////////////////////////////////////////////////////
245
246// explicit instantiation and registering
247template class ContentStoreImpl<lru_policy_traits>;
248template class ContentStoreImpl<random_policy_traits>;
249template class ContentStoreImpl<fifo_policy_traits>;
250
251NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, lru_policy_traits);
252NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, random_policy_traits);
253NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, fifo_policy_traits);
254
255
256} // namespace cs
257} // namespace ndn
258} // namespace ns3