blob: 8a2430699becd580837986bcb6fab32d516239ef [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
HangZhangc85a23c2014-03-01 15:55:55 +080033Measurements::Measurements(NameTree& nameTree)
34 : m_nameTree(nameTree)
35 , m_nItems(0)
Junxiao Shi65d00722014-02-17 10:50:20 -070036{
37}
38
Junxiao Shi65d00722014-02-17 10:50:20 -070039shared_ptr<measurements::Entry>
Junxiao Shie368d992014-12-02 23:44:31 -070040Measurements::get(name_tree::Entry& nte)
Junxiao Shi65d00722014-02-17 10:50:20 -070041{
Junxiao Shie368d992014-12-02 23:44:31 -070042 shared_ptr<measurements::Entry> entry = nte.getMeasurementsEntry();
43 if (entry != nullptr)
HangZhangc85a23c2014-03-01 15:55:55 +080044 return entry;
Junxiao Shi19838042014-06-21 00:34:01 -070045
Junxiao Shie368d992014-12-02 23:44:31 -070046 entry = make_shared<measurements::Entry>(nte.getPrefix());
47 nte.setMeasurementsEntry(entry);
Junxiao Shi19838042014-06-21 00:34:01 -070048 ++m_nItems;
49
50 entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
51 entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
Junxiao Shie368d992014-12-02 23:44:31 -070052 bind(&Measurements::cleanup, this, ref(*entry)));
Junxiao Shi19838042014-06-21 00:34:01 -070053
Junxiao Shi65d00722014-02-17 10:50:20 -070054 return entry;
55}
56
57shared_ptr<measurements::Entry>
HangZhangcb4fc832014-03-11 16:57:11 +080058Measurements::get(const Name& name)
59{
Junxiao Shie368d992014-12-02 23:44:31 -070060 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
61 return this->get(*nte);
HangZhangcb4fc832014-03-11 16:57:11 +080062}
63
64shared_ptr<measurements::Entry>
Junxiao Shi65d00722014-02-17 10:50:20 -070065Measurements::get(const fib::Entry& fibEntry)
66{
Junxiao Shie368d992014-12-02 23:44:31 -070067 shared_ptr<name_tree::Entry> nte = m_nameTree.get(fibEntry);
68 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070069}
70
71shared_ptr<measurements::Entry>
72Measurements::get(const pit::Entry& pitEntry)
73{
Junxiao Shie368d992014-12-02 23:44:31 -070074 shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
75 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070076}
77
78shared_ptr<measurements::Entry>
Junxiao Shie368d992014-12-02 23:44:31 -070079Measurements::getParent(const measurements::Entry& child)
Junxiao Shi65d00722014-02-17 10:50:20 -070080{
Junxiao Shie368d992014-12-02 23:44:31 -070081 if (child.getName().size() == 0) { // the root entry
82 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -070083 }
84
Junxiao Shie368d992014-12-02 23:44:31 -070085 shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child);
86 shared_ptr<name_tree::Entry> nte = nteChild->getParent();
87 BOOST_ASSERT(nte != nullptr);
88 return this->get(*nte);
Junxiao Shi65d00722014-02-17 10:50:20 -070089}
90
HangZhangc85a23c2014-03-01 15:55:55 +080091shared_ptr<measurements::Entry>
92Measurements::findLongestPrefixMatch(const Name& name) const
Junxiao Shi65d00722014-02-17 10:50:20 -070093{
Junxiao Shie368d992014-12-02 23:44:31 -070094 shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(name,
95 [] (const name_tree::Entry& nte) { return nte.getMeasurementsEntry() != nullptr; });
96 if (nte != nullptr) {
97 return nte->getMeasurementsEntry();
Junxiao Shi65d00722014-02-17 10:50:20 -070098 }
Junxiao Shie368d992014-12-02 23:44:31 -070099 return nullptr;
HangZhangc85a23c2014-03-01 15:55:55 +0800100}
Junxiao Shi65d00722014-02-17 10:50:20 -0700101
HangZhangc85a23c2014-03-01 15:55:55 +0800102shared_ptr<measurements::Entry>
103Measurements::findExactMatch(const Name& name) const
104{
Junxiao Shie368d992014-12-02 23:44:31 -0700105 shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
106 if (nte != nullptr)
107 return nte->getMeasurementsEntry();
108 return nullptr;
Junxiao Shi65d00722014-02-17 10:50:20 -0700109}
110
111void
Junxiao Shie368d992014-12-02 23:44:31 -0700112Measurements::extendLifetime(measurements::Entry& entry,
Junxiao Shiee5a4442014-07-27 17:13:43 -0700113 const time::nanoseconds& lifetime)
Junxiao Shi65d00722014-02-17 10:50:20 -0700114{
Junxiao Shie368d992014-12-02 23:44:31 -0700115 shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
116 if (nte == nullptr ||
117 nte->getMeasurementsEntry().get() != &entry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700118 // entry is already gone; it is a dangling reference
119 return;
HangZhangc85a23c2014-03-01 15:55:55 +0800120 }
Junxiao Shiee5a4442014-07-27 17:13:43 -0700121
122 time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
Junxiao Shie368d992014-12-02 23:44:31 -0700123 if (entry.m_expiry >= expiry) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700124 // has longer lifetime, not extending
125 return;
126 }
127
Junxiao Shie368d992014-12-02 23:44:31 -0700128 scheduler::cancel(entry.m_cleanup);
129 entry.m_expiry = expiry;
130 entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
HangZhangc85a23c2014-03-01 15:55:55 +0800131}
132
133void
Junxiao Shie368d992014-12-02 23:44:31 -0700134Measurements::cleanup(measurements::Entry& entry)
HangZhangc85a23c2014-03-01 15:55:55 +0800135{
Junxiao Shie368d992014-12-02 23:44:31 -0700136 shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
137 if (nte != nullptr) {
138 nte->setMeasurementsEntry(nullptr);
139 m_nameTree.eraseEntryIfEmpty(nte);
HangZhangc85a23c2014-03-01 15:55:55 +0800140 m_nItems--;
141 }
Junxiao Shi65d00722014-02-17 10:50:20 -0700142}
143
144} // namespace nfd