nrd-config: Adding processing of command line arguments and processing of conf file.
refs #1450
Change-Id: I18fa5c1c12ec2367670659be331a9f95e0a920eb
diff --git a/src/main.cpp b/src/main.cpp
index e360e5d..5d0e5ed 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,21 +4,97 @@
* See COPYING for copyright and distribution information.
*/
+#include "common.hpp"
#include "nrd.hpp"
+#include "nrd-config.hpp"
+#include <getopt.h>
+
+struct ProgramOptions
+{
+ bool showUsage;
+ std::string config;
+};
+
+static void
+printUsage(std::ostream& os, const std::string& programName)
+{
+ os << "Usage: \n"
+ << " " << programName << " [options]\n"
+ << "\n"
+ << "Run NRD daemon\n"
+ << "\n"
+ << "Options:\n"
+ << " [--help] - print this help message\n"
+ << " [--config /path/to/nrd.conf] - path to configuration file "
+ << "(default: " << DEFAULT_CONFIG_FILE << ")\n"
+ ;
+}
+
+static bool
+parseCommandLine(int argc, char** argv, ProgramOptions& options)
+{
+ options.showUsage = false;
+ options.config = DEFAULT_CONFIG_FILE;
+
+ while (true) {
+ int optionIndex = 0;
+ static ::option longOptions[] = {
+ { "help" , no_argument , 0, 0 },
+ { "config" , required_argument, 0, 0 },
+ { 0 , 0 , 0, 0 }
+ };
+ int c = getopt_long_only(argc, argv, "", longOptions, &optionIndex);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 0:
+ switch (optionIndex) {
+ case 0: // help
+ options.showUsage = true;
+ break;
+ case 1: // config
+ options.config = ::optarg;
+ break;
+ default:
+ return false;
+ }
+ break;
+ }
+ }
+ return true;
+}
int
main(int argc, char** argv)
{
- try {
- // TODO: the configFilename should be obtained from command line arguments.
- std::string configFilename("nrd.conf");
+ //processing command line arguments, if available
+ ProgramOptions options;
+ bool isCommandLineValid = parseCommandLine(argc, argv, options);
+ if (!isCommandLineValid) {
+ printUsage(std::cerr, argv[0]);
+ return 1;
+ }
+ if (options.showUsage) {
+ printUsage(std::cout, argv[0]);
+ return 0;
+ }
- ndn::nrd::Nrd nrd(configFilename);
+ //Now read the config file and pass the security section to validator
+ try {
+ std::string configFilename(options.config);
+
+ ndn::nrd::NrdConfig config;
+ config.load(configFilename);
+ ndn::nrd::ConfigSection securitySection = config.getSecuritySection();
+
+ ndn::nrd::Nrd nrd(securitySection, configFilename);
nrd.enableLocalControlHeader();
nrd.listen();
}
catch (std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
}
+
return 0;
}