blob: 1254bb25a58df44f39485c26d2f98e1cdcd85f63 [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
Alexander Afanasyev0c395372014-12-20 15:54:02 -080024#include "ndn-content-store.hpp"
25
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070026#include "ns3/packet.h"
Alexander Afanasyevbd9c18e2012-11-19 15:23:41 -080027#include "ns3/ndn-interest.h"
Alexander Afanasyev6eba36f2013-08-07 17:42:54 -070028#include "ns3/ndn-data.h"
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070029#include <boost/foreach.hpp>
30
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080031#include "ns3/log.h"
32#include "ns3/uinteger.h"
33#include "ns3/string.h"
34
Alexander Afanasyev0c395372014-12-20 15:54:02 -080035#include "../../utils/trie/trie-with-policy.hpp"
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036
37namespace ns3 {
38namespace ndn {
39namespace cs {
40
Alexander Afanasyev79206512013-07-27 16:49:12 -070041/**
42 * @ingroup ndn-cs
43 * @brief Cache entry implementation with additional references to the base container
44 */
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070045template<class CS>
46class EntryImpl : public Entry
47{
48public:
Alexander Afanasyev8566f452012-12-10 15:21:51 -080049 typedef Entry base_type;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080050
Alexander Afanasyev8566f452012-12-10 15:21:51 -080051public:
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070052 EntryImpl (Ptr<ContentStore> cs, Ptr<const Data> data)
Alexander Afanasyevb989b122013-07-10 17:15:46 -070053 : Entry (cs, data)
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070054 , item_ (0)
55 {
56 }
57
58 void
59 SetTrie (typename CS::super::iterator item)
60 {
61 item_ = item;
62 }
63
64 typename CS::super::iterator to_iterator () { return item_; }
65 typename CS::super::const_iterator to_iterator () const { return item_; }
Alexander Afanasyev25093a32013-02-01 13:00:13 -080066
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070067private:
68 typename CS::super::iterator item_;
69};
70
71
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072
Alexander Afanasyev79206512013-07-27 16:49:12 -070073/**
74 * @ingroup ndn-cs
75 * @brief Base implementation of NDN content store
76 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070077template<class Policy>
78class ContentStoreImpl : public ContentStore,
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070079 protected ndnSIM::trie_with_policy< Name,
Alexander Afanasyev8566f452012-12-10 15:21:51 -080080 ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070081 Policy >
82{
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070083public:
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070084 typedef ndnSIM::trie_with_policy< Name,
Alexander Afanasyev8566f452012-12-10 15:21:51 -080085 ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070086 Policy > super;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080087
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070088 typedef EntryImpl< ContentStoreImpl< Policy > > entry;
Alexander Afanasyev25093a32013-02-01 13:00:13 -080089
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090 static TypeId
91 GetTypeId ();
Alexander Afanasyev25093a32013-02-01 13:00:13 -080092
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093 ContentStoreImpl () { };
94 virtual ~ContentStoreImpl () { };
Alexander Afanasyev25093a32013-02-01 13:00:13 -080095
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 // from ContentStore
Alexander Afanasyev25093a32013-02-01 13:00:13 -080097
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070098 virtual inline Ptr<Data>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070099 Lookup (Ptr<const Interest> interest);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800100
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101 virtual inline bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700102 Add (Ptr<const Data> data);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700103
104 // virtual bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700105 // Remove (Ptr<Interest> header);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800106
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700107 virtual inline void
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800108 Print (std::ostream &os) const;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700109
Alexander Afanasyev29c19b92012-09-03 23:46:41 -0700110 virtual uint32_t
111 GetSize () const;
112
113 virtual Ptr<Entry>
114 Begin ();
115
116 virtual Ptr<Entry>
117 End ();
118
119 virtual Ptr<Entry>
120 Next (Ptr<Entry>);
121
Alexander Afanasyevf21bfc12013-11-17 11:58:33 -0800122 const typename super::policy_container &
123 GetPolicy () const { return super::getPolicy (); }
124
125 typename super::policy_container &
126 GetPolicy () { return super::getPolicy (); }
127
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700128private:
129 void
130 SetMaxSize (uint32_t maxSize);
131
132 uint32_t
133 GetMaxSize () const;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800134
135private:
136 static LogComponent g_log; ///< @brief Logging variable
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800137
138 /// @brief trace of for entry additions (fired every time entry is successfully added to the cache): first parameter is pointer to the CS entry
139 TracedCallback< Ptr<const Entry> > m_didAddEntry;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700140};
141
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800142//////////////////////////////////////////
143////////// Implementation ////////////////
144//////////////////////////////////////////
145
146
147template<class Policy>
148LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ());
149
150
151template<class Policy>
152TypeId
153ContentStoreImpl< Policy >::GetTypeId ()
154{
155 static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ())
156 .SetGroupName ("Ndn")
157 .SetParent<ContentStore> ()
158 .AddConstructor< ContentStoreImpl< Policy > > ()
159 .AddAttribute ("MaxSize",
160 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
161 StringValue ("100"),
162 MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize,
163 &ContentStoreImpl< Policy >::SetMaxSize),
164 MakeUintegerChecker<uint32_t> ())
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800165
166 .AddTraceSource ("DidAddEntry", "Trace fired every time entry is successfully added to the cache",
167 MakeTraceSourceAccessor (&ContentStoreImpl< Policy >::m_didAddEntry))
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800168 ;
169
170 return tid;
171}
172
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -0700173struct isNotExcluded
174{
175 inline
176 isNotExcluded (const Exclude &exclude)
177 : m_exclude (exclude)
178 {
179 }
180
181 bool
182 operator () (const name::Component &comp) const
183 {
184 return !m_exclude.isExcluded (comp);
185 }
186
187private:
188 const Exclude &m_exclude;
189};
190
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800191template<class Policy>
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700192Ptr<Data>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700193ContentStoreImpl<Policy>::Lookup (Ptr<const Interest> interest)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800194{
195 NS_LOG_FUNCTION (this << interest->GetName ());
196
Alexander Afanasyeveec89ba2013-07-19 16:34:30 -0700197 typename super::const_iterator node;
198 if (interest->GetExclude () == 0)
199 {
200 node = this->deepest_prefix_match (interest->GetName ());
201 }
202 else
203 {
204 node = this->deepest_prefix_match_if_next_level (interest->GetName (),
205 isNotExcluded (*interest->GetExclude ()));
206 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800207
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800208 if (node != this->end ())
209 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700210 this->m_cacheHitsTrace (interest, node->payload ()->GetData ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800211
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700212 Ptr<Data> copy = Create<Data> (*node->payload ()->GetData ());
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700213 ConstCast<Packet> (copy->GetPayload ())->RemoveAllPacketTags ();
214 return copy;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800215 }
216 else
217 {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800218 this->m_cacheMissesTrace (interest);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700219 return 0;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800220 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800221}
222
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800223template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800224bool
Alexander Afanasyev772f51b2013-08-01 18:53:25 -0700225ContentStoreImpl<Policy>::Add (Ptr<const Data> data)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800226{
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700227 NS_LOG_FUNCTION (this << data->GetName ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800228
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700229 Ptr< entry > newEntry = Create< entry > (this, data);
230 std::pair< typename super::iterator, bool > result = super::insert (data->GetName (), newEntry);
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800231
232 if (result.first != super::end ())
233 {
234 if (result.second)
235 {
236 newEntry->SetTrie (result.first);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800237
238 m_didAddEntry (newEntry);
239 return true;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800240 }
241 else
242 {
243 // should we do anything?
244 // update payload? add new payload?
245 return false;
246 }
247 }
248 else
249 return false; // cannot insert entry
250}
251
252template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800253void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800254ContentStoreImpl<Policy>::Print (std::ostream &os) const
255{
256 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
257 item != this->getPolicy ().end ();
258 item++)
259 {
260 os << item->payload ()->GetName () << std::endl;
261 }
262}
263
264template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800265void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800266ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
267{
268 this->getPolicy ().set_max_size (maxSize);
269}
270
271template<class Policy>
272uint32_t
273ContentStoreImpl<Policy>::GetMaxSize () const
274{
275 return this->getPolicy ().get_max_size ();
276}
277
278template<class Policy>
279uint32_t
280ContentStoreImpl<Policy>::GetSize () const
281{
282 return this->getPolicy ().size ();
283}
284
285template<class Policy>
286Ptr<Entry>
287ContentStoreImpl<Policy>::Begin ()
288{
289 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
290 for (; item != end; item++)
291 {
292 if (item->payload () == 0) continue;
293 break;
294 }
295
296 if (item == end)
297 return End ();
298 else
299 return item->payload ();
300}
301
302template<class Policy>
303Ptr<Entry>
304ContentStoreImpl<Policy>::End ()
305{
306 return 0;
307}
308
309template<class Policy>
310Ptr<Entry>
311ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
312{
313 if (from == 0) return 0;
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800314
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800315 typename super::parent_trie::recursive_iterator
316 item (*StaticCast< entry > (from)->to_iterator ()),
317 end (0);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800318
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800319 for (item++; item != end; item++)
320 {
321 if (item->payload () == 0) continue;
322 break;
323 }
324
325 if (item == end)
326 return End ();
327 else
328 return item->payload ();
329}
330
331
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700332} // namespace cs
333} // namespace ndn
334} // namespace ns3
335
336#endif // NDN_CONTENT_STORE_IMPL_H_