blob: a168636b1040eb301d363a6e625f5e388ca1982f [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:
44 EntryImpl (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet)
45 : Entry (header, packet)
46 , item_ (0)
47 {
48 }
49
50 void
51 SetTrie (typename CS::super::iterator item)
52 {
53 item_ = item;
54 }
55
56 typename CS::super::iterator to_iterator () { return item_; }
57 typename CS::super::const_iterator to_iterator () const { return item_; }
58
59private:
60 typename CS::super::iterator item_;
61};
62
63
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064
65template<class Policy>
66class ContentStoreImpl : public ContentStore,
67 protected ndnSIM::trie_with_policy< NameComponents,
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070068 ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > > >,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070069 Policy >
70{
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070071public:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072 typedef ndnSIM::trie_with_policy< NameComponents,
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070073 ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > > >,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074 Policy > super;
75
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070076 typedef EntryImpl< ContentStoreImpl< Policy > > entry;
77
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070078 static TypeId
79 GetTypeId ();
80
81 ContentStoreImpl () { };
82 virtual ~ContentStoreImpl () { };
83
84 // from ContentStore
85
86 virtual inline boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> >
87 Lookup (Ptr<const InterestHeader> interest);
88
89 virtual inline bool
90 Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet);
91
92 // virtual bool
93 // Remove (Ptr<InterestHeader> header);
94
95 virtual inline void
96 Print (std::ostream &os) const;
97
Alexander Afanasyev29c19b92012-09-03 23:46:41 -070098 virtual uint32_t
99 GetSize () const;
100
101 virtual Ptr<Entry>
102 Begin ();
103
104 virtual Ptr<Entry>
105 End ();
106
107 virtual Ptr<Entry>
108 Next (Ptr<Entry>);
109
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110private:
111 void
112 SetMaxSize (uint32_t maxSize);
113
114 uint32_t
115 GetMaxSize () const;
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800116
117private:
118 static LogComponent g_log; ///< @brief Logging variable
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700119};
120
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -0800121//////////////////////////////////////////
122////////// Implementation ////////////////
123//////////////////////////////////////////
124
125
126template<class Policy>
127LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ());
128
129
130template<class Policy>
131TypeId
132ContentStoreImpl< Policy >::GetTypeId ()
133{
134 static TypeId tid = TypeId (("ns3::ndn::cs::"+Policy::GetName ()).c_str ())
135 .SetGroupName ("Ndn")
136 .SetParent<ContentStore> ()
137 .AddConstructor< ContentStoreImpl< Policy > > ()
138 .AddAttribute ("MaxSize",
139 "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
140 StringValue ("100"),
141 MakeUintegerAccessor (&ContentStoreImpl< Policy >::GetMaxSize,
142 &ContentStoreImpl< Policy >::SetMaxSize),
143 MakeUintegerChecker<uint32_t> ())
144 ;
145
146 return tid;
147}
148
149template<class Policy>
150boost::tuple<Ptr<Packet>, Ptr<const ContentObjectHeader>, Ptr<const Packet> >
151ContentStoreImpl<Policy>::Lookup (Ptr<const InterestHeader> interest)
152{
153 NS_LOG_FUNCTION (this << interest->GetName ());
154
155 /// @todo Change to search with predicate
156 typename super::const_iterator node = this->deepest_prefix_match (interest->GetName ());
157
158 if (node != this->end ())
159 {
160 this->m_cacheHitsTrace (interest, node->payload ()->GetHeader ());
161
162 // NS_LOG_DEBUG ("cache hit with " << node->payload ()->GetHeader ()->GetName ());
163 return boost::make_tuple (node->payload ()->GetFullyFormedNdnPacket (),
164 node->payload ()->GetHeader (),
165 node->payload ()->GetPacket ());
166 }
167 else
168 {
169 // NS_LOG_DEBUG ("cache miss for " << interest->GetName ());
170 this->m_cacheMissesTrace (interest);
171 return boost::tuple<Ptr<Packet>, Ptr<ContentObjectHeader>, Ptr<Packet> > (0, 0, 0);
172 }
173}
174
175template<class Policy>
176bool
177ContentStoreImpl<Policy>::Add (Ptr<const ContentObjectHeader> header, Ptr<const Packet> packet)
178{
179 NS_LOG_FUNCTION (this << header->GetName ());
180
181 Ptr< entry > newEntry = Create< entry > (header, packet);
182 std::pair< typename super::iterator, bool > result = super::insert (header->GetName (), newEntry);
183
184 if (result.first != super::end ())
185 {
186 if (result.second)
187 {
188 newEntry->SetTrie (result.first);
189 return newEntry;
190 }
191 else
192 {
193 // should we do anything?
194 // update payload? add new payload?
195 return false;
196 }
197 }
198 else
199 return false; // cannot insert entry
200}
201
202template<class Policy>
203void
204ContentStoreImpl<Policy>::Print (std::ostream &os) const
205{
206 for (typename super::policy_container::const_iterator item = this->getPolicy ().begin ();
207 item != this->getPolicy ().end ();
208 item++)
209 {
210 os << item->payload ()->GetName () << std::endl;
211 }
212}
213
214template<class Policy>
215void
216ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
217{
218 this->getPolicy ().set_max_size (maxSize);
219}
220
221template<class Policy>
222uint32_t
223ContentStoreImpl<Policy>::GetMaxSize () const
224{
225 return this->getPolicy ().get_max_size ();
226}
227
228template<class Policy>
229uint32_t
230ContentStoreImpl<Policy>::GetSize () const
231{
232 return this->getPolicy ().size ();
233}
234
235template<class Policy>
236Ptr<Entry>
237ContentStoreImpl<Policy>::Begin ()
238{
239 typename super::parent_trie::recursive_iterator item (super::getTrie ()), end (0);
240 for (; item != end; item++)
241 {
242 if (item->payload () == 0) continue;
243 break;
244 }
245
246 if (item == end)
247 return End ();
248 else
249 return item->payload ();
250}
251
252template<class Policy>
253Ptr<Entry>
254ContentStoreImpl<Policy>::End ()
255{
256 return 0;
257}
258
259template<class Policy>
260Ptr<Entry>
261ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
262{
263 if (from == 0) return 0;
264
265 typename super::parent_trie::recursive_iterator
266 item (*StaticCast< entry > (from)->to_iterator ()),
267 end (0);
268
269 for (item++; item != end; item++)
270 {
271 if (item->payload () == 0) continue;
272 break;
273 }
274
275 if (item == end)
276 return End ();
277 else
278 return item->payload ();
279}
280
281
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700282} // namespace cs
283} // namespace ndn
284} // namespace ns3
285
286#endif // NDN_CONTENT_STORE_IMPL_H_