akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame^] | 1 | #include <iostream> |
| 2 | #include <fstream> |
| 3 | #include <string> |
| 4 | #include <cstdlib> |
| 5 | #include <sstream> |
| 6 | |
| 7 | #include "conf-file-processor.hpp" |
| 8 | #include "conf-parameter.hpp" |
| 9 | #include "utility/tokenizer.hpp" |
| 10 | #include "adjacent.hpp" |
| 11 | |
| 12 | |
| 13 | namespace nlsr { |
| 14 | |
| 15 | using namespace std; |
| 16 | |
| 17 | int |
| 18 | ConfFileProcessor::processConfFile(Nlsr& pnlsr) |
| 19 | { |
| 20 | int ret = 0; |
| 21 | if (!m_confFileName.empty()) |
| 22 | { |
| 23 | std::ifstream inputFile(m_confFileName.c_str()); |
| 24 | if (inputFile.is_open()) |
| 25 | { |
| 26 | for (string line; getline(inputFile, line);) |
| 27 | { |
| 28 | if (!line.empty()) |
| 29 | { |
| 30 | if (line[0] != '#' && line[0] != '!') |
| 31 | { |
| 32 | ret = processConfCommand(pnlsr, line); |
| 33 | if (ret == -1) |
| 34 | { |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | std::cerr << "Configuration file: (" << m_confFileName << ") does not exist :("; |
| 44 | std::cerr << endl; |
| 45 | ret = -1; |
| 46 | } |
| 47 | } |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | int |
| 53 | ConfFileProcessor::processConfCommand(Nlsr& pnlsr, string command) |
| 54 | { |
| 55 | int ret = 0; |
| 56 | Tokenizer nt(command, " "); |
| 57 | if ((nt.getFirstToken() == "network")) |
| 58 | { |
| 59 | ret = processConfCommandNetwork(pnlsr, nt.getRestOfLine()); |
| 60 | } |
| 61 | else if ((nt.getFirstToken() == "site-name")) |
| 62 | { |
| 63 | ret = processConfCommandSiteName(pnlsr, nt.getRestOfLine()); |
| 64 | } |
| 65 | else if ((nt.getFirstToken() == "root-key-prefix")) |
| 66 | { |
| 67 | ret = processConfCommandRootKeyPrefix(pnlsr, nt.getRestOfLine()); |
| 68 | } |
| 69 | else if ((nt.getFirstToken() == "router-name")) |
| 70 | { |
| 71 | ret = processConfCommandRouterName(pnlsr, nt.getRestOfLine()); |
| 72 | } |
| 73 | else if ((nt.getFirstToken() == "ndnneighbor")) |
| 74 | { |
| 75 | ret = processConfCommandNdnNeighbor(pnlsr, nt.getRestOfLine()); |
| 76 | } |
| 77 | else if ((nt.getFirstToken() == "link-cost")) |
| 78 | { |
| 79 | ret = processConfCommandLinkCost(pnlsr, nt.getRestOfLine()); |
| 80 | } |
| 81 | else if ((nt.getFirstToken() == "ndnname")) |
| 82 | { |
| 83 | ret = processConfCommandNdnName(pnlsr, nt.getRestOfLine()); |
| 84 | } |
| 85 | else if ((nt.getFirstToken() == "interest-retry-num")) |
| 86 | { |
| 87 | processConfCommandInterestRetryNumber(pnlsr, nt.getRestOfLine()); |
| 88 | } |
| 89 | else if ((nt.getFirstToken() == "interest-resend-time")) |
| 90 | { |
| 91 | processConfCommandInterestResendTime(pnlsr, nt.getRestOfLine()); |
| 92 | } |
| 93 | else if ((nt.getFirstToken() == "lsa-refresh-time")) |
| 94 | { |
| 95 | processConfCommandLsaRefreshTime(pnlsr, nt.getRestOfLine()); |
| 96 | } |
| 97 | else if ((nt.getFirstToken() == "max-faces-per-prefix")) |
| 98 | { |
| 99 | processConfCommandMaxFacesPerPrefix(pnlsr, nt.getRestOfLine()); |
| 100 | } |
| 101 | else if ((nt.getFirstToken() == "log-dir")) |
| 102 | { |
| 103 | processConfCommandLogDir(pnlsr, nt.getRestOfLine()); |
| 104 | } |
| 105 | else if ((nt.getFirstToken() == "cert-dir")) |
| 106 | { |
| 107 | processConfCommandCertDir(pnlsr, nt.getRestOfLine()); |
| 108 | } |
| 109 | else if ((nt.getFirstToken() == "detailed-logging")) |
| 110 | { |
| 111 | processConfCommandDetailedLogging(pnlsr, nt.getRestOfLine()); |
| 112 | } |
| 113 | else if ((nt.getFirstToken() == "debugging")) |
| 114 | { |
| 115 | processConfCommandDebugging(pnlsr, nt.getRestOfLine()); |
| 116 | } |
| 117 | else if ((nt.getFirstToken() == "chronosync-sync-prefix")) |
| 118 | { |
| 119 | processConfCommandChronosyncSyncPrefix(pnlsr, nt.getRestOfLine()); |
| 120 | } |
| 121 | else if ((nt.getFirstToken() == "hyperbolic-cordinate")) |
| 122 | { |
| 123 | processConfCommandHyperbolicCordinate(pnlsr, nt.getRestOfLine()); |
| 124 | } |
| 125 | else if ((nt.getFirstToken() == "hyperbolic-routing")) |
| 126 | { |
| 127 | processConfCommandIsHyperbolicCalc(pnlsr, nt.getRestOfLine()); |
| 128 | } |
| 129 | else if ((nt.getFirstToken() == "tunnel-type")) |
| 130 | { |
| 131 | processConfCommandTunnelType(pnlsr, nt.getRestOfLine()); |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | cout << "Wrong configuration Command: " << nt.getFirstToken() << endl; |
| 136 | } |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | int |
| 141 | ConfFileProcessor::processConfCommandNetwork(Nlsr& pnlsr, string command) |
| 142 | { |
| 143 | if (command.empty()) |
| 144 | { |
| 145 | cerr << " Network can not be null or empty :( !" << endl; |
| 146 | return -1; |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | if (command[command.size() - 1] == '/') |
| 151 | { |
| 152 | command.erase(command.size() - 1); |
| 153 | } |
| 154 | if (command[0] == '/') |
| 155 | { |
| 156 | command.erase(0, 1); |
| 157 | } |
| 158 | pnlsr.getConfParameter().setNetwork(command); |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | int |
| 164 | ConfFileProcessor::processConfCommandSiteName(Nlsr& pnlsr, string command) |
| 165 | { |
| 166 | if (command.empty()) |
| 167 | { |
| 168 | cerr << "Site name can not be null or empty :( !" << endl; |
| 169 | return -1; |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | if (command[command.size() - 1] == '/') |
| 174 | { |
| 175 | command.erase(command.size() - 1); |
| 176 | } |
| 177 | if (command[0] == '/') |
| 178 | { |
| 179 | command.erase(0, 1); |
| 180 | } |
| 181 | pnlsr.getConfParameter().setSiteName(command); |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int |
| 187 | ConfFileProcessor::processConfCommandRootKeyPrefix(Nlsr& pnlsr, string command) |
| 188 | { |
| 189 | if (command.empty()) |
| 190 | { |
| 191 | cerr << "Root Key Prefix can not be null or empty :( !" << endl; |
| 192 | return -1; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | if (command[command.size() - 1] == '/') |
| 197 | { |
| 198 | command.erase(command.size() - 1); |
| 199 | } |
| 200 | if (command[0] == '/') |
| 201 | { |
| 202 | command.erase(0, 1); |
| 203 | } |
| 204 | pnlsr.getConfParameter().setRootKeyPrefix(command); |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | int |
| 211 | ConfFileProcessor::processConfCommandRouterName(Nlsr& pnlsr, string command) |
| 212 | { |
| 213 | if (command.empty()) |
| 214 | { |
| 215 | cerr << " Router name can not be null or empty :( !" << endl; |
| 216 | return -1; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | if (command[command.size() - 1] == '/') |
| 221 | { |
| 222 | command.erase(command.size() - 1); |
| 223 | } |
| 224 | if (command[0] == '/') |
| 225 | { |
| 226 | command.erase(0, 1); |
| 227 | } |
| 228 | pnlsr.getConfParameter().setRouterName(command); |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | int |
| 234 | ConfFileProcessor::processConfCommandInterestRetryNumber(Nlsr& pnlsr, |
| 235 | string command) |
| 236 | { |
| 237 | if (command.empty()) |
| 238 | { |
| 239 | cerr << " Wrong command format ! [interest-retry-num n]" << endl; |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | int irn; |
| 244 | stringstream ss(command.c_str()); |
| 245 | ss >> irn; |
| 246 | if (irn >= 1 && irn <= 5) |
| 247 | { |
| 248 | pnlsr.getConfParameter().setInterestRetryNumber(irn); |
| 249 | } |
| 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | int |
| 255 | ConfFileProcessor::processConfCommandInterestResendTime(Nlsr& pnlsr, |
| 256 | string command) |
| 257 | { |
| 258 | if (command.empty()) |
| 259 | { |
| 260 | cerr << " Wrong command format ! [interest-resend-time s]" << endl; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | int irt; |
| 265 | stringstream ss(command.c_str()); |
| 266 | ss >> irt; |
| 267 | if (irt >= 1 && irt <= 20) |
| 268 | { |
| 269 | pnlsr.getConfParameter().setInterestResendTime(irt); |
| 270 | } |
| 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | int |
| 276 | ConfFileProcessor::processConfCommandLsaRefreshTime(Nlsr& pnlsr, string command) |
| 277 | { |
| 278 | if (command.empty()) |
| 279 | { |
| 280 | cerr << " Wrong command format ! [interest-resend-time s]" << endl; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | int lrt; |
| 285 | stringstream ss(command.c_str()); |
| 286 | ss >> lrt; |
| 287 | if (lrt >= 240 && lrt <= 7200) |
| 288 | { |
| 289 | pnlsr.getConfParameter().setLsaRefreshTime(lrt); |
| 290 | } |
| 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | int |
| 296 | ConfFileProcessor::processConfCommandMaxFacesPerPrefix(Nlsr& pnlsr, |
| 297 | string command) |
| 298 | { |
| 299 | if (command.empty()) |
| 300 | { |
| 301 | cerr << " Wrong command format ! [max-faces-per-prefix n]" << endl; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | int mfpp; |
| 306 | stringstream ss(command.c_str()); |
| 307 | ss >> mfpp; |
| 308 | if (mfpp >= 0 && mfpp <= 60) |
| 309 | { |
| 310 | pnlsr.getConfParameter().setMaxFacesPerPrefix(mfpp); |
| 311 | } |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | int |
| 317 | ConfFileProcessor::processConfCommandTunnelType(Nlsr& pnlsr, string command) |
| 318 | { |
| 319 | if (command.empty()) |
| 320 | { |
| 321 | cerr << " Wrong command format ! [tunnel-type tcp/udp]!" << endl; |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | if (command == "tcp" || command == "TCP") |
| 326 | { |
| 327 | pnlsr.getConfParameter().setTunnelType(1); |
| 328 | } |
| 329 | else if (command == "udp" || command == "UDP") |
| 330 | { |
| 331 | pnlsr.getConfParameter().setTunnelType(0); |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | cerr << " Wrong command format ! [tunnel-type tcp/udp]!" << endl; |
| 336 | } |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | int |
| 342 | ConfFileProcessor::processConfCommandChronosyncSyncPrefix(Nlsr& pnlsr, |
| 343 | string command) |
| 344 | { |
| 345 | if (command.empty()) |
| 346 | { |
| 347 | cerr << " Wrong command format ! [chronosync-sync-prefix name/prefix]!" << endl; |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | pnlsr.getConfParameter().setChronosyncSyncPrefix(command); |
| 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | int |
| 358 | ConfFileProcessor::processConfCommandLogDir(Nlsr& pnlsr, string command) |
| 359 | { |
| 360 | if (command.empty()) |
| 361 | { |
| 362 | cerr << " Wrong command format ! [log-dir /path/to/log/dir]!" << endl; |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | pnlsr.getConfParameter().setLogDir(command); |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | int |
| 372 | ConfFileProcessor::processConfCommandCertDir(Nlsr& pnlsr, string command) |
| 373 | { |
| 374 | if (command.empty()) |
| 375 | { |
| 376 | cerr << " Wrong command format ! [cert-dir /path/to/cert/dir]!" << endl; |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | pnlsr.getConfParameter().setCertDir(command); |
| 381 | } |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | int |
| 386 | ConfFileProcessor::processConfCommandDebugging(Nlsr& pnlsr, string command) |
| 387 | { |
| 388 | if (command.empty()) |
| 389 | { |
| 390 | cerr << " Wrong command format ! [debugging on/of]!" << endl; |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | if (command == "on" || command == "ON") |
| 395 | { |
| 396 | pnlsr.getConfParameter().setDebugging(1); |
| 397 | } |
| 398 | else if (command == "off" || command == "off") |
| 399 | { |
| 400 | pnlsr.getConfParameter().setDebugging(0); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | cerr << " Wrong command format ! [debugging on/off]!" << endl; |
| 405 | } |
| 406 | } |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int |
| 411 | ConfFileProcessor::processConfCommandDetailedLogging(Nlsr& pnlsr, |
| 412 | string command) |
| 413 | { |
| 414 | if (command.empty()) |
| 415 | { |
| 416 | cerr << " Wrong command format ! [detailed-logging on/off]!" << endl; |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | if (command == "on" || command == "ON") |
| 421 | { |
| 422 | pnlsr.getConfParameter().setDetailedLogging(1); |
| 423 | } |
| 424 | else if (command == "off" || command == "off") |
| 425 | { |
| 426 | pnlsr.getConfParameter().setDetailedLogging(0); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | cerr << " Wrong command format ! [detailed-logging on/off]!" << endl; |
| 431 | } |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | int |
| 437 | ConfFileProcessor::processConfCommandIsHyperbolicCalc(Nlsr& pnlsr, |
| 438 | string command) |
| 439 | { |
| 440 | if (command.empty()) |
| 441 | { |
| 442 | cerr << " Wrong command format ! [hyperbolic-routing on/off/dry-run]!" << endl; |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | if (command == "on" || command == "ON") |
| 447 | { |
| 448 | pnlsr.getConfParameter().setIsHyperbolicCalc(1); |
| 449 | } |
| 450 | else if (command == "dry-run" || command == "DRY-RUN") |
| 451 | { |
| 452 | pnlsr.getConfParameter().setIsHyperbolicCalc(2); |
| 453 | } |
| 454 | else if (command == "off" || command == "off") |
| 455 | { |
| 456 | pnlsr.getConfParameter().setIsHyperbolicCalc(0); |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | cerr << " Wrong command format ! [hyperbolic-routing on/off/dry-run]!" << endl; |
| 461 | } |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | int |
| 467 | ConfFileProcessor::processConfCommandHyperbolicCordinate(Nlsr& pnlsr, |
| 468 | string command) |
| 469 | { |
| 470 | if (command.empty()) |
| 471 | { |
| 472 | cerr << " Wrong command format ! [hyperbolic-cordinate r 0]!" << endl; |
| 473 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0) |
| 474 | { |
| 475 | return -1; |
| 476 | } |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | Tokenizer nt(command, " "); |
| 481 | stringstream ssr(nt.getFirstToken().c_str()); |
| 482 | stringstream sst(nt.getRestOfLine().c_str()); |
| 483 | double r, theta; |
| 484 | ssr >> r; |
| 485 | sst >> theta; |
| 486 | pnlsr.getConfParameter().setCorR(r); |
| 487 | pnlsr.getConfParameter().setCorTheta(theta); |
| 488 | } |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | int |
| 494 | ConfFileProcessor::processConfCommandNdnNeighbor(Nlsr& pnlsr, string command) |
| 495 | { |
| 496 | if (command.empty()) |
| 497 | { |
| 498 | cerr << " Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!" << endl; |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | Tokenizer nt(command, " "); |
| 503 | if (nt.getRestOfLine().empty()) |
| 504 | { |
| 505 | cerr << " Wrong command format ! [ndnneighbor /nbr/name/ FaceId]!" << endl; |
| 506 | return 0; |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | stringstream sst(nt.getRestOfLine().c_str()); |
| 511 | int faceId; |
| 512 | sst >> faceId; |
| 513 | Adjacent adj(nt.getFirstToken(), faceId, 0.0, 0, 0); |
| 514 | pnlsr.getAdl().insert(adj); |
| 515 | } |
| 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | int |
| 521 | ConfFileProcessor::processConfCommandNdnName(Nlsr& pnlsr, string command) |
| 522 | { |
| 523 | if (command.empty()) |
| 524 | { |
| 525 | cerr << " Wrong command format ! [ndnname name/prefix]!" << endl; |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | pnlsr.getNpl().insert(command); |
| 530 | } |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | int |
| 536 | ConfFileProcessor::processConfCommandLinkCost(Nlsr& pnlsr, string command) |
| 537 | { |
| 538 | if (command.empty()) |
| 539 | { |
| 540 | cerr << " Wrong command format ! [link-cost nbr/name cost]!" << endl; |
| 541 | if (pnlsr.getConfParameter().getIsHyperbolicCalc() > 0) |
| 542 | { |
| 543 | return -1; |
| 544 | } |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | Tokenizer nt(command, " "); |
| 549 | stringstream sst(nt.getRestOfLine().c_str()); |
| 550 | double cost; |
| 551 | sst >> cost; |
| 552 | pnlsr.getAdl().updateAdjacentLinkCost(nt.getFirstToken(), cost); |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | } //namespace nlsr |
| 558 | |