blob: 9ae7e2eb8da63145f3c891e21008aed012fe603b [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"
Alexander Afanasyev6eba36f2013-08-07 17:42:54 -070027#include "ns3/ndn-data.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 Afanasyev79206512013-07-27 16:49:12 -070040/**
41 * @ingroup ndn-cs
42 * @brief Cache entry implementation with additional references to the base container
43 */
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070044template<class CS>
45class EntryImpl : public Entry
46{
47public:
Alexander Afanasyev8566f452012-12-10 15:21:51 -080048 typedef Entry base_type;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080049
Alexander Afanasyev8566f452012-12-10 15:21:51 -080050public:
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070051 EntryImpl (Ptr<ContentStore> cs, Ptr<const Data> data)
Alexander Afanasyevb989b122013-07-10 17:15:46 -070052 : Entry (cs, data)
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070053 , item_ (0)
54 {
55 }
56
57 void
58 SetTrie (typename CS::super::iterator item)
59 {
60 item_ = item;
61 }
62
63 typename CS::super::iterator to_iterator () { return item_; }
64 typename CS::super::const_iterator to_iterator () const { return item_; }
Alexander Afanasyev25093a32013-02-01 13:00:13 -080065
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070066private:
67 typename CS::super::iterator item_;
68};
69
70
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070071
Alexander Afanasyev79206512013-07-27 16:49:12 -070072/**
73 * @ingroup ndn-cs
74 * @brief Base implementation of NDN content store
75 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070076template<class Policy>
77class ContentStoreImpl : public ContentStore,
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070078 protected ndnSIM::trie_with_policy< Name,
Alexander Afanasyev8566f452012-12-10 15:21:51 -080079 ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080 Policy >
81{
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070082public:
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070083 typedef ndnSIM::trie_with_policy< Name,
Alexander Afanasyev8566f452012-12-10 15:21:51 -080084 ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085 Policy > super;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080086
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070087 typedef EntryImpl< ContentStoreImpl< Policy > > entry;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080088
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070089 static TypeId
90 GetTypeId ();
Alexander Afanasyev25093a32013-02-01 13:00:13 -080091
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092 ContentStoreImpl () { };
93 virtual ~ContentStoreImpl () { };
Alexander Afanasyev25093a32013-02-01 13:00:13 -080094
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095 // from ContentStore
Alexander Afanasyev25093a32013-02-01 13:00:13 -080096
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070097 virtual inline Ptr<Data>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070098 Lookup (Ptr<const Interest> interest);
Alexander Afanasyev25093a32013-02-01 13:00:13 -080099
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700100 virtual inline bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700101 Add (Ptr<const Data> data);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102
103 // virtual bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700104 // Remove (Ptr<Interest> header);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800105
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700106 virtual inline void
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800107 Print (std::ostream &os) const;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700108
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700109 virtual uint32_t
110 GetSize () const;
111
112 virtual Ptr<Entry>
113 Begin ();
114
115 virtual Ptr<Entry>
116 End ();
117
118 virtual Ptr<Entry>
119 Next (Ptr<Entry>);
120
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121private:
122 void
123 SetMaxSize (uint32_t maxSize);
124
125 uint32_t
126 GetMaxSize () const;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800127
128private:
129 static LogComponent g_log; ///< @brief Logging variable
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800130
131 /// @brief trace of for entry additions (fired every time entry is successfully added to the cache): first parameter is pointer to the CS entry
132 TracedCallback< Ptr<const Entry> > m_didAddEntry;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700133};
134
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800135//////////////////////////////////////////
136////////// Implementation ////////////////
137//////////////////////////////////////////
138
139
140template<class Policy>
141LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ());
142
143
144template<class Policy>
145TypeId
146ContentStoreImpl< Policy >::GetTypeId ()
147{
148 static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ())
149 .SetGroupName ("Ndn")
150 .SetParent<ContentStore> ()
151 .AddConstructor< ContentStoreImpl< Policy > > ()
152 .AddAttribute ("MaxSize",
153 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
154 StringValue ("100"),
155 MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize,
156 &ContentStoreImpl< Policy >::SetMaxSize),
157 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800158
159 .AddTraceSource ("DidAddEntry", "Trace fired every time entry is successfully added to the cache",
160 MakeTraceSourceAccessor (&ContentStoreImpl< Policy >::m_didAddEntry))
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800161 ;
162
163 return tid;
164}
165
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -0700166struct isNotExcluded
167{
168 inline
169 isNotExcluded (const Exclude &exclude)
170 : m_exclude (exclude)
171 {
172 }
173
174 bool
175 operator () (const name::Component &comp) const
176 {
177 return !m_exclude.isExcluded (comp);
178 }
179
180private:
181 const Exclude &m_exclude;
182};
183
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800184template<class Policy>
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700185Ptr<Data>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700186ContentStoreImpl<Policy>::Lookup (Ptr<const Interest> interest)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800187{
188 NS_LOG_FUNCTION (this << interest->GetName ());
189
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -0700190 typename super::const_iterator node;
191 if (interest->GetExclude () == 0)
192 {
193 node = this->deepest_prefix_match (interest->GetName ());
194 }
195 else
196 {
197 node = this->deepest_prefix_match_if_next_level (interest->GetName (),
198 isNotExcluded (*interest->GetExclude ()));
199 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800200
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800201 if (node != this->end ())
202 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700203 this->m_cacheHitsTrace (interest, node->payload ()->GetData ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800204
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700205 Ptr<Data> copy = Create<Data> (*node->payload ()->GetData ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700206 ConstCast<Packet> (copy->GetPayload ())->RemoveAllPacketTags ();
207 return copy;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800208 }
209 else
210 {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800211 this->m_cacheMissesTrace (interest);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700212 return 0;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800213 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800214}
215
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800216template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800217bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700218ContentStoreImpl<Policy>::Add (Ptr<const Data> data)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800219{
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700220 NS_LOG_FUNCTION (this << data->GetName ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800221
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700222 Ptr< entry > newEntry = Create< entry > (this, data);
223 std::pair< typename super::iterator, bool > result = super::insert (data->GetName (), newEntry);
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800224
225 if (result.first != super::end ())
226 {
227 if (result.second)
228 {
229 newEntry->SetTrie (result.first);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800230
231 m_didAddEntry (newEntry);
232 return true;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800233 }
234 else
235 {
236 // should we do anything?
237 // update payload? add new payload?
238 return false;
239 }
240 }
241 else
242 return false; // cannot insert entry
243}
244
245template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800246void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800247ContentStoreImpl<Policy>::Print (std::ostream &os) const
248{
249 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
250 item != this->getPolicy ().end ();
251 item++)
252 {
253 os << item->payload ()->GetName () << std::endl;
254 }
255}
256
257template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800258void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800259ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
260{
261 this->getPolicy ().set_max_size (maxSize);
262}
263
264template<class Policy>
265uint32_t
266ContentStoreImpl<Policy>::GetMaxSize () const
267{
268 return this->getPolicy ().get_max_size ();
269}
270
271template<class Policy>
272uint32_t
273ContentStoreImpl<Policy>::GetSize () const
274{
275 return this->getPolicy ().size ();
276}
277
278template<class Policy>
279Ptr<Entry>
280ContentStoreImpl<Policy>::Begin ()
281{
282 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
283 for (; item != end; item++)
284 {
285 if (item->payload () == 0) continue;
286 break;
287 }
288
289 if (item == end)
290 return End ();
291 else
292 return item->payload ();
293}
294
295template<class Policy>
296Ptr<Entry>
297ContentStoreImpl<Policy>::End ()
298{
299 return 0;
300}
301
302template<class Policy>
303Ptr<Entry>
304ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
305{
306 if (from == 0) return 0;
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800307
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800308 typename super::parent_trie::recursive_iterator
309 item (*StaticCast< entry > (from)->to_iterator ()),
310 end (0);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800311
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800312 for (item++; item != end; item++)
313 {
314 if (item->payload () == 0) continue;
315 break;
316 }
317
318 if (item == end)
319 return End ();
320 else
321 return item->payload ();
322}
323
324
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700325} // namespace cs
326} // namespace ndn
327} // namespace ns3
328
329#endif // NDN_CONTENT_STORE_IMPL_H_