blob: ffe8038b257dcdb107baca28dfe2bfbe6408d9f6 [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 Afanasyeveae83ee2013-03-15 15:01:10 -070047 EntryImpl (Ptr<ContentStore> cs, Ptr<const ContentObject> header, Ptr<const Packet> packet)
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -070048 : Entry (cs, header, packet)
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 Afanasyeveae83ee2013-03-15 15:01:10 -070089 virtual inline boost::tuple<Ptr<Packet>, Ptr<const ContentObject>, Ptr<const Packet> >
90 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 Afanasyeveae83ee2013-03-15 15:01:10 -070093 Add (Ptr<const ContentObject> header, Ptr<const Packet> packet);
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 Afanasyeveae83ee2013-03-15 15:01:10 -0700159boost::tuple<Ptr<Packet>, Ptr<const ContentObject>, Ptr<const Packet> >
160ContentStoreImpl<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 {
169 this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
170
171 // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
172 return boost::make_tuple (node->payload ()->GetFullyFormedNdnPacket (),
173 node->payload ()->GetHeader (),
174 node->payload ()->GetPacket ());
175 }
176 else
177 {
178 // NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
179 this->m_cacheMissesTrace (interest);
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700180 return boost::tuple<Ptr<Packet>, Ptr<ContentObject>, Ptr<Packet> > (0, 0, 0);
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800181 }
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800182}
183
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800184template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800185bool
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700186ContentStoreImpl<Policy>::Add (Ptr<const ContentObject> header, Ptr<const Packet> packet)
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800187{
188 NS_LOG_FUNCTION (this << header->GetName ());
189
Alexander Afanasyev0e4ae8e2013-03-12 15:59:18 -0700190 Ptr< entry > newEntry = Create< entry > (this, header, packet);
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800191 std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry);
192
193 if (result.first != super::end ())
194 {
195 if (result.second)
196 {
197 newEntry->SetTrie (result.first);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800198
199 m_didAddEntry (newEntry);
200 return true;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800201 }
202 else
203 {
204 // should we do anything?
205 // update payload? add new payload?
206 return false;
207 }
208 }
209 else
210 return false; // cannot insert entry
211}
212
213template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800214void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800215ContentStoreImpl<Policy>::Print (std::ostream &os) const
216{
217 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
218 item != this->getPolicy ().end ();
219 item++)
220 {
221 os << item->payload ()->GetName () << std::endl;
222 }
223}
224
225template<class Policy>
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800226void
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800227ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
228{
229 this->getPolicy ().set_max_size (maxSize);
230}
231
232template<class Policy>
233uint32_t
234ContentStoreImpl<Policy>::GetMaxSize () const
235{
236 return this->getPolicy ().get_max_size ();
237}
238
239template<class Policy>
240uint32_t
241ContentStoreImpl<Policy>::GetSize () const
242{
243 return this->getPolicy ().size ();
244}
245
246template<class Policy>
247Ptr<Entry>
248ContentStoreImpl<Policy>::Begin ()
249{
250 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
251 for (; item != end; item++)
252 {
253 if (item->payload () == 0) continue;
254 break;
255 }
256
257 if (item == end)
258 return End ();
259 else
260 return item->payload ();
261}
262
263template<class Policy>
264Ptr<Entry>
265ContentStoreImpl<Policy>::End ()
266{
267 return 0;
268}
269
270template<class Policy>
271Ptr<Entry>
272ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
273{
274 if (from == 0) return 0;
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800275
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800276 typename super::parent_trie::recursive_iterator
277 item (*StaticCast< entry > (from)->to_iterator ()),
278 end (0);
Alexander Afanasyev25093a32013-02-01 13:00:13 -0800279
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800280 for (item++; item != end; item++)
281 {
282 if (item->payload () == 0) continue;
283 break;
284 }
285
286 if (item == end)
287 return End ();
288 else
289 return item->payload ();
290}
291
292
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700293} // namespace cs
294} // namespace ndn
295} // namespace ns3
296
297#endif // NDN_CONTENT_STORE_IMPL_H_