Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * 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 DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 24 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 25 | #include "config-file.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #include "logger.hpp" |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 27 | |
| 28 | #include <boost/property_tree/info_parser.hpp> |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 29 | #include <fstream> |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 33 | void |
| 34 | ConfigFile::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 | |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 43 | BOOST_THROW_EXCEPTION(ConfigFile::Error(msg)); |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void |
| 47 | ConfigFile::ignoreUnknownSection(const std::string& filename, |
| 48 | const std::string& sectionName, |
| 49 | const ConfigSection& section, |
| 50 | bool isDryRun) |
| 51 | { |
| 52 | // do nothing |
| 53 | } |
| 54 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 55 | bool |
| 56 | ConfigFile::parseYesNo(const ConfigSection::const_iterator& i, |
| 57 | const std::string& optionName, |
| 58 | const std::string& sectionName) |
| 59 | { |
| 60 | const std::string value = i->second.get_value<std::string>(); |
| 61 | if (value == "yes") { |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | if (value == "no") { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" + |
| 70 | optionName + "\" in \"" + |
| 71 | sectionName + "\" section")); |
| 72 | } |
| 73 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 74 | ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback) |
| 75 | : m_unknownSectionCallback(unknownSectionCallback) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 76 | { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
| 80 | ConfigFile::addSectionHandler(const std::string& sectionName, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 81 | ConfigSectionHandler subscriber) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 82 | { |
| 83 | m_subscriptions[sectionName] = subscriber; |
| 84 | } |
| 85 | |
| 86 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 87 | ConfigFile::parse(const std::string& filename, bool isDryRun) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 88 | { |
| 89 | std::ifstream inputFile; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 90 | inputFile.open(filename.c_str()); |
| 91 | if (!inputFile.good() || !inputFile.is_open()) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 92 | { |
| 93 | std::string msg = "Failed to read configuration file: "; |
| 94 | msg += filename; |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 95 | BOOST_THROW_EXCEPTION(Error(msg)); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 96 | } |
| 97 | parse(inputFile, isDryRun, filename); |
| 98 | inputFile.close(); |
| 99 | } |
| 100 | |
| 101 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 102 | ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 103 | { |
| 104 | std::istringstream inputStream(input); |
| 105 | parse(inputStream, isDryRun, filename); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 110 | ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 111 | { |
| 112 | try |
| 113 | { |
| 114 | boost::property_tree::read_info(input, m_global); |
| 115 | } |
| 116 | catch (const boost::property_tree::info_parser_error& error) |
| 117 | { |
| 118 | std::stringstream msg; |
| 119 | msg << "Failed to parse configuration file"; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 120 | msg << " " << filename; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 121 | msg << " " << error.message() << " line " << error.line(); |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 122 | BOOST_THROW_EXCEPTION(Error(msg.str())); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | process(isDryRun, filename); |
| 126 | } |
| 127 | |
| 128 | void |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 129 | ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename) |
| 130 | { |
| 131 | m_global = config; |
| 132 | process(isDryRun, filename); |
| 133 | } |
| 134 | |
| 135 | void |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 136 | ConfigFile::process(bool isDryRun, const std::string& filename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 137 | { |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 138 | BOOST_ASSERT(!filename.empty()); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 139 | // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):(""))); |
| 140 | |
| 141 | if (m_global.begin() == m_global.end()) |
| 142 | { |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 143 | std::string msg = "Error processing configuration file: "; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 144 | msg += filename; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 145 | msg += " no data"; |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 146 | BOOST_THROW_EXCEPTION(Error(msg)); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i) |
| 150 | { |
| 151 | const std::string& sectionName = i->first; |
| 152 | const ConfigSection& section = i->second; |
| 153 | |
| 154 | SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName); |
| 155 | if (subscriberIt != m_subscriptions.end()) |
| 156 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 157 | ConfigSectionHandler subscriber = subscriberIt->second; |
| 158 | subscriber(section, isDryRun, filename); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 159 | } |
| 160 | else |
| 161 | { |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 162 | m_unknownSectionCallback(filename, sectionName, section, isDryRun); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | } |