blob: 9cd934e4b783ef1c4888135a47f11adbf24aff30 [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
158template<class Policy>
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700159Ptr<const ContentObject>
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700160ContentStoreImpl<Policy>::Lookup (Ptr<const Interest> interest)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800161{
162 NS_LOG_FUNCTION (this << interest->GetName ());
163
164 /// @todo Change to search with predicate
165 typename super::const_iterator node = this->deepest_prefix_match (interest->GetName ());
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800166
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800167 if (node != this->end ())
168 {
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700169 this->m_cacheHitsTrace (interest, node->payload ()->GetData ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800170
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700171 Ptr<ContentObject> copy = Create<ContentObject> (node->payload ()->GetData ());
172 ConstCast<Packet> (copy->GetPayload ())->RemoveAllPacketTags ();
173 return copy;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800174 }
175 else
176 {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800177 this->m_cacheMissesTrace (interest);
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700178 return 0;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800179 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800180}
181
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800182template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800183bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700184ContentStoreImpl<Policy>::Add (Ptr<const ContentObject> data)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800185{
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700186 NS_LOG_FUNCTION (this << data->GetName ());
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800187
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700188 Ptr< entry > newEntry = Create< entry > (this, data);
189 std::pair< typename super::iterator, bool > result = super::insert (data->GetName (), newEntry);
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800190
191 if (result.first != super::end ())
192 {
193 if (result.second)
194 {
195 newEntry->SetTrie (result.first);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800196
197 m_didAddEntry (newEntry);
198 return true;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800199 }
200 else
201 {
202 // should we do anything?
203 // update payload? add new payload?
204 return false;
205 }
206 }
207 else
208 return false; // cannot insert entry
209}
210
211template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800212void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800213ContentStoreImpl<Policy>::Print (std::ostream &os) const
214{
215 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
216 item != this->getPolicy ().end ();
217 item++)
218 {
219 os << item->payload ()->GetName () << std::endl;
220 }
221}
222
223template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800224void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800225ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
226{
227 this->getPolicy ().set_max_size (maxSize);
228}
229
230template<class Policy>
231uint32_t
232ContentStoreImpl<Policy>::GetMaxSize () const
233{
234 return this->getPolicy ().get_max_size ();
235}
236
237template<class Policy>
238uint32_t
239ContentStoreImpl<Policy>::GetSize () const
240{
241 return this->getPolicy ().size ();
242}
243
244template<class Policy>
245Ptr<Entry>
246ContentStoreImpl<Policy>::Begin ()
247{
248 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
249 for (; item != end; item++)
250 {
251 if (item->payload () == 0) continue;
252 break;
253 }
254
255 if (item == end)
256 return End ();
257 else
258 return item->payload ();
259}
260
261template<class Policy>
262Ptr<Entry>
263ContentStoreImpl<Policy>::End ()
264{
265 return 0;
266}
267
268template<class Policy>
269Ptr<Entry>
270ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
271{
272 if (from == 0) return 0;
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800273
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800274 typename super::parent_trie::recursive_iterator
275 item (*StaticCast< entry > (from)->to_iterator ()),
276 end (0);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800277
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800278 for (item++; item != end; item++)
279 {
280 if (item->payload () == 0) continue;
281 break;
282 }
283
284 if (item == end)
285 return End ();
286 else
287 return item->payload ();
288}
289
290
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700291} // namespace cs
292} // namespace ndn
293} // namespace ns3
294
295#endif // NDN_CONTENT_STORE_IMPL_H_