blob: 247a5842be3c676fe6f454d0b8254f6c1631a2f3 [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);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070070 if (nte == nullptr) {
71 // must be Fib::s_emptyEntry that is unattched
72 BOOST_ASSERT(fibEntry.getPrefix().empty());
73 nte = m_nameTree.lookup(fibEntry.getPrefix());
74 }
75
76 BOOST_ASSERT(nte != nullptr);
Junxiao Shie368d992014-12-02 23:44:31 -070077 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070078}
79
Junxiao Shib30c7b02015-01-07 15:45:54 -070080shared_ptr<Entry>
Junxiao Shi65d00722014-02-17 10:50:20 -070081Measurements::get(const pit::Entry& pitEntry)
82{
Junxiao Shie368d992014-12-02 23:44:31 -070083 shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070084
85 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 Shib30c7b02015-01-07 15:45:54 -070089shared_ptr<Entry>
90Measurements::getParent(const Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070091{
Junxiao Shie368d992014-12-02 23:44:31 -070092 if (child.getName().size() == 0) { // the root entry
93 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070094 }
95
Junxiao Shie368d992014-12-02 23:44:31 -070096 shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child);
97 shared_ptr<name_tree::Entry> nte = nteChild->getParent();
98 BOOST_ASSERT(nte != nullptr);
99 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -0700100}
101
Junxiao Shi767cb332015-01-08 09:35:49 -0700102template<typename K>
Junxiao Shib30c7b02015-01-07 15:45:54 -0700103shared_ptr<Entry>
Junxiao Shi767cb332015-01-08 09:35:49 -0700104Measurements::findLongestPrefixMatchImpl(const K& key,
105 const measurements::EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -0700106{
Junxiao Shi767cb332015-01-08 09:35:49 -0700107 shared_ptr<name_tree::Entry> match = m_nameTree.findLongestPrefixMatch(key,
Junxiao Shib30c7b02015-01-07 15:45:54 -0700108 [pred] (const name_tree::Entry& nte) -> bool {
109 shared_ptr<Entry> entry = nte.getMeasurementsEntry();
110 return entry != nullptr && pred(*entry);
111 });
Junxiao Shi767cb332015-01-08 09:35:49 -0700112 if (match != nullptr) {
113 return match->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700114 }
Junxiao Shie368d992014-12-02 23:44:31 -0700115 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800116}
Junxiao Shi65d00722014-02-17 10:50:20 -0700117
Junxiao Shib30c7b02015-01-07 15:45:54 -0700118shared_ptr<Entry>
Junxiao Shi767cb332015-01-08 09:35:49 -0700119Measurements::findLongestPrefixMatch(const Name& name,
120 const measurements::EntryPredicate& pred) const
121{
122 return this->findLongestPrefixMatchImpl(name, pred);
123}
124
125shared_ptr<Entry>
126Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry,
127 const measurements::EntryPredicate& pred) const
128{
129 shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
130 return this->findLongestPrefixMatchImpl(nte, pred);
131}
132
133shared_ptr<Entry>
HangZhangc85a23c2014-03-01 15:55:55 +0800134Measurements::findExactMatch(const Name& name) const
135{
Junxiao Shie368d992014-12-02 23:44:31 -0700136 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
137 if (nte != nullptr)
138 return nte->getMeasurementsEntry();
139 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -0700140}
141
142void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700143Measurements::extendLifetime(Entry& entry,
Junxiao Shiee5a4442014-07-27 17:13:43 -0700144 const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700145{
Junxiao Shie368d992014-12-02 23:44:31 -0700146 shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
147 if (nte == nullptr ||
148 nte->getMeasurementsEntry().get() != &entry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700149 // entry is already gone; it is a dangling reference
150 return;
HangZhangc85a23c2014-03-01 15:55:55 +0800151 }
Junxiao Shiee5a4442014-07-27 17:13:43 -0700152
153 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700154 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700155 // has longer lifetime, not extending
156 return;
157 }
158
Junxiao Shie368d992014-12-02 23:44:31 -0700159 scheduler::cancel(entry.m_cleanup);
160 entry.m_expiry = expiry;
161 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800162}
163
164void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700165Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800166{
Junxiao Shie368d992014-12-02 23:44:31 -0700167 shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
168 if (nte != nullptr) {
169 nte->setMeasurementsEntry(nullptr);
170 m_nameTree.eraseEntryIfEmpty(nte);
HangZhangc85a23c2014-03-01 15:55:55 +0800171 m_nItems--;
172 }
Junxiao Shi65d00722014-02-17 10:50:20 -0700173}
174
175} // namespace nfd