blob: 03c7927b9b7b233e46a494a3d0b29041d8e1cbc8 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi767cb332015-01-08 09:35:49 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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 {
32
Junxiao Shib30c7b02015-01-07 15:45:54 -070033using measurements::Entry;
34
HangZhangc85a23c2014-03-01 15:55:55 +080035Measurements::Measurements(NameTree& nameTree)
36 : m_nameTree(nameTree)
37 , m_nItems(0)
Junxiao Shi65d00722014-02-17 10:50:20 -070038{
39}
40
Junxiao Shib30c7b02015-01-07 15:45:54 -070041shared_ptr<Entry>
Junxiao Shie368d992014-12-02 23:44:31 -070042Measurements::get(name_tree::Entry& nte)
Junxiao Shi65d00722014-02-17 10:50:20 -070043{
Junxiao Shib30c7b02015-01-07 15:45:54 -070044 shared_ptr<Entry> entry = nte.getMeasurementsEntry();
Junxiao Shie368d992014-12-02 23:44:31 -070045 if (entry != nullptr)
HangZhangc85a23c2014-03-01 15:55:55 +080046 return entry;
Junxiao Shi19838042014-06-21 00:34:01 -070047
Junxiao Shib30c7b02015-01-07 15:45:54 -070048 entry = make_shared<Entry>(nte.getPrefix());
Junxiao Shie368d992014-12-02 23:44:31 -070049 nte.setMeasurementsEntry(entry);
Junxiao Shi19838042014-06-21 00:34:01 -070050 ++m_nItems;
51
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 Shi65d00722014-02-17 10:50:20 -070056 return entry;
57}
58
Junxiao Shib30c7b02015-01-07 15:45:54 -070059shared_ptr<Entry>
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);
63 return this->get(*nte);
HangZhangcb4fc832014-03-11 16:57:11 +080064}
65
Junxiao Shib30c7b02015-01-07 15:45:54 -070066shared_ptr<Entry>
Junxiao Shi65d00722014-02-17 10:50:20 -070067Measurements::get(const fib::Entry& fibEntry)
68{
Junxiao Shie368d992014-12-02 23:44:31 -070069 shared_ptr<name_tree::Entry> nte = m_nameTree.get(fibEntry);
70 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070071}
72
Junxiao Shib30c7b02015-01-07 15:45:54 -070073shared_ptr<Entry>
Junxiao Shi65d00722014-02-17 10:50:20 -070074Measurements::get(const pit::Entry& pitEntry)
75{
Junxiao Shie368d992014-12-02 23:44:31 -070076 shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
77 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070078}
79
Junxiao Shib30c7b02015-01-07 15:45:54 -070080shared_ptr<Entry>
81Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070082{
Junxiao Shie368d992014-12-02 23:44:31 -070083 if (child.getName().size() == 0) { // the root entry
84 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070085 }
86
Junxiao Shie368d992014-12-02 23:44:31 -070087 shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child);
88 shared_ptr<name_tree::Entry> nte = nteChild->getParent();
89 BOOST_ASSERT(nte != nullptr);
90 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070091}
92
Junxiao Shi767cb332015-01-08 09:35:49 -070093template<typename K>
Junxiao Shib30c7b02015-01-07 15:45:54 -070094shared_ptr<Entry>
Junxiao Shi767cb332015-01-08 09:35:49 -070095Measurements::findLongestPrefixMatchImpl(const K& key,
96 const measurements::EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -070097{
Junxiao Shi767cb332015-01-08 09:35:49 -070098 shared_ptr<name_tree::Entry> match = m_nameTree.findLongestPrefixMatch(key,
Junxiao Shib30c7b02015-01-07 15:45:54 -070099 [pred] (const name_tree::Entry& nte) -> bool {
100 shared_ptr<Entry> entry = nte.getMeasurementsEntry();
101 return entry != nullptr && pred(*entry);
102 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700103 if (match != nullptr) {
104 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700105 }
Junxiao Shie368d992014-12-02 23:44:31 -0700106 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800107}
Junxiao Shi65d00722014-02-17 10:50:20 -0700108
Junxiao Shib30c7b02015-01-07 15:45:54 -0700109shared_ptr<Entry>
Junxiao Shi767cb332015-01-08 09:35:49 -0700110Measurements::findLongestPrefixMatch(const Name& name,
111 const measurements::EntryPredicate& pred) const
112{
113 return this->findLongestPrefixMatchImpl(name, pred);
114}
115
116shared_ptr<Entry>
117Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry,
118 const measurements::EntryPredicate& pred) const
119{
120 shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
121 return this->findLongestPrefixMatchImpl(nte, pred);
122}
123
124shared_ptr<Entry>
HangZhangc85a23c2014-03-01 15:55:55 +0800125Measurements::findExactMatch(const Name& name) const
126{
Junxiao Shie368d992014-12-02 23:44:31 -0700127 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
128 if (nte != nullptr)
129 return nte->getMeasurementsEntry();
130 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -0700131}
132
133void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700134Measurements::extendLifetime(Entry& entry,
Junxiao Shiee5a4442014-07-27 17:13:43 -0700135 const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700136{
Junxiao Shie368d992014-12-02 23:44:31 -0700137 shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
138 if (nte == nullptr ||
139 nte->getMeasurementsEntry().get() != &entry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700140 // entry is already gone; it is a dangling reference
141 return;
HangZhangc85a23c2014-03-01 15:55:55 +0800142 }
Junxiao Shiee5a4442014-07-27 17:13:43 -0700143
144 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700145 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700146 // has longer lifetime, not extending
147 return;
148 }
149
Junxiao Shie368d992014-12-02 23:44:31 -0700150 scheduler::cancel(entry.m_cleanup);
151 entry.m_expiry = expiry;
152 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800153}
154
155void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700156Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800157{
Junxiao Shie368d992014-12-02 23:44:31 -0700158 shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
159 if (nte != nullptr) {
160 nte->setMeasurementsEntry(nullptr);
161 m_nameTree.eraseEntryIfEmpty(nte);
HangZhangc85a23c2014-03-01 15:55:55 +0800162 m_nItems--;
163 }
Junxiao Shi65d00722014-02-17 10:50:20 -0700164}
165
166} // namespace nfd