blob: 6c19d10af221bb21236fd87a347dbe5a7c044c86 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shi65d00722014-02-17 10:50:20 -070024
25#include "measurements.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080026#include "name-tree.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070027#include "pit-entry.hpp"
HangZhangc85a23c2014-03-01 15:55:55 +080028#include "fib-entry.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070029
30namespace nfd {
31
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070032const time::nanoseconds Measurements::s_defaultLifetime = time::seconds(4);
Junxiao Shi65d00722014-02-17 10:50:20 -070033
HangZhangc85a23c2014-03-01 15:55:55 +080034Measurements::Measurements(NameTree& nameTree)
35 : m_nameTree(nameTree)
36 , m_nItems(0)
Junxiao Shi65d00722014-02-17 10:50:20 -070037{
38}
39
40Measurements::~Measurements()
41{
42}
43
Junxiao Shi40631842014-03-01 13:52:37 -070044static inline bool
45predicate_NameTreeEntry_hasMeasurementsEntry(const name_tree::Entry& entry)
46{
47 return static_cast<bool>(entry.getMeasurementsEntry());
48}
49
Junxiao Shi65d00722014-02-17 10:50:20 -070050shared_ptr<measurements::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +080051Measurements::get(shared_ptr<name_tree::Entry> nameTreeEntry)
Junxiao Shi65d00722014-02-17 10:50:20 -070052{
HangZhangc85a23c2014-03-01 15:55:55 +080053 shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry();
54 if (static_cast<bool>(entry))
55 return entry;
HangZhangcb4fc832014-03-11 16:57:11 +080056 entry = make_shared<measurements::Entry>(nameTreeEntry->getPrefix());
HangZhangc85a23c2014-03-01 15:55:55 +080057 nameTreeEntry->setMeasurementsEntry(entry);
58 m_nItems++;
Junxiao Shi65d00722014-02-17 10:50:20 -070059 return entry;
60}
61
62shared_ptr<measurements::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +080063Measurements::get(const Name& name)
64{
65 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
66 return get(nameTreeEntry);
67}
68
69shared_ptr<measurements::Entry>
Junxiao Shi65d00722014-02-17 10:50:20 -070070Measurements::get(const fib::Entry& fibEntry)
71{
HangZhangcb4fc832014-03-11 16:57:11 +080072 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(fibEntry);
73
74 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
75
76 return get(nameTreeEntry);
Junxiao Shi65d00722014-02-17 10:50:20 -070077}
78
79shared_ptr<measurements::Entry>
80Measurements::get(const pit::Entry& pitEntry)
81{
HangZhangcb4fc832014-03-11 16:57:11 +080082 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
83
84 BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
85
86 return get(nameTreeEntry);
Junxiao Shi65d00722014-02-17 10:50:20 -070087}
88
89shared_ptr<measurements::Entry>
90Measurements::getParent(shared_ptr<measurements::Entry> child)
91{
HangZhangc85a23c2014-03-01 15:55:55 +080092 BOOST_ASSERT(child);
93
Junxiao Shi65d00722014-02-17 10:50:20 -070094 if (child->getName().size() == 0) {
95 return shared_ptr<measurements::Entry>();
96 }
97
98 return this->get(child->getName().getPrefix(-1));
99}
100
HangZhangc85a23c2014-03-01 15:55:55 +0800101shared_ptr<measurements::Entry>
102Measurements::findLongestPrefixMatch(const Name& name) const
Junxiao Shi65d00722014-02-17 10:50:20 -0700103{
Junxiao Shi40631842014-03-01 13:52:37 -0700104 shared_ptr<name_tree::Entry> nameTreeEntry =
105 m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry);
106 if (static_cast<bool>(nameTreeEntry)) {
107 return nameTreeEntry->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700108 }
HangZhangc85a23c2014-03-01 15:55:55 +0800109 return shared_ptr<measurements::Entry>();
110}
Junxiao Shi65d00722014-02-17 10:50:20 -0700111
HangZhangc85a23c2014-03-01 15:55:55 +0800112shared_ptr<measurements::Entry>
113Measurements::findExactMatch(const Name& name) const
114{
115 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
116 if (static_cast<bool>(nameTreeEntry))
117 return nameTreeEntry->getMeasurementsEntry();
118 return shared_ptr<measurements::Entry>();
Junxiao Shi65d00722014-02-17 10:50:20 -0700119}
120
121void
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700122Measurements::extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700123{
HangZhangc85a23c2014-03-01 15:55:55 +0800124 shared_ptr<measurements::Entry> ret = this->findExactMatch(entry.getName());
125 if (static_cast<bool>(ret))
126 {
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700127 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
HangZhangc85a23c2014-03-01 15:55:55 +0800128 if (ret->m_expiry >= expiry) // has longer lifetime, not extending
129 return;
130 scheduler::cancel(entry.m_cleanup);
131 entry.m_expiry = expiry;
132 entry.m_cleanup = scheduler::schedule(lifetime,
133 bind(&Measurements::cleanup, this, ret));
134 }
135}
136
137void
138Measurements::cleanup(shared_ptr<measurements::Entry> entry)
139{
140 BOOST_ASSERT(entry);
141
142 shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(entry->getName());
143 if (static_cast<bool>(nameTreeEntry))
144 {
HangZhangcb4fc832014-03-11 16:57:11 +0800145 nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>());
HangZhangc85a23c2014-03-01 15:55:55 +0800146 m_nItems--;
147 }
HangZhangcb4fc832014-03-11 16:57:11 +0800148
Junxiao Shi65d00722014-02-17 10:50:20 -0700149}
150
151} // namespace nfd