blob: 5887c2bbb3db94db4de967adffda4ab7777dcf91 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedettobb75b552014-02-08 12:12:11 -070024
Steve DiBenedettobb75b552014-02-08 12:12:11 -070025#include "config-file.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#include "logger.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070027
28#include <boost/property_tree/info_parser.hpp>
Davide Pesavento52a18f92014-04-10 00:55:01 +020029#include <fstream>
Steve DiBenedettobb75b552014-02-08 12:12:11 -070030
31namespace nfd {
32
33NFD_LOG_INIT("ConfigFile");
34
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060035void
36ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
37 const std::string& sectionName,
38 const ConfigSection& section,
39 bool isDryRun)
40{
41 std::string msg = "Error processing configuration file ";
42 msg += filename;
43 msg += ": no module subscribed for section \"" + sectionName + "\"";
44
45 throw ConfigFile::Error(msg);
46}
47
48void
49ConfigFile::ignoreUnknownSection(const std::string& filename,
50 const std::string& sectionName,
51 const ConfigSection& section,
52 bool isDryRun)
53{
54 // do nothing
55}
56
57ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
58 : m_unknownSectionCallback(unknownSectionCallback)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070059{
Steve DiBenedettobb75b552014-02-08 12:12:11 -070060}
61
62void
63ConfigFile::addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060064 ConfigSectionHandler subscriber)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070065{
66 m_subscriptions[sectionName] = subscriber;
67}
68
69void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060070ConfigFile::parse(const std::string& filename, bool isDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070071{
72 std::ifstream inputFile;
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060073 inputFile.open(filename.c_str());
74 if (!inputFile.good() || !inputFile.is_open())
Steve DiBenedettobb75b552014-02-08 12:12:11 -070075 {
76 std::string msg = "Failed to read configuration file: ";
77 msg += filename;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070078 throw Error(msg);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070079 }
80 parse(inputFile, isDryRun, filename);
81 inputFile.close();
82}
83
84void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060085ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070086{
87 std::istringstream inputStream(input);
88 parse(inputStream, isDryRun, filename);
89}
90
91
92void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060093ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070094{
95 try
96 {
97 boost::property_tree::read_info(input, m_global);
98 }
99 catch (const boost::property_tree::info_parser_error& error)
100 {
101 std::stringstream msg;
102 msg << "Failed to parse configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600103 msg << " " << filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700104 msg << " " << error.message() << " line " << error.line();
105 throw Error(msg.str());
106 }
107
108 process(isDryRun, filename);
109}
110
111void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600112ConfigFile::process(bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700113{
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600114 BOOST_ASSERT(!filename.empty());
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700115 // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
116
117 if (m_global.begin() == m_global.end())
118 {
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600119 std::string msg = "Error processing configuration file: ";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600120 msg += filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700121 msg += " no data";
122 throw Error(msg);
123 }
124
125 for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
126 {
127 const std::string& sectionName = i->first;
128 const ConfigSection& section = i->second;
129
130 SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
131 if (subscriberIt != m_subscriptions.end())
132 {
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600133 ConfigSectionHandler subscriber = subscriberIt->second;
134 subscriber(section, isDryRun, filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700135 }
136 else
137 {
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600138 m_unknownSectionCallback(filename, sectionName, section, isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700139 }
140 }
141}
142
143}