blob: 84b5e6d24ca4dfe593577f52efac0399991b383f [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include <ndn-cpp-dev/face.hpp>
2#include <ndn-cpp-dev/security/key-chain.hpp>
3#include <ndn-cpp-dev/util/scheduler.hpp>
akmhoque298385a2014-02-13 14:13:09 -06004#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -06005#include <string>
akmhoque298385a2014-02-13 14:13:09 -06006#include <sstream>
7
8#include "nlsr.hpp"
akmhoque298385a2014-02-13 14:13:09 -06009#include "nlsr_conf_processor.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060010#include "utility/nlsr_logger.hpp"
11
akmhoque298385a2014-02-13 14:13:09 -060012
akmhoque1fd8c1e2014-02-19 19:41:49 -060013namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060014{
akmhoque1fd8c1e2014-02-19 19:41:49 -060015
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;
akmhoque2bb198e2014-02-28 11:46:27 -060023 getNlsrFace()->shutdown();
akmhoque1fd8c1e2014-02-19 19:41:49 -060024 }
akmhoque298385a2014-02-13 14:13:09 -060025
26
akmhoque1fd8c1e2014-02-19 19:41:49 -060027 void
28 Nlsr::setInterestFilterNlsr(const string& name)
29 {
akmhoque2bb198e2014-02-28 11:46:27 -060030 getNlsrFace()->setInterestFilter(name,
31 func_lib::bind(&interestManager::processInterest, &im,
32 boost::ref(*this), _1, _2),
33 func_lib::bind(&Nlsr::nlsrRegistrationFailed, this, _1));
akmhoque1fd8c1e2014-02-19 19:41:49 -060034 }
akmhoque298385a2014-02-13 14:13:09 -060035
akmhoque2bb198e2014-02-28 11:46:27 -060036 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 }
akmhoque298385a2014-02-13 14:13:09 -060049
akmhoque1fd8c1e2014-02-19 19:41:49 -060050 void
51 Nlsr::startEventLoop()
52 {
akmhoque2bb198e2014-02-28 11:46:27 -060053 io->run();
akmhoque1fd8c1e2014-02-19 19:41:49 -060054 }
akmhoque298385a2014-02-13 14:13:09 -060055
akmhoque1fd8c1e2014-02-19 19:41:49 -060056 int
57 Nlsr::usage(const string& progname)
58 {
akmhoque298385a2014-02-13 14:13:09 -060059 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;
akmhoque298385a2014-02-13 14:13:09 -060065 exit(EXIT_FAILURE);
akmhoque1fd8c1e2014-02-19 19:41:49 -060066 }
akmhoque298385a2014-02-13 14:13:09 -060067
akmhoqueb1710aa2014-02-19 17:13:36 -060068
69} // namespace nlsr
70
71using namespace nlsr;
72
akmhoque1fd8c1e2014-02-19 19:41:49 -060073int
74main(int argc, char **argv)
75{
76 nlsr::Nlsr nlsr_;
77 string programName(argv[0]);
78 nlsr_.setConfFileName("nlsr.conf");
akmhoque1fd8c1e2014-02-19 19:41:49 -060079 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 }
akmhoque298385a2014-02-13 14:13:09 -0600103 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600104 ConfFileProcessor cfp(nlsr_.getConfFileName());
105 int res=cfp.processConfFile(nlsr_);
106 if ( res < 0 )
107 {
108 return EXIT_FAILURE;
109 }
akmhoque2bb198e2014-02-28 11:46:27 -0600110 nlsr_.initNlsr();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600111 /* debugging purpose start */
112 cout << nlsr_.getConfParameter();
113 nlsr_.getAdl().printAdl();
114 nlsr_.getNpl().printNpl();
115 /* debugging purpose end */
akmhoque1fd8c1e2014-02-19 19:41:49 -0600116 nlsr_.getLsdb().buildAndInstallOwnNameLsa(nlsr_);
117 nlsr_.getLsdb().buildAndInstallOwnCorLsa(nlsr_);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600118 nlsr_.setInterestFilterNlsr(nlsr_.getConfParameter().getRouterPrefix());
akmhoque2bb198e2014-02-28 11:46:27 -0600119 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());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600127 nlsr_.getIm().scheduleInfoInterest(nlsr_,1);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600128 try
129 {
130 nlsr_.startEventLoop();
131 }
132 catch(std::exception &e)
133 {
134 std::cerr << "ERROR: " << e.what() << std::endl;
135 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600136 return EXIT_SUCCESS;
akmhoque298385a2014-02-13 14:13:09 -0600137}