blob: a6861e3e5f9640cf7deb01bb9ab4397776a0cab2 [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
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060033void
34ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
35 const std::string& sectionName,
36 const ConfigSection& section,
37 bool isDryRun)
38{
39 std::string msg = "Error processing configuration file ";
40 msg += filename;
41 msg += ": no module subscribed for section \"" + sectionName + "\"";
42
43 throw ConfigFile::Error(msg);
44}
45
46void
47ConfigFile::ignoreUnknownSection(const std::string& filename,
48 const std::string& sectionName,
49 const ConfigSection& section,
50 bool isDryRun)
51{
52 // do nothing
53}
54
55ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
56 : m_unknownSectionCallback(unknownSectionCallback)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070057{
Steve DiBenedettobb75b552014-02-08 12:12:11 -070058}
59
60void
61ConfigFile::addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060062 ConfigSectionHandler subscriber)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070063{
64 m_subscriptions[sectionName] = subscriber;
65}
66
67void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060068ConfigFile::parse(const std::string& filename, bool isDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070069{
70 std::ifstream inputFile;
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060071 inputFile.open(filename.c_str());
72 if (!inputFile.good() || !inputFile.is_open())
Steve DiBenedettobb75b552014-02-08 12:12:11 -070073 {
74 std::string msg = "Failed to read configuration file: ";
75 msg += filename;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070076 throw Error(msg);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070077 }
78 parse(inputFile, isDryRun, filename);
79 inputFile.close();
80}
81
82void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060083ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070084{
85 std::istringstream inputStream(input);
86 parse(inputStream, isDryRun, filename);
87}
88
89
90void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060091ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070092{
93 try
94 {
95 boost::property_tree::read_info(input, m_global);
96 }
97 catch (const boost::property_tree::info_parser_error& error)
98 {
99 std::stringstream msg;
100 msg << "Failed to parse configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600101 msg << " " << filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700102 msg << " " << error.message() << " line " << error.line();
103 throw Error(msg.str());
104 }
105
106 process(isDryRun, filename);
107}
108
109void
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800110ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
111{
112 m_global = config;
113 process(isDryRun, filename);
114}
115
116void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600117ConfigFile::process(bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700118{
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600119 BOOST_ASSERT(!filename.empty());
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700120 // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
121
122 if (m_global.begin() == m_global.end())
123 {
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600124 std::string msg = "Error processing configuration file: ";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600125 msg += filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700126 msg += " no data";
127 throw Error(msg);
128 }
129
130 for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
131 {
132 const std::string& sectionName = i->first;
133 const ConfigSection& section = i->second;
134
135 SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
136 if (subscriberIt != m_subscriptions.end())
137 {
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600138 ConfigSectionHandler subscriber = subscriberIt->second;
139 subscriber(section, isDryRun, filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700140 }
141 else
142 {
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600143 m_unknownSectionCallback(filename, sectionName, section, isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700144 }
145 }
146}
147
148}