blob: a92f8cb57796cfa9260db00d056287c32900ae68 [file] [log] [blame]
akmhoque87347a32014-01-31 11:00:44 -06001#include <ndn-cpp-dev/face.hpp>
2#include <ndn-cpp-dev/security/key-chain.hpp>
3#include <ndn-cpp-dev/util/scheduler.hpp>
4
5#include <cstdlib>
akmhoquedfa4a5b2014-02-03 20:12:29 -06006#include<string>
7#include <sstream>
akmhoque87347a32014-01-31 11:00:44 -06008
9#include "nlsr.hpp"
akmhoque204e7542014-01-31 16:08:25 -060010#include "nlsr_conf_param.hpp"
11#include "nlsr_conf_processor.hpp"
akmhoquebd7c8e62014-02-01 14:57:40 -060012#include "nlsr_lsdb.hpp"
akmhoquedfa4a5b2014-02-03 20:12:29 -060013//test purpose of NLSR
14#include "nlsr_test.hpp"
akmhoque87347a32014-01-31 11:00:44 -060015
16using namespace ndn;
17using namespace std;
18
akmhoque87347a32014-01-31 11:00:44 -060019void
20nlsr::nlsrRegistrationFailed(const ptr_lib::shared_ptr<const Name>&)
21{
akmhoque147f4992014-01-31 15:52:49 -060022 cerr << "ERROR: Failed to register prefix in local hub's daemon" << endl;
akmhoquea8cd6b92014-01-31 20:13:26 -060023 getNlsrFace().shutdown();
akmhoque87347a32014-01-31 11:00:44 -060024}
25
akmhoque147f4992014-01-31 15:52:49 -060026
akmhoque87347a32014-01-31 11:00:44 -060027void
akmhoque147f4992014-01-31 15:52:49 -060028nlsr::setInterestFilterNlsr(const string& name)
akmhoque87347a32014-01-31 11:00:44 -060029{
akmhoquea8cd6b92014-01-31 20:13:26 -060030 getNlsrFace().setInterestFilter(name,
akmhoque147f4992014-01-31 15:52:49 -060031 func_lib::bind(&interestManager::processInterest, &im,
32 boost::ref(*this), _1, _2),
akmhoque87347a32014-01-31 11:00:44 -060033 func_lib::bind(&nlsr::nlsrRegistrationFailed, this, _1));
34}
35
akmhoque147f4992014-01-31 15:52:49 -060036
akmhoque87347a32014-01-31 11:00:44 -060037void
akmhoque147f4992014-01-31 15:52:49 -060038nlsr::startEventLoop()
akmhoque87347a32014-01-31 11:00:44 -060039{
akmhoquea8cd6b92014-01-31 20:13:26 -060040 getNlsrFace().processEvents();
akmhoque87347a32014-01-31 11:00:44 -060041}
42
43int
akmhoque147f4992014-01-31 15:52:49 -060044nlsr::usage(const string& progname)
45{
akmhoque87347a32014-01-31 11:00:44 -060046
47 cout << "Usage: " << progname << " [OPTIONS...]"<<endl;
48 cout << " NDN routing...." << endl;
49 cout << " -d, --daemon Run in daemon mode" << endl;
50 cout << " -f, --config_file Specify configuration file name" <<endl;
51 cout << " -p, --api_port port where api client will connect" <<endl;
52 cout << " -h, --help Display this help message" << endl;
53
54 exit(EXIT_FAILURE);
55}
56
57int
akmhoquedfa4a5b2014-02-03 20:12:29 -060058main(int argc, char **argv){
akmhoque87347a32014-01-31 11:00:44 -060059 nlsr nlsr;
akmhoquedfa4a5b2014-02-03 20:12:29 -060060 string programName(argv[0]);
akmhoque87347a32014-01-31 11:00:44 -060061 nlsr.setConfFileName("nlsr.conf");
akmhoquedfa4a5b2014-02-03 20:12:29 -060062
63 int opt;
64 while ((opt = getopt(argc, argv, "df:p:h")) != -1) {
65 switch (opt) {
66 case 'f':
67 nlsr.setConfFileName(optarg);
68 break;
69 case 'd':
70 nlsr.setIsDaemonProcess(optarg);
71 break;
72 case 'p':
73 {
74 stringstream sst(optarg);
75 int ap;
76 sst>>ap;
77 nlsr.setApiPort(ap);
78 }
79 break;
80 case 'h':
81 default:
82 nlsr.usage(programName);
83 return EXIT_FAILURE;
84 }
85 }
86
87
akmhoque87347a32014-01-31 11:00:44 -060088 ConfFileProcessor cfp(nlsr.getConfFileName());
akmhoquedfa4a5b2014-02-03 20:12:29 -060089 int res=cfp.processConfFile(nlsr);
90 if ( res < 0 )
91 {
92 return EXIT_FAILURE;
93 }
akmhoquee77d8142014-02-11 11:59:57 -060094
akmhoquea8cd6b92014-01-31 20:13:26 -060095 nlsr.getConfParameter().buildRouterPrefix();
akmhoquee77d8142014-02-11 11:59:57 -060096 nlsr.getLsdb().setLsaRefreshTime(nlsr.getConfParameter().getLsaRefreshTime());
97 nlsr.getFib().setFibEntryRefreshTime(2*nlsr.getConfParameter().getLsaRefreshTime());
98 nlsr.getFib().scheduleFibRefreshing(nlsr, 60);
99 nlsr.getLsdb().setThisRouterPrefix(nlsr.getConfParameter().getRouterPrefix());
akmhoquebd7c8e62014-02-01 14:57:40 -0600100
akmhoquedfa4a5b2014-02-03 20:12:29 -0600101 /* debugging purpose start */
akmhoquecd552472014-02-01 21:22:16 -0600102 cout << nlsr.getConfParameter();
akmhoquea8cd6b92014-01-31 20:13:26 -0600103 nlsr.getAdl().printAdl();
104 nlsr.getNpl().printNpl();
akmhoquedfa4a5b2014-02-03 20:12:29 -0600105 /* debugging purpose end */
106
107 nlsr.getLsdb().buildAndInstallOwnNameLsa(nlsr);
108 nlsr.getLsdb().buildAndInstallOwnCorLsa(nlsr);
109
110 //testing purpose
111 nlsr.getNlsrTesting().schedlueAddingLsas(nlsr);
112
akmhoquea8cd6b92014-01-31 20:13:26 -0600113 nlsr.setInterestFilterNlsr(nlsr.getConfParameter().getRouterPrefix());
114 nlsr.getIm().scheduleInfoInterest(nlsr,1);
akmhoque87347a32014-01-31 11:00:44 -0600115
116
117
118
119 try{
120 nlsr.startEventLoop();
121 }catch(std::exception &e) {
122 std::cerr << "ERROR: " << e.what() << std::endl;
123 }
124
akmhoquedfa4a5b2014-02-03 20:12:29 -0600125 return EXIT_SUCCESS;
akmhoque87347a32014-01-31 11:00:44 -0600126}