Obaid | 2ea377f | 2014-02-27 19:45:15 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Obaid | 5748c0c | 2014-04-09 18:00:42 -0500 | [diff] [blame] | 7 | #include "common.hpp" |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 8 | #include "nrd.hpp" |
Obaid | 5748c0c | 2014-04-09 18:00:42 -0500 | [diff] [blame] | 9 | #include "nrd-config.hpp" |
| 10 | #include <getopt.h> |
| 11 | |
| 12 | struct ProgramOptions |
| 13 | { |
| 14 | bool showUsage; |
| 15 | std::string config; |
| 16 | }; |
| 17 | |
| 18 | static void |
| 19 | printUsage(std::ostream& os, const std::string& programName) |
| 20 | { |
| 21 | os << "Usage: \n" |
| 22 | << " " << programName << " [options]\n" |
| 23 | << "\n" |
| 24 | << "Run NRD daemon\n" |
| 25 | << "\n" |
| 26 | << "Options:\n" |
| 27 | << " [--help] - print this help message\n" |
| 28 | << " [--config /path/to/nrd.conf] - path to configuration file " |
| 29 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 30 | ; |
| 31 | } |
| 32 | |
| 33 | static bool |
| 34 | parseCommandLine(int argc, char** argv, ProgramOptions& options) |
| 35 | { |
| 36 | options.showUsage = false; |
| 37 | options.config = DEFAULT_CONFIG_FILE; |
| 38 | |
| 39 | while (true) { |
| 40 | int optionIndex = 0; |
| 41 | static ::option longOptions[] = { |
| 42 | { "help" , no_argument , 0, 0 }, |
| 43 | { "config" , required_argument, 0, 0 }, |
| 44 | { 0 , 0 , 0, 0 } |
| 45 | }; |
| 46 | int c = getopt_long_only(argc, argv, "", longOptions, &optionIndex); |
| 47 | if (c == -1) |
| 48 | break; |
| 49 | |
| 50 | switch (c) { |
| 51 | case 0: |
| 52 | switch (optionIndex) { |
| 53 | case 0: // help |
| 54 | options.showUsage = true; |
| 55 | break; |
| 56 | case 1: // config |
| 57 | options.config = ::optarg; |
| 58 | break; |
| 59 | default: |
| 60 | return false; |
| 61 | } |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | } |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 67 | |
Obaid | 2ea377f | 2014-02-27 19:45:15 -0600 | [diff] [blame] | 68 | int |
| 69 | main(int argc, char** argv) |
| 70 | { |
Obaid | 5748c0c | 2014-04-09 18:00:42 -0500 | [diff] [blame] | 71 | //processing command line arguments, if available |
| 72 | ProgramOptions options; |
| 73 | bool isCommandLineValid = parseCommandLine(argc, argv, options); |
| 74 | if (!isCommandLineValid) { |
| 75 | printUsage(std::cerr, argv[0]); |
| 76 | return 1; |
| 77 | } |
| 78 | if (options.showUsage) { |
| 79 | printUsage(std::cout, argv[0]); |
| 80 | return 0; |
| 81 | } |
Yingdi Yu | defb5e4 | 2014-03-31 18:18:09 -0700 | [diff] [blame] | 82 | |
Obaid | 5748c0c | 2014-04-09 18:00:42 -0500 | [diff] [blame] | 83 | //Now read the config file and pass the security section to validator |
| 84 | try { |
| 85 | std::string configFilename(options.config); |
| 86 | |
| 87 | ndn::nrd::NrdConfig config; |
| 88 | config.load(configFilename); |
| 89 | ndn::nrd::ConfigSection securitySection = config.getSecuritySection(); |
| 90 | |
| 91 | ndn::nrd::Nrd nrd(securitySection, configFilename); |
Obaid | 3f48fe5 | 2014-02-27 21:45:23 -0600 | [diff] [blame] | 92 | nrd.enableLocalControlHeader(); |
| 93 | nrd.listen(); |
| 94 | } |
| 95 | catch (std::exception& e) { |
| 96 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 97 | } |
Obaid | 5748c0c | 2014-04-09 18:00:42 -0500 | [diff] [blame] | 98 | |
Obaid | 2ea377f | 2014-02-27 19:45:15 -0600 | [diff] [blame] | 99 | return 0; |
| 100 | } |