blob: 6ebd86514aec95717d844a0a991224792c6272a9 [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#ifndef NDN_CONTENT_STORE_IMPL_H_
22#define NDN_CONTENT_STORE_IMPL_H_
23
24#include "ndn-content-store.h"
25#include "ns3/packet.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080026#include "ns3/ndn-interest.h"
27#include "ns3/ndn-content-object.h"
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070028#include <boost/foreach.hpp>
29
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080030#include "ns3/log.h"
31#include "ns3/uinteger.h"
32#include "ns3/string.h"
33
Alexander Afanasyev1a2df6a2012-08-17 13:21:51 -070034#include "../../utils/trie/trie-with-policy.h"
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070035
36namespace ns3 {
37namespace ndn {
38namespace cs {
39
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070040template<class CS>
41class EntryImpl : public Entry
42{
43public:
Alexander Afanasyev8566f452012-12-10 15:21:51 -080044 typedef Entry base_type;
45
46public:
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070047 EntryImpl (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet)
48 : Entry (header, packet)
49 , item_ (0)
50 {
51 }
52
53 void
54 SetTrie (typename CS::super::iterator item)
55 {
56 item_ = item;
57 }
58
59 typename CS::super::iterator to_iterator () { return item_; }
60 typename CS::super::const_iterator to_iterator () const { return item_; }
61
62private:
63 typename CS::super::iterator item_;
64};
65
66
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070067
68template<class Policy>
69class ContentStoreImpl : public ContentStore,
70 protected ndnSIM::trie_with_policy< NameComponents,
Alexander Afanasyev8566f452012-12-10 15:21:51 -080071 ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072 Policy >
73{
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070074public:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070075 typedef ndnSIM::trie_with_policy< NameComponents,
Alexander Afanasyev8566f452012-12-10 15:21:51 -080076 ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070077 Policy > super;
78
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070079 typedef EntryImpl< ContentStoreImpl< Policy > > entry;
80
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081 static TypeId
82 GetTypeId ();
83
84 ContentStoreImpl () { };
85 virtual ~ContentStoreImpl () { };
86
87 // from ContentStore
88
89 virtual inline boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> >
90 Lookup (Ptr<const InterestHeader> interest);
91
92 virtual inline bool
93 Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet);
94
95 // virtual bool
96 // Remove (Ptr<InterestHeader> header);
97
98 virtual inline void
99 Print (std::ostream &os) const;
100
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700101 virtual uint32_t
102 GetSize () const;
103
104 virtual Ptr<Entry>
105 Begin ();
106
107 virtual Ptr<Entry>
108 End ();
109
110 virtual Ptr<Entry>
111 Next (Ptr<Entry>);
112
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700113private:
114 void
115 SetMaxSize (uint32_t maxSize);
116
117 uint32_t
118 GetMaxSize () const;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800119
120private:
121 static LogComponent g_log; ///< @brief Logging variable
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122};
123
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800124//////////////////////////////////////////
125////////// Implementation ////////////////
126//////////////////////////////////////////
127
128
129template<class Policy>
130LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ());
131
132
133template<class Policy>
134TypeId
135ContentStoreImpl< Policy >::GetTypeId ()
136{
137 static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ())
138 .SetGroupName ("Ndn")
139 .SetParent<ContentStore> ()
140 .AddConstructor< ContentStoreImpl< Policy > > ()
141 .AddAttribute ("MaxSize",
142 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
143 StringValue ("100"),
144 MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize,
145 &ContentStoreImpl< Policy >::SetMaxSize),
146 MakeUintegerChecker<uint32_t> ())
147 ;
148
149 return tid;
150}
151
152template<class Policy>
153boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> >
154ContentStoreImpl<Policy>::Lookup (Ptr<const InterestHeader> interest)
155{
156 NS_LOG_FUNCTION (this << interest->GetName ());
157
158 /// @todo Change to search with predicate
159 typename super::const_iterator node = this->deepest_prefix_match (interest->GetName ());
160
161 if (node != this->end ())
162 {
163 this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
164
165 // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
166 return boost::make_tuple (node->payload ()->GetFullyFormedNdnPacket (),
167 node->payload ()->GetHeader (),
168 node->payload ()->GetPacket ());
169 }
170 else
171 {
172 // NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
173 this->m_cacheMissesTrace (interest);
174 return boost::tuple<Ptr<Packet>, Ptr<ContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
175 }
176}
177
178template<class Policy>
179bool
180ContentStoreImpl<Policy>::Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet)
181{
182 NS_LOG_FUNCTION (this << header->GetName ());
183
184 Ptr< entry > newEntry = Create< entry > (header, packet);
185 std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry);
186
187 if (result.first != super::end ())
188 {
189 if (result.second)
190 {
191 newEntry->SetTrie (result.first);
192 return newEntry;
193 }
194 else
195 {
196 // should we do anything?
197 // update payload? add new payload?
198 return false;
199 }
200 }
201 else
202 return false; // cannot insert entry
203}
204
205template<class Policy>
206void
207ContentStoreImpl<Policy>::Print (std::ostream &os) const
208{
209 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
210 item != this->getPolicy ().end ();
211 item++)
212 {
213 os << item->payload ()->GetName () << std::endl;
214 }
215}
216
217template<class Policy>
218void
219ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
220{
221 this->getPolicy ().set_max_size (maxSize);
222}
223
224template<class Policy>
225uint32_t
226ContentStoreImpl<Policy>::GetMaxSize () const
227{
228 return this->getPolicy ().get_max_size ();
229}
230
231template<class Policy>
232uint32_t
233ContentStoreImpl<Policy>::GetSize () const
234{
235 return this->getPolicy ().size ();
236}
237
238template<class Policy>
239Ptr<Entry>
240ContentStoreImpl<Policy>::Begin ()
241{
242 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
243 for (; item != end; item++)
244 {
245 if (item->payload () == 0) continue;
246 break;
247 }
248
249 if (item == end)
250 return End ();
251 else
252 return item->payload ();
253}
254
255template<class Policy>
256Ptr<Entry>
257ContentStoreImpl<Policy>::End ()
258{
259 return 0;
260}
261
262template<class Policy>
263Ptr<Entry>
264ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
265{
266 if (from == 0) return 0;
267
268 typename super::parent_trie::recursive_iterator
269 item (*StaticCast< entry > (from)->to_iterator ()),
270 end (0);
271
272 for (item++; item != end; item++)
273 {
274 if (item->payload () == 0) continue;
275 break;
276 }
277
278 if (item == end)
279 return End ();
280 else
281 return item->payload ();
282}
283
284
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700285} // namespace cs
286} // namespace ndn
287} // namespace ns3
288
289#endif // NDN_CONTENT_STORE_IMPL_H_