mgmt: TablesConfigSection constructor accepts Forwarder&
refs #2181
Change-Id: I1a87dd573f25f3806bc1b1f3c60ec9eef0eca4e3
diff --git a/core/config-file.cpp b/core/config-file.cpp
index 19bf1cb..b9f40b1 100644
--- a/core/config-file.cpp
+++ b/core/config-file.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2015, Regents of the University of California,
+ * Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -59,10 +59,10 @@
}
bool
-ConfigFile::parseYesNo(const ConfigSection::value_type& option,
+ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
const std::string& sectionName)
{
- auto value = option.second.get_value<std::string>();
+ auto value = node.get_value<std::string>();
if (value == "yes") {
return true;
@@ -72,7 +72,7 @@
}
BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
- option.first + "\" in \"" + sectionName + "\" section"));
+ key + "\" in \"" + sectionName + "\" section"));
}
void
@@ -85,14 +85,10 @@
void
ConfigFile::parse(const std::string& filename, bool isDryRun)
{
- std::ifstream inputFile;
- inputFile.open(filename.c_str());
- if (!inputFile.good() || !inputFile.is_open())
- {
- std::string msg = "Failed to read configuration file: ";
- msg += filename;
- BOOST_THROW_EXCEPTION(Error(msg));
- }
+ std::ifstream inputFile(filename);
+ if (!inputFile.good() || !inputFile.is_open()) {
+ BOOST_THROW_EXCEPTION(Error("Failed to read configuration file: " + filename));
+ }
parse(inputFile, isDryRun, filename);
inputFile.close();
}
@@ -107,18 +103,16 @@
void
ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
{
- try
- {
- boost::property_tree::read_info(input, m_global);
- }
- catch (const boost::property_tree::info_parser_error& error)
- {
- std::stringstream msg;
- msg << "Failed to parse configuration file";
- msg << " " << filename;
- msg << " " << error.message() << " line " << error.line();
- BOOST_THROW_EXCEPTION(Error(msg.str()));
- }
+ try {
+ boost::property_tree::read_info(input, m_global);
+ }
+ catch (const boost::property_tree::info_parser_error& error) {
+ std::stringstream msg;
+ msg << "Failed to parse configuration file";
+ msg << " " << filename;
+ msg << " " << error.message() << " line " << error.line();
+ BOOST_THROW_EXCEPTION(Error(msg.str()));
+ }
process(isDryRun, filename);
}
@@ -144,7 +138,7 @@
for (const auto& i : m_global) {
try {
- ConfigSectionHandler subscriber = m_subscriptions.at(i.first);
+ const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
subscriber(i.second, isDryRun, filename);
}
catch (const std::out_of_range&) {
@@ -153,4 +147,4 @@
}
}
-}
+} // namespace nfd
diff --git a/core/config-file.hpp b/core/config-file.hpp
index f6e4aaf..890a22c 100644
--- a/core/config-file.hpp
+++ b/core/config-file.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2015, Regents of the University of California,
+ * Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -45,6 +45,8 @@
const ConfigSection& /*section*/,
bool /*isDryRun*/)> UnknownConfigSectionHandler;
+/** \brief configuration file parsing utility
+ */
class ConfigFile : noncopyable
{
public:
@@ -61,6 +63,7 @@
explicit
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
+public: // unknown section handlers
static void
throwErrorOnUnknownSection(const std::string& filename,
const std::string& sectionName,
@@ -73,27 +76,50 @@
const ConfigSection& section,
bool isDryRun);
- /**
- * \brief parse a config option that can be either "yes" or "no"
- *
- * \return true if "yes", false if "no"
- * \throw Error the value is neither "yes" nor "no"
+public: // parse helpers
+ /** \brief parse a config option that can be either "yes" or "no"
+ * \retval true "yes"
+ * \retval false "no"
+ * \throw Error the value is neither "yes" nor "no"
*/
static bool
- parseYesNo(const ConfigSection::value_type& option,
- const std::string& sectionName);
+ parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
+
+ static bool
+ parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
+ {
+ return parseYesNo(option.second, option.first, sectionName);
+ }
/**
* \brief parse a numeric (integral or floating point) config option
+ * \tparam T an arithmetic type
*
* \return the numeric value of the parsed option
* \throw Error the value cannot be converted to the specified type
*/
- template <typename T>
- static typename std::enable_if<std::is_arithmetic<T>::value, T>::type
- parseNumber(const ConfigSection::value_type& option,
- const std::string& sectionName);
+ template<typename T>
+ static T
+ parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
+ {
+ static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
+ boost::optional<T> value = node.get_value_optional<T>();
+ if (value) {
+ return *value;
+ }
+ BOOST_THROW_EXCEPTION(Error("Invalid value \"" + node.get_value<std::string>() +
+ "\" for option \"" + key + "\" in \"" + sectionName + "\" section"));
+ }
+
+ template <typename T>
+ static T
+ parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
+ {
+ return parseNumber<T>(option.second, option.first, sectionName);
+ }
+
+public: // setup and parsing
/// \brief setup notification of configuration file sections
void
addSectionHandler(const std::string& sectionName,
@@ -146,22 +172,6 @@
ConfigSection m_global;
};
-template <typename T>
-inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
-ConfigFile::parseNumber(const ConfigSection::value_type& option,
- const std::string& sectionName)
-{
- auto value = option.second.get_value<std::string>();
-
- try {
- return boost::lexical_cast<T>(value);
- }
- catch (const boost::bad_lexical_cast&) {
- BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
- option.first + "\" in \"" + sectionName + "\" section"));
- }
-}
-
} // namespace nfd
#endif // NFD_CORE_CONFIG_FILE_HPP
diff --git a/daemon/mgmt/tables-config-section.cpp b/daemon/mgmt/tables-config-section.cpp
index 8422a5b..5095da8 100644
--- a/daemon/mgmt/tables-config-section.cpp
+++ b/daemon/mgmt/tables-config-section.cpp
@@ -24,9 +24,7 @@
*/
#include "tables-config-section.hpp"
-
#include "core/logger.hpp"
-#include "core/config-file.hpp"
namespace nfd {
@@ -34,180 +32,113 @@
const size_t TablesConfigSection::DEFAULT_CS_MAX_PACKETS = 65536;
-TablesConfigSection::TablesConfigSection(Cs& cs,
- Pit& pit,
- Fib& fib,
- StrategyChoice& strategyChoice,
- Measurements& measurements,
- NetworkRegionTable& networkRegionTable)
- : m_cs(cs)
- // , m_pit(pit)
- // , m_fib(fib)
- , m_strategyChoice(strategyChoice)
- // , m_measurements(measurements)
- , m_networkRegionTable(networkRegionTable)
- , m_areTablesConfigured(false)
+TablesConfigSection::TablesConfigSection(Forwarder& forwarder)
+ : m_forwarder(forwarder)
+ , m_isConfigured(false)
{
-
}
void
TablesConfigSection::setConfigFile(ConfigFile& configFile)
{
configFile.addSectionHandler("tables",
- bind(&TablesConfigSection::processConfig, this, _1, _2, _3));
+ bind(&TablesConfigSection::processConfig, this, _1, _2));
}
-
void
-TablesConfigSection::ensureTablesAreConfigured()
+TablesConfigSection::ensureConfigured()
{
- if (m_areTablesConfigured) {
+ if (m_isConfigured) {
return;
}
NFD_LOG_INFO("Setting CS max packets to " << DEFAULT_CS_MAX_PACKETS);
- m_cs.setLimit(DEFAULT_CS_MAX_PACKETS);
+ m_forwarder.getCs().setLimit(DEFAULT_CS_MAX_PACKETS);
- m_areTablesConfigured = true;
+ m_isConfigured = true;
}
void
-TablesConfigSection::processConfig(const ConfigSection& configSection,
- bool isDryRun,
- const std::string& filename)
+TablesConfigSection::processConfig(const ConfigSection& section, bool isDryRun)
{
- // tables
- // {
- // cs_max_packets 65536
- //
- // strategy_choice
- // {
- // / /localhost/nfd/strategy/best-route
- // /localhost /localhost/nfd/strategy/multicast
- // /localhost/nfd /localhost/nfd/strategy/best-route
- // /ndn/broadcast /localhost/nfd/strategy/multicast
- // }
- //
- // network_region
- // {
- // /example/region1
- // /example/region2
- // }
- // }
+ typedef boost::optional<const ConfigSection&> OptionalNode;
size_t nCsMaxPackets = DEFAULT_CS_MAX_PACKETS;
-
- boost::optional<const ConfigSection&> csMaxPacketsNode =
- configSection.get_child_optional("cs_max_packets");
-
+ OptionalNode csMaxPacketsNode = section.get_child_optional("cs_max_packets");
if (csMaxPacketsNode) {
- boost::optional<size_t> valCsMaxPackets =
- configSection.get_optional<size_t>("cs_max_packets");
-
- if (!valCsMaxPackets) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"cs_max_packets\""
- " in \"tables\" section"));
- }
-
- nCsMaxPackets = *valCsMaxPackets;
+ nCsMaxPackets = ConfigFile::parseNumber<size_t>(*csMaxPacketsNode, "cs_max_packets", "tables");
}
- boost::optional<const ConfigSection&> strategyChoiceSection =
- configSection.get_child_optional("strategy_choice");
-
+ OptionalNode strategyChoiceSection = section.get_child_optional("strategy_choice");
if (strategyChoiceSection) {
processStrategyChoiceSection(*strategyChoiceSection, isDryRun);
}
- boost::optional<const ConfigSection&> networkRegionSection =
- configSection.get_child_optional("network_region");
-
+ OptionalNode networkRegionSection = section.get_child_optional("network_region");
if (networkRegionSection) {
processNetworkRegionSection(*networkRegionSection, isDryRun);
}
- if (!isDryRun) {
- NFD_LOG_INFO("Setting CS max packets to " << nCsMaxPackets);
-
- m_cs.setLimit(nCsMaxPackets);
- m_areTablesConfigured = true;
+ if (isDryRun) {
+ return;
}
+
+ NFD_LOG_INFO("Setting CS max packets to " << nCsMaxPackets);
+ m_forwarder.getCs().setLimit(nCsMaxPackets);
+
+ m_isConfigured = true;
}
void
-TablesConfigSection::processStrategyChoiceSection(const ConfigSection& configSection,
- bool isDryRun)
+TablesConfigSection::processStrategyChoiceSection(const ConfigSection& section, bool isDryRun)
{
- // strategy_choice
- // {
- // / /localhost/nfd/strategy/best-route
- // /localhost /localhost/nfd/strategy/multicast
- // /localhost/nfd /localhost/nfd/strategy/best-route
- // /ndn/broadcast /localhost/nfd/strategy/multicast
- // }
+ StrategyChoice& sc = m_forwarder.getStrategyChoice();
std::map<Name, Name> choices;
- for (const auto& prefixAndStrategy : configSection) {
- const Name prefix(prefixAndStrategy.first);
- if (choices.find(prefix) != choices.end()) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate strategy choice for prefix \"" +
- prefix.toUri() + "\" in \"strategy_choice\" "
- "section"));
+ for (const auto& prefixAndStrategy : section) {
+ Name prefix(prefixAndStrategy.first);
+ Name strategy(prefixAndStrategy.second.get_value<std::string>());
+
+ if (!sc.hasStrategy(strategy)) {
+ BOOST_THROW_EXCEPTION(ConfigFile::Error(
+ "Unknown strategy \"" + prefixAndStrategy.second.get_value<std::string>() +
+ "\" for prefix \"" + prefix.toUri() + "\" in \"strategy_choice\" section"));
}
- const std::string strategyString(prefixAndStrategy.second.get_value<std::string>());
- if (strategyString.empty()) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"\" for prefix \"" +
- prefix.toUri() + "\" in \"strategy_choice\" "
- "section"));
+ if (!choices.emplace(prefix, strategy).second) {
+ BOOST_THROW_EXCEPTION(ConfigFile::Error(
+ "Duplicate strategy choice for prefix \"" + prefix.toUri() +
+ "\" in \"strategy_choice\" section"));
}
-
- const Name strategyName(strategyString);
- if (!m_strategyChoice.hasStrategy(strategyName)) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid strategy choice \"" +
- strategyName.toUri() + "\" for prefix \"" +
- prefix.toUri() + "\" in \"strategy_choice\" "
- "section"));
- }
-
- choices[prefix] = strategyName;
}
+ if (isDryRun) {
+ return;
+ }
for (const auto& prefixAndStrategy : choices) {
- if (!isDryRun && !m_strategyChoice.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Failed to set strategy \"" +
- prefixAndStrategy.second.toUri() + "\" for "
- "prefix \"" + prefixAndStrategy.first.toUri() +
- "\" in \"strategy_choicev\""));
+ if (!sc.insert(prefixAndStrategy.first, prefixAndStrategy.second)) {
+ BOOST_THROW_EXCEPTION(ConfigFile::Error(
+ "Failed to set strategy \"" + prefixAndStrategy.second.toUri() + "\" for "
+ "prefix \"" + prefixAndStrategy.first.toUri() + "\" in \"strategy_choicev\""));
}
}
}
void
-TablesConfigSection::processNetworkRegionSection(const ConfigSection& configSection,
- bool isDryRun)
+TablesConfigSection::processNetworkRegionSection(const ConfigSection& section, bool isDryRun)
{
- // network_region
- // {
- // /example/region1
- // /example/region2
- // }
-
- if (!isDryRun) {
- m_networkRegionTable.clear();
+ if (isDryRun) {
+ return;
}
- for (const auto& pair : configSection) {
- const Name region(pair.first);
-
- if (!isDryRun) {
- m_networkRegionTable.insert(region);
- }
+ NetworkRegionTable& nrt = m_forwarder.getNetworkRegionTable();
+ nrt.clear();
+ for (const auto& pair : section) {
+ Name region(pair.first);
+ nrt.insert(region);
}
}
-
} // namespace nfd
diff --git a/daemon/mgmt/tables-config-section.hpp b/daemon/mgmt/tables-config-section.hpp
index 82cb877..4aff010 100644
--- a/daemon/mgmt/tables-config-section.hpp
+++ b/daemon/mgmt/tables-config-section.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2015, Regents of the University of California,
+ * Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -26,67 +26,66 @@
#ifndef NFD_MGMT_TABLES_CONFIG_SECTION_HPP
#define NFD_MGMT_TABLES_CONFIG_SECTION_HPP
-#include "table/fib.hpp"
-#include "table/pit.hpp"
-#include "table/cs.hpp"
-#include "table/measurements.hpp"
-#include "table/network-region-table.hpp"
-#include "table/strategy-choice.hpp"
-
+#include "fw/forwarder.hpp"
#include "core/config-file.hpp"
namespace nfd {
-/**
- * \brief Provides parsing for `tables` configuration file section.
+/** \brief handles 'tables' config section
*
- * This class enables configuration of CS, PIT, FIB, Strategy Choice, Measurements, and
- * Network Region tables.
+ * This class recognizes a config section that looks like
+ * \code{.unparsed}
+ * tables
+ * {
+ * cs_max_packets 65536
+ *
+ * strategy_choice
+ * {
+ * / /localhost/nfd/strategy/best-route
+ * /localhost /localhost/nfd/strategy/multicast
+ * /localhost/nfd /localhost/nfd/strategy/best-route
+ * /ndn/broadcast /localhost/nfd/strategy/multicast
+ * }
+ *
+ * network_region
+ * {
+ * /example/region1
+ * /example/region2
+ * }
+ * }
+ * \endcode
*/
-class TablesConfigSection
+class TablesConfigSection : noncopyable
{
public:
- TablesConfigSection(Cs& cs,
- Pit& pit,
- Fib& fib,
- StrategyChoice& strategyChoice,
- Measurements& measurements,
- NetworkRegionTable& networkRegionTable);
+ explicit
+ TablesConfigSection(Forwarder& forwarder);
void
setConfigFile(ConfigFile& configFile);
+ /** \brief apply default configuration, if tables section was omitted in configuration file
+ */
void
- ensureTablesAreConfigured();
+ ensureConfigured();
private:
+ void
+ processConfig(const ConfigSection& section, bool isDryRun);
void
- processConfig(const ConfigSection& configSection,
- bool isDryRun,
- const std::string& filename);
+ processStrategyChoiceSection(const ConfigSection& section, bool isDryRun);
void
- processStrategyChoiceSection(const ConfigSection& configSection,
- bool isDryRun);
-
- void
- processNetworkRegionSection(const ConfigSection& configSection,
- bool isDryRun);
+ processNetworkRegionSection(const ConfigSection& section, bool isDryRun);
private:
- Cs& m_cs;
- // Pit& m_pit;
- // Fib& m_fib;
- StrategyChoice& m_strategyChoice;
- // Measurements& m_measurements;
- NetworkRegionTable& m_networkRegionTable;
-
- bool m_areTablesConfigured;
-
private:
-
static const size_t DEFAULT_CS_MAX_PACKETS;
+
+ Forwarder& m_forwarder;
+
+ bool m_isConfigured;
};
} // namespace nfd
diff --git a/daemon/nfd.cpp b/daemon/nfd.cpp
index 5d6bec7..b3369db 100644
--- a/daemon/nfd.cpp
+++ b/daemon/nfd.cpp
@@ -155,12 +155,7 @@
ConfigFile config(&ignoreRibAndLogSections);
general::setConfigFile(config);
- TablesConfigSection tablesConfig(m_forwarder->getCs(),
- m_forwarder->getPit(),
- m_forwarder->getFib(),
- m_forwarder->getStrategyChoice(),
- m_forwarder->getMeasurements(),
- m_forwarder->getNetworkRegionTable());
+ TablesConfigSection tablesConfig(*m_forwarder);
tablesConfig.setConfigFile(config);
m_authenticator->setConfigFile(config);
@@ -176,7 +171,7 @@
config.parse(m_configSection, false, INTERNAL_CONFIG);
}
- tablesConfig.ensureTablesAreConfigured();
+ tablesConfig.ensureConfigured();
// add FIB entry for NFD Management Protocol
Name topPrefix("/localhost/nfd");
@@ -196,12 +191,7 @@
general::setConfigFile(config);
- TablesConfigSection tablesConfig(m_forwarder->getCs(),
- m_forwarder->getPit(),
- m_forwarder->getFib(),
- m_forwarder->getStrategyChoice(),
- m_forwarder->getMeasurements(),
- m_forwarder->getNetworkRegionTable());
+ TablesConfigSection tablesConfig(*m_forwarder);
tablesConfig.setConfigFile(config);
m_authenticator->setConfigFile(config);
diff --git a/tests/daemon/mgmt/tables-config-section.t.cpp b/tests/daemon/mgmt/tables-config-section.t.cpp
index f3cf743..d87c6e6 100644
--- a/tests/daemon/mgmt/tables-config-section.t.cpp
+++ b/tests/daemon/mgmt/tables-config-section.t.cpp
@@ -44,7 +44,7 @@
, m_strategyChoice(m_forwarder.getStrategyChoice())
, m_measurements(m_forwarder.getMeasurements())
, m_networkRegionTable(m_forwarder.getNetworkRegionTable())
- , m_tablesConfig(m_cs, m_pit, m_fib, m_strategyChoice, m_measurements, m_networkRegionTable)
+ , m_tablesConfig(m_forwarder)
{
m_tablesConfig.setConfigFile(m_config);
}
@@ -76,7 +76,7 @@
{
const size_t initialLimit = m_cs.getLimit();
- m_tablesConfig.ensureTablesAreConfigured();
+ m_tablesConfig.ensureConfigured();
BOOST_CHECK_NE(initialLimit, m_cs.getLimit());
}
@@ -97,7 +97,7 @@
const size_t defaultLimit = m_cs.getLimit();
- m_tablesConfig.ensureTablesAreConfigured();
+ m_tablesConfig.ensureConfigured();
BOOST_CHECK_EQUAL(defaultLimit, m_cs.getLimit());
}
@@ -119,7 +119,7 @@
passiveConfig.parse(CONFIG, false, "dummy-config");
BOOST_REQUIRE_EQUAL(initialLimit, m_cs.getLimit());
- m_tablesConfig.ensureTablesAreConfigured();
+ m_tablesConfig.ensureConfigured();
BOOST_CHECK_NE(initialLimit, m_cs.getLimit());
}
@@ -141,7 +141,7 @@
BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, false));
BOOST_CHECK_EQUAL(m_cs.getLimit(), 101);
- m_tablesConfig.ensureTablesAreConfigured();
+ m_tablesConfig.ensureConfigured();
BOOST_CHECK_EQUAL(m_cs.getLimit(), 101);
}
@@ -371,7 +371,7 @@
BOOST_AUTO_TEST_SUITE_END() // NetworkRegion
-BOOST_AUTO_TEST_SUITE_END() // TestTableConfigSection
+BOOST_AUTO_TEST_SUITE_END() // TestTablesConfigSection
BOOST_AUTO_TEST_SUITE_END() // Mgmt
} // namespace tests