akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <fstream> |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 3 | #include <boost/algorithm/string.hpp> |
| 4 | #include <boost/property_tree/info_parser.hpp> |
| 5 | #include <boost/property_tree/ptree.hpp> |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame^] | 6 | #include <boost/filesystem.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 7 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 8 | #include <ndn-cxx/name.hpp> |
| 9 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 10 | #include "conf-parameter.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 11 | #include "conf-file-processor.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 12 | #include "adjacent.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 13 | #include "utility/name-helper.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 14 | |
| 15 | |
| 16 | namespace nlsr { |
| 17 | |
| 18 | using namespace std; |
| 19 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 20 | bool |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 21 | ConfFileProcessor::processConfFile() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 23 | bool ret = true; |
| 24 | ifstream inputFile; |
| 25 | inputFile.open(m_confFileName.c_str()); |
| 26 | if (!inputFile.is_open()) { |
| 27 | string msg = "Failed to read configuration file: "; |
| 28 | msg += m_confFileName; |
| 29 | cerr << msg << endl; |
| 30 | ret = false; |
| 31 | } |
| 32 | ret = load(inputFile); |
| 33 | inputFile.close(); |
| 34 | return ret; |
| 35 | } |
| 36 | |
| 37 | bool |
| 38 | ConfFileProcessor::load(istream& input) |
| 39 | { |
| 40 | boost::property_tree::ptree pt; |
| 41 | bool ret = true; |
| 42 | try { |
| 43 | boost::property_tree::read_info(input, pt); |
| 44 | } |
| 45 | catch (const boost::property_tree::info_parser_error& error) { |
| 46 | stringstream msg; |
| 47 | std::cerr << "Failed to parse configuration file " << std::endl; |
| 48 | std::cerr << m_confFileName << std::endl; |
| 49 | return false; |
| 50 | } |
| 51 | for (boost::property_tree::ptree::const_iterator tn = pt.begin(); |
| 52 | tn != pt.end(); ++tn) { |
| 53 | std::string section = tn->first; |
| 54 | boost::property_tree::ptree SectionAttributeTree = tn ->second; |
| 55 | ret = processSection(section, SectionAttributeTree); |
| 56 | if (ret == false) { |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | bool |
| 64 | ConfFileProcessor::processSection(const std::string& section, |
| 65 | boost::property_tree::ptree SectionAttributeTree) |
| 66 | { |
| 67 | bool ret = true; |
| 68 | if (section == "general") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 69 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 70 | ret = processConfSectionGeneral(SectionAttributeTree); |
| 71 | } |
| 72 | else if (section == "neighbors") |
| 73 | { |
| 74 | ret = processConfSectionNeighbors(SectionAttributeTree); |
| 75 | } |
| 76 | else if (section == "hyperbolic") |
| 77 | { |
| 78 | ret = processConfSectionHyperbolic(SectionAttributeTree); |
| 79 | } |
| 80 | else if (section == "fib") |
| 81 | { |
| 82 | ret = processConfSectionFib(SectionAttributeTree); |
| 83 | } |
| 84 | else if (section == "advertising") |
| 85 | { |
| 86 | ret = processConfSectionAdvertising(SectionAttributeTree); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | std::cerr << "Wrong configuration Command: " << section << std::endl; |
| 91 | } |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | bool |
| 96 | ConfFileProcessor::processConfSectionGeneral(boost::property_tree::ptree |
| 97 | SectionAttributeTree) |
| 98 | { |
| 99 | try { |
| 100 | std::string network = SectionAttributeTree.get<string>("network"); |
| 101 | std::string site = SectionAttributeTree.get<string>("site"); |
| 102 | std::string router = SectionAttributeTree.get<string>("router"); |
| 103 | ndn::Name networkName(network); |
| 104 | if (!networkName.empty()) { |
| 105 | m_nlsr.getConfParameter().setNetwork(networkName); |
| 106 | } |
| 107 | else { |
| 108 | cerr << " Network can not be null or empty or in bad URI format :(!" << endl; |
| 109 | return false; |
| 110 | } |
| 111 | ndn::Name siteName(site); |
| 112 | if (!siteName.empty()) { |
| 113 | m_nlsr.getConfParameter().setSiteName(siteName); |
| 114 | } |
| 115 | else { |
| 116 | cerr << "Site can not be null or empty or in bad URI format:( !" << endl; |
| 117 | return false; |
| 118 | } |
| 119 | ndn::Name routerName(router); |
| 120 | if (!routerName.empty()) { |
| 121 | m_nlsr.getConfParameter().setRouterName(routerName); |
| 122 | } |
| 123 | else { |
| 124 | cerr << " Router name can not be null or empty or in bad URI format:( !" << endl; |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | catch (const std::exception& ex) { |
| 129 | cerr << ex.what() << endl; |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | try { |
| 134 | int32_t lsaRefreshTime = SectionAttributeTree.get<int32_t>("lsa-refresh-time"); |
| 135 | if (lsaRefreshTime >= LSA_REFRESH_TIME_MIN && |
| 136 | lsaRefreshTime <= LSA_REFRESH_TIME_MAX) { |
| 137 | m_nlsr.getConfParameter().setLsaRefreshTime(lsaRefreshTime); |
| 138 | } |
| 139 | else { |
| 140 | std::cerr << "Wrong value for lsa-refresh-time "; |
| 141 | std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";; |
| 142 | std::cerr << LSA_REFRESH_TIME_MAX << std::endl; |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | catch (const std::exception& ex) { |
| 147 | std::cerr << ex.what() << std::endl; |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | try { |
| 152 | std::string logLevel = SectionAttributeTree.get<string>("log-level"); |
| 153 | if ( boost::iequals(logLevel, "info") || boost::iequals(logLevel, "debug")) { |
| 154 | m_nlsr.getConfParameter().setLogLevel(logLevel); |
| 155 | } |
| 156 | else { |
| 157 | std::cerr << "Wrong value for log-level "; |
| 158 | std::cerr << "Allowed value: INFO, DEBUG" << std::endl; |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | catch (const std::exception& ex) { |
| 163 | std::cerr << ex.what() << std::endl; |
| 164 | return false; |
| 165 | } |
| 166 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame^] | 167 | try { |
| 168 | std::string logDir = SectionAttributeTree.get<string>("log-dir"); |
| 169 | if (boost::filesystem::exists(logDir)) { |
| 170 | if (boost::filesystem::is_directory(logDir)) { |
| 171 | std::string testFileName=logDir+"/test.log"; |
| 172 | ofstream testOutFile; |
| 173 | testOutFile.open(testFileName.c_str()); |
| 174 | if (testOutFile.is_open() && testOutFile.good()) { |
| 175 | m_nlsr.getConfParameter().setLogDir(logDir); |
| 176 | } |
| 177 | else { |
| 178 | std::cerr << "User does not have read and write permission on the directory"; |
| 179 | std::cerr << std::endl; |
| 180 | return false; |
| 181 | } |
| 182 | testOutFile.close(); |
| 183 | remove(testFileName.c_str()); |
| 184 | } |
| 185 | else { |
| 186 | std::cerr << "Provided path is not a directory" << std::endl; |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | else { |
| 191 | std::cerr << "Log directory provided does not exists" << std::endl; |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | catch (const std::exception& ex) { |
| 196 | std::cerr << "You must configure log directory" << std::endl; |
| 197 | std::cerr << ex.what() << std::endl; |
| 198 | return false; |
| 199 | } |
| 200 | try { |
| 201 | std::string seqDir = SectionAttributeTree.get<string>("seq-dir"); |
| 202 | if (boost::filesystem::exists(seqDir)) { |
| 203 | if (boost::filesystem::is_directory(seqDir)) { |
| 204 | std::string testFileName=seqDir+"/test.seq"; |
| 205 | ofstream testOutFile; |
| 206 | testOutFile.open(testFileName.c_str()); |
| 207 | if (testOutFile.is_open() && testOutFile.good()) { |
| 208 | m_nlsr.getConfParameter().setSeqFileDir(seqDir); |
| 209 | } |
| 210 | else { |
| 211 | std::cerr << "User does not have read and write permission on the directory"; |
| 212 | std::cerr << std::endl; |
| 213 | return false; |
| 214 | } |
| 215 | testOutFile.close(); |
| 216 | remove(testFileName.c_str()); |
| 217 | } |
| 218 | else { |
| 219 | std::cerr << "Provided path is not a directory" << std::endl; |
| 220 | return false; |
| 221 | } |
| 222 | } |
| 223 | else { |
| 224 | std::cerr << "Seq directory provided does not exists" << std::endl; |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | catch (const std::exception& ex) { |
| 229 | std::cerr << "You must configure sequence directory" << std::endl; |
| 230 | std::cerr << ex.what() << std::endl; |
| 231 | return false; |
| 232 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
| 236 | bool |
| 237 | ConfFileProcessor::processConfSectionNeighbors(boost::property_tree::ptree |
| 238 | SectionAttributeTree) |
| 239 | { |
| 240 | try { |
| 241 | int retrials = SectionAttributeTree.get<int>("hello-retries"); |
| 242 | if (retrials >= HELLO_RETRIES_MIN && retrials <= HELLO_RETRIES_MAX) { |
| 243 | m_nlsr.getConfParameter().setInterestRetryNumber(retrials); |
| 244 | } |
| 245 | else { |
| 246 | std::cerr << "Wrong value for hello-retries. "; |
| 247 | std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-"; |
| 248 | std::cerr << HELLO_RETRIES_MAX << std::endl; |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | catch (const std::exception& ex) { |
| 253 | std::cerr << ex.what() << std::endl; |
| 254 | return false; |
| 255 | } |
| 256 | try { |
| 257 | int timeOut = SectionAttributeTree.get<int>("hello-timeout"); |
| 258 | if (timeOut >= HELLO_TIMEOUT_MIN && timeOut <= HELLO_TIMEOUT_MAX) { |
| 259 | m_nlsr.getConfParameter().setInterestResendTime(timeOut); |
| 260 | } |
| 261 | else { |
| 262 | std::cerr << "Wrong value for hello-timeout. "; |
| 263 | std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-"; |
| 264 | std::cerr << HELLO_TIMEOUT_MAX << std::endl; |
| 265 | return false; |
| 266 | } |
| 267 | } |
| 268 | catch (const std::exception& ex) { |
| 269 | std::cerr << ex.what() << std::endl; |
| 270 | } |
| 271 | try { |
| 272 | int interval = SectionAttributeTree.get<int>("hello-interval"); |
| 273 | if (interval >= HELLO_INTERVAL_MIN && interval <= HELLO_INTERVAL_MAX) { |
| 274 | m_nlsr.getConfParameter().setInfoInterestInterval(interval); |
| 275 | } |
| 276 | else { |
| 277 | std::cerr << "Wrong value for hello-interval. "; |
| 278 | std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-"; |
| 279 | std::cerr << HELLO_INTERVAL_MAX << std::endl; |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | catch (const std::exception& ex) { |
| 284 | std::cerr << ex.what() << std::endl; |
| 285 | } |
| 286 | for (boost::property_tree::ptree::const_iterator tn = |
| 287 | SectionAttributeTree.begin(); tn != SectionAttributeTree.end(); ++tn) { |
| 288 | |
| 289 | if (tn->first == "neighbor") |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 290 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 291 | try { |
| 292 | boost::property_tree::ptree CommandAttriTree = tn->second; |
| 293 | std::string name = CommandAttriTree.get<std::string>("name"); |
| 294 | std::string faceUri = CommandAttriTree.get<std::string>("face-uri"); |
| 295 | double linkCost = CommandAttriTree.get<double>("link-cost", |
| 296 | Adjacent::DEFAULT_LINK_COST); |
| 297 | ndn::Name neighborName(name); |
| 298 | if (!neighborName.empty()) { |
| 299 | Adjacent adj(name, faceUri, linkCost, ADJACENT_STATUS_INACTIVE, 0); |
| 300 | m_nlsr.getAdjacencyList().insert(adj); |
| 301 | } |
| 302 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame^] | 303 | std::cerr << " Wrong command format ! [name /nbr/name/ \n face-uri /uri\n]"; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 304 | std::cerr << " or bad URI format" << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 305 | } |
| 306 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 307 | catch (const std::exception& ex) { |
| 308 | std::cerr << ex.what() << std::endl; |
| 309 | return false; |
| 310 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 311 | } |
| 312 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 313 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 314 | } |
| 315 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 316 | bool |
| 317 | ConfFileProcessor::processConfSectionHyperbolic(boost::property_tree::ptree |
| 318 | SectionAttributeTree) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 319 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 320 | std::string state; |
| 321 | try { |
| 322 | state= SectionAttributeTree.get<string>("state","off"); |
| 323 | if (boost::iequals(state, "off")) { |
| 324 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_OFF); |
| 325 | } |
| 326 | else if (boost::iequals(state, "on")) { |
| 327 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON); |
| 328 | } |
| 329 | else if (state == "dry-run") { |
| 330 | m_nlsr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN); |
| 331 | } |
| 332 | else { |
| 333 | std::cerr << "Wrong format for hyperbolic state." << std::endl; |
| 334 | std::cerr << "Allowed value: off, on, dry-run" << std::endl; |
| 335 | return false; |
| 336 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 337 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 338 | catch (const std::exception& ex) { |
| 339 | std::cerr << ex.what() << std::endl; |
| 340 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 341 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 342 | |
| 343 | try { |
| 344 | /* Radius and angle is mandatory configuration parameter in hyperbolic section. |
| 345 | * Even if router can have hyperbolic routing calculation off but other router |
| 346 | * in the network may use hyperbolic routing calculation for FIB generation. |
| 347 | * So each router need to advertise its hyperbolic coordinates in the network |
| 348 | */ |
| 349 | double radius = SectionAttributeTree.get<double>("radius"); |
| 350 | double angle = SectionAttributeTree.get<double>("angle"); |
| 351 | if (!m_nlsr.getConfParameter().setCorR(radius)) { |
| 352 | return false; |
| 353 | } |
| 354 | m_nlsr.getConfParameter().setCorTheta(angle); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 355 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 356 | catch (const std::exception& ex) { |
| 357 | std::cerr << ex.what() << std::endl; |
| 358 | if (state == "on" || state == "dry-run") { |
| 359 | return false; |
| 360 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 361 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 362 | |
| 363 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 364 | } |
| 365 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 366 | bool |
| 367 | ConfFileProcessor::processConfSectionFib(boost::property_tree::ptree |
| 368 | SectionAttributeTree) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 369 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 370 | try { |
| 371 | int maxFacesPerPrefixNumber = |
| 372 | SectionAttributeTree.get<int>("max-faces-per-prefix"); |
| 373 | if (maxFacesPerPrefixNumber >= MAX_FACES_PER_PREFIX_MIN && |
| 374 | maxFacesPerPrefixNumber <= MAX_FACES_PER_PREFIX_MAX) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 375 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 376 | m_nlsr.getConfParameter().setMaxFacesPerPrefix(maxFacesPerPrefixNumber); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 377 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 378 | else { |
| 379 | std::cerr << "Wrong value for max-faces-per-prefix. "; |
| 380 | std::cerr << "NLSR will user default value"; |
| 381 | std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl; |
| 382 | return false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 383 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 384 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 385 | catch (const std::exception& ex) { |
| 386 | cerr << ex.what() << endl; |
| 387 | return false; |
| 388 | } |
| 389 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 390 | } |
| 391 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 392 | bool |
| 393 | ConfFileProcessor::processConfSectionAdvertising(boost::property_tree::ptree |
| 394 | SectionAttributeTree) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 395 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 396 | for (boost::property_tree::ptree::const_iterator tn = |
| 397 | SectionAttributeTree.begin(); tn != SectionAttributeTree.end(); ++tn) { |
| 398 | if (tn->first == "prefix") { |
| 399 | try { |
| 400 | std::string prefix = tn->second.data(); |
| 401 | ndn::Name namePrefix(prefix); |
| 402 | if (!namePrefix.empty()) { |
| 403 | m_nlsr.getNamePrefixList().insert(namePrefix); |
| 404 | } |
| 405 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame^] | 406 | std::cerr << " Wrong command format ! [prefix /name/prefix] or bad URI" << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | catch (const std::exception& ex) { |
| 411 | std::cerr << ex.what() << std::endl; |
| 412 | return false; |
| 413 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 414 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 415 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 416 | return true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 417 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 418 | }//namespace NLSR |