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