blob: 4af6f9109df5f57db0c61dbc806b31a802e5e4dc [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"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060026#include "core/logger.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070027
28#include <boost/property_tree/info_parser.hpp>
29
30namespace nfd {
31
32NFD_LOG_INIT("ConfigFile");
33
34ConfigFile::ConfigFile()
35{
36
37}
38
39void
40ConfigFile::addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060041 ConfigSectionHandler subscriber)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070042{
43 m_subscriptions[sectionName] = subscriber;
44}
45
46void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060047ConfigFile::parse(const std::string& filename, bool isDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070048{
49 std::ifstream inputFile;
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060050 inputFile.open(filename.c_str());
51 if (!inputFile.good() || !inputFile.is_open())
Steve DiBenedettobb75b552014-02-08 12:12:11 -070052 {
53 std::string msg = "Failed to read configuration file: ";
54 msg += filename;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070055 throw Error(msg);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070056 }
57 parse(inputFile, isDryRun, filename);
58 inputFile.close();
59}
60
61void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060062ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070063{
64 std::istringstream inputStream(input);
65 parse(inputStream, isDryRun, filename);
66}
67
68
69void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060070ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070071{
72 try
73 {
74 boost::property_tree::read_info(input, m_global);
75 }
76 catch (const boost::property_tree::info_parser_error& error)
77 {
78 std::stringstream msg;
79 msg << "Failed to parse configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060080 msg << " " << filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070081 msg << " " << error.message() << " line " << error.line();
82 throw Error(msg.str());
83 }
84
85 process(isDryRun, filename);
86}
87
88void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060089ConfigFile::process(bool isDryRun, const std::string& filename)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070090{
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060091 BOOST_ASSERT(!filename.empty());
Steve DiBenedettobb75b552014-02-08 12:12:11 -070092 // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
93
94 if (m_global.begin() == m_global.end())
95 {
96 std::string msg = "Error processing configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060097 msg += ": ";
98 msg += filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070099 msg += " no data";
100 throw Error(msg);
101 }
102
103 for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
104 {
105 const std::string& sectionName = i->first;
106 const ConfigSection& section = i->second;
107
108 SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
109 if (subscriberIt != m_subscriptions.end())
110 {
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600111 ConfigSectionHandler subscriber = subscriberIt->second;
112 subscriber(section, isDryRun, filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700113 }
114 else
115 {
116 std::string msg = "Error processing configuration file";
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600117 msg += " ";
118 msg += filename;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700119 msg += " no module subscribed for section: " + sectionName;
120 throw Error(msg);
121 }
122 }
123}
124
125}