blob: c6181f9cb4a71b113e323b5b4683621643a0e8de [file] [log] [blame]
Obaid5748c0c2014-04-09 18:00:42 -05001#include "nrd-config.hpp"
2
3namespace ndn {
4namespace nrd {
5
6NrdConfig::NrdConfig()
7 : m_isSecuritySectionDefined(false)
8{
9}
10
11void
12NrdConfig::load(const std::string& filename)
13{
14 std::ifstream inputFile;
15 inputFile.open(filename.c_str());
16 if (!inputFile.good() || !inputFile.is_open())
17 {
18 std::string msg = "Failed to read configuration file: ";
19 msg += filename;
20 std::cerr << filename << std::endl;
21 throw Error(msg);
22 }
23 load(inputFile, filename);
24 inputFile.close();
25}
26
27void
28NrdConfig::load(const std::string& input, const std::string& filename)
29{
30 std::istringstream inputStream(input);
31 load(inputStream, filename);
32}
33
34void
35NrdConfig::load(std::istream& input, const std::string& filename)
36{
37 BOOST_ASSERT(!filename.empty());
38
39 ConfigSection ptree;
40 try
41 {
42 boost::property_tree::read_info(input, ptree);
43 }
44 catch (const boost::property_tree::info_parser_error& error)
45 {
46 std::stringstream msg;
47 msg << "Failed to parse configuration file";
48 msg << " " << filename;
49 msg << " " << error.message() << " line " << error.line();
50 throw Error(msg.str());
51 }
52 process(ptree, filename);
53}
54
55void
56NrdConfig::process(const ConfigSection& configSection,
57 const std::string& filename)
58{
59 BOOST_ASSERT(!filename.empty());
60
61 if (configSection.begin() == configSection.end())
62 {
63 std::string msg = "Error processing configuration file";
64 msg += ": ";
65 msg += filename;
66 msg += " no data";
67 throw Error(msg);
68 }
69
70 for (ConfigSection::const_iterator i = configSection.begin();
71 i != configSection.end(); ++i)
72 {
73 const std::string& sectionName = i->first;
74 const ConfigSection& section = i->second;
75
76 if (boost::iequals(sectionName, "security"))
77 {
78 onSectionSecurity(section, filename);
79 }
80 //Add more sections here as needed
81 //else if (boost::iequals(sectionName, "another-section"))
82 //{
83 //onSectionAnotherSection(section, filename);
84 //}
85 else
86 {
87 std::string msg = "Error processing configuration file";
88 msg += " ";
89 msg += filename;
90 msg += " unrecognized section: " + sectionName;
91 throw Error(msg);
92 }
93 }
94}
95
96void
97NrdConfig::onSectionSecurity(const ConfigSection& section,
98 const std::string& filename)
99{
100 if (!m_isSecuritySectionDefined) {
101 //setSecturitySection(section);
102 m_securitySection = section;
103 m_filename = filename;
104 m_isSecuritySectionDefined = true;
105 }
106 else {
107 std::string msg = "Error processing configuration file";
108 msg += " ";
109 msg += filename;
110 msg += " security section can appear only once";
111 throw Error(msg);
112 }
113}
114
115} //namespace nrd
116} //namespace ndn