NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
config-file.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25 #include "config-file.hpp"
26 #include "logger.hpp"
27 
28 #include <boost/property_tree/info_parser.hpp>
29 #include <fstream>
30 
31 namespace nfd {
32 
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 
43  BOOST_THROW_EXCEPTION(ConfigFile::Error(msg));
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 
56  : m_unknownSectionCallback(unknownSectionCallback)
57 {
58 }
59 
60 void
61 ConfigFile::addSectionHandler(const std::string& sectionName,
62  ConfigSectionHandler subscriber)
63 {
64  m_subscriptions[sectionName] = subscriber;
65 }
66 
67 void
68 ConfigFile::parse(const std::string& filename, bool isDryRun)
69 {
70  std::ifstream inputFile;
71  inputFile.open(filename.c_str());
72  if (!inputFile.good() || !inputFile.is_open())
73  {
74  std::string msg = "Failed to read configuration file: ";
75  msg += filename;
76  BOOST_THROW_EXCEPTION(Error(msg));
77  }
78  parse(inputFile, isDryRun, filename);
79  inputFile.close();
80 }
81 
82 void
83 ConfigFile::parse(const std::string& input, bool isDryRun, const std::string& filename)
84 {
85  std::istringstream inputStream(input);
86  parse(inputStream, isDryRun, filename);
87 }
88 
89 
90 void
91 ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
92 {
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";
101  msg << " " << filename;
102  msg << " " << error.message() << " line " << error.line();
103  BOOST_THROW_EXCEPTION(Error(msg.str()));
104  }
105 
106  process(isDryRun, filename);
107 }
108 
109 void
110 ConfigFile::parse(const ConfigSection& config, bool isDryRun, const std::string& filename)
111 {
112  m_global = config;
113  process(isDryRun, filename);
114 }
115 
116 void
117 ConfigFile::process(bool isDryRun, const std::string& filename)
118 {
119  BOOST_ASSERT(!filename.empty());
120  // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
121 
122  if (m_global.begin() == m_global.end())
123  {
124  std::string msg = "Error processing configuration file: ";
125  msg += filename;
126  msg += " no data";
127  BOOST_THROW_EXCEPTION(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  {
138  ConfigSectionHandler subscriber = subscriberIt->second;
139  subscriber(section, isDryRun, filename);
140  }
141  else
142  {
143  m_unknownSectionCallback(filename, sectionName, section, isDryRun);
144  }
145  }
146 }
147 
148 }
function< void(const ConfigSection &, bool, const std::string &)> ConfigSectionHandler
callback for config file sections
Definition: config-file.hpp:39
Table::const_iterator iterator
Definition: cs-internal.hpp:41
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:34
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:61
function< void(const std::string &, const std::string &, const ConfigSection &, bool)> UnknownConfigSectionHandler
callback for config file sections without a subscribed handler
Definition: config-file.hpp:45
boost::property_tree::ptree ConfigSection
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:55
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:47
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:68