blob: 3830c10c20694f2b7101bc5b19c0d0a504623dc6 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib184e532016-05-26 18:09:57 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shi767cb332015-01-08 09:35:49 -07004 * Arizona Board of Regents,
5 * Colorado State University,
Junxiao Shi35353962015-01-08 09:13:47 -07006 * University Pierre & Marie Curie, Sorbonne University,
Junxiao Shi767cb332015-01-08 09:35:49 -07007 * 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 {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000032namespace measurements {
Junxiao Shib30c7b02015-01-07 15:45:54 -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
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000040Entry&
Junxiao Shie368d992014-12-02 23:44:31 -070041Measurements::get(name_tree::Entry& nte)
Junxiao Shi65d00722014-02-17 10:50:20 -070042{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000043 Entry* entry = nte.getMeasurementsEntry();
44 if (entry != nullptr) {
45 return *entry;
46 }
Junxiao Shi19838042014-06-21 00:34:01 -070047
Junxiao Shie3cf2852016-08-09 03:50:56 +000048 nte.setMeasurementsEntry(make_unique<Entry>(nte.getName()));
Junxiao Shi19838042014-06-21 00:34:01 -070049 ++m_nItems;
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000050 entry = nte.getMeasurementsEntry();
Junxiao Shi19838042014-06-21 00:34:01 -070051
52 entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
53 entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
Junxiao Shie368d992014-12-02 23:44:31 -070054 bind(&Measurements::cleanup, this, ref(*entry)));
Junxiao Shi19838042014-06-21 00:34:01 -070055
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000056 return *entry;
Junxiao Shi65d00722014-02-17 10:50:20 -070057}
58
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000059Entry&
HangZhangcb4fc832014-03-11 16:57:11 +080060Measurements::get(const Name& name)
61{
Junxiao Shi7f358432016-08-11 17:06:33 +000062 name_tree::Entry& nte = m_nameTree.lookup(name);
63 return this->get(nte);
HangZhangcb4fc832014-03-11 16:57:11 +080064}
65
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000066Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070067Measurements::get(const fib::Entry& fibEntry)
68{
Junxiao Shi7f358432016-08-11 17:06:33 +000069 name_tree::Entry& nte = m_nameTree.lookup(fibEntry);
70 return this->get(nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070071}
72
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000073Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070074Measurements::get(const pit::Entry& pitEntry)
75{
Junxiao Shi7f358432016-08-11 17:06:33 +000076 name_tree::Entry& nte = m_nameTree.lookup(pitEntry);
77 return this->get(nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070078}
79
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000080Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -070081Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070082{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000083 if (child.getName().empty()) { // the root entry
Junxiao Shie368d992014-12-02 23:44:31 -070084 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070085 }
86
Junxiao Shi7f358432016-08-11 17:06:33 +000087 name_tree::Entry* nteChild = m_nameTree.getEntry(child);
Junxiao Shib660b4c2016-08-06 20:47:44 +000088 name_tree::Entry* nte = nteChild->getParent();
Junxiao Shie368d992014-12-02 23:44:31 -070089 BOOST_ASSERT(nte != nullptr);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000090 return &this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070091}
92
Junxiao Shi767cb332015-01-08 09:35:49 -070093template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000094Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +000095Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -070096{
Junxiao Shi811c0102016-08-10 04:12:45 +000097 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 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700102 if (match != nullptr) {
103 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700104 }
Junxiao Shie368d992014-12-02 23:44:31 -0700105 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800106}
Junxiao Shi65d00722014-02-17 10:50:20 -0700107
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000108Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000109Measurements::findLongestPrefixMatch(const Name& name, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700110{
111 return this->findLongestPrefixMatchImpl(name, pred);
112}
113
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000114Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000115Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700116{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000117 return this->findLongestPrefixMatchImpl(pitEntry, pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700118}
119
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000120Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800121Measurements::findExactMatch(const Name& name) const
122{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000123 const name_tree::Entry* nte = m_nameTree.findExactMatch(name);
124 return nte == nullptr ? nullptr : nte->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700125}
126
127void
Junxiao Shib184e532016-05-26 18:09:57 +0000128Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700129{
Davide Pesavento1c2c6c42016-08-16 12:36:06 +0000130 BOOST_ASSERT(m_nameTree.getEntry(entry) != nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700131
132 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700133 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700134 // has longer lifetime, not extending
135 return;
136 }
137
Junxiao Shie368d992014-12-02 23:44:31 -0700138 scheduler::cancel(entry.m_cleanup);
139 entry.m_expiry = expiry;
140 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800141}
142
143void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700144Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800145{
Junxiao Shi7f358432016-08-11 17:06:33 +0000146 name_tree::Entry* nte = m_nameTree.getEntry(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000147 BOOST_ASSERT(nte != nullptr);
148
149 nte->setMeasurementsEntry(nullptr);
Junxiao Shi7f358432016-08-11 17:06:33 +0000150 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000151 --m_nItems;
Junxiao Shi65d00722014-02-17 10:50:20 -0700152}
153
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000154} // namespace measurements
Junxiao Shi65d00722014-02-17 10:50:20 -0700155} // namespace nfd