NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2014-2017, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
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  BOOST_ASSERT(nte.getName().size() <= NameTree::getMaxDepth());
44 
45  Entry* entry = nte.getMeasurementsEntry();
46  if (entry != nullptr) {
47  return *entry;
48  }
49 
50  nte.setMeasurementsEntry(make_unique<Entry>(nte.getName()));
51  ++m_nItems;
52  entry = nte.getMeasurementsEntry();
53 
54  entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
55  entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
56  bind(&Measurements::cleanup, this, ref(*entry)));
57 
58  return *entry;
59 }
60 
61 Entry&
63 {
64  name_tree::Entry& nte = m_nameTree.lookup(name, true);
65  return this->get(nte);
66 }
67 
68 Entry&
69 Measurements::get(const fib::Entry& fibEntry)
70 {
71  name_tree::Entry& nte = m_nameTree.lookup(fibEntry.getPrefix(), true);
72  return this->get(nte);
73 }
74 
75 Entry&
76 Measurements::get(const pit::Entry& pitEntry)
77 {
78  name_tree::Entry& nte = m_nameTree.lookup(pitEntry.getName(), true);
79  return this->get(nte);
80 }
81 
82 Entry*
84 {
85  if (child.getName().empty()) { // the root entry
86  return nullptr;
87  }
88 
89  name_tree::Entry* nteChild = m_nameTree.getEntry(child);
90  name_tree::Entry* nte = nteChild->getParent();
91  BOOST_ASSERT(nte != nullptr);
92  return &this->get(*nte);
93 }
94 
95 template<typename K>
96 Entry*
97 Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
98 {
99  name_tree::Entry* match = m_nameTree.findLongestPrefixMatch(key,
100  [&pred] (const name_tree::Entry& nte) {
101  const Entry* entry = nte.getMeasurementsEntry();
102  return entry != nullptr && pred(*entry);
103  });
104  if (match != nullptr) {
105  return match->getMeasurementsEntry();
106  }
107  return nullptr;
108 }
109 
110 Entry*
112 {
113  return this->findLongestPrefixMatchImpl(name.getPrefix(NameTree::getMaxDepth()), pred);
114 }
115 
116 Entry*
118 {
119  return this->findLongestPrefixMatch(pitEntry.getName(), pred);
120 }
121 
122 Entry*
124 {
125  const name_tree::Entry* nte = m_nameTree.findExactMatch(name);
126  return nte == nullptr ? nullptr : nte->getMeasurementsEntry();
127 }
128 
129 void
130 Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
131 {
132  BOOST_ASSERT(m_nameTree.getEntry(entry) != nullptr);
133 
135  if (entry.m_expiry >= expiry) {
136  // has longer lifetime, not extending
137  return;
138  }
139 
140  scheduler::cancel(entry.m_cleanup);
141  entry.m_expiry = expiry;
142  entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
143 }
144 
145 void
146 Measurements::cleanup(Entry& entry)
147 {
148  name_tree::Entry* nte = m_nameTree.getEntry(entry);
149  BOOST_ASSERT(nte != nullptr);
150 
151  nte->setMeasurementsEntry(nullptr);
152  m_nameTree.eraseIfEmpty(nte);
153  --m_nItems;
154 }
155 
156 } // namespace measurements
157 } // namespace nfd
const Name & getName() const
Entry * findExactMatch(const Name &name) const
perform an exact match
time_point TimePoint
Definition: time.hpp:226
const Name & getPrefix() const
Definition: fib-entry.hpp:58
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
std::function< bool(const Entry &)> EntryPredicate
a predicate that accepts or rejects an entry
Entry * findLongestPrefixMatch(const Name &name, const EntryPredicate &pred=AnyEntry()) const
perform a longest prefix match for name
Entry * findLongestPrefixMatch(const Name &name, const EntrySelector &entrySelector=AnyEntry()) const
longest prefix matching
Definition: name-tree.cpp:156
void setMeasurementsEntry(unique_ptr< measurements::Entry > measurementsEntry)
represents a Measurements entry
measurements::Entry * getMeasurementsEntry() const
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
Entry & lookup(const Name &name, bool enforceMaxDepth=false)
find or insert an entry with specified name
Definition: name-tree.cpp:44
Entry * getEntry(const EntryT &tableEntry) const
Definition: name-tree.hpp:80
Measurements(NameTree &nametree)
Represents an absolute name.
Definition: name.hpp:42
size_t size() const
Get number of components.
Definition: name.hpp:154
size_t eraseIfEmpty(Entry *entry, bool canEraseAncestors=true)
delete the entry if it is empty
Definition: name-tree.cpp:122
a common index structure for FIB, PIT, StrategyChoice, and Measurements
Definition: name-tree.hpp:36
const Name & getName() const
Definition: pit-entry.hpp:77
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 * findExactMatch(const Name &name, size_t prefixLen=std::numeric_limits< size_t >::max()) const
exact match lookup
Definition: name-tree.cpp:149
Entry * getParent() const
Entry & get(const Name &name)
find or insert a Measurements entry for name
const Name & getName() const
static time::nanoseconds getInitialLifetime()
an entry in the name tree
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Definition: name-tree.hpp:54