blob: fe6346b5bbff47c5632b599d354bb0c2ed1b26c1 [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
26#include "../../utils/random-policy.h"
27#include "../../utils/lru-policy.h"
28#include "../../utils/fifo-policy.h"
29
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
141 return
142 this->insert (header->GetName (), Create<Entry> (header, packet))
143 .second;
144}
145
146template<class Policy>
147void
148ContentStoreImpl<Policy>::Print (std::ostream &os) const
149{
150 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
151 item != this->getPolicy ().end ();
152 item++)
153 // BOOST_FOREACH (const typename super::parent_trie &item, this->getPolicy ())
154 {
155 os << item->payload ()->GetName () << std::endl;
156 }
157}
158
159template<class Policy>
160void
161ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
162{
163 this->getPolicy ().set_max_size (maxSize);
164}
165
166template<class Policy>
167uint32_t
168ContentStoreImpl<Policy>::GetMaxSize () const
169{
170 return this->getPolicy ().get_max_size ();
171}
172
173////////////////////////////////////////////////////////////////////
174////////////////////////////////////////////////////////////////////
175
176// explicit instantiation and registering
177template class ContentStoreImpl<lru_policy_traits>;
178template class ContentStoreImpl<random_policy_traits>;
179template class ContentStoreImpl<fifo_policy_traits>;
180
181NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, lru_policy_traits);
182NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, random_policy_traits);
183NS_OBJECT_ENSURE_REGISTERED_TEMPL(ContentStoreImpl, fifo_policy_traits);
184
185
186} // namespace cs
187} // namespace ndn
188} // namespace ns3