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
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 
34  : m_nameTree(nameTree)
35  , m_nItems(0)
36 {
37 }
38 
39 shared_ptr<measurements::Entry>
41 {
42  shared_ptr<measurements::Entry> entry = nte.getMeasurementsEntry();
43  if (entry != nullptr)
44  return entry;
45 
46  entry = make_shared<measurements::Entry>(nte.getPrefix());
47  nte.setMeasurementsEntry(entry);
48  ++m_nItems;
49 
50  entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
51  entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
52  bind(&Measurements::cleanup, this, ref(*entry)));
53 
54  return entry;
55 }
56 
57 shared_ptr<measurements::Entry>
58 Measurements::get(const Name& name)
59 {
60  shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
61  return this->get(*nte);
62 }
63 
64 shared_ptr<measurements::Entry>
65 Measurements::get(const fib::Entry& fibEntry)
66 {
67  shared_ptr<name_tree::Entry> nte = m_nameTree.get(fibEntry);
68  return this->get(*nte);
69 }
70 
71 shared_ptr<measurements::Entry>
72 Measurements::get(const pit::Entry& pitEntry)
73 {
74  shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
75  return this->get(*nte);
76 }
77 
78 shared_ptr<measurements::Entry>
80 {
81  if (child.getName().size() == 0) { // the root entry
82  return nullptr;
83  }
84 
85  shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child);
86  shared_ptr<name_tree::Entry> nte = nteChild->getParent();
87  BOOST_ASSERT(nte != nullptr);
88  return this->get(*nte);
89 }
90 
91 shared_ptr<measurements::Entry>
93 {
94  shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(name,
95  [] (const name_tree::Entry& nte) { return nte.getMeasurementsEntry() != nullptr; });
96  if (nte != nullptr) {
97  return nte->getMeasurementsEntry();
98  }
99  return nullptr;
100 }
101 
102 shared_ptr<measurements::Entry>
104 {
105  shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
106  if (nte != nullptr)
107  return nte->getMeasurementsEntry();
108  return nullptr;
109 }
110 
111 void
113  const time::nanoseconds& lifetime)
114 {
115  shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
116  if (nte == nullptr ||
117  nte->getMeasurementsEntry().get() != &entry) {
118  // entry is already gone; it is a dangling reference
119  return;
120  }
121 
122  time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
123  if (entry.m_expiry >= expiry) {
124  // has longer lifetime, not extending
125  return;
126  }
127 
128  scheduler::cancel(entry.m_cleanup);
129  entry.m_expiry = expiry;
130  entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
131 }
132 
133 void
134 Measurements::cleanup(measurements::Entry& entry)
135 {
136  shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
137  if (nte != nullptr) {
138  nte->setMeasurementsEntry(nullptr);
139  m_nameTree.eraseEntryIfEmpty(nte);
140  m_nItems--;
141  }
142 }
143 
144 } // namespace nfd
shared_ptr< measurements::Entry > getParent(const measurements::Entry &child)
find or insert a Measurements entry for child's parent
shared_ptr< Entry > getParent() const
void cancel(const EventId &eventId)
cancel a scheduled event
Definition: scheduler.cpp:58
represents a FIB entry
Definition: fib-entry.hpp:53
const Name & getName() const
bool eraseEntryIfEmpty(shared_ptr< name_tree::Entry > entry)
Delete a Name Tree Entry if this entry is empty.
Definition: name-tree.cpp:327
shared_ptr< name_tree::Entry > get(const fib::Entry &fibEntry) const
get NameTree entry from attached FIB entry
Definition: name-tree.hpp:348
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:275
represents a Measurements entry
represents a PIT entry
Definition: pit-entry.hpp:67
void setMeasurementsEntry(shared_ptr< measurements::Entry > measurementsEntry)
Class Name Tree.
Definition: name-tree.hpp:79
shared_ptr< measurements::Entry > findExactMatch(const Name &name) const
perform an exact match
EventId schedule(const time::nanoseconds &after, const std::function< void()> &event)
schedule an event
Definition: scheduler.cpp:50
shared_ptr< name_tree::Entry > lookup(const Name &prefix)
Look for the Name Tree Entry that contains this name prefix.
Definition: name-tree.cpp:205
const Name & getPrefix() const
static time::nanoseconds getInitialLifetime()
shared_ptr< measurements::Entry > findLongestPrefixMatch(const Name &name) const
perform a longest prefix match
Measurements(NameTree &nametree)
void extendLifetime(measurements::Entry &entry, const time::nanoseconds &lifetime)
extend lifetime of an entry
shared_ptr< measurements::Entry > get(const Name &name)
find or insert a Measurements entry for name
shared_ptr< measurements::Entry > getMeasurementsEntry() const
Name Tree Entry Class.