blob: f9b9bd310598e86c0fb516673d51da53f2994393 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi19838042014-06-21 00:34:01 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * 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 Shib30c7b02015-01-07 15:45:54 -070093shared_ptr<Entry>
94Measurements::findLongestPrefixMatch(const Name& name,
95 const measurements::EntryPredicate& pred) const
Junxiao Shi65d00722014-02-17 10:50:20 -070096{
Junxiao Shie368d992014-12-02 23:44:31 -070097 shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(name,
Junxiao Shib30c7b02015-01-07 15:45:54 -070098 [pred] (const name_tree::Entry& nte) -> bool {
99 shared_ptr<Entry> entry = nte.getMeasurementsEntry();
100 return entry != nullptr && pred(*entry);
101 });
Junxiao Shie368d992014-12-02 23:44:31 -0700102 if (nte != nullptr) {
103 return nte->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -0700104 }
Junxiao Shie368d992014-12-02 23:44:31 -0700105 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800106}
Junxiao Shi65d00722014-02-17 10:50:20 -0700107
Junxiao Shib30c7b02015-01-07 15:45:54 -0700108shared_ptr<Entry>
HangZhangc85a23c2014-03-01 15:55:55 +0800109Measurements::findExactMatch(const Name& name) const
110{
Junxiao Shie368d992014-12-02 23:44:31 -0700111 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
112 if (nte != nullptr)
113 return nte->getMeasurementsEntry();
114 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -0700115}
116
117void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700118Measurements::extendLifetime(Entry& entry,
Junxiao Shiee5a4442014-07-27 17:13:43 -0700119 const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700120{
Junxiao Shie368d992014-12-02 23:44:31 -0700121 shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
122 if (nte == nullptr ||
123 nte->getMeasurementsEntry().get() != &entry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700124 // entry is already gone; it is a dangling reference
125 return;
HangZhangc85a23c2014-03-01 15:55:55 +0800126 }
Junxiao Shiee5a4442014-07-27 17:13:43 -0700127
128 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700129 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700130 // has longer lifetime, not extending
131 return;
132 }
133
Junxiao Shie368d992014-12-02 23:44:31 -0700134 scheduler::cancel(entry.m_cleanup);
135 entry.m_expiry = expiry;
136 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800137}
138
139void
Junxiao Shib30c7b02015-01-07 15:45:54 -0700140Measurements::cleanup(Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800141{
Junxiao Shie368d992014-12-02 23:44:31 -0700142 shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
143 if (nte != nullptr) {
144 nte->setMeasurementsEntry(nullptr);
145 m_nameTree.eraseEntryIfEmpty(nte);
HangZhangc85a23c2014-03-01 15:55:55 +0800146 m_nItems--;
147 }
Junxiao Shi65d00722014-02-17 10:50:20 -0700148}
149
150} // namespace nfd