blob: 525287d0be52485d9fd419334b923cdfa6244d4f [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#include "config-file.hpp"
44#include "logger.hpp"
45
46#include <boost/property_tree/info_parser.hpp>
47#include <fstream>
48
49
50namespace ndn {
51namespace ndns {
52
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070053NDNS_LOG_INIT("ConfigFile")
Shock Jiangcde28712014-10-19 21:17:20 -070054
55void
56ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
57 const std::string& sectionName,
58 const ConfigSection& section,
59 bool isDryRun)
60{
61 std::string msg = "Error processing configuration file ";
62 msg += filename;
63 msg += ": no module subscribed for section \"" + sectionName + "\"";
64
65 throw ConfigFile::Error(msg);
66}
67
68void
69ConfigFile::ignoreUnknownSection(const std::string& filename,
70 const std::string& sectionName,
71 const ConfigSection& section,
72 bool isDryRun)
73{
74 // do nothing
75}
76
77ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
78 : m_unknownSectionCallback(unknownSectionCallback)
79{
80}
81
82void
83ConfigFile::addSectionHandler(const std::string& sectionName,
84 ConfigSectionHandler subscriber)
85{
86 m_subscriptions[sectionName] = subscriber;
87}
88
89void
90ConfigFile::parse(const std::string& filename, bool isDryRun)
91{
92 BOOST_ASSERT(isDryRun == false);
93 std::ifstream inputFile;
94 inputFile.open(filename.c_str());
95 if (!inputFile.good() || !inputFile.is_open())
96 {
97 std::string msg = "Failed to read configuration file: ";
98 msg += filename;
99 throw Error(msg);
100 }
101 parse(inputFile, isDryRun, filename);
102 inputFile.close();
103}
104
105void
106ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
107{
108 std::istringstream inputStream(input);
109 parse(inputStream, isDryRun, filename);
110}
111
112
113void
114ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
115{
116 try
117 {
118 boost::property_tree::read_info(input, m_global);
119 }
120 catch (const boost::property_tree::info_parser_error& error)
121 {
122 std::stringstream msg;
123 msg << "Failed to parse configuration file";
124 msg << " " << filename;
125 msg << " " << error.message() << " line " << error.line();
126 throw Error(msg.str());
127 }
128
129 process(isDryRun, filename);
130}
131
132void
133ConfigFile::process(bool isDryRun, const std::string& filename)
134{
135 BOOST_ASSERT(isDryRun == false);
136 BOOST_ASSERT(!filename.empty());
137 // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
138
139 if (m_global.begin() == m_global.end())
140 {
141 std::string msg = "Error processing configuration file: ";
142 msg += filename;
143 msg += " no data";
144 throw Error(msg);
145 }
146
147 for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
148 {
149 const std::string& sectionName = i->first;
150 const ConfigSection& section = i->second;
151
152 SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
153 if (subscriberIt != m_subscriptions.end())
154 {
155 ConfigSectionHandler subscriber = subscriberIt->second;
156 subscriber(section, isDryRun, filename);
157 }
158 else
159 {
160 m_unknownSectionCallback(filename, sectionName, section, isDryRun);
161 }
162 }
163}
164
165} // namespace ndns
166} // namespace ndn