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  return this->get(*nte);
71 }
72 
73 shared_ptr<Entry>
74 Measurements::get(const pit::Entry& pitEntry)
75 {
76  shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
77  return this->get(*nte);
78 }
79 
80 shared_ptr<Entry>
81 Measurements::getParent(const Entry& child)
82 {
83  if (child.getName().size() == 0) { // the root entry
84  return nullptr;
85  }
86 
87  shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child);
88  shared_ptr<name_tree::Entry> nte = nteChild->getParent();
89  BOOST_ASSERT(nte != nullptr);
90  return this->get(*nte);
91 }
92 
93 template<typename K>
94 shared_ptr<Entry>
95 Measurements::findLongestPrefixMatchImpl(const K& key,
96  const measurements::EntryPredicate& pred) const
97 {
98  shared_ptr<name_tree::Entry> match = m_nameTree.findLongestPrefixMatch(key,
99  [pred] (const name_tree::Entry& nte) -> bool {
100  shared_ptr<Entry> entry = nte.getMeasurementsEntry();
101  return entry != nullptr && pred(*entry);
102  });
103  if (match != nullptr) {
104  return match->getMeasurementsEntry();
105  }
106  return nullptr;
107 }
108 
109 shared_ptr<Entry>
111  const measurements::EntryPredicate& pred) const
112 {
113  return this->findLongestPrefixMatchImpl(name, pred);
114 }
115 
116 shared_ptr<Entry>
118  const measurements::EntryPredicate& pred) const
119 {
120  shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
121  return this->findLongestPrefixMatchImpl(nte, pred);
122 }
123 
124 shared_ptr<Entry>
126 {
127  shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
128  if (nte != nullptr)
129  return nte->getMeasurementsEntry();
130  return nullptr;
131 }
132 
133 void
135  const time::nanoseconds& lifetime)
136 {
137  shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
138  if (nte == nullptr ||
139  nte->getMeasurementsEntry().get() != &entry) {
140  // entry is already gone; it is a dangling reference
141  return;
142  }
143 
145  if (entry.m_expiry >= expiry) {
146  // has longer lifetime, not extending
147  return;
148  }
149 
150  scheduler::cancel(entry.m_cleanup);
151  entry.m_expiry = expiry;
152  entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
153 }
154 
155 void
156 Measurements::cleanup(Entry& entry)
157 {
158  shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
159  if (nte != nullptr) {
160  nte->setMeasurementsEntry(nullptr);
161  m_nameTree.eraseEntryIfEmpty(nte);
162  m_nItems--;
163  }
164 }
165 
166 } // namespace nfd
shared_ptr< measurements::Entry > getParent(const measurements::Entry &child)
find or insert a Measurements entry for child&#39;s parent
time_point TimePoint
Definition: time.hpp:108
shared_ptr< measurements::Entry > findLongestPrefixMatch(const Name &name, const measurements::EntryPredicate &pred=measurements::AnyEntry()) const
perform a longest prefix match for name
shared_ptr< measurements::Entry > get(const Name &name)
find or insert a Measurements entry for name
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
static time_point now() noexcept
Definition: time.cpp:79
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
std::function< bool(const Entry &)> EntryPredicate
a predicate that accepts or rejects a 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:275
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
represents a PIT entry
Definition: pit-entry.hpp:67
void setMeasurementsEntry(shared_ptr< measurements::Entry > measurementsEntry)
shared_ptr< measurements::Entry > findExactMatch(const Name &name) const
perform an exact match
Class Name Tree.
Definition: name-tree.hpp:79
EventId schedule(const time::nanoseconds &after, const std::function< void()> &event)
schedule an event
Definition: scheduler.cpp:50
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:205
const Name & getPrefix() const
static time::nanoseconds getInitialLifetime()
Measurements(NameTree &nametree)
void extendLifetime(measurements::Entry &entry, const time::nanoseconds &lifetime)
extend lifetime of an entry
shared_ptr< measurements::Entry > getMeasurementsEntry() const
Name Tree Entry Class.