blob: 2ed0f65a2a5edd59323f08b45ab3a66bf2a083cf [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi3f9234b2017-12-07 17:41:50 +00002/*
3 * Copyright (c) 2014-2017, 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 Shi3f9234b2017-12-07 17:41:50 +000043 BOOST_ASSERT(nte.getName().size() <= NameTree::getMaxDepth());
44
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000045 Entry* entry = nte.getMeasurementsEntry();
46 if (entry != nullptr) {
47 return *entry;
48 }
Junxiao Shi19838042014-06-21 00:34:01 -070049
Junxiao Shie3cf2852016-08-09 03:50:56 +000050 nte.setMeasurementsEntry(make_unique<Entry>(nte.getName()));
Junxiao Shi19838042014-06-21 00:34:01 -070051 ++m_nItems;
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000052 entry = nte.getMeasurementsEntry();
Junxiao Shi19838042014-06-21 00:34:01 -070053
54 entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
55 entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
Junxiao Shie368d992014-12-02 23:44:31 -070056 bind(&Measurements::cleanup, this, ref(*entry)));
Junxiao Shi19838042014-06-21 00:34:01 -070057
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000058 return *entry;
Junxiao Shi65d00722014-02-17 10:50:20 -070059}
60
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000061Entry&
HangZhangcb4fc832014-03-11 16:57:11 +080062Measurements::get(const Name& name)
63{
Junxiao Shi3f9234b2017-12-07 17:41:50 +000064 name_tree::Entry& nte = m_nameTree.lookup(name, true);
Junxiao Shi7f358432016-08-11 17:06:33 +000065 return this->get(nte);
HangZhangcb4fc832014-03-11 16:57:11 +080066}
67
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000068Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070069Measurements::get(const fib::Entry& fibEntry)
70{
Junxiao Shi3f9234b2017-12-07 17:41:50 +000071 name_tree::Entry& nte = m_nameTree.lookup(fibEntry.getPrefix(), true);
Junxiao Shi7f358432016-08-11 17:06:33 +000072 return this->get(nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070073}
74
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000075Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070076Measurements::get(const pit::Entry& pitEntry)
77{
Junxiao Shi3f9234b2017-12-07 17:41:50 +000078 name_tree::Entry& nte = m_nameTree.lookup(pitEntry.getName(), true);
Junxiao Shi7f358432016-08-11 17:06:33 +000079 return this->get(nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070080}
81
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000082Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -070083Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070084{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000085 if (child.getName().empty()) { // the root entry
Junxiao Shie368d992014-12-02 23:44:31 -070086 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070087 }
88
Junxiao Shi7f358432016-08-11 17:06:33 +000089 name_tree::Entry* nteChild = m_nameTree.getEntry(child);
Junxiao Shib660b4c2016-08-06 20:47:44 +000090 name_tree::Entry* nte = nteChild->getParent();
Junxiao Shie368d992014-12-02 23:44:31 -070091 BOOST_ASSERT(nte != nullptr);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000092 return &this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070093}
94
Junxiao Shi767cb332015-01-08 09:35:49 -070095template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000096Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +000097Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -070098{
Junxiao Shi811c0102016-08-10 04:12:45 +000099 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 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700104 if (match != nullptr) {
105 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700106 }
Junxiao Shie368d992014-12-02 23:44:31 -0700107 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800108}
Junxiao Shi65d00722014-02-17 10:50:20 -0700109
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000110Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000111Measurements::findLongestPrefixMatch(const Name& name, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700112{
Junxiao Shi3f9234b2017-12-07 17:41:50 +0000113 return this->findLongestPrefixMatchImpl(name.getPrefix(NameTree::getMaxDepth()), pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700114}
115
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000116Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000117Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700118{
Junxiao Shi3f9234b2017-12-07 17:41:50 +0000119 return this->findLongestPrefixMatch(pitEntry.getName(), pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700120}
121
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000122Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800123Measurements::findExactMatch(const Name& name) const
124{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000125 const name_tree::Entry* nte = m_nameTree.findExactMatch(name);
126 return nte == nullptr ? nullptr : nte->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700127}
128
129void
Junxiao Shib184e532016-05-26 18:09:57 +0000130Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700131{
Davide Pesavento1c2c6c42016-08-16 12:36:06 +0000132 BOOST_ASSERT(m_nameTree.getEntry(entry) != nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700133
134 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700135 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700136 // has longer lifetime, not extending
137 return;
138 }
139
Junxiao Shie368d992014-12-02 23:44:31 -0700140 scheduler::cancel(entry.m_cleanup);
141 entry.m_expiry = expiry;
142 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800143}
144
145void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700146Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800147{
Junxiao Shi7f358432016-08-11 17:06:33 +0000148 name_tree::Entry* nte = m_nameTree.getEntry(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000149 BOOST_ASSERT(nte != nullptr);
150
151 nte->setMeasurementsEntry(nullptr);
Junxiao Shi7f358432016-08-11 17:06:33 +0000152 m_nameTree.eraseIfEmpty(nte);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000153 --m_nItems;
Junxiao Shi65d00722014-02-17 10:50:20 -0700154}
155
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000156} // namespace measurements
Junxiao Shi65d00722014-02-17 10:50:20 -0700157} // namespace nfd