blob: 8c810a428ca53ede7c667ffc663fde4015f57c99 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, 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/>.
*/
#ifndef NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP
#define NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP
#include "strategy-choice-entry.hpp"
#include "name-tree.hpp"
#include <boost/range/adaptor/transformed.hpp>
namespace nfd {
class Forwarder;
namespace strategy_choice {
/** \brief Represents the Strategy Choice table
*
* The Strategy Choice table maintains available Strategy types,
* and associates Name prefixes with Strategy types.
*
* Each strategy is identified by a strategyName.
* It's recommended to include a version number as the last component of strategyName.
*
* A Name prefix is owned by a strategy if a longest prefix match on the
* Strategy Choice table returns that strategy.
*/
class StrategyChoice : noncopyable
{
public:
explicit
StrategyChoice(Forwarder& forwarder);
size_t
size() const
{
return m_nItems;
}
/** \brief Set the default strategy
*
* This must be called by forwarder constructor.
*/
void
setDefaultStrategy(const Name& strategyName);
public: // Strategy Choice table
class InsertResult
{
public:
explicit
operator bool() const
{
return m_status == OK;
}
bool
isRegistered() const
{
return m_status == OK || m_status == EXCEPTION;
}
/** \brief Get a status code for use in management command response
*/
int
getStatusCode() const
{
return static_cast<int>(m_status);
}
private:
enum Status {
OK = 200,
NOT_REGISTERED = 404,
EXCEPTION = 409,
DEPTH_EXCEEDED = 414,
};
// implicitly constructible from Status
InsertResult(Status status, const std::string& exceptionMessage = "");
private:
Status m_status;
std::string m_exceptionMessage;
friend StrategyChoice;
friend std::ostream& operator<<(std::ostream&, const InsertResult&);
};
/** \brief Set strategy of \p prefix to be \p strategyName
* \param prefix the name prefix to change strategy
* \param strategyName strategy instance name, may contain version and parameters;
* strategy must have been registered
* \return convertible to true on success; convertible to false on failure
*/
InsertResult
insert(const Name& prefix, const Name& strategyName);
/** \brief Make prefix to inherit strategy from its parent
* \note Not allowed for root prefix (ndn:/)
*/
void
erase(const Name& prefix);
/** \brief Get strategy Name of prefix
* \return true and strategyName at exact match, or false
*/
std::pair<bool, Name>
get(const Name& prefix) const;
public: // effective strategy
/** \brief Get effective strategy for \p prefix
*/
fw::Strategy&
findEffectiveStrategy(const Name& prefix) const;
/** \brief Get effective strategy for \p pitEntry
*
* This is equivalent to `findEffectiveStrategy(pitEntry.getName())`
*/
fw::Strategy&
findEffectiveStrategy(const pit::Entry& pitEntry) const;
/** \brief Get effective strategy for \p measurementsEntry
*
* This is equivalent to `findEffectiveStrategy(measurementsEntry.getName())`
*/
fw::Strategy&
findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
public: // enumeration
using Range = boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range>;
using const_iterator = boost::range_iterator<Range>::type;
/** \return an iterator to the beginning
* \note Iteration order is implementation-defined.
* \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
* is inserted or erased during enumeration.
*/
const_iterator
begin() const
{
return this->getRange().begin();
}
/** \return an iterator to the end
* \sa begin()
*/
const_iterator
end() const
{
return this->getRange().end();
}
private:
void
changeStrategy(Entry& entry,
fw::Strategy& oldStrategy,
fw::Strategy& newStrategy);
/** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch
*/
template<typename K>
fw::Strategy&
findEffectiveStrategyImpl(const K& key) const;
Range
getRange() const;
private:
Forwarder& m_forwarder;
NameTree& m_nameTree;
size_t m_nItems = 0;
};
std::ostream&
operator<<(std::ostream& os, const StrategyChoice::InsertResult& res);
} // namespace strategy_choice
using strategy_choice::StrategyChoice;
} // namespace nfd
#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP