blob: 5d0e5ed3128a96c9d8b5fa4857f2f685cfc77152 [file] [log] [blame]
Obaid2ea377f2014-02-27 19:45:15 -06001/* -*- 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
Obaid5748c0c2014-04-09 18:00:42 -05007#include "common.hpp"
Obaid3f48fe52014-02-27 21:45:23 -06008#include "nrd.hpp"
Obaid5748c0c2014-04-09 18:00:42 -05009#include "nrd-config.hpp"
10#include <getopt.h>
11
12struct ProgramOptions
13{
14 bool showUsage;
15 std::string config;
16};
17
18static void
19printUsage(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
33static bool
34parseCommandLine(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}
Obaid3f48fe52014-02-27 21:45:23 -060067
Obaid2ea377f2014-02-27 19:45:15 -060068int
69main(int argc, char** argv)
70{
Obaid5748c0c2014-04-09 18:00:42 -050071 //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 Yudefb5e42014-03-31 18:18:09 -070082
Obaid5748c0c2014-04-09 18:00:42 -050083 //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);
Obaid3f48fe52014-02-27 21:45:23 -060092 nrd.enableLocalControlHeader();
93 nrd.listen();
94 }
95 catch (std::exception& e) {
96 std::cerr << "ERROR: " << e.what() << std::endl;
97 }
Obaid5748c0c2014-04-09 18:00:42 -050098
Obaid2ea377f2014-02-27 19:45:15 -060099 return 0;
100}