NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
measurements.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "measurements.hpp"
27 #include "name-tree.hpp"
28 #include "pit-entry.hpp"
29 #include "fib-entry.hpp"
30 
31 namespace nfd {
32 
33 using measurements::Entry;
34 
36  : m_nameTree(nameTree)
37  , m_nItems(0)
38 {
39 }
40 
41 shared_ptr<Entry>
43 {
44  shared_ptr<Entry> entry = nte.getMeasurementsEntry();
45  if (entry != nullptr)
46  return entry;
47 
48  entry = make_shared<Entry>(nte.getPrefix());
49  nte.setMeasurementsEntry(entry);
50  ++m_nItems;
51 
52  entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
53  entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
54  bind(&Measurements::cleanup, this, ref(*entry)));
55 
56  return entry;
57 }
58 
59 shared_ptr<Entry>
61 {
62  shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
63  return this->get(*nte);
64 }
65 
66 shared_ptr<Entry>
67 Measurements::get(const fib::Entry& fibEntry)
68 {
69  shared_ptr<name_tree::Entry> nte = m_nameTree.get(fibEntry);
70  if (nte == nullptr) {
71  // must be Fib::s_emptyEntry that is unattched
72  BOOST_ASSERT(fibEntry.getPrefix().empty());
73  nte = m_nameTree.lookup(fibEntry.getPrefix());
74  }
75 
76  BOOST_ASSERT(nte != nullptr);
77  return this->get(*nte);
78 }
79 
80 shared_ptr<Entry>
81 Measurements::get(const pit::Entry& pitEntry)
82 {
83  shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
84  // a workaround for PIT entry deleted from PIT, but still alive as an object
85  if (nte == nullptr) {
86  return nullptr;
87  }
88 
89  return this->get(*nte);
90 }
91 
92 shared_ptr<Entry>
94 {
95  if (child.getName().size() == 0) { // the root entry
96  return nullptr;
97  }
98 
99  shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child);
100  shared_ptr<name_tree::Entry> nte = nteChild->getParent();
101  BOOST_ASSERT(nte != nullptr);
102  return this->get(*nte);
103 }
104 
105 template<typename K>
106 shared_ptr<Entry>
107 Measurements::findLongestPrefixMatchImpl(const K& key,
108  const measurements::EntryPredicate& pred) const
109 {
110  shared_ptr<name_tree::Entry> match = m_nameTree.findLongestPrefixMatch(key,
111  [pred] (const name_tree::Entry& nte) -> bool {
112  shared_ptr<Entry> entry = nte.getMeasurementsEntry();
113  return entry != nullptr && pred(*entry);
114  });
115  if (match != nullptr) {
116  return match->getMeasurementsEntry();
117  }
118  return nullptr;
119 }
120 
121 shared_ptr<Entry>
123  const measurements::EntryPredicate& pred) const
124 {
125  return this->findLongestPrefixMatchImpl(name, pred);
126 }
127 
128 shared_ptr<Entry>
130  const measurements::EntryPredicate& pred) const
131 {
132  shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
133  return this->findLongestPrefixMatchImpl(nte, pred);
134 }
135 
136 shared_ptr<Entry>
138 {
139  shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
140  if (nte != nullptr)
141  return nte->getMeasurementsEntry();
142  return nullptr;
143 }
144 
145 void
147  const time::nanoseconds& lifetime)
148 {
149  shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
150  if (nte == nullptr ||
151  nte->getMeasurementsEntry().get() != &entry) {
152  // entry is already gone; it is a dangling reference
153  return;
154  }
155 
157  if (entry.m_expiry >= expiry) {
158  // has longer lifetime, not extending
159  return;
160  }
161 
162  scheduler::cancel(entry.m_cleanup);
163  entry.m_expiry = expiry;
164  entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
165 }
166 
167 void
168 Measurements::cleanup(Entry& entry)
169 {
170  shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
171  if (nte != nullptr) {
172  nte->setMeasurementsEntry(nullptr);
173  m_nameTree.eraseEntryIfEmpty(nte);
174  m_nItems--;
175  }
176 }
177 
178 } // namespace nfd
shared_ptr< Entry > getParent() const
shared_ptr< measurements::Entry > getParent(const measurements::Entry &child)
find or insert a Measurements entry for child&#39;s parent
const Name & getName() const
time_point TimePoint
Definition: time.hpp:108
const Name & getPrefix() const
Definition: fib-entry.hpp:115
shared_ptr< measurements::Entry > get(const Name &name)
find or insert a Measurements entry for name
void cancel(const EventId &eventId)
cancel a scheduled event
Definition: scheduler.cpp:53
represents a FIB entry
Definition: fib-entry.hpp:53
static time_point now() noexcept
Definition: time.cpp:79
shared_ptr< measurements::Entry > findExactMatch(const Name &name) const
perform an exact match
bool eraseEntryIfEmpty(shared_ptr< name_tree::Entry > entry)
Delete a Name Tree Entry if this entry is empty.
Definition: name-tree.cpp:245
std::function< bool(const Entry &)> EntryPredicate
a predicate that accepts or rejects a Entry
shared_ptr< name_tree::Entry > get(const fib::Entry &fibEntry) const
get NameTree entry from FIB entry
Definition: name-tree.hpp:367
const Name & getPrefix() const
shared_ptr< measurements::Entry > getMeasurementsEntry() const
represents a Measurements entry
shared_ptr< name_tree::Entry > findLongestPrefixMatch(const Name &prefix, const name_tree::EntrySelector &entrySelector=name_tree::AnyEntry()) const
Longest prefix matching for the given name.
Definition: name-tree.cpp:374
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
represents a PIT entry
Definition: pit-entry.hpp:69
void setMeasurementsEntry(shared_ptr< measurements::Entry > measurementsEntry)
Class Name Tree.
Definition: name-tree.hpp:79
Name abstraction to represent an absolute name.
Definition: name.hpp:46
shared_ptr< name_tree::Entry > lookup(const Name &prefix)
Look for the Name Tree Entry that contains this name prefix.
Definition: name-tree.cpp:207
size_t size() const
Get the number of components.
Definition: name.hpp:408
static time::nanoseconds getInitialLifetime()
shared_ptr< measurements::Entry > findLongestPrefixMatch(const Name &name, const measurements::EntryPredicate &pred=measurements::AnyEntry()) const
perform a longest prefix match for name
EventId schedule(const time::nanoseconds &after, const Scheduler::Event &event)
schedule an event
Definition: scheduler.cpp:47
bool empty() const
Check if name is emtpy.
Definition: name.hpp:398
Measurements(NameTree &nametree)
void extendLifetime(measurements::Entry &entry, const time::nanoseconds &lifetime)
extend lifetime of an entry
Name Tree Entry Class.