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