Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_CONFIG_FILE_HPP |
| 23 | #define NDN_MANAGEMENT_CONFIG_FILE_HPP |
| 24 | |
| 25 | #include "../common.hpp" |
| 26 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 27 | #include <fstream> |
| 28 | |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 29 | #include <boost/property_tree/ptree.hpp> |
| 30 | #include <boost/filesystem.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 34 | /** |
| 35 | * @brief System configuration file for NDN platform |
| 36 | * |
| 37 | * The config file controls the default transport to connect to NDN forwarder, type and |
| 38 | * location of PIB and TPM. |
| 39 | * |
| 40 | * Looking for the configuration file in these well-known locations (in order): |
| 41 | * |
| 42 | * - `$HOME/.ndn/client.conf` |
| 43 | * - `@SYSCONFDIR@/ndn/client.conf` |
| 44 | * - `/etc/ndn/client.conf` |
| 45 | * |
| 46 | * @sa Manpage of ndn-client.conf |
| 47 | */ |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 48 | class ConfigFile : noncopyable |
| 49 | { |
| 50 | public: |
| 51 | |
| 52 | class Error : public std::runtime_error |
| 53 | { |
| 54 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 55 | using std::runtime_error::runtime_error; |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | typedef boost::property_tree::ptree Parsed; |
| 59 | |
| 60 | /** |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 61 | * @brief Locate, open, and parse a library configuration file. |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 62 | * |
| 63 | * @throws ConfigFile::Error on parse error |
| 64 | */ |
| 65 | ConfigFile(); |
| 66 | |
| 67 | ~ConfigFile(); |
| 68 | |
| 69 | const boost::filesystem::path& |
| 70 | getPath() const; |
| 71 | |
| 72 | const Parsed& |
| 73 | getParsedConfiguration() const; |
| 74 | |
| 75 | private: |
| 76 | |
| 77 | bool |
| 78 | open(); |
| 79 | |
| 80 | void |
| 81 | close(); |
| 82 | |
| 83 | /** |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 84 | * @brief Parse a previously discovered and opened configuration file. |
| 85 | * |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 86 | * For convenience this method will attempt to open the file |
| 87 | * if it has previously been located, but open() has not been called. |
| 88 | * |
| 89 | * @throws ConfigFile::Error on parse error |
| 90 | * @throws ConfigFile::Error on failure to open previously un-open configuration file |
| 91 | * @throws ConfigFile::Error if no configuration file was previously located |
| 92 | */ |
| 93 | const Parsed& |
| 94 | parse(); |
| 95 | |
| 96 | /** |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 97 | * @brief Find the configuration file in well-known locations |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 98 | * |
Alexander Afanasyev | 57e0036 | 2016-06-23 13:22:54 -0700 | [diff] [blame] | 99 | * The well-known locations include (in order): |
| 100 | * |
| 101 | * - `$HOME/.ndn/client.conf` |
| 102 | * - `@SYSCONFDIR@/ndn/client.conf` |
| 103 | * - `/etc/ndn/client.conf` |
Steve DiBenedetto | c07b3a2 | 2014-03-19 12:32:52 -0600 | [diff] [blame] | 104 | * |
| 105 | * @return path to preferred configuration (according to above order) or empty path on failure |
| 106 | */ |
| 107 | |
| 108 | boost::filesystem::path |
| 109 | findConfigFile(); |
| 110 | |
| 111 | private: |
| 112 | boost::filesystem::path m_path; // absolute path to active configuration file (if any) |
| 113 | std::ifstream m_input; |
| 114 | Parsed m_config; |
| 115 | }; |
| 116 | |
| 117 | inline const boost::filesystem::path& |
| 118 | ConfigFile::getPath() const |
| 119 | { |
| 120 | return m_path; |
| 121 | } |
| 122 | |
| 123 | inline const ConfigFile::Parsed& |
| 124 | ConfigFile::getParsedConfiguration() const |
| 125 | { |
| 126 | return m_config; |
| 127 | } |
| 128 | |
| 129 | } // namespace ndn |
| 130 | |
| 131 | |
| 132 | #endif // NDN_MANAGEMENT_CONFIG_FILE_HPP |