NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
content-store-impl.h
1 /* -*- 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"
26 #include "ns3/ndn-interest.h"
27 #include "ns3/ndn-data.h"
28 #include <boost/foreach.hpp>
29 
30 #include "ns3/log.h"
31 #include "ns3/uinteger.h"
32 #include "ns3/string.h"
33 
34 #include "../../utils/trie/trie-with-policy.h"
35 
36 namespace ns3 {
37 namespace ndn {
38 namespace cs {
39 
44 template<class CS>
45 class EntryImpl : public Entry
46 {
47 public:
48  typedef Entry base_type;
49 
50 public:
51  EntryImpl (Ptr<ContentStore> cs, Ptr<const Data> data)
52  : Entry (cs, data)
53  , 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_; }
65 
66 private:
67  typename CS::super::iterator item_;
68 };
69 
70 
71 
76 template<class Policy>
78  protected ndnSIM::trie_with_policy< Name,
79  ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >,
80  Policy >
81 {
82 public:
85  Policy > super;
86 
88 
89  static TypeId
90  GetTypeId ();
91 
92  ContentStoreImpl () { };
93  virtual ~ContentStoreImpl () { };
94 
95  // from ContentStore
96 
97  virtual inline Ptr<Data>
98  Lookup (Ptr<const Interest> interest);
99 
100  virtual inline bool
101  Add (Ptr<const Data> data);
102 
103  // virtual bool
104  // Remove (Ptr<Interest> header);
105 
106  virtual inline void
107  Print (std::ostream &os) const;
108 
109  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 
121  const typename super::policy_container &
122  GetPolicy () const { return super::getPolicy (); }
123 
124  typename super::policy_container &
125  GetPolicy () { return super::getPolicy (); }
126 
127 private:
128  void
129  SetMaxSize (uint32_t maxSize);
130 
131  uint32_t
132  GetMaxSize () const;
133 
134 private:
135  static LogComponent g_log;
136 
138  TracedCallback< Ptr<const Entry> > m_didAddEntry;
139 };
140 
144 
145 
146 template<class Policy>
147 LogComponent ContentStoreImpl< Policy >::g_log = LogComponent (("ndn.cs." + Policy::GetName ()).c_str ());
148 
149 
150 template<class Policy>
151 TypeId
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,
163  MakeUintegerChecker<uint32_t> ())
164 
165  .AddTraceSource ("DidAddEntry", "Trace fired every time entry is successfully added to the cache",
166  MakeTraceSourceAccessor (&ContentStoreImpl< Policy >::m_didAddEntry))
167  ;
168 
169  return tid;
170 }
171 
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 
186 private:
187  const Exclude &m_exclude;
188 };
189 
190 template<class Policy>
191 Ptr<Data>
192 ContentStoreImpl<Policy>::Lookup (Ptr<const Interest> interest)
193 {
194  NS_LOG_FUNCTION (this << interest->GetName ());
195 
196  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  }
206 
207  if (node != this->end ())
208  {
209  this->m_cacheHitsTrace (interest, node->payload ()->GetData ());
210 
211  Ptr<Data> copy = Create<Data> (*node->payload ()->GetData ());
212  ConstCast<Packet> (copy->GetPayload ())->RemoveAllPacketTags ();
213  return copy;
214  }
215  else
216  {
217  this->m_cacheMissesTrace (interest);
218  return 0;
219  }
220 }
221 
222 template<class Policy>
223 bool
224 ContentStoreImpl<Policy>::Add (Ptr<const Data> data)
225 {
226  NS_LOG_FUNCTION (this << data->GetName ());
227 
228  Ptr< entry > newEntry = Create< entry > (this, data);
229  std::pair< typename super::iterator, bool > result = super::insert (data->GetName (), newEntry);
230 
231  if (result.first != super::end ())
232  {
233  if (result.second)
234  {
235  newEntry->SetTrie (result.first);
236 
237  m_didAddEntry (newEntry);
238  return true;
239  }
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 
251 template<class Policy>
252 void
253 ContentStoreImpl<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 
263 template<class Policy>
264 void
265 ContentStoreImpl<Policy>::SetMaxSize (uint32_t maxSize)
266 {
267  this->getPolicy ().set_max_size (maxSize);
268 }
269 
270 template<class Policy>
271 uint32_t
272 ContentStoreImpl<Policy>::GetMaxSize () const
273 {
274  return this->getPolicy ().get_max_size ();
275 }
276 
277 template<class Policy>
278 uint32_t
280 {
281  return this->getPolicy ().size ();
282 }
283 
284 template<class Policy>
285 Ptr<Entry>
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 
301 template<class Policy>
302 Ptr<Entry>
304 {
305  return 0;
306 }
307 
308 template<class Policy>
309 Ptr<Entry>
310 ContentStoreImpl<Policy>::Next (Ptr<Entry> from)
311 {
312  if (from == 0) return 0;
313 
314  typename super::parent_trie::recursive_iterator
315  item (*StaticCast< entry > (from)->to_iterator ()),
316  end (0);
317 
318  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 
331 } // namespace cs
332 } // namespace ndn
333 } // namespace ns3
334 
335 #endif // NDN_CONTENT_STORE_IMPL_H_
bool isExcluded(const name::Component &comp) const
Check if name component is excluded.
Definition: exclude.cc:34
Base class for NDN content store.
Class for NDN Name.
Definition: name.h:29
Class to represent Exclude component in NDN interests.
Definition: exclude.h:25
virtual Ptr< Data > Lookup(Ptr< const Interest > interest)
Find corresponding CS entry for the given interest.
virtual bool Add(Ptr< const Data > data)
Add a new content to the content store.
virtual Ptr< Entry > End()
Return item next after last (no order guaranteed)
virtual uint32_t GetSize() const
Get number of entries in content store.
NDN content store entry.
Base implementation of NDN content store.
Cache entry implementation with additional references to the base container.
Class to representing binary blob of NDN name component.
virtual Ptr< Entry > Begin()
Return first element of content store (no order guaranteed)
virtual void Print(std::ostream &os) const
Print out content store entries.
Entry(Ptr< ContentStore > cs, Ptr< const Data > data)
Construct content store entry.