NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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
57  SetTrie(typename CS::super::iterator item)
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 private:
141  void
142  SetMaxSize(uint32_t maxSize);
143 
144  uint32_t
145  GetMaxSize() const;
146 
147 private:
148  static LogComponent g_log;
149 
152  TracedCallback<Ptr<const Entry>> m_didAddEntry;
153 };
154 
158 
159 template<class Policy>
160 LogComponent
161  ContentStoreImpl<Policy>::g_log = LogComponent(("ndn.cs." + Policy::GetName()).c_str());
162 
163 template<class Policy>
164 TypeId
166 {
167  static TypeId tid =
168  TypeId(("ns3::ndn::cs::" + Policy::GetName()).c_str())
169  .SetGroupName("Ndn")
170  .SetParent<ContentStore>()
171  .AddConstructor<ContentStoreImpl<Policy>>()
172  .AddAttribute("MaxSize",
173  "Set maximum number of entries in ContentStore. If 0, limit is not enforced",
174  StringValue("100"), MakeUintegerAccessor(&ContentStoreImpl<Policy>::GetMaxSize,
176  MakeUintegerChecker<uint32_t>())
177 
178  .AddTraceSource("DidAddEntry",
179  "Trace fired every time entry is successfully added to the cache",
180  MakeTraceSourceAccessor(&ContentStoreImpl<Policy>::m_didAddEntry));
181 
182  return tid;
183 }
184 
186  inline isNotExcluded(const Exclude& exclude)
187  : m_exclude(exclude)
188  {
189  }
190 
191  bool
192  operator()(const name::Component& comp) const
193  {
194  return !m_exclude.isExcluded(comp);
195  }
196 
197 private:
198  const Exclude& m_exclude;
199 };
200 
201 template<class Policy>
202 shared_ptr<Data>
203 ContentStoreImpl<Policy>::Lookup(shared_ptr<const Interest> interest)
204 {
205  NS_LOG_FUNCTION(this << interest->getName());
206 
207  typename super::const_iterator node;
208  if (interest->getExclude().empty()) {
209  node = this->deepest_prefix_match(interest->getName());
210  }
211  else {
212  node = this->deepest_prefix_match_if_next_level(interest->getName(),
213  isNotExcluded(interest->getExclude()));
214  }
215 
216  if (node != this->end()) {
217  this->m_cacheHitsTrace(interest, node->payload()->GetData());
218 
219  shared_ptr<Data> copy = make_shared<Data>(*node->payload()->GetData());
220  return copy;
221  }
222  else {
223  this->m_cacheMissesTrace(interest);
224  return 0;
225  }
226 }
227 
228 template<class Policy>
229 bool
230 ContentStoreImpl<Policy>::Add(shared_ptr<const Data> data)
231 {
232  NS_LOG_FUNCTION(this << data->getName());
233 
234  Ptr<entry> newEntry = Create<entry>(this, data);
235  std::pair<typename super::iterator, bool> result = super::insert(data->getName(), newEntry);
236 
237  if (result.first != super::end()) {
238  if (result.second) {
239  newEntry->SetTrie(result.first);
240 
241  m_didAddEntry(newEntry);
242  return true;
243  }
244  else {
245  // should we do anything?
246  // update payload? add new payload?
247  return false;
248  }
249  }
250  else
251  return false; // cannot insert entry
252 }
253 
254 template<class Policy>
255 void
256 ContentStoreImpl<Policy>::Print(std::ostream& os) const
257 {
258  for (typename super::policy_container::const_iterator item = this->getPolicy().begin();
259  item != this->getPolicy().end(); item++) {
260  os << item->payload ()->GetName () << std::endl;
261  }
262 }
263 
264 template<class Policy>
265 void
266 ContentStoreImpl<Policy>::SetMaxSize(uint32_t maxSize)
267 {
268  this->getPolicy().set_max_size(maxSize);
269 }
270 
271 template<class Policy>
272 uint32_t
273 ContentStoreImpl<Policy>::GetMaxSize() const
274 {
275  return this->getPolicy().get_max_size();
276 }
277 
278 template<class Policy>
279 uint32_t
281 {
282  return this->getPolicy().size();
283 }
284 
285 template<class Policy>
286 Ptr<Entry>
288 {
289  typename super::parent_trie::recursive_iterator item(super::getTrie()), end(0);
290  for (; item != end; item++) {
291  if (item->payload() == 0)
292  continue;
293  break;
294  }
295 
296  if (item == end)
297  return End();
298  else
299  return item->payload();
300 }
301 
302 template<class Policy>
303 Ptr<Entry>
305 {
306  return 0;
307 }
308 
309 template<class Policy>
310 Ptr<Entry>
312 {
313  if (from == 0)
314  return 0;
315 
316  typename super::parent_trie::recursive_iterator item(*StaticCast<entry>(from)->to_iterator()),
317  end(0);
318 
319  for (item++; item != end; item++) {
320  if (item->payload() == 0)
321  continue;
322  break;
323  }
324 
325  if (item == end)
326  return End();
327  else
328  return item->payload();
329 }
330 
331 } // namespace cs
332 } // namespace ndn
333 } // namespace ns3
334 
335 #endif // NDN_CONTENT_STORE_IMPL_H_
virtual Ptr< Entry > Begin()
Return first element of content store (no order guaranteed)
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()
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)
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.