blob: 403ceefe795680306fe8e72c5502480b4cd0f52a [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 Afanasyevf21bfc12013-11-17 11:58:33 -0800121 const typename super::policy_container &
122 GetPolicy () const { return super::getPolicy (); }
123
124 typename super::policy_container &
125 GetPolicy () { return super::getPolicy (); }
126
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127private:
128 void
129 SetMaxSize (uint32_t maxSize);
130
131 uint32_t
132 GetMaxSize () const;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800133
134private:
135 static LogComponent g_log; ///< @brief Logging variable
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800136
137 /// @brief trace of for entry additions (fired every time entry is successfully added to the cache): first parameter is pointer to the CS entry
138 TracedCallback< Ptr<const Entry> > m_didAddEntry;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700139};
140
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800141//////////////////////////////////////////
142////////// Implementation ////////////////
143//////////////////////////////////////////
144
145
146template<class Policy>
147LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ());
148
149
150template<class Policy>
151TypeId
152ContentStoreImpl< Policy >::GetTypeId ()
153{
154 static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ())
155 .SetGroupName ("Ndn")
156 .SetParent<ContentStore> ()
157 .AddConstructor< ContentStoreImpl< Policy > > ()
158 .AddAttribute ("MaxSize",
159 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
160 StringValue ("100"),
161 MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize,
162 &ContentStoreImpl< Policy >::SetMaxSize),
163 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800164
165 .AddTraceSource ("DidAddEntry", "Trace fired every time entry is successfully added to the cache",
166 MakeTraceSourceAccessor (&ContentStoreImpl< Policy >::m_didAddEntry))
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800167 ;
168
169 return tid;
170}
171
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -0700172struct isNotExcluded
173{
174 inline
175 isNotExcluded (const Exclude &exclude)
176 : m_exclude (exclude)
177 {
178 }
179
180 bool
181 operator () (const name::Component &comp) const
182 {
183 return !m_exclude.isExcluded (comp);
184 }
185
186private:
187 const Exclude &m_exclude;
188};
189
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800190template<class Policy>
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700191Ptr<Data>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700192ContentStoreImpl<Policy>::Lookup (Ptr<const Interest> interest)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800193{
194 NS_LOG_FUNCTION (this << interest->GetName ());
195
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -0700196 typename super::const_iterator node;
197 if (interest->GetExclude () == 0)
198 {
199 node = this->deepest_prefix_match (interest->GetName ());
200 }
201 else
202 {
203 node = this->deepest_prefix_match_if_next_level (interest->GetName (),
204 isNotExcluded (*interest->GetExclude ()));
205 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800206
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800207 if (node != this->end ())
208 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700209 this->m_cacheHitsTrace (interest, node->payload ()->GetData ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800210
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700211 Ptr<Data> copy = Create<Data> (*node->payload ()->GetData ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700212 ConstCast<Packet> (copy->GetPayload ())->RemoveAllPacketTags ();
213 return copy;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800214 }
215 else
216 {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800217 this->m_cacheMissesTrace (interest);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700218 return 0;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800219 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800220}
221
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800222template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800223bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700224ContentStoreImpl<Policy>::Add (Ptr<const Data> data)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800225{
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700226 NS_LOG_FUNCTION (this << data->GetName ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800227
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700228 Ptr< entry > newEntry = Create< entry > (this, data);
229 std::pair< typename super::iterator, bool > result = super::insert (data->GetName (), newEntry);
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800230
231 if (result.first != super::end ())
232 {
233 if (result.second)
234 {
235 newEntry->SetTrie (result.first);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800236
237 m_didAddEntry (newEntry);
238 return true;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800239 }
240 else
241 {
242 // should we do anything?
243 // update payload? add new payload?
244 return false;
245 }
246 }
247 else
248 return false; // cannot insert entry
249}
250
251template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800252void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800253ContentStoreImpl<Policy>::Print (std::ostream &os) const
254{
255 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
256 item != this->getPolicy ().end ();
257 item++)
258 {
259 os << item->payload ()->GetName () << std::endl;
260 }
261}
262
263template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800264void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800265ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
266{
267 this->getPolicy ().set_max_size (maxSize);
268}
269
270template<class Policy>
271uint32_t
272ContentStoreImpl<Policy>::GetMaxSize () const
273{
274 return this->getPolicy ().get_max_size ();
275}
276
277template<class Policy>
278uint32_t
279ContentStoreImpl<Policy>::GetSize () const
280{
281 return this->getPolicy ().size ();
282}
283
284template<class Policy>
285Ptr<Entry>
286ContentStoreImpl<Policy>::Begin ()
287{
288 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
289 for (; item != end; item++)
290 {
291 if (item->payload () == 0) continue;
292 break;
293 }
294
295 if (item == end)
296 return End ();
297 else
298 return item->payload ();
299}
300
301template<class Policy>
302Ptr<Entry>
303ContentStoreImpl<Policy>::End ()
304{
305 return 0;
306}
307
308template<class Policy>
309Ptr<Entry>
310ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
311{
312 if (from == 0) return 0;
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800313
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800314 typename super::parent_trie::recursive_iterator
315 item (*StaticCast< entry > (from)->to_iterator ()),
316 end (0);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800317
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800318 for (item++; item != end; item++)
319 {
320 if (item->payload () == 0) continue;
321 break;
322 }
323
324 if (item == end)
325 return End ();
326 else
327 return item->payload ();
328}
329
330
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700331} // namespace cs
332} // namespace ndn
333} // namespace ns3
334
335#endif // NDN_CONTENT_STORE_IMPL_H_