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. " 293 m_nlsr.getConfParameter().setSyncInterestLifetime(syncInterestLifetime);
296 std::cerr <<
"Wrong value for sync-interest-lifetime. " 304 std::string seqDir = section.get<std::string>(
"seq-dir");
305 if (boost::filesystem::exists(seqDir)) {
306 if (boost::filesystem::is_directory(seqDir)) {
307 std::string testFileName=seqDir+
"/test.seq";
308 std::ofstream testOutFile;
309 testOutFile.open(testFileName.c_str());
310 if (testOutFile.is_open() && testOutFile.good()) {
311 m_nlsr.getConfParameter().setSeqFileDir(seqDir);
314 std::cerr <<
"User does not have read and write permission on the directory";
315 std::cerr << std::endl;
319 remove(testFileName.c_str());
322 std::cerr <<
"Provided path is not a directory" << std::endl;
327 std::cerr <<
"Provided sequence directory <" << seqDir <<
"> does not exist" << std::endl;
331 catch (
const std::exception& ex) {
332 std::cerr <<
"You must configure sequence directory" << std::endl;
333 std::cerr << ex.what() << std::endl;
341 ConfFileProcessor::processConfSectionNeighbors(
const ConfigSection& section)
347 m_nlsr.getConfParameter().setInterestRetryNumber(retrials);
350 std::cerr <<
"Wrong value for hello-retries." << std::endl;
361 m_nlsr.getConfParameter().setInterestResendTime(timeOut);
364 std::cerr <<
"Wrong value for hello-timeout. ";
375 m_nlsr.getConfParameter().setInfoInterestInterval(interval);
378 std::cerr <<
"Wrong value for hello-interval. ";
389 &m_nlsr.getConfParameter(), _1));
399 &m_nlsr.getConfParameter(),
413 &m_nlsr.getConfParameter(),
427 &m_nlsr.getConfParameter(), _1));
435 for (ConfigSection::const_iterator tn =
436 section.begin(); tn != section.end(); ++tn) {
438 if (tn->first ==
"neighbor") {
440 ConfigSection CommandAttriTree = tn->second;
441 std::string name = CommandAttriTree.get<std::string>(
"name");
442 std::string uriString = CommandAttriTree.get<std::string>(
"face-uri");
444 ndn::FaceUri faceUri;
445 if (! faceUri.parse(uriString)) {
446 std::cerr <<
"parsing failed!" << std::endl;
450 double linkCost = CommandAttriTree.get<
double>(
"link-cost",
452 ndn::Name neighborName(name);
453 if (!neighborName.empty()) {
455 m_nlsr.getAdjacencyList().insert(adj);
458 std::cerr <<
" Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]";
459 std::cerr <<
" or bad URI format" << std::endl;
462 catch (
const std::exception& ex) {
463 std::cerr << ex.what() << std::endl;
472 ConfFileProcessor::processConfSectionHyperbolic(
const ConfigSection& section)
475 std::string state = section.get<std::string>(
"state",
"off");
477 if (boost::iequals(state,
"off")) {
480 else if (boost::iequals(state,
"on")) {
483 else if (state ==
"dry-run") {
487 std::cerr <<
"Wrong format for hyperbolic state." << std::endl;
488 std::cerr <<
"Allowed value: off, on, dry-run" << std::endl;
498 double radius = section.get<
double>(
"radius");
499 std::string angleString = section.get<std::string>(
"angle");
501 std::stringstream ss(angleString);
502 std::vector<double> angles;
506 while (ss >> angle) {
507 angles.push_back(angle);
508 if (ss.peek() ==
',' || ss.peek() ==
' ') {
513 if (!m_nlsr.getConfParameter().setCorR(radius)) {
516 m_nlsr.getConfParameter().setCorTheta(angles);
518 catch (
const std::exception& ex) {
519 std::cerr << ex.what() << std::endl;
520 if (state ==
"on" || state ==
"dry-run") {
529 ConfFileProcessor::processConfSectionFib(
const ConfigSection& section)
537 m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefix);
540 std::cerr <<
"Wrong value for max-faces-per-prefix. ";
549 &m_nlsr.getConfParameter(), _1));
561 ConfFileProcessor::processConfSectionAdvertising(
const ConfigSection& section)
563 for (ConfigSection::const_iterator tn =
564 section.begin(); tn != section.end(); ++tn) {
565 if (tn->first ==
"prefix") {
567 std::string prefix = tn->second.data();
568 ndn::Name namePrefix(prefix);
569 if (!namePrefix.empty()) {
570 m_nlsr.getNamePrefixList().insert(namePrefix);
573 std::cerr <<
" Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl;
577 catch (
const std::exception& ex) {
578 std::cerr << ex.what() << std::endl;
587 ConfFileProcessor::processConfSectionSecurity(
const ConfigSection& section)
589 ConfigSection::const_iterator it = section.begin();
591 if (it == section.end() || it->first !=
"validator") {
592 std::cerr <<
"Error: Expect validator section!" << std::endl;
596 m_nlsr.loadValidator(it->second, m_confFileName);
599 if (it != section.end() && it->first ==
"prefix-update-validator") {
600 m_nlsr.getPrefixUpdateProcessor().loadValidator(it->second, m_confFileName);
603 for (; it != section.end(); it++) {
604 using namespace boost::filesystem;
606 if (it->first !=
"cert-to-publish") {
607 std::cerr <<
"Error: Expect cert-to-publish!" << std::endl;
611 std::string file = it->second.data();
612 path certfilePath = absolute(file, path(m_confFileName).parent_path());
613 std::shared_ptr<ndn::security::v2::Certificate> idCert =
614 ndn::io::load<ndn::security::v2::Certificate>(certfilePath.string());
616 if (idCert ==
nullptr) {
617 std::cerr <<
"Error: Cannot load cert-to-publish: " << file <<
"!" << std::endl;
621 m_nlsr.loadCertToPublish(*idCert);
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-2018, 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.