shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 4 | * |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 7 | * |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 20 | #include "app/name-server.hpp" |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 21 | #include "boost/program_options.hpp" |
| 22 | #include "boost/filesystem.hpp" |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 23 | |
| 24 | int main(int argc, char * argv[]) |
| 25 | { |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 26 | string route = "/"; |
| 27 | string zoneName = "/"; |
| 28 | string dbfile="src/db/ndns-local.db"; |
| 29 | string hint = "/"; |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 30 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 31 | try{ |
| 32 | |
| 33 | namespace po = boost::program_options; |
| 34 | po::variables_map vm; |
| 35 | |
| 36 | |
| 37 | po::options_description generic("Generic Options"); |
| 38 | generic.add_options() |
| 39 | ("help,h", "print help message") |
| 40 | ; |
| 41 | |
| 42 | |
| 43 | po::options_description config("Configuration"); |
| 44 | config.add_options() |
| 45 | ("dbfile,f", po::value<std::string>(&dbfile), "set database file. default: src/db/ndns-local.db") |
| 46 | ("hint,H", po::value<std::string>(&hint), "set Forwarding Hint, which is disable by default") |
| 47 | ; |
| 48 | |
| 49 | |
| 50 | po::options_description hidden("Hidden Options"); |
| 51 | hidden.add_options() |
| 52 | ("prefix,p", po::value<string>(&route), "routable prefix of the server") |
| 53 | ("zone,z", po::value<string>(&zoneName), "name of the zone") |
| 54 | ; |
| 55 | |
| 56 | po::positional_options_description postion; |
| 57 | postion.add("prefix", 1); |
| 58 | postion.add("zone", 1); |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | po::options_description cmdline_options; |
| 64 | cmdline_options.add(generic).add(config).add(hidden); |
| 65 | |
| 66 | po::options_description config_file_options; |
| 67 | config_file_options.add(config).add(hidden); |
| 68 | |
| 69 | po::options_description visible("Allowed options"); |
| 70 | visible.add(generic).add(config); |
| 71 | |
| 72 | |
| 73 | po::parsed_options parsed = po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run(); |
| 74 | |
| 75 | |
| 76 | po::store(parsed, vm); |
| 77 | po::notify(vm); |
| 78 | |
| 79 | if (vm.count("help")) |
| 80 | { |
| 81 | cout<<"E.g: name-server-daemon /name/of/zone /routable/prefix/announced"<<endl; |
| 82 | cout<<visible<<endl; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | cout<<"zone="<<zoneName<<" routablePrefix="<<route<<" dbfile="<<dbfile<<endl; |
| 87 | } |
| 88 | catch(const std::exception& ex) |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 89 | { |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 90 | cout << "Parameter Error: " << ex.what() << endl; |
| 91 | return 0; |
| 92 | } |
| 93 | catch(...) |
| 94 | { |
| 95 | cout << "Parameter Unknown error" << endl; |
| 96 | return 0; |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 97 | } |
| 98 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 99 | |
| 100 | |
| 101 | ndn::ndns::NameServer server(argv[0], route.c_str(), zoneName.c_str(), dbfile); |
| 102 | |
| 103 | if (hint != "/") { |
| 104 | server.setEnableForwardingHint(1); |
| 105 | server.setForwardingHint(Name(hint)); |
| 106 | } |
| 107 | |
| 108 | |
shockjiang | 8531202 | 2014-07-27 23:22:16 -0700 | [diff] [blame] | 109 | server.run(); |
| 110 | |
| 111 | cout<<"the server ends with hasError="<<server.hasError()<<endl; |
| 112 | |
| 113 | if (server.hasError()){ |
| 114 | return 0; |
| 115 | } else { |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |