blob: b2e8637292ced8df78d04fc13d9ac1e8bc8e54e0 [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 Shie368d992014-12-02 23:44:31 -070062 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000063 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070064 return this->get(*nte);
HangZhangcb4fc832014-03-11 16:57:11 +080065}
66
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000067Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070068Measurements::get(const fib::Entry& fibEntry)
69{
Junxiao Shib184e532016-05-26 18:09:57 +000070 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(fibEntry);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070071 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070072 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 Shib184e532016-05-26 18:09:57 +000078 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070079 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070080 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070081}
82
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000083Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -070084Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070085{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000086 if (child.getName().empty()) { // the root entry
Junxiao Shie368d992014-12-02 23:44:31 -070087 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070088 }
89
Junxiao Shif2420fc2016-08-11 13:18:21 +000090 shared_ptr<name_tree::Entry> nteChild = m_nameTree.getEntry(child);
Junxiao Shib660b4c2016-08-06 20:47:44 +000091 name_tree::Entry* nte = nteChild->getParent();
Junxiao Shie368d992014-12-02 23:44:31 -070092 BOOST_ASSERT(nte != nullptr);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000093 return &this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070094}
95
Junxiao Shi767cb332015-01-08 09:35:49 -070096template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000097Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +000098Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -070099{
Junxiao Shi811c0102016-08-10 04:12:45 +0000100 name_tree::Entry* match = m_nameTree.findLongestPrefixMatch(key,
101 [&pred] (const name_tree::Entry& nte) {
102 const Entry* entry = nte.getMeasurementsEntry();
103 return entry != nullptr && pred(*entry);
104 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700105 if (match != nullptr) {
106 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700107 }
Junxiao Shie368d992014-12-02 23:44:31 -0700108 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800109}
Junxiao Shi65d00722014-02-17 10:50:20 -0700110
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000111Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000112Measurements::findLongestPrefixMatch(const Name& name, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700113{
114 return this->findLongestPrefixMatchImpl(name, pred);
115}
116
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000117Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000118Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700119{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000120 return this->findLongestPrefixMatchImpl(pitEntry, pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700121}
122
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000123Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800124Measurements::findExactMatch(const Name& name) const
125{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000126 const name_tree::Entry* nte = m_nameTree.findExactMatch(name);
127 return nte == nullptr ? nullptr : nte->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700128}
129
130void
Junxiao Shib184e532016-05-26 18:09:57 +0000131Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700132{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000133 shared_ptr<name_tree::Entry> nte = m_nameTree.getEntry(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000134 BOOST_ASSERT(nte != nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700135
136 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700137 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700138 // has longer lifetime, not extending
139 return;
140 }
141
Junxiao Shie368d992014-12-02 23:44:31 -0700142 scheduler::cancel(entry.m_cleanup);
143 entry.m_expiry = expiry;
144 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800145}
146
147void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700148Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800149{
Junxiao Shif2420fc2016-08-11 13:18:21 +0000150 shared_ptr<name_tree::Entry> nte = m_nameTree.getEntry(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000151 BOOST_ASSERT(nte != nullptr);
152
153 nte->setMeasurementsEntry(nullptr);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000154 m_nameTree.eraseIfEmpty(nte.get());
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000155 --m_nItems;
Junxiao Shi65d00722014-02-17 10:50:20 -0700156}
157
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000158} // namespace measurements
Junxiao Shi65d00722014-02-17 10:50:20 -0700159} // namespace nfd