akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 1 | #include <ndn-cpp-dev/face.hpp> |
| 2 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 3 | #include <ndn-cpp-dev/util/scheduler.hpp> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 4 | #include <cstdlib> |
akmhoque | 92afde4 | 2014-02-18 14:04:07 -0600 | [diff] [blame] | 5 | #include <string> |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 6 | #include <sstream> |
| 7 | |
| 8 | #include "nlsr.hpp" |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 9 | #include "nlsr_conf_processor.hpp" |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame^] | 10 | #include "utility/nlsr_logger.hpp" |
| 11 | |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 12 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 13 | namespace nlsr |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 14 | { |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 15 | |
| 16 | using namespace ndn; |
| 17 | using namespace std; |
| 18 | |
| 19 | void |
| 20 | Nlsr::nlsrRegistrationFailed(const ndn::Name& name) |
| 21 | { |
| 22 | cerr << "ERROR: Failed to register prefix in local hub's daemon" << endl; |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame^] | 23 | getNlsrFace()->shutdown(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 24 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 25 | |
| 26 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 27 | void |
| 28 | Nlsr::setInterestFilterNlsr(const string& name) |
| 29 | { |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame^] | 30 | getNlsrFace()->setInterestFilter(name, |
| 31 | func_lib::bind(&interestManager::processInterest, &im, |
| 32 | boost::ref(*this), _1, _2), |
| 33 | func_lib::bind(&Nlsr::nlsrRegistrationFailed, this, _1)); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 34 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 35 | |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame^] | 36 | void |
| 37 | Nlsr::initNlsr() |
| 38 | { |
| 39 | confParam.buildRouterPrefix(); |
| 40 | nlsrLogger.initNlsrLogger(confParam.getLogDir()); |
| 41 | nlsrLsdb.setLsaRefreshTime(confParam.getLsaRefreshTime()); |
| 42 | nlsrLsdb.setThisRouterPrefix(confParam.getRouterPrefix()); |
| 43 | fib.setFibEntryRefreshTime(2*confParam.getLsaRefreshTime()); |
| 44 | km.initKeyManager(confParam); |
| 45 | sm.setSeqFileName(confParam.getSeqFileDir()); |
| 46 | sm.initiateSeqNoFromFile(); |
| 47 | slh.setSyncPrefix(confParam.getChronosyncSyncPrefix()); |
| 48 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 49 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 50 | void |
| 51 | Nlsr::startEventLoop() |
| 52 | { |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame^] | 53 | io->run(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 54 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 55 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 56 | int |
| 57 | Nlsr::usage(const string& progname) |
| 58 | { |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 59 | cout << "Usage: " << progname << " [OPTIONS...]"<<endl; |
| 60 | cout << " NDN routing...." << endl; |
| 61 | cout << " -d, --daemon Run in daemon mode" << endl; |
| 62 | cout << " -f, --config_file Specify configuration file name" <<endl; |
| 63 | cout << " -p, --api_port port where api client will connect" <<endl; |
| 64 | cout << " -h, --help Display this help message" << endl; |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 65 | exit(EXIT_FAILURE); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 66 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 67 | |
akmhoque | b1710aa | 2014-02-19 17:13:36 -0600 | [diff] [blame] | 68 | |
| 69 | } // namespace nlsr |
| 70 | |
| 71 | using namespace nlsr; |
| 72 | |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 73 | int |
| 74 | main(int argc, char **argv) |
| 75 | { |
| 76 | nlsr::Nlsr nlsr_; |
| 77 | string programName(argv[0]); |
| 78 | nlsr_.setConfFileName("nlsr.conf"); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 79 | int opt; |
| 80 | while ((opt = getopt(argc, argv, "df:p:h")) != -1) |
| 81 | { |
| 82 | switch (opt) |
| 83 | { |
| 84 | case 'f': |
| 85 | nlsr_.setConfFileName(optarg); |
| 86 | break; |
| 87 | case 'd': |
| 88 | nlsr_.setIsDaemonProcess(optarg); |
| 89 | break; |
| 90 | case 'p': |
| 91 | { |
| 92 | stringstream sst(optarg); |
| 93 | int ap; |
| 94 | sst>>ap; |
| 95 | nlsr_.setApiPort(ap); |
| 96 | } |
| 97 | break; |
| 98 | case 'h': |
| 99 | default: |
| 100 | nlsr_.usage(programName); |
| 101 | return EXIT_FAILURE; |
| 102 | } |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 103 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 104 | ConfFileProcessor cfp(nlsr_.getConfFileName()); |
| 105 | int res=cfp.processConfFile(nlsr_); |
| 106 | if ( res < 0 ) |
| 107 | { |
| 108 | return EXIT_FAILURE; |
| 109 | } |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame^] | 110 | nlsr_.initNlsr(); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 111 | /* debugging purpose start */ |
| 112 | cout << nlsr_.getConfParameter(); |
| 113 | nlsr_.getAdl().printAdl(); |
| 114 | nlsr_.getNpl().printNpl(); |
| 115 | /* debugging purpose end */ |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 116 | nlsr_.getLsdb().buildAndInstallOwnNameLsa(nlsr_); |
| 117 | nlsr_.getLsdb().buildAndInstallOwnCorLsa(nlsr_); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 118 | nlsr_.setInterestFilterNlsr(nlsr_.getConfParameter().getRouterPrefix()); |
akmhoque | 2bb198e | 2014-02-28 11:46:27 -0600 | [diff] [blame^] | 119 | nlsr_.setInterestFilterNlsr(nlsr_.getConfParameter().getChronosyncLsaPrefix()+ |
| 120 | nlsr_.getConfParameter().getRouterPrefix()); |
| 121 | nlsr_.setInterestFilterNlsr(nlsr_.getConfParameter().getRootKeyPrefix()); |
| 122 | nlsr_.getSlh().createSyncSocket(nlsr_); |
| 123 | nlsr_.getSlh().publishKeyUpdate(nlsr_.getKeyManager()); |
| 124 | nlsr_.getSlh().publishRoutingUpdate(nlsr_.getSm(), |
| 125 | nlsr_.getConfParameter().getChronosyncLsaPrefix() |
| 126 | + nlsr_.getConfParameter().getRouterPrefix()); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 127 | nlsr_.getIm().scheduleInfoInterest(nlsr_,1); |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 128 | try |
| 129 | { |
| 130 | nlsr_.startEventLoop(); |
| 131 | } |
| 132 | catch(std::exception &e) |
| 133 | { |
| 134 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 135 | } |
akmhoque | 1fd8c1e | 2014-02-19 19:41:49 -0600 | [diff] [blame] | 136 | return EXIT_SUCCESS; |
akmhoque | 298385a | 2014-02-13 14:13:09 -0600 | [diff] [blame] | 137 | } |