blob: fca4af28cb325e04c2f5ebe3305b35396aaf095f [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * Copyright (c) 2014 Regents of the University of California,
22 * Arizona Board of Regents,
23 * Colorado State University,
24 * University Pierre & Marie Curie, Sorbonne University,
25 * Washington University in St. Louis,
26 * Beijing Institute of Technology
27 *
28 * This file is part of NFD (Named Data Networking Forwarding Daemon).
29 * See AUTHORS.md for complete list of NFD authors and contributors.
30 *
31 * NFD is free software: you can redistribute it and/or modify it under the terms
32 * of the GNU General Public License as published by the Free Software Foundation,
33 * either version 3 of the License, or (at your option) any later version.
34 *
35 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
36 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
37 * PURPOSE. See the GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License along with
40 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
41 **/
42
43#ifndef NDNS_DAEMON_CONFIG_FILE_HPP
44#define NDNS_DAEMON_CONFIG_FILE_HPP
45
46#include "common.hpp"
47#include <functional>
48#include <map>
49#include <boost/noncopyable.hpp>
50#include <boost/property_tree/ptree.hpp>
51
52
53namespace ndn {
54namespace ndns {
55
56typedef boost::property_tree::ptree ConfigSection;
57
58/// \brief callback for config file sections
59typedef std::function<void(const ConfigSection& /*section*/,
60 bool /*isDryRun*/,
61 const std::string& /*filename*/)> ConfigSectionHandler;
62
63/// \brief callback for config file sections without a subscribed handler
64typedef std::function<void(const std::string& /*filename*/,
65 const std::string& /*sectionName*/,
66 const ConfigSection& /*section*/,
67 bool /*isDryRun*/)> UnknownConfigSectionHandler;
68
69class ConfigFile : boost::noncopyable
70{
71public:
72
73 class Error : public std::runtime_error
74 {
75 public:
76 explicit
77 Error(const std::string& what)
78 : std::runtime_error(what)
79 {
80
81 }
82 };
83
84 ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
85
86 static void
87 throwErrorOnUnknownSection(const std::string& filename,
88 const std::string& sectionName,
89 const ConfigSection& section,
90 bool isDryRun);
91
92 static void
93 ignoreUnknownSection(const std::string& filename,
94 const std::string& sectionName,
95 const ConfigSection& section,
96 bool isDryRun);
97
98 /// \brief setup notification of configuration file sections
99 void
100 addSectionHandler(const std::string& sectionName,
101 ConfigSectionHandler subscriber);
102
103
104 /**
105 * \param filename file to parse
106 * \param isDryRun true if performing a dry run of configuration, false otherwise
107 * \throws ConfigFile::Error if file not found
108 * \throws ConfigFile::Error if parse error
109 */
110 void
111 parse(const std::string& filename, bool isDryRun);
112
113 /**
114 * \param input configuration (as a string) to parse
115 * \param isDryRun true if performing a dry run of configuration, false otherwise
116 * \param filename optional convenience argument to provide more detailed error messages
117 * \throws ConfigFile::Error if file not found
118 * \throws ConfigFile::Error if parse error
119 */
120 void
121 parse(const std::string& input, bool isDryRun, const std::string& filename);
122
123 /**
124 * \param input stream to parse
125 * \param isDryRun true if performing a dry run of configuration, false otherwise
126 * \param filename optional convenience argument to provide more detailed error messages
127 * \throws ConfigFile::Error if parse error
128 */
129 void
130 parse(std::istream& input, bool isDryRun, const std::string& filename);
131
132private:
133
134 void
135 process(bool isDryRun, const std::string& filename);
136
137private:
138 UnknownConfigSectionHandler m_unknownSectionCallback;
139
140 typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable;
141
142 SubscriptionTable m_subscriptions;
143
144 ConfigSection m_global;
145};
146
147} // namespace ndns
148} // namespace ndn
149
150#endif // NDNS_DAEMON_CONFIG_FILE_HPP