NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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 namespace measurements {
33 
35  : m_nameTree(nameTree)
36  , m_nItems(0)
37 {
38 }
39 
40 Entry&
42 {
43  Entry* entry = nte.getMeasurementsEntry();
44  if (entry != nullptr) {
45  return *entry;
46  }
47 
48  nte.setMeasurementsEntry(make_unique<Entry>(nte.getName()));
49  ++m_nItems;
50  entry = nte.getMeasurementsEntry();
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 Entry&
61 {
62  name_tree::Entry& nte = m_nameTree.lookup(name);
63  return this->get(nte);
64 }
65 
66 Entry&
67 Measurements::get(const fib::Entry& fibEntry)
68 {
69  name_tree::Entry& nte = m_nameTree.lookup(fibEntry);
70  return this->get(nte);
71 }
72 
73 Entry&
74 Measurements::get(const pit::Entry& pitEntry)
75 {
76  name_tree::Entry& nte = m_nameTree.lookup(pitEntry);
77  return this->get(nte);
78 }
79 
80 Entry*
82 {
83  if (child.getName().empty()) { // the root entry
84  return nullptr;
85  }
86 
87  name_tree::Entry* nteChild = m_nameTree.getEntry(child);
88  name_tree::Entry* nte = nteChild->getParent();
89  BOOST_ASSERT(nte != nullptr);
90  return &this->get(*nte);
91 }
92 
93 template<typename K>
94 Entry*
95 Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
96 {
97  name_tree::Entry* match = m_nameTree.findLongestPrefixMatch(key,
98  [&pred] (const name_tree::Entry& nte) {
99  const Entry* entry = nte.getMeasurementsEntry();
100  return entry != nullptr && pred(*entry);
101  });
102  if (match != nullptr) {
103  return match->getMeasurementsEntry();
104  }
105  return nullptr;
106 }
107 
108 Entry*
110 {
111  return this->findLongestPrefixMatchImpl(name, pred);
112 }
113 
114 Entry*
116 {
117  return this->findLongestPrefixMatchImpl(pitEntry, pred);
118 }
119 
120 Entry*
122 {
123  const name_tree::Entry* nte = m_nameTree.findExactMatch(name);
124  return nte == nullptr ? nullptr : nte->getMeasurementsEntry();
125 }
126 
127 void
128 Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
129 {
130  BOOST_ASSERT(m_nameTree.getEntry(entry) != nullptr);
131 
133  if (entry.m_expiry >= expiry) {
134  // has longer lifetime, not extending
135  return;
136  }
137 
138  scheduler::cancel(entry.m_cleanup);
139  entry.m_expiry = expiry;
140  entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
141 }
142 
143 void
144 Measurements::cleanup(Entry& entry)
145 {
146  name_tree::Entry* nte = m_nameTree.getEntry(entry);
147  BOOST_ASSERT(nte != nullptr);
148 
149  nte->setMeasurementsEntry(nullptr);
150  m_nameTree.eraseIfEmpty(nte);
151  --m_nItems;
152 }
153 
154 } // namespace measurements
155 } // namespace nfd
Entry * findExactMatch(const Name &name) const
perform an exact match
time_point TimePoint
Definition: time.hpp:120
void cancel(const EventId &eventId)
cancel a scheduled event
Definition: scheduler.cpp:53
represents a FIB entry
Definition: fib-entry.hpp:51
static time_point now() noexcept
Definition: time.cpp:80
const Name & getName() const
std::function< bool(const Entry &)> EntryPredicate
a predicate that accepts or rejects an entry
const Name & getName() const
Entry * getParent() const
void setMeasurementsEntry(unique_ptr< measurements::Entry > measurementsEntry)
represents a Measurements entry
Entry * getParent(const Entry &child)
find or insert a Measurements entry for child&#39;s parent
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
an Interest table entry
Definition: pit-entry.hpp:57
void extendLifetime(Entry &entry, const time::nanoseconds &lifetime)
extend lifetime of an entry
Measurements(NameTree &nametree)
Represents an absolute name.
Definition: name.hpp:42
EventId schedule(time::nanoseconds after, const EventCallback &event)
schedule an event
Definition: scheduler.cpp:47
bool empty() const
Check if name is empty.
Definition: name.hpp:146
Entry * findLongestPrefixMatch(const Name &name, const EntryPredicate &pred=AnyEntry()) const
perform a longest prefix match for name
measurements::Entry * getMeasurementsEntry() const
Entry & get(const Name &name)
find or insert a Measurements entry for name
static time::nanoseconds getInitialLifetime()
an entry in the name tree