blob: 29a05873022fdb12f6eb7c1b3e8954634e604d84 [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev08d18742018-03-15 16:31:28 -04002/*
Davide Pesavento38fd3982022-04-18 22:22:02 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyev08d18742018-03-15 16:31:28 -04004 * 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 Jiangcde28712014-10-19 21:17:20 -070010 *
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 Jiangcde28712014-10-19 21:17:20 -070026#ifndef NDNS_DAEMON_CONFIG_FILE_HPP
27#define NDNS_DAEMON_CONFIG_FILE_HPP
28
29#include "common.hpp"
Shock Jiangcde28712014-10-19 21:17:20 -070030
Davide Pesavento948c50c2020-12-26 21:30:45 -050031#include <boost/property_tree/ptree.hpp>
32
Shock Jiangcde28712014-10-19 21:17:20 -070033namespace ndn {
34namespace ndns {
35
Alexander Afanasyev08d18742018-03-15 16:31:28 -040036/** \brief a config file section
37 */
Shock Jiangcde28712014-10-19 21:17:20 -070038typedef boost::property_tree::ptree ConfigSection;
39
Alexander Afanasyev08d18742018-03-15 16:31:28 -040040/** \brief an optional config file section
41 */
42typedef boost::optional<const ConfigSection&> OptionalConfigSection;
Shock Jiangcde28712014-10-19 21:17:20 -070043
Alexander Afanasyev08d18742018-03-15 16:31:28 -040044/** \brief callback to process a config file section
45 */
46typedef function<void(const ConfigSection& section,
47 bool isDryRun,
48 const std::string& filename)> ConfigSectionHandler;
Shock Jiangcde28712014-10-19 21:17:20 -070049
Alexander Afanasyev08d18742018-03-15 16:31:28 -040050/** \brief callback to process a config file section without a \p ConfigSectionHandler
51 */
52typedef 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 Pesavento948c50c2020-12-26 21:30:45 -050059class ConfigFile : boost::noncopyable
Shock Jiangcde28712014-10-19 21:17:20 -070060{
61public:
Shock Jiangcde28712014-10-19 21:17:20 -070062 class Error : public std::runtime_error
63 {
64 public:
Davide Pesavento948c50c2020-12-26 21:30:45 -050065 using std::runtime_error::runtime_error;
Shock Jiangcde28712014-10-19 21:17:20 -070066 };
67
Alexander Afanasyev08d18742018-03-15 16:31:28 -040068 explicit
Shock Jiangcde28712014-10-19 21:17:20 -070069 ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
70
Alexander Afanasyev08d18742018-03-15 16:31:28 -040071public: // unknown section handlers
Shock Jiangcde28712014-10-19 21:17:20 -070072 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 Afanasyev08d18742018-03-15 16:31:28 -040084public: // 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 {
Davide Pesavento38fd3982022-04-18 22:22:02 -0400110 static_assert(std::is_arithmetic_v<T>, "T must be an arithmetic type");
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400111
112 boost::optional<T> value = node.get_value_optional<T>();
113 if (value) {
114 return *value;
115 }
Davide Pesavento948c50c2020-12-26 21:30:45 -0500116 NDN_THROW(Error("Invalid value \"" + node.get_value<std::string>() +
117 "\" for option \"" + key + "\" in \"" + sectionName + "\" section"));
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400118 }
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
127public: // setup and parsing
Shock Jiangcde28712014-10-19 21:17:20 -0700128 /// \brief setup notification of configuration file sections
129 void
130 addSectionHandler(const std::string& sectionName,
131 ConfigSectionHandler subscriber);
132
Shock Jiangcde28712014-10-19 21:17:20 -0700133 /**
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 Afanasyev08d18742018-03-15 16:31:28 -0400145 * \param filename logical filename of the config file, can appear in error messages
Shock Jiangcde28712014-10-19 21:17:20 -0700146 * \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 Afanasyev08d18742018-03-15 16:31:28 -0400155 * \param filename logical filename of the config file, can appear in error messages
Shock Jiangcde28712014-10-19 21:17:20 -0700156 * \throws ConfigFile::Error if parse error
157 */
158 void
159 parse(std::istream& input, bool isDryRun, const std::string& filename);
160
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400161 /**
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 Jiangcde28712014-10-19 21:17:20 -0700167 void
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400168 parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
169
170private:
171 void
172 process(bool isDryRun, const std::string& filename) const;
Shock Jiangcde28712014-10-19 21:17:20 -0700173
174private:
175 UnknownConfigSectionHandler m_unknownSectionCallback;
Alexander Afanasyev08d18742018-03-15 16:31:28 -0400176 std::map<std::string, ConfigSectionHandler> m_subscriptions;
Shock Jiangcde28712014-10-19 21:17:20 -0700177 ConfigSection m_global;
178};
179
180} // namespace ndns
181} // namespace ndn
182
183#endif // NDNS_DAEMON_CONFIG_FILE_HPP