blob: a63c602eef9b229f178e09a7ea4d5932e343f18c [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi19838042014-06-21 00:34:01 -07003 * Copyright (c) 2014, 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
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi19838042014-06-21 00:34:01 -070024 */
Junxiao Shi65d00722014-02-17 10:50:20 -070025
26#include "measurements.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080027#include "name-tree.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070028#include "pit-entry.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080029#include "fib-entry.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070030
31namespace nfd {
32
HangZhangc85a23c2014-03-01 15:55:55 +080033Measurements::Measurements(NameTree& nameTree)
34 : m_nameTree(nameTree)
35 , m_nItems(0)
Junxiao Shi65d00722014-02-17 10:50:20 -070036{
37}
38
39Measurements::~Measurements()
40{
41}
42
Junxiao Shi40631842014-03-01 13:52:37 -070043static inline bool
44predicate_NameTreeEntry_hasMeasurementsEntry(const name_tree::Entry& entry)
45{
46 return static_cast<bool>(entry.getMeasurementsEntry());
47}
48
Junxiao Shi65d00722014-02-17 10:50:20 -070049shared_ptr<measurements::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +080050Measurements::get(shared_ptr<name_tree::Entry> nameTreeEntry)
Junxiao Shi65d00722014-02-17 10:50:20 -070051{
HangZhangc85a23c2014-03-01 15:55:55 +080052 shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry();
53 if (static_cast<bool>(entry))
54 return entry;
Junxiao Shi19838042014-06-21 00:34:01 -070055
HangZhangcb4fc832014-03-11 16:57:11 +080056 entry = make_shared<measurements::Entry>(nameTreeEntry->getPrefix());
HangZhangc85a23c2014-03-01 15:55:55 +080057 nameTreeEntry->setMeasurementsEntry(entry);
Junxiao Shi19838042014-06-21 00:34:01 -070058 ++m_nItems;
59
60 entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
61 entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
62 bind(&Measurements::cleanup, this, entry));
63
Junxiao Shi65d00722014-02-17 10:50:20 -070064 return entry;
65}
66
67shared_ptr<measurements::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +080068Measurements::get(const Name& name)
69{
70 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
71 return get(nameTreeEntry);
72}
73
74shared_ptr<measurements::Entry>
Junxiao Shi65d00722014-02-17 10:50:20 -070075Measurements::get(const fib::Entry& fibEntry)
76{
HangZhangcb4fc832014-03-11 16:57:11 +080077 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(fibEntry);
78
79 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
80
81 return get(nameTreeEntry);
Junxiao Shi65d00722014-02-17 10:50:20 -070082}
83
84shared_ptr<measurements::Entry>
85Measurements::get(const pit::Entry& pitEntry)
86{
HangZhangcb4fc832014-03-11 16:57:11 +080087 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
88
89 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
90
91 return get(nameTreeEntry);
Junxiao Shi65d00722014-02-17 10:50:20 -070092}
93
94shared_ptr<measurements::Entry>
95Measurements::getParent(shared_ptr<measurements::Entry> child)
96{
HangZhangc85a23c2014-03-01 15:55:55 +080097 BOOST_ASSERT(child);
98
Junxiao Shi65d00722014-02-17 10:50:20 -070099 if (child->getName().size() == 0) {
100 return shared_ptr<measurements::Entry>();
101 }
102
103 return this->get(child->getName().getPrefix(-1));
104}
105
HangZhangc85a23c2014-03-01 15:55:55 +0800106shared_ptr<measurements::Entry>
107Measurements::findLongestPrefixMatch(const Name& name) const
Junxiao Shi65d00722014-02-17 10:50:20 -0700108{
Junxiao Shi40631842014-03-01 13:52:37 -0700109 shared_ptr<name_tree::Entry> nameTreeEntry =
110 m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry);
111 if (static_cast<bool>(nameTreeEntry)) {
112 return nameTreeEntry->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700113 }
HangZhangc85a23c2014-03-01 15:55:55 +0800114 return shared_ptr<measurements::Entry>();
115}
Junxiao Shi65d00722014-02-17 10:50:20 -0700116
HangZhangc85a23c2014-03-01 15:55:55 +0800117shared_ptr<measurements::Entry>
118Measurements::findExactMatch(const Name& name) const
119{
120 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
121 if (static_cast<bool>(nameTreeEntry))
122 return nameTreeEntry->getMeasurementsEntry();
123 return shared_ptr<measurements::Entry>();
Junxiao Shi65d00722014-02-17 10:50:20 -0700124}
125
126void
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700127Measurements::extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700128{
HangZhangc85a23c2014-03-01 15:55:55 +0800129 shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName());
130 if (static_cast<bool>(ret))
131 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700132 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
HangZhangc85a23c2014-03-01 15:55:55 +0800133 if (ret->m_expiry >= expiry) // has longer lifetime, not extending
134 return;
135 scheduler::cancel(entry.m_cleanup);
136 entry.m_expiry = expiry;
137 entry.m_cleanup = scheduler::schedule(lifetime,
138 bind(&Measurements::cleanup, this, ret));
139 }
140}
141
142void
143Measurements::cleanup(shared_ptr<measurements::Entry> entry)
144{
145 BOOST_ASSERT(entry);
146
147 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry->getName());
148 if (static_cast<bool>(nameTreeEntry))
149 {
HangZhangcb4fc832014-03-11 16:57:11 +0800150 nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>());
HangZhangc85a23c2014-03-01 15:55:55 +0800151 m_nItems--;
152 }
HangZhangcb4fc832014-03-11 16:57:11 +0800153
Junxiao Shi65d00722014-02-17 10:50:20 -0700154}
155
156} // namespace nfd