akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * \author A K M Mahmudul Hoque <ahoque1@memphis.edu> |
| 21 | * \author Minsheng Zhang <mzhang4@memphis.edu> |
| 22 | * |
| 23 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | #include <iostream> |
| 25 | #include <fstream> |
Alexander Afanasyev | b669f9c | 2014-11-14 12:41:54 -0800 | [diff] [blame^] | 26 | |
| 27 | #include <ndn-cxx/name.hpp> |
| 28 | |
| 29 | // boost needs to be included after ndn-cxx, otherwise there will be conflict with _1, _2, ... |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 30 | #include <boost/algorithm/string.hpp> |
| 31 | #include <boost/property_tree/info_parser.hpp> |
| 32 | #include <boost/property_tree/ptree.hpp> |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 33 | #include <boost/filesystem.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 34 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | #include "conf-parameter.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 36 | #include "conf-file-processor.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | #include "adjacent.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 38 | #include "utility/name-helper.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | namespace nlsr { |
| 41 | |
| 42 | using namespace std; |
| 43 | |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 44 | template <class T> |
| 45 | class ConfigurationVariable |
| 46 | { |
| 47 | public: |
| 48 | typedef ndn::function<void(T)> ConfParameterCallback; |
| 49 | typedef boost::property_tree::ptree ConfigSection; |
| 50 | |
| 51 | ConfigurationVariable(const std::string& key, const ConfParameterCallback& setter) |
| 52 | : m_key(key) |
| 53 | , m_setterCallback(setter) |
| 54 | , m_minValue(0) |
| 55 | , m_maxValue(0) |
| 56 | , m_shouldCheckRange(false) |
| 57 | , m_isRequired(true) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | bool |
| 62 | parseFromConfigSection(const ConfigSection& section) |
| 63 | { |
| 64 | try { |
| 65 | T value = section.get<T>(m_key); |
| 66 | |
| 67 | if (!isValidValue(value)) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | m_setterCallback(value); |
| 72 | return true; |
| 73 | } |
| 74 | catch (const std::exception& ex) { |
| 75 | |
| 76 | if (m_isRequired) { |
| 77 | std::cerr << ex.what() << std::endl; |
| 78 | std::cerr << "Missing required configuration variable" << std::endl; |
| 79 | return false; |
| 80 | } |
| 81 | else { |
| 82 | m_setterCallback(m_defaultValue); |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | setMinAndMaxValue(T min, T max) |
| 92 | { |
| 93 | m_minValue = min; |
| 94 | m_maxValue = max; |
| 95 | m_shouldCheckRange = true; |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | setOptional(T defaultValue) |
| 100 | { |
| 101 | m_isRequired = false; |
| 102 | m_defaultValue = defaultValue; |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | void |
| 107 | printOutOfRangeError(T value) |
| 108 | { |
| 109 | std::cerr << "Invalid value for " << m_key << ": " |
| 110 | << value << ". " |
| 111 | << "Valid values: " |
| 112 | << m_minValue << " - " |
| 113 | << m_maxValue << std::endl; |
| 114 | } |
| 115 | |
| 116 | bool |
| 117 | isValidValue(T value) |
| 118 | { |
| 119 | if (!m_shouldCheckRange) { |
| 120 | return true; |
| 121 | } |
| 122 | else if (value < m_minValue || value > m_maxValue) |
| 123 | { |
| 124 | printOutOfRangeError(value); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | const std::string m_key; |
| 133 | const ConfParameterCallback m_setterCallback; |
| 134 | T m_defaultValue; |
| 135 | |
| 136 | T m_minValue; |
| 137 | T m_maxValue; |
| 138 | |
| 139 | bool m_shouldCheckRange; |
| 140 | bool m_isRequired; |
| 141 | }; |
| 142 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 143 | bool |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 144 | ConfFileProcessor::processConfFile() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 145 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 146 | bool ret = true; |
| 147 | ifstream inputFile; |
| 148 | inputFile.open(m_confFileName.c_str()); |
| 149 | if (!inputFile.is_open()) { |
| 150 | string msg = "Failed to read configuration file: "; |
| 151 | msg += m_confFileName; |
| 152 | cerr << msg << endl; |
akmhoque | ad5fe95 | 2014-06-26 13:34:12 -0500 | [diff] [blame] | 153 | return false; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 154 | } |
| 155 | ret = load(inputFile); |
| 156 | inputFile.close(); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | bool |
| 161 | ConfFileProcessor::load(istream& input) |
| 162 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 163 | ConfigSection pt; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 164 | bool ret = true; |
| 165 | try { |
| 166 | boost::property_tree::read_info(input, pt); |
| 167 | } |
| 168 | catch (const boost::property_tree::info_parser_error& error) { |
| 169 | stringstream msg; |
| 170 | std::cerr << "Failed to parse configuration file " << std::endl; |
| 171 | std::cerr << m_confFileName << std::endl; |
| 172 | return false; |
| 173 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 174 | |
| 175 | for (ConfigSection::const_iterator tn = pt.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 176 | tn != pt.end(); ++tn) { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 177 | ret = processSection(tn->first, tn->second); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 178 | if (ret == false) { |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 186 | ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 187 | { |
| 188 | bool ret = true; |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 189 | if (sectionName == "general") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 190 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 191 | ret = processConfSectionGeneral(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 192 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 193 | else if (sectionName == "neighbors") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 194 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 195 | ret = processConfSectionNeighbors(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 196 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 197 | else if (sectionName == "hyperbolic") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 198 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 199 | ret = processConfSectionHyperbolic(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 200 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 201 | else if (sectionName == "fib") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 202 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 203 | ret = processConfSectionFib(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 204 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 205 | else if (sectionName == "advertising") |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 206 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 207 | ret = processConfSectionAdvertising(section); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 208 | } |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 209 | else if (sectionName == "security") |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 210 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 211 | ret = processConfSectionSecurity(section); |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 212 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 213 | else |
| 214 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 215 | std::cerr << "Wrong configuration section: " << sectionName << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 216 | } |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 221 | ConfFileProcessor::processConfSectionGeneral(const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 222 | { |
| 223 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 224 | std::string network = section.get<string>("network"); |
| 225 | std::string site = section.get<string>("site"); |
| 226 | std::string router = section.get<string>("router"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 227 | ndn::Name networkName(network); |
| 228 | if (!networkName.empty()) { |
| 229 | m_nlsr.getConfParameter().setNetwork(networkName); |
| 230 | } |
| 231 | else { |
| 232 | cerr << " Network can not be null or empty or in bad URI format :(!" << endl; |
| 233 | return false; |
| 234 | } |
| 235 | ndn::Name siteName(site); |
| 236 | if (!siteName.empty()) { |
| 237 | m_nlsr.getConfParameter().setSiteName(siteName); |
| 238 | } |
| 239 | else { |
| 240 | cerr << "Site can not be null or empty or in bad URI format:( !" << endl; |
| 241 | return false; |
| 242 | } |
| 243 | ndn::Name routerName(router); |
| 244 | if (!routerName.empty()) { |
| 245 | m_nlsr.getConfParameter().setRouterName(routerName); |
| 246 | } |
| 247 | else { |
| 248 | cerr << " Router name can not be null or empty or in bad URI format:( !" << endl; |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | catch (const std::exception& ex) { |
| 253 | cerr << ex.what() << endl; |
| 254 | return false; |
| 255 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 256 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 257 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 258 | int32_t lsaRefreshTime = section.get<int32_t>("lsa-refresh-time"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 259 | if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && |
| 260 | lsaRefreshTime <= LSA_REFRESH_TIME_MAX) { |
| 261 | m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime); |
| 262 | } |
| 263 | else { |
| 264 | std::cerr << "Wrong value for lsa-refresh-time "; |
| 265 | std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";; |
| 266 | std::cerr << LSA_REFRESH_TIME_MAX << std::endl; |
| 267 | return false; |
| 268 | } |
| 269 | } |
| 270 | catch (const std::exception& ex) { |
| 271 | std::cerr << ex.what() << std::endl; |
| 272 | return false; |
| 273 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 274 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 275 | try { |
Alexander Afanasyev | 1cf1e10 | 2014-08-17 19:47:57 -0700 | [diff] [blame] | 276 | int32_t routerDeadInterval = section.get<int32_t>("router-dead-interval"); |
| 277 | |
| 278 | if (routerDeadInterval > m_nlsr.getConfParameter().getLsaRefreshTime()) { |
| 279 | m_nlsr.getConfParameter().setRouterDeadInterval(routerDeadInterval); |
| 280 | } |
| 281 | else { |
| 282 | std::cerr << "Value of router-dead-interval must be larger than lsa-refresh-time" |
| 283 | << std::endl; |
| 284 | return false; |
| 285 | } |
| 286 | } |
| 287 | catch (const std::exception& ex) { |
| 288 | std::cerr << ex.what() << std::endl; |
| 289 | // non-critical error. default value is 2 * lsa-refresh-time |
| 290 | } |
| 291 | |
| 292 | try { |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 293 | int lifetime = section.get<int>("lsa-interest-lifetime"); |
| 294 | if (lifetime >= LSA_INTEREST_LIFETIME_MIN && lifetime <= LSA_INTEREST_LIFETIME_MAX) { |
| 295 | m_nlsr.getConfParameter().setLsaInterestLifetime(ndn::time::seconds(lifetime)); |
| 296 | } |
| 297 | else { |
| 298 | std::cerr << "Wrong value for lsa-interest-timeout. " |
| 299 | << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-" |
| 300 | << LSA_INTEREST_LIFETIME_MAX << std::endl; |
| 301 | return false; |
| 302 | } |
| 303 | } |
| 304 | catch (const std::exception& ex) { |
| 305 | std::cerr << ex.what() << std::endl; |
Alexander Afanasyev | 1cf1e10 | 2014-08-17 19:47:57 -0700 | [diff] [blame] | 306 | // non-critical error. default value is 4 |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | try { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 310 | |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 311 | std::string logLevel = section.get<string>("log-level"); |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 312 | |
| 313 | if (isValidLogLevel(logLevel)) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 314 | m_nlsr.getConfParameter().setLogLevel(logLevel); |
| 315 | } |
| 316 | else { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 317 | std::cerr << "Invalid value for log-level "; |
| 318 | std::cerr << "Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, NONE" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | catch (const std::exception& ex) { |
| 323 | std::cerr << ex.what() << std::endl; |
| 324 | return false; |
| 325 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 326 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 327 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 328 | std::string logDir = section.get<string>("log-dir"); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 329 | if (boost::filesystem::exists(logDir)) { |
| 330 | if (boost::filesystem::is_directory(logDir)) { |
| 331 | std::string testFileName=logDir+"/test.log"; |
| 332 | ofstream testOutFile; |
| 333 | testOutFile.open(testFileName.c_str()); |
| 334 | if (testOutFile.is_open() && testOutFile.good()) { |
| 335 | m_nlsr.getConfParameter().setLogDir(logDir); |
| 336 | } |
| 337 | else { |
| 338 | std::cerr << "User does not have read and write permission on the directory"; |
| 339 | std::cerr << std::endl; |
| 340 | return false; |
| 341 | } |
| 342 | testOutFile.close(); |
| 343 | remove(testFileName.c_str()); |
| 344 | } |
| 345 | else { |
| 346 | std::cerr << "Provided path is not a directory" << std::endl; |
| 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | else { |
| 351 | std::cerr << "Log directory provided does not exists" << std::endl; |
| 352 | return false; |
| 353 | } |
| 354 | } |
| 355 | catch (const std::exception& ex) { |
| 356 | std::cerr << "You must configure log directory" << std::endl; |
| 357 | std::cerr << ex.what() << std::endl; |
| 358 | return false; |
| 359 | } |
| 360 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 361 | std::string seqDir = section.get<string>("seq-dir"); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 362 | if (boost::filesystem::exists(seqDir)) { |
| 363 | if (boost::filesystem::is_directory(seqDir)) { |
| 364 | std::string testFileName=seqDir+"/test.seq"; |
| 365 | ofstream testOutFile; |
| 366 | testOutFile.open(testFileName.c_str()); |
| 367 | if (testOutFile.is_open() && testOutFile.good()) { |
| 368 | m_nlsr.getConfParameter().setSeqFileDir(seqDir); |
| 369 | } |
| 370 | else { |
| 371 | std::cerr << "User does not have read and write permission on the directory"; |
| 372 | std::cerr << std::endl; |
| 373 | return false; |
| 374 | } |
| 375 | testOutFile.close(); |
| 376 | remove(testFileName.c_str()); |
| 377 | } |
| 378 | else { |
| 379 | std::cerr << "Provided path is not a directory" << std::endl; |
| 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | else { |
| 384 | std::cerr << "Seq directory provided does not exists" << std::endl; |
| 385 | return false; |
| 386 | } |
| 387 | } |
| 388 | catch (const std::exception& ex) { |
| 389 | std::cerr << "You must configure sequence directory" << std::endl; |
| 390 | std::cerr << ex.what() << std::endl; |
| 391 | return false; |
| 392 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 393 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 394 | return true; |
| 395 | } |
| 396 | |
| 397 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 398 | ConfFileProcessor::processConfSectionNeighbors(const ConfigSection& section) |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 399 | { |
| 400 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 401 | int retrials = section.get<int>("hello-retries"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 402 | if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) { |
| 403 | m_nlsr.getConfParameter().setInterestRetryNumber(retrials); |
| 404 | } |
| 405 | else { |
| 406 | std::cerr << "Wrong value for hello-retries. "; |
| 407 | std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-"; |
| 408 | std::cerr << HELLO_RETRIES_MAX << std::endl; |
| 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | catch (const std::exception& ex) { |
| 413 | std::cerr << ex.what() << std::endl; |
| 414 | return false; |
| 415 | } |
| 416 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 417 | int timeOut = section.get<int>("hello-timeout"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 418 | if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) { |
| 419 | m_nlsr.getConfParameter().setInterestResendTime(timeOut); |
| 420 | } |
| 421 | else { |
| 422 | std::cerr << "Wrong value for hello-timeout. "; |
| 423 | std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-"; |
| 424 | std::cerr << HELLO_TIMEOUT_MAX << std::endl; |
| 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | catch (const std::exception& ex) { |
| 429 | std::cerr << ex.what() << std::endl; |
| 430 | } |
| 431 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 432 | int interval = section.get<int>("hello-interval"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 433 | if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) { |
| 434 | m_nlsr.getConfParameter().setInfoInterestInterval(interval); |
| 435 | } |
| 436 | else { |
| 437 | std::cerr << "Wrong value for hello-interval. "; |
| 438 | std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-"; |
| 439 | std::cerr << HELLO_INTERVAL_MAX << std::endl; |
| 440 | return false; |
| 441 | } |
| 442 | } |
| 443 | catch (const std::exception& ex) { |
| 444 | std::cerr << ex.what() << std::endl; |
| 445 | } |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 446 | |
| 447 | // Event intervals |
| 448 | // adj-lsa-build-interval |
| 449 | ConfigurationVariable<uint32_t> adjLsaBuildInterval("adj-lsa-build-interval", |
| 450 | bind(&ConfParameter::setAdjLsaBuildInterval, |
| 451 | &m_nlsr.getConfParameter(), _1)); |
| 452 | adjLsaBuildInterval.setMinAndMaxValue(ADJ_LSA_BUILD_INTERVAL_MIN, ADJ_LSA_BUILD_INTERVAL_MAX); |
| 453 | adjLsaBuildInterval.setOptional(ADJ_LSA_BUILD_INTERVAL_DEFAULT); |
| 454 | |
| 455 | if (!adjLsaBuildInterval.parseFromConfigSection(section)) { |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | // first-hello-interval |
| 460 | ConfigurationVariable<uint32_t> firstHelloInterval("first-hello-interval", |
| 461 | bind(&ConfParameter::setFirstHelloInterval, |
| 462 | &m_nlsr.getConfParameter(), _1)); |
| 463 | firstHelloInterval.setMinAndMaxValue(FIRST_HELLO_INTERVAL_MIN, FIRST_HELLO_INTERVAL_MAX); |
| 464 | firstHelloInterval.setOptional(FIRST_HELLO_INTERVAL_DEFAULT); |
| 465 | |
| 466 | if (!firstHelloInterval.parseFromConfigSection(section)) { |
| 467 | return false; |
| 468 | } |
| 469 | |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 470 | for (ConfigSection::const_iterator tn = |
| 471 | section.begin(); tn != section.end(); ++tn) { |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 472 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 473 | if (tn->first == "neighbor") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 474 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 475 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 476 | ConfigSection CommandAttriTree = tn->second; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 477 | std::string name = CommandAttriTree.get<std::string>("name"); |
| 478 | std::string faceUri = CommandAttriTree.get<std::string>("face-uri"); |
| 479 | double linkCost = CommandAttriTree.get<double>("link-cost", |
| 480 | Adjacent::DEFAULT_LINK_COST); |
| 481 | ndn::Name neighborName(name); |
| 482 | if (!neighborName.empty()) { |
Vince Lehman | cb76ade | 2014-08-28 21:24:41 -0500 | [diff] [blame] | 483 | Adjacent adj(name, faceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 484 | m_nlsr.getAdjacencyList().insert(adj); |
| 485 | } |
| 486 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 487 | std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]"; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 488 | std::cerr << " or bad URI format" << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 489 | } |
| 490 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 491 | catch (const std::exception& ex) { |
| 492 | std::cerr << ex.what() << std::endl; |
| 493 | return false; |
| 494 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 495 | } |
| 496 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 497 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 498 | } |
| 499 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 500 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 501 | ConfFileProcessor::processConfSectionHyperbolic(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 502 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 503 | std::string state; |
| 504 | try { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 505 | state= section.get<string>("state","off"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 506 | if (boost::iequals(state, "off")) { |
| 507 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF); |
| 508 | } |
| 509 | else if (boost::iequals(state, "on")) { |
| 510 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON); |
| 511 | } |
| 512 | else if (state == "dry-run") { |
| 513 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN); |
| 514 | } |
| 515 | else { |
| 516 | std::cerr << "Wrong format for hyperbolic state." << std::endl; |
| 517 | std::cerr << "Allowed value: off, on, dry-run" << std::endl; |
| 518 | return false; |
| 519 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 520 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 521 | catch (const std::exception& ex) { |
| 522 | std::cerr << ex.what() << std::endl; |
| 523 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 524 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 525 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 526 | try { |
| 527 | /* Radius and angle is mandatory configuration parameter in hyperbolic section. |
| 528 | * Even if router can have hyperbolic routing calculation off but other router |
| 529 | * in the network may use hyperbolic routing calculation for FIB generation. |
| 530 | * So each router need to advertise its hyperbolic coordinates in the network |
| 531 | */ |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 532 | double radius = section.get<double>("radius"); |
| 533 | double angle = section.get<double>("angle"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 534 | if (!m_nlsr.getConfParameter().setCorR(radius)) { |
| 535 | return false; |
| 536 | } |
| 537 | m_nlsr.getConfParameter().setCorTheta(angle); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 538 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 539 | catch (const std::exception& ex) { |
| 540 | std::cerr << ex.what() << std::endl; |
| 541 | if (state == "on" || state == "dry-run") { |
| 542 | return false; |
| 543 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 544 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 545 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 546 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 547 | } |
| 548 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 549 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 550 | ConfFileProcessor::processConfSectionFib(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 551 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 552 | try { |
| 553 | int maxFacesPerPrefixNumber = |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 554 | section.get<int>("max-faces-per-prefix"); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 555 | if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN && |
| 556 | maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 557 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 558 | m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 559 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 560 | else { |
| 561 | std::cerr << "Wrong value for max-faces-per-prefix. "; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 562 | std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl; |
| 563 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 564 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 565 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 566 | catch (const std::exception& ex) { |
| 567 | cerr << ex.what() << endl; |
| 568 | return false; |
| 569 | } |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 570 | |
| 571 | // routing-calc-interval |
| 572 | ConfigurationVariable<uint32_t> routingCalcInterval("routing-calc-interval", |
| 573 | bind(&ConfParameter::setRoutingCalcInterval, |
| 574 | &m_nlsr.getConfParameter(), _1)); |
| 575 | routingCalcInterval.setMinAndMaxValue(ROUTING_CALC_INTERVAL_MIN, ROUTING_CALC_INTERVAL_MAX); |
| 576 | routingCalcInterval.setOptional(ROUTING_CALC_INTERVAL_DEFAULT); |
| 577 | |
| 578 | if (!routingCalcInterval.parseFromConfigSection(section)) { |
| 579 | return false; |
| 580 | } |
| 581 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 582 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 583 | } |
| 584 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 585 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 586 | ConfFileProcessor::processConfSectionAdvertising(const ConfigSection& section) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 587 | { |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 588 | for (ConfigSection::const_iterator tn = |
| 589 | section.begin(); tn != section.end(); ++tn) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 590 | if (tn->first == "prefix") { |
| 591 | try { |
| 592 | std::string prefix = tn->second.data(); |
| 593 | ndn::Name namePrefix(prefix); |
| 594 | if (!namePrefix.empty()) { |
| 595 | m_nlsr.getNamePrefixList().insert(namePrefix); |
| 596 | } |
| 597 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 598 | std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 599 | return false; |
| 600 | } |
| 601 | } |
| 602 | catch (const std::exception& ex) { |
| 603 | std::cerr << ex.what() << std::endl; |
| 604 | return false; |
| 605 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 606 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 607 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 608 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 609 | } |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 610 | |
| 611 | bool |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 612 | ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section) |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 613 | { |
| 614 | ConfigSection::const_iterator it = section.begin(); |
| 615 | |
| 616 | if (it == section.end() || it->first != "validator") |
| 617 | { |
| 618 | std::cerr << "Error: Expect validator section!" << std::endl; |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | m_nlsr.loadValidator(it->second, m_confFileName); |
akmhoque | d57f367 | 2014-06-10 10:41:32 -0500 | [diff] [blame] | 623 | it++; |
Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame] | 624 | |
| 625 | for (; it != section.end(); it++) |
| 626 | { |
| 627 | using namespace boost::filesystem; |
| 628 | if (it->first != "cert-to-publish") |
| 629 | { |
| 630 | std::cerr << "Error: Expect cert-to-publish!" << std::endl; |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | std::string file = it->second.data(); |
| 635 | path certfilePath = absolute(file, path(m_confFileName).parent_path()); |
| 636 | ndn::shared_ptr<ndn::IdentityCertificate> idCert = |
| 637 | ndn::io::load<ndn::IdentityCertificate>(certfilePath.string()); |
| 638 | |
| 639 | if (!static_cast<bool>(idCert)) |
| 640 | { |
| 641 | std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl; |
| 642 | return false; |
| 643 | } |
| 644 | |
| 645 | m_nlsr.loadCertToPublish(idCert); |
| 646 | } |
| 647 | |
| 648 | return true; |
| 649 | } |
| 650 | |
Alexander Afanasyev | 8388ec6 | 2014-08-16 18:38:57 -0700 | [diff] [blame] | 651 | } // namespace nlsr |