blob: 5981371dd5b4c3afb6e4a985a200fcaecd1c7473 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "measurements.hpp"
#include "name-tree.hpp"
#include "pit-entry.hpp"
#include "fib-entry.hpp"
namespace nfd {
using measurements::Entry;
Measurements::Measurements(NameTree& nameTree)
: m_nameTree(nameTree)
, m_nItems(0)
{
}
shared_ptr<Entry>
Measurements::get(name_tree::Entry& nte)
{
shared_ptr<Entry> entry = nte.getMeasurementsEntry();
if (entry != nullptr)
return entry;
entry = make_shared<Entry>(nte.getPrefix());
nte.setMeasurementsEntry(entry);
++m_nItems;
entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
bind(&Measurements::cleanup, this, ref(*entry)));
return entry;
}
shared_ptr<Entry>
Measurements::get(const Name& name)
{
shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
return this->get(*nte);
}
shared_ptr<Entry>
Measurements::get(const fib::Entry& fibEntry)
{
shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(fibEntry);
if (nte == nullptr) {
// must be Fib::s_emptyEntry that is unattched
BOOST_ASSERT(fibEntry.getPrefix().empty());
nte = m_nameTree.lookup(fibEntry.getPrefix());
}
BOOST_ASSERT(nte != nullptr);
return this->get(*nte);
}
shared_ptr<Entry>
Measurements::get(const pit::Entry& pitEntry)
{
shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry);
BOOST_ASSERT(nte != nullptr);
return this->get(*nte);
}
shared_ptr<Entry>
Measurements::getParent(const Entry& child)
{
if (child.getName().size() == 0) { // the root entry
return nullptr;
}
shared_ptr<name_tree::Entry> nteChild = m_nameTree.lookup(child);
shared_ptr<name_tree::Entry> nte = nteChild->getParent();
BOOST_ASSERT(nte != nullptr);
return this->get(*nte);
}
template<typename K>
shared_ptr<Entry>
Measurements::findLongestPrefixMatchImpl(const K& key,
const measurements::EntryPredicate& pred) const
{
shared_ptr<name_tree::Entry> match = m_nameTree.findLongestPrefixMatch(key,
[pred] (const name_tree::Entry& nte) -> bool {
shared_ptr<Entry> entry = nte.getMeasurementsEntry();
return entry != nullptr && pred(*entry);
});
if (match != nullptr) {
return match->getMeasurementsEntry();
}
return nullptr;
}
shared_ptr<Entry>
Measurements::findLongestPrefixMatch(const Name& name,
const measurements::EntryPredicate& pred) const
{
return this->findLongestPrefixMatchImpl(name, pred);
}
shared_ptr<Entry>
Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry,
const measurements::EntryPredicate& pred) const
{
shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(pitEntry);
BOOST_ASSERT(nte != nullptr);
return this->findLongestPrefixMatchImpl(nte, pred);
}
shared_ptr<Entry>
Measurements::findExactMatch(const Name& name) const
{
shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
if (nte != nullptr)
return nte->getMeasurementsEntry();
return nullptr;
}
void
Measurements::extendLifetime(Entry& entry, const time::nanoseconds& lifetime)
{
shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
if (nte == nullptr || nte->getMeasurementsEntry().get() != &entry) {
// entry is already gone; it is a dangling reference
return;
}
time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
if (entry.m_expiry >= expiry) {
// has longer lifetime, not extending
return;
}
scheduler::cancel(entry.m_cleanup);
entry.m_expiry = expiry;
entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
}
void
Measurements::cleanup(Entry& entry)
{
shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(entry);
if (nte != nullptr) {
nte->setMeasurementsEntry(nullptr);
m_nameTree.eraseEntryIfEmpty(nte);
m_nItems--;
}
}
} // namespace nfd