Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, Regents of the University of California, |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 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. |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 12 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 13 | * |
| 14 | * NDNS 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 | * NDNS 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 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 26 | #ifndef NDNS_DAEMON_CONFIG_FILE_HPP |
| 27 | #define NDNS_DAEMON_CONFIG_FILE_HPP |
| 28 | |
| 29 | #include "common.hpp" |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 31 | #include <boost/property_tree/ptree.hpp> |
| 32 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 33 | namespace ndn { |
| 34 | namespace ndns { |
| 35 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 36 | /** \brief a config file section |
| 37 | */ |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 38 | typedef boost::property_tree::ptree ConfigSection; |
| 39 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 40 | /** \brief an optional config file section |
| 41 | */ |
| 42 | typedef boost::optional<const ConfigSection&> OptionalConfigSection; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 44 | /** \brief callback to process a config file section |
| 45 | */ |
| 46 | typedef function<void(const ConfigSection& section, |
| 47 | bool isDryRun, |
| 48 | const std::string& filename)> ConfigSectionHandler; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 50 | /** \brief callback to process a config file section without a \p ConfigSectionHandler |
| 51 | */ |
| 52 | typedef function<void(const std::string& filename, |
| 53 | const std::string& sectionName, |
| 54 | const ConfigSection& section, |
| 55 | bool isDryRun)> UnknownConfigSectionHandler; |
| 56 | |
| 57 | /** \brief configuration file parsing utility |
| 58 | */ |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 59 | class ConfigFile : boost::noncopyable |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 60 | { |
| 61 | public: |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 62 | class Error : public std::runtime_error |
| 63 | { |
| 64 | public: |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 65 | using std::runtime_error::runtime_error; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 68 | explicit |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 69 | ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection); |
| 70 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 71 | public: // unknown section handlers |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 72 | static void |
| 73 | throwErrorOnUnknownSection(const std::string& filename, |
| 74 | const std::string& sectionName, |
| 75 | const ConfigSection& section, |
| 76 | bool isDryRun); |
| 77 | |
| 78 | static void |
| 79 | ignoreUnknownSection(const std::string& filename, |
| 80 | const std::string& sectionName, |
| 81 | const ConfigSection& section, |
| 82 | bool isDryRun); |
| 83 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 84 | public: // parse helpers |
| 85 | /** \brief parse a config option that can be either "yes" or "no" |
| 86 | * \retval true "yes" |
| 87 | * \retval false "no" |
| 88 | * \throw Error the value is neither "yes" nor "no" |
| 89 | */ |
| 90 | static bool |
| 91 | parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName); |
| 92 | |
| 93 | static bool |
| 94 | parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName) |
| 95 | { |
| 96 | return parseYesNo(option.second, option.first, sectionName); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * \brief parse a numeric (integral or floating point) config option |
| 101 | * \tparam T an arithmetic type |
| 102 | * |
| 103 | * \return the numeric value of the parsed option |
| 104 | * \throw Error the value cannot be converted to the specified type |
| 105 | */ |
| 106 | template<typename T> |
| 107 | static T |
| 108 | parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName) |
| 109 | { |
| 110 | static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type"); |
| 111 | |
| 112 | boost::optional<T> value = node.get_value_optional<T>(); |
| 113 | if (value) { |
| 114 | return *value; |
| 115 | } |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 116 | NDN_THROW(Error("Invalid value \"" + node.get_value<std::string>() + |
| 117 | "\" for option \"" + key + "\" in \"" + sectionName + "\" section")); |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | template <typename T> |
| 121 | static T |
| 122 | parseNumber(const ConfigSection::value_type& option, const std::string& sectionName) |
| 123 | { |
| 124 | return parseNumber<T>(option.second, option.first, sectionName); |
| 125 | } |
| 126 | |
| 127 | public: // setup and parsing |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 128 | /// \brief setup notification of configuration file sections |
| 129 | void |
| 130 | addSectionHandler(const std::string& sectionName, |
| 131 | ConfigSectionHandler subscriber); |
| 132 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 133 | /** |
| 134 | * \param filename file to parse |
| 135 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 136 | * \throws ConfigFile::Error if file not found |
| 137 | * \throws ConfigFile::Error if parse error |
| 138 | */ |
| 139 | void |
| 140 | parse(const std::string& filename, bool isDryRun); |
| 141 | |
| 142 | /** |
| 143 | * \param input configuration (as a string) to parse |
| 144 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 145 | * \param filename logical filename of the config file, can appear in error messages |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 146 | * \throws ConfigFile::Error if file not found |
| 147 | * \throws ConfigFile::Error if parse error |
| 148 | */ |
| 149 | void |
| 150 | parse(const std::string& input, bool isDryRun, const std::string& filename); |
| 151 | |
| 152 | /** |
| 153 | * \param input stream to parse |
| 154 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 155 | * \param filename logical filename of the config file, can appear in error messages |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 156 | * \throws ConfigFile::Error if parse error |
| 157 | */ |
| 158 | void |
| 159 | parse(std::istream& input, bool isDryRun, const std::string& filename); |
| 160 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 161 | /** |
| 162 | * \param config ConfigSection that needs to be processed |
| 163 | * \param isDryRun true if performing a dry run of configuration, false otherwise |
| 164 | * \param filename logical filename of the config file, can appear in error messages |
| 165 | * \throws ConfigFile::Error if parse error |
| 166 | */ |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 167 | void |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 168 | parse(const ConfigSection& config, bool isDryRun, const std::string& filename); |
| 169 | |
| 170 | private: |
| 171 | void |
| 172 | process(bool isDryRun, const std::string& filename) const; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 173 | |
| 174 | private: |
| 175 | UnknownConfigSectionHandler m_unknownSectionCallback; |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 176 | std::map<std::string, ConfigSectionHandler> m_subscriptions; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 177 | ConfigSection m_global; |
| 178 | }; |
| 179 | |
| 180 | } // namespace ndns |
| 181 | } // namespace ndn |
| 182 | |
| 183 | #endif // NDNS_DAEMON_CONFIG_FILE_HPP |