blob: 922c9cd83cce0884ef4e9d1e6ac68995a862ea53 [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 if (nte == nullptr) {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000072 // must be Fib::s_emptyEntry that is unattached
Junxiao Shi8c0500f2015-11-11 08:30:16 -070073 BOOST_ASSERT(fibEntry.getPrefix().empty());
74 nte = m_nameTree.lookup(fibEntry.getPrefix());
75 }
76
77 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070078 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070079}
80
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000081Entry&
Junxiao Shi65d00722014-02-17 10:50:20 -070082Measurements::get(const pit::Entry& pitEntry)
83{
Junxiao Shib184e532016-05-26 18:09:57 +000084 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070085 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070086 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070087}
88
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000089Entry*
Junxiao Shib30c7b02015-01-07 15:45:54 -070090Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070091{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000092 if (child.getName().empty()) { // the root entry
Junxiao Shie368d992014-12-02 23:44:31 -070093 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070094 }
95
Junxiao Shib184e532016-05-26 18:09:57 +000096 shared_ptr<name_tree::Entry> nteChild = m_nameTree.lookup(child);
Junxiao Shib660b4c2016-08-06 20:47:44 +000097 name_tree::Entry* nte = nteChild->getParent();
Junxiao Shie368d992014-12-02 23:44:31 -070098 BOOST_ASSERT(nte != nullptr);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000099 return &this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -0700100}
101
Junxiao Shi767cb332015-01-08 09:35:49 -0700102template<typename K>
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000103Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000104Measurements::findLongestPrefixMatchImpl(const K& key, const EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -0700105{
Junxiao Shi811c0102016-08-10 04:12:45 +0000106 name_tree::Entry* match = m_nameTree.findLongestPrefixMatch(key,
107 [&pred] (const name_tree::Entry& nte) {
108 const Entry* entry = nte.getMeasurementsEntry();
109 return entry != nullptr && pred(*entry);
110 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700111 if (match != nullptr) {
112 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700113 }
Junxiao Shie368d992014-12-02 23:44:31 -0700114 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800115}
Junxiao Shi65d00722014-02-17 10:50:20 -0700116
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000117Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000118Measurements::findLongestPrefixMatch(const Name& name, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700119{
120 return this->findLongestPrefixMatchImpl(name, pred);
121}
122
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000123Entry*
Junxiao Shi811c0102016-08-10 04:12:45 +0000124Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, const EntryPredicate& pred) const
Junxiao Shi767cb332015-01-08 09:35:49 -0700125{
Junxiao Shib184e532016-05-26 18:09:57 +0000126 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry);
127 BOOST_ASSERT(nte != nullptr);
Junxiao Shi811c0102016-08-10 04:12:45 +0000128 return this->findLongestPrefixMatchImpl(*nte, pred);
Junxiao Shi767cb332015-01-08 09:35:49 -0700129}
130
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000131Entry*
HangZhangc85a23c2014-03-01 15:55:55 +0800132Measurements::findExactMatch(const Name& name) const
133{
Junxiao Shie368d992014-12-02 23:44:31 -0700134 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000135 if (nte != nullptr) {
Junxiao Shie368d992014-12-02 23:44:31 -0700136 return nte->getMeasurementsEntry();
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000137 }
Junxiao Shie368d992014-12-02 23:44:31 -0700138 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -0700139}
140
141void
Junxiao Shib184e532016-05-26 18:09:57 +0000142Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700143{
Junxiao Shib184e532016-05-26 18:09:57 +0000144 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000145 BOOST_ASSERT(nte != nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700146
147 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700148 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700149 // has longer lifetime, not extending
150 return;
151 }
152
Junxiao Shie368d992014-12-02 23:44:31 -0700153 scheduler::cancel(entry.m_cleanup);
154 entry.m_expiry = expiry;
155 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800156}
157
158void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700159Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800160{
Junxiao Shib184e532016-05-26 18:09:57 +0000161 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000162 BOOST_ASSERT(nte != nullptr);
163
164 nte->setMeasurementsEntry(nullptr);
Junxiao Shie3cf2852016-08-09 03:50:56 +0000165 m_nameTree.eraseIfEmpty(nte.get());
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000166 --m_nItems;
Junxiao Shi65d00722014-02-17 10:50:20 -0700167}
168
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000169} // namespace measurements
Junxiao Shi65d00722014-02-17 10:50:20 -0700170} // namespace nfd