28 #include <boost/cstdint.hpp> 30 #include <ndn-cxx/name.hpp> 31 #include <ndn-cxx/net/face-uri.hpp> 47 , m_setterCallback(setter)
50 , m_shouldCheckRange(false)
59 T value = section.get<T>(m_key);
61 if (!isValidValue(value)) {
65 m_setterCallback(value);
68 catch (
const std::exception& ex) {
71 std::cerr << ex.what() << std::endl;
72 std::cerr <<
"Missing required configuration variable" << std::endl;
76 m_setterCallback(m_defaultValue);
89 m_shouldCheckRange =
true;
96 m_defaultValue = defaultValue;
101 printOutOfRangeError(T value)
103 std::cerr <<
"Invalid value for " << m_key <<
": " 106 << m_minValue <<
" - " 107 << m_maxValue << std::endl;
111 isValidValue(T value)
113 if (!m_shouldCheckRange) {
116 else if (value < m_minValue || value > m_maxValue)
118 printOutOfRangeError(value);
126 const std::string m_key;
127 const ConfParameterCallback m_setterCallback;
133 bool m_shouldCheckRange;
141 std::ifstream inputFile;
142 inputFile.open(m_confFileName.c_str());
143 if (!inputFile.is_open()) {
144 std::string msg =
"Failed to read configuration file: ";
145 msg += m_confFileName;
146 std::cerr << msg << std::endl;
149 ret = load(inputFile);
155 ConfFileProcessor::load(std::istream& input)
160 boost::property_tree::read_info(input, pt);
162 catch (
const boost::property_tree::info_parser_error& error) {
163 std::stringstream msg;
164 std::cerr <<
"Failed to parse configuration file " << std::endl;
165 std::cerr << m_confFileName << std::endl;
169 for (ConfigSection::const_iterator tn = pt.begin();
170 tn != pt.end(); ++tn) {
171 ret = processSection(tn->first, tn->second);
180 ConfFileProcessor::processSection(
const std::string& sectionName,
const ConfigSection& section)
183 if (sectionName ==
"general")
185 ret = processConfSectionGeneral(section);
187 else if (sectionName ==
"neighbors")
189 ret = processConfSectionNeighbors(section);
191 else if (sectionName ==
"hyperbolic")
193 ret = processConfSectionHyperbolic(section);
195 else if (sectionName ==
"fib")
197 ret = processConfSectionFib(section);
199 else if (sectionName ==
"advertising")
201 ret = processConfSectionAdvertising(section);
203 else if (sectionName ==
"security")
205 ret = processConfSectionSecurity(section);
209 std::cerr <<
"Wrong configuration section: " << sectionName << std::endl;
215 ConfFileProcessor::processConfSectionGeneral(
const ConfigSection& section)
218 std::string network = section.get<std::string>(
"network");
219 std::string site = section.get<std::string>(
"site");
220 std::string router = section.get<std::string>(
"router");
221 ndn::Name networkName(network);
222 if (!networkName.empty()) {
223 m_nlsr.getConfParameter().setNetwork(networkName);
226 std::cerr <<
" Network can not be null or empty or in bad URI format :(!" << std::endl;
229 ndn::Name siteName(site);
230 if (!siteName.empty()) {
231 m_nlsr.getConfParameter().setSiteName(siteName);
234 std::cerr <<
"Site can not be null or empty or in bad URI format:( !" << std::endl;
237 ndn::Name routerName(router);
238 if (!routerName.empty()) {
239 m_nlsr.getConfParameter().setRouterName(routerName);
242 std::cerr <<
" Router name can not be null or empty or in bad URI format:( !" << std::endl;
246 catch (
const std::exception& ex) {
247 std::cerr << ex.what() << std::endl;
255 m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime);
258 std::cerr <<
"Wrong value for lsa-refresh-time ";
266 uint32_t routerDeadInterval = section.get<uint32_t>(
"router-dead-interval", (2*lsaRefreshTime));
268 if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) {
269 m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval);
272 std::cerr <<
"Value of router-dead-interval must be larger than lsa-refresh-time" << std::endl;
280 m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime));
283 std::cerr <<
"Wrong value for lsa-interest-timeout. " 291 std::string logLevel = section.get<std::string>(
"log-level",
"INFO");
294 m_nlsr.getConfParameter().setLogLevel(logLevel);
297 std::cerr <<
"Invalid value for log-level ";
298 std::cerr <<
"Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl;
303 std::string logDir = section.get<std::string>(
"log-dir");
304 if (boost::filesystem::exists(logDir)) {
305 if (boost::filesystem::is_directory(logDir)) {
306 std::string testFileName=logDir+
"/test.log";
307 std::ofstream testOutFile;
308 testOutFile.open(testFileName.c_str());
309 if (testOutFile.is_open() && testOutFile.good()) {
310 m_nlsr.getConfParameter().setLogDir(logDir);
313 std::cerr <<
"User does not have read and write permission on the directory";
314 std::cerr << std::endl;
318 remove(testFileName.c_str());
321 std::cerr <<
"Provided path is not a directory" << std::endl;
326 std::cerr <<
"Provided log directory <" << logDir <<
"> does not exist" << std::endl;
330 catch (
const std::exception& ex) {
331 std::cerr <<
"You must configure log directory" << std::endl;
332 std::cerr << ex.what() << std::endl;
337 std::string seqDir = section.get<std::string>(
"seq-dir");
338 if (boost::filesystem::exists(seqDir)) {
339 if (boost::filesystem::is_directory(seqDir)) {
340 std::string testFileName=seqDir+
"/test.seq";
341 std::ofstream testOutFile;
342 testOutFile.open(testFileName.c_str());
343 if (testOutFile.is_open() && testOutFile.good()) {
344 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
347 std::cerr <<
"User does not have read and write permission on the directory";
348 std::cerr << std::endl;
352 remove(testFileName.c_str());
355 std::cerr <<
"Provided path is not a directory" << std::endl;
360 std::cerr <<
"Provided sequence directory <" << seqDir <<
"> does not exist" << std::endl;
364 catch (
const std::exception& ex) {
365 std::cerr <<
"You must configure sequence directory" << std::endl;
366 std::cerr << ex.what() << std::endl;
371 std::string log4cxxPath = section.get<std::string>(
"log4cxx-conf");
373 if (log4cxxPath ==
"") {
374 std::cerr <<
"No value provided for log4cxx-conf" << std::endl;
378 if (boost::filesystem::exists(log4cxxPath)) {
379 m_nlsr.getConfParameter().setLog4CxxConfPath(log4cxxPath);
382 std::cerr <<
"Provided path for log4cxx-conf <" << log4cxxPath
383 <<
"> does not exist" << std::endl;
388 catch (
const std::exception& ex) {
396 ConfFileProcessor::processConfSectionNeighbors(
const ConfigSection& section)
402 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
405 std::cerr <<
"Wrong value for hello-retries." << std::endl;
416 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
419 std::cerr <<
"Wrong value for hello-timeout. ";
430 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
433 std::cerr <<
"Wrong value for hello-interval. ";
444 &m_nlsr.getConfParameter(), _1));
454 &m_nlsr.getConfParameter(),
468 &m_nlsr.getConfParameter(),
482 &m_nlsr.getConfParameter(), _1));
490 for (ConfigSection::const_iterator tn =
491 section.begin(); tn != section.end(); ++tn) {
493 if (tn->first ==
"neighbor") {
495 ConfigSection CommandAttriTree = tn->second;
496 std::string name = CommandAttriTree.get<std::string>(
"name");
497 std::string uriString = CommandAttriTree.get<std::string>(
"face-uri");
499 ndn::FaceUri faceUri;
500 if (! faceUri.parse(uriString)) {
501 std::cerr <<
"parsing failed!" << std::endl;
505 double linkCost = CommandAttriTree.get<
double>(
"link-cost",
507 ndn::Name neighborName(name);
508 if (!neighborName.empty()) {
510 m_nlsr.getAdjacencyList().insert(adj);
513 std::cerr <<
" Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
514 std::cerr <<
" or bad URI format" << std::endl;
517 catch (
const std::exception& ex) {
518 std::cerr << ex.what() << std::endl;
527 ConfFileProcessor::processConfSectionHyperbolic(
const ConfigSection& section)
530 std::string state = section.get<std::string>(
"state",
"off");
532 if (boost::iequals(state,
"off")) {
535 else if (boost::iequals(state,
"on")) {
538 else if (state ==
"dry-run") {
542 std::cerr <<
"Wrong format for hyperbolic state." << std::endl;
543 std::cerr <<
"Allowed value: off, on, dry-run" << std::endl;
553 double radius = section.get<
double>(
"radius");
554 std::string angleString = section.get<std::string>(
"angle");
556 std::stringstream ss(angleString);
557 std::vector<double> angles;
561 while (ss >> angle) {
562 angles.push_back(angle);
563 if (ss.peek() ==
',' || ss.peek() ==
' ') {
568 if (!m_nlsr.getConfParameter().setCorR(radius)) {
571 m_nlsr.getConfParameter().setCorTheta(angles);
573 catch (
const std::exception& ex) {
574 std::cerr << ex.what() << std::endl;
575 if (state ==
"on" || state ==
"dry-run") {
584 ConfFileProcessor::processConfSectionFib(
const ConfigSection& section)
592 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefix);
595 std::cerr <<
"Wrong value for max-faces-per-prefix. ";
604 &m_nlsr.getConfParameter(), _1));
616 ConfFileProcessor::processConfSectionAdvertising(
const ConfigSection& section)
618 for (ConfigSection::const_iterator tn =
619 section.begin(); tn != section.end(); ++tn) {
620 if (tn->first ==
"prefix") {
622 std::string prefix = tn->second.data();
623 ndn::Name namePrefix(prefix);
624 if (!namePrefix.empty()) {
625 m_nlsr.getNamePrefixList().insert(namePrefix);
628 std::cerr <<
" Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
632 catch (
const std::exception& ex) {
633 std::cerr << ex.what() << std::endl;
642 ConfFileProcessor::processConfSectionSecurity(
const ConfigSection& section)
644 ConfigSection::const_iterator it = section.begin();
646 if (it == section.end() || it->first !=
"validator") {
647 std::cerr <<
"Error: Expect validator section!" << std::endl;
651 m_nlsr.loadValidator(it->second, m_confFileName);
654 if (it != section.end() && it->first ==
"prefix-update-validator") {
655 m_nlsr.getPrefixUpdateProcessor().loadValidator(it->second, m_confFileName);
658 for (; it != section.end(); it++) {
659 using namespace boost::filesystem;
661 if (it->first !=
"cert-to-publish") {
662 std::cerr <<
"Error: Expect cert-to-publish!" << std::endl;
666 std::string file = it->second.data();
667 path certfilePath = absolute(file, path(m_confFileName).parent_path());
668 std::shared_ptr<ndn::security::v2::Certificate> idCert =
669 ndn::io::load<ndn::security::v2::Certificate>(certfilePath.string());
671 if (idCert ==
nullptr) {
672 std::cerr <<
"Error: Cannot load cert-to-publish: " << file <<
"!" << std::endl;
676 m_nlsr.loadCertToPublish(*idCert);
bool isValidLogLevel(const std::string &logLevel)
static const float DEFAULT_LINK_COST
void setRoutingCalcInterval(uint32_t interval)
void setAdjLsaBuildInterval(uint32_t interval)
ConfigurationVariable(const std::string &key, const ConfParameterCallback &setter)
boost::property_tree::ptree ConfigSection
void setFaceDatasetFetchInterval(uint32_t interval)
void setMinAndMaxValue(T min, T max)
A neighbor reachable over a Face.
void setOptional(T defaultValue)
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void setFirstHelloInterval(uint32_t interval)
void setFaceDatasetFetchTries(uint32_t count)
std::function< void(T)> ConfParameterCallback
bool parseFromConfigSection(const ConfigSection §ion)
bool processConfFile()
Load and parse the configuration file, then populate NLSR.