blob: 90d0c955624a8fa714db17efa133f3ecdf4ce2dd [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;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080045
Alexander Afanasyev8566f452012-12-10 15:21:51 -080046public:
Alexander Afanasyevb989b122013-07-10 17:15:46 -070047 EntryImpl (Ptr<ContentStore> cs, Ptr<const ContentObject> data)
48 : Entry (cs, data)
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070049 , 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_; }
Alexander Afanasyev25093a32013-02-01 13:00:13 -080061
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070062private:
63 typename CS::super::iterator item_;
64};
65
66
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070067
68template<class Policy>
69class ContentStoreImpl : public ContentStore,
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070070 protected ndnSIM::trie_with_policy< Name,
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 Afanasyevcfdc14f2013-03-15 14:38:44 -070075 typedef ndnSIM::trie_with_policy< Name,
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;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080078
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070079 typedef EntryImpl< ContentStoreImpl< Policy > > entry;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080080
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081 static TypeId
82 GetTypeId ();
Alexander Afanasyev25093a32013-02-01 13:00:13 -080083
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070084 ContentStoreImpl () { };
85 virtual ~ContentStoreImpl () { };
Alexander Afanasyev25093a32013-02-01 13:00:13 -080086
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087 // from ContentStore
Alexander Afanasyev25093a32013-02-01 13:00:13 -080088
Alexander Afanasyevb989b122013-07-10 17:15:46 -070089 virtual inline Ptr<ContentObject>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070090 Lookup (Ptr<const Interest> interest);
Alexander Afanasyev25093a32013-02-01 13:00:13 -080091
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092 virtual inline bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -070093 Add (Ptr<const ContentObject> data);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094
95 // virtual bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070096 // Remove (Ptr<Interest> header);
Alexander Afanasyev25093a32013-02-01 13:00:13 -080097
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098 virtual inline void
Alexander Afanasyev25093a32013-02-01 13:00:13 -080099 Print (std::ostream &os) const;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700100
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 Afanasyev25093a32013-02-01 13:00:13 -0800122
123 /// @brief trace of for entry additions (fired every time entry is successfully added to the cache): first parameter is pointer to the CS entry
124 TracedCallback< Ptr<const Entry> > m_didAddEntry;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125};
126
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800127//////////////////////////////////////////
128////////// Implementation ////////////////
129//////////////////////////////////////////
130
131
132template<class Policy>
133LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ());
134
135
136template<class Policy>
137TypeId
138ContentStoreImpl< Policy >::GetTypeId ()
139{
140 static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ())
141 .SetGroupName ("Ndn")
142 .SetParent<ContentStore> ()
143 .AddConstructor< ContentStoreImpl< Policy > > ()
144 .AddAttribute ("MaxSize",
145 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
146 StringValue ("100"),
147 MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize,
148 &ContentStoreImpl< Policy >::SetMaxSize),
149 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800150
151 .AddTraceSource ("DidAddEntry", "Trace fired every time entry is successfully added to the cache",
152 MakeTraceSourceAccessor (&ContentStoreImpl< Policy >::m_didAddEntry))
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800153 ;
154
155 return tid;
156}
157
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -0700158struct isNotExcluded
159{
160 inline
161 isNotExcluded (const Exclude &exclude)
162 : m_exclude (exclude)
163 {
164 }
165
166 bool
167 operator () (const name::Component &comp) const
168 {
169 return !m_exclude.isExcluded (comp);
170 }
171
172private:
173 const Exclude &m_exclude;
174};
175
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800176template<class Policy>
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700177Ptr<ContentObject>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700178ContentStoreImpl<Policy>::Lookup (Ptr<const Interest> interest)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800179{
180 NS_LOG_FUNCTION (this << interest->GetName ());
181
182 /// @todo Change to search with predicate
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -0700183 typename super::const_iterator node;
184 if (interest->GetExclude () == 0)
185 {
186 node = this->deepest_prefix_match (interest->GetName ());
187 }
188 else
189 {
190 node = this->deepest_prefix_match_if_next_level (interest->GetName (),
191 isNotExcluded (*interest->GetExclude ()));
192 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800193
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800194 if (node != this->end ())
195 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700196 this->m_cacheHitsTrace (interest, node->payload ()->GetData ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800197
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -0700198 Ptr<ContentObject> copy = Create<ContentObject> (*node->payload ()->GetData ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700199 ConstCast<Packet> (copy->GetPayload ())->RemoveAllPacketTags ();
200 return copy;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800201 }
202 else
203 {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800204 this->m_cacheMissesTrace (interest);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700205 return 0;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800206 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800207}
208
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800209template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800210bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700211ContentStoreImpl<Policy>::Add (Ptr<const ContentObject> data)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800212{
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700213 NS_LOG_FUNCTION (this << data->GetName ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800214
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700215 Ptr< entry > newEntry = Create< entry > (this, data);
216 std::pair< typename super::iterator, bool > result = super::insert (data->GetName (), newEntry);
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800217
218 if (result.first != super::end ())
219 {
220 if (result.second)
221 {
222 newEntry->SetTrie (result.first);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800223
224 m_didAddEntry (newEntry);
225 return true;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800226 }
227 else
228 {
229 // should we do anything?
230 // update payload? add new payload?
231 return false;
232 }
233 }
234 else
235 return false; // cannot insert entry
236}
237
238template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800239void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800240ContentStoreImpl<Policy>::Print (std::ostream &os) const
241{
242 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
243 item != this->getPolicy ().end ();
244 item++)
245 {
246 os << item->payload ()->GetName () << std::endl;
247 }
248}
249
250template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800251void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800252ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
253{
254 this->getPolicy ().set_max_size (maxSize);
255}
256
257template<class Policy>
258uint32_t
259ContentStoreImpl<Policy>::GetMaxSize () const
260{
261 return this->getPolicy ().get_max_size ();
262}
263
264template<class Policy>
265uint32_t
266ContentStoreImpl<Policy>::GetSize () const
267{
268 return this->getPolicy ().size ();
269}
270
271template<class Policy>
272Ptr<Entry>
273ContentStoreImpl<Policy>::Begin ()
274{
275 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
276 for (; item != end; item++)
277 {
278 if (item->payload () == 0) continue;
279 break;
280 }
281
282 if (item == end)
283 return End ();
284 else
285 return item->payload ();
286}
287
288template<class Policy>
289Ptr<Entry>
290ContentStoreImpl<Policy>::End ()
291{
292 return 0;
293}
294
295template<class Policy>
296Ptr<Entry>
297ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
298{
299 if (from == 0) return 0;
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800300
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800301 typename super::parent_trie::recursive_iterator
302 item (*StaticCast< entry > (from)->to_iterator ()),
303 end (0);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800304
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800305 for (item++; item != end; item++)
306 {
307 if (item->payload () == 0) continue;
308 break;
309 }
310
311 if (item == end)
312 return End ();
313 else
314 return item->payload ();
315}
316
317
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700318} // namespace cs
319} // namespace ndn
320} // namespace ns3
321
322#endif // NDN_CONTENT_STORE_IMPL_H_