NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
content-store-impl.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #ifndef NDN_CONTENT_STORE_IMPL_H_
21 #define NDN_CONTENT_STORE_IMPL_H_
22 
23 #include "ns3/ndnSIM/model/ndn-common.hpp"
24 
25 #include "ndn-content-store.hpp"
26 
27 #include "ns3/packet.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.hpp"
35 
36 namespace ns3 {
37 namespace ndn {
38 namespace cs {
39 
44 template<class CS>
45 class EntryImpl : public Entry {
46 public:
47  typedef Entry base_type;
48 
49 public:
50  EntryImpl(Ptr<ContentStore> cs, shared_ptr<const Data> data)
51  : Entry(cs, data)
52  , item_(0)
53  {
54  }
55 
56  void
58  {
59  item_ = item;
60  }
61 
62  typename CS::super::iterator
64  {
65  return item_;
66  }
67  typename CS::super::const_iterator
68  to_iterator() const
69  {
70  return item_;
71  }
72 
73 private:
74  typename CS::super::iterator item_;
75 };
76 
81 template<class Policy>
83  : public ContentStore,
84  protected ndnSIM::
85  trie_with_policy<Name,
86  ndnSIM::smart_pointer_payload_traits<EntryImpl<ContentStoreImpl<Policy>>,
87  Entry>,
88  Policy> {
89 public:
90  typedef ndnSIM::
91  trie_with_policy<Name, ndnSIM::smart_pointer_payload_traits<EntryImpl<ContentStoreImpl<Policy>>,
92  Entry>,
93  Policy> super;
94 
96 
97  static TypeId
98  GetTypeId();
99 
101  virtual ~ContentStoreImpl(){};
102 
103  // from ContentStore
104 
105  virtual inline shared_ptr<Data>
106  Lookup(shared_ptr<const Interest> interest);
107 
108  virtual inline bool
109  Add(shared_ptr<const Data> data);
110 
111  // virtual bool
112  // Remove (shared_ptr<Interest> header);
113 
114  virtual inline void
115  Print(std::ostream& os) const;
116 
117  virtual uint32_t
118  GetSize() const;
119 
120  virtual Ptr<Entry>
121  Begin();
122 
123  virtual Ptr<Entry>
124  End();
125 
126  virtual Ptr<Entry> Next(Ptr<Entry>);
127 
128  const typename super::policy_container&
129  GetPolicy() const
130  {
131  return super::getPolicy();
132  }
133 
134  typename super::policy_container&
136  {
137  return super::getPolicy();
138  }
139 
140 public:
141  typedef void (*CsEntryCallback)(Ptr<const Entry>);
142 
143 private:
144  void
145  SetMaxSize(uint32_t maxSize);
146 
147  uint32_t
148  GetMaxSize() const;
149 
150 private:
151  static LogComponent g_log;
152 
155  TracedCallback<Ptr<const Entry>> m_didAddEntry;
156 };
157 
161 
162 template<class Policy>
163 LogComponent ContentStoreImpl<Policy>::g_log = LogComponent(("ndn.cs." + Policy::GetName()).c_str(), __FILE__);
164 
165 template<class Policy>
166 TypeId
168 {
169  static TypeId tid =
170  TypeId(("ns3::ndn::cs::" + Policy::GetName()).c_str())
171  .SetGroupName("Ndn")
172  .SetParent<ContentStore>()
173  .AddConstructor<ContentStoreImpl<Policy>>()
174  .AddAttribute("MaxSize",
175  "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
176  StringValue("100"), MakeUintegerAccessor(&ContentStoreImpl<Policy>::GetMaxSize,
178  MakeUintegerChecker<uint32_t>())
179 
180  .AddTraceSource("DidAddEntry",
181  "Trace fired every time entry is successfully added to the cache",
182  MakeTraceSourceAccessor(&ContentStoreImpl<Policy>::m_didAddEntry),
183  "ns3::ndn::cs::ContentStoreImpl::CsEntryCallback");
184 
185  return tid;
186 }
187 
189  inline isNotExcluded(const Exclude& exclude)
190  : m_exclude(exclude)
191  {
192  }
193 
194  bool
195  operator()(const name::Component& comp) const
196  {
197  return !m_exclude.isExcluded(comp);
198  }
199 
200 private:
201  const Exclude& m_exclude;
202 };
203 
204 template<class Policy>
205 shared_ptr<Data>
206 ContentStoreImpl<Policy>::Lookup(shared_ptr<const Interest> interest)
207 {
208  NS_LOG_FUNCTION(this << interest->getName());
209 
210  typename super::const_iterator node;
211  if (interest->getExclude().empty()) {
212  node = this->deepest_prefix_match(interest->getName());
213  }
214  else {
215  node = this->deepest_prefix_match_if_next_level(interest->getName(),
216  isNotExcluded(interest->getExclude()));
217  }
218 
219  if (node != this->end()) {
220  this->m_cacheHitsTrace(interest, node->payload()->GetData());
221 
222  shared_ptr<Data> copy = make_shared<Data>(*node->payload()->GetData());
223  return copy;
224  }
225  else {
226  this->m_cacheMissesTrace(interest);
227  return 0;
228  }
229 }
230 
231 template<class Policy>
232 bool
233 ContentStoreImpl<Policy>::Add(shared_ptr<const Data> data)
234 {
235  NS_LOG_FUNCTION(this << data->getName());
236 
237  Ptr<entry> newEntry = Create<entry>(this, data);
238  std::pair<typename super::iterator, bool> result = super::insert(data->getName(), newEntry);
239 
240  if (result.first != super::end()) {
241  if (result.second) {
242  newEntry->SetTrie(result.first);
243 
244  m_didAddEntry(newEntry);
245  return true;
246  }
247  else {
248  // should we do anything?
249  // update payload? add new payload?
250  return false;
251  }
252  }
253  else
254  return false; // cannot insert entry
255 }
256 
257 template<class Policy>
258 void
259 ContentStoreImpl<Policy>::Print(std::ostream& os) const
260 {
261  for (typename super::policy_container::const_iterator item = this->getPolicy().begin();
262  item != this->getPolicy().end(); item++) {
263  os << item->payload ()->GetName () << std::endl;
264  }
265 }
266 
267 template<class Policy>
268 void
269 ContentStoreImpl<Policy>::SetMaxSize(uint32_t maxSize)
270 {
271  this->getPolicy().set_max_size(maxSize);
272 }
273 
274 template<class Policy>
275 uint32_t
277 {
278  return this->getPolicy().get_max_size();
279 }
280 
281 template<class Policy>
282 uint32_t
284 {
285  return this->getPolicy().size();
286 }
287 
288 template<class Policy>
289 Ptr<Entry>
291 {
292  typename super::parent_trie::recursive_iterator item(super::getTrie()), end(0);
293  for (; item != end; item++) {
294  if (item->payload() == 0)
295  continue;
296  break;
297  }
298 
299  if (item == end)
300  return End();
301  else
302  return item->payload();
303 }
304 
305 template<class Policy>
306 Ptr<Entry>
308 {
309  return 0;
310 }
311 
312 template<class Policy>
313 Ptr<Entry>
315 {
316  if (from == 0)
317  return 0;
318 
319  typename super::parent_trie::recursive_iterator item(*StaticCast<entry>(from)->to_iterator()),
320  end(0);
321 
322  for (item++; item != end; item++) {
323  if (item->payload() == 0)
324  continue;
325  break;
326  }
327 
328  if (item == end)
329  return End();
330  else
331  return item->payload();
332 }
333 
334 } // namespace cs
335 } // namespace ndn
336 } // namespace ns3
337 
338 #endif // NDN_CONTENT_STORE_IMPL_H_
virtual Ptr< Entry > Begin()
Return first element of content store (no order guaranteed)
Copyright (c) 2011-2015 Regents of the University of California.
void SetTrie(typename CS::super::iterator item)
virtual shared_ptr< Data > Lookup(shared_ptr< const Interest > interest)
Find corresponding CS entry for the given interest.
EntryImpl(Ptr< ContentStore > cs, shared_ptr< const Data > data)
NDN content store entry.
EntryImpl< ContentStoreImpl< Policy > > entry
super::policy_container & GetPolicy()
ndnSIM::trie_with_policy< Name, ndnSIM::smart_pointer_payload_traits< EntryImpl< ContentStoreImpl< Policy > >, Entry >, Policy > super
bool operator()(const name::Component &comp) const
Cache entry implementation with additional references to the base container.
const super::policy_container & GetPolicy() const
CS::super::iterator to_iterator()
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Copyright (c) 2011-2015 Regents of the University of California.
CS::super::const_iterator to_iterator() const
virtual Ptr< Entry > End()
Return item next after last (no order guaranteed)
virtual bool Add(shared_ptr< const Data > data)
Add a new content to the content store.
virtual void Print(std::ostream &os) const
Print out content store entries.
isNotExcluded(const Exclude &exclude)
Component holds a read-only name component value.
Base implementation of NDN content store.
virtual Ptr< Entry > Next(Ptr< Entry >)
virtual uint32_t GetSize() const
Get number of entries in content store.
Base class for NDN content store.