blob: 613e800106fc28180e9a23590e7f01b5eea3c49f [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include <cstdlib>
akmhoque92afde42014-02-18 14:04:07 -06002#include <string>
akmhoque298385a2014-02-13 14:13:09 -06003#include <sstream>
akmhoque05d5fcf2014-04-15 14:58:45 -05004#include <cstdio>
akmhoque298385a2014-02-13 14:13:09 -06005
6#include "nlsr.hpp"
akmhoque157b0a42014-05-13 00:26:37 -05007#include "adjacent.hpp"
akmhoque674b0b12014-05-20 14:33:28 -05008#include "logger.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -06009
akmhoque298385a2014-02-13 14:13:09 -060010
akmhoque53353462014-04-22 08:43:45 -050011namespace nlsr {
12
akmhoque674b0b12014-05-20 14:33:28 -050013INIT_LOGGER("nlsr");
14
akmhoque53353462014-04-22 08:43:45 -050015using namespace ndn;
16using namespace std;
17
18void
19Nlsr::registrationFailed(const ndn::Name& name)
akmhoque298385a2014-02-13 14:13:09 -060020{
akmhoquefdbddb12014-05-02 18:35:19 -050021 std::cerr << "ERROR: Failed to register prefix in local hub's daemon" << endl;
22 throw Error("Error: Prefix registration failed");
akmhoque53353462014-04-22 08:43:45 -050023}
akmhoque1fd8c1e2014-02-19 19:41:49 -060024
akmhoque157b0a42014-05-13 00:26:37 -050025void
26Nlsr::onRegistrationSuccess(const ndn::Name& name)
27{
28}
akmhoque1fd8c1e2014-02-19 19:41:49 -060029
akmhoque53353462014-04-22 08:43:45 -050030void
akmhoque31d1d4b2014-05-05 22:08:14 -050031Nlsr::setInfoInterestFilter()
akmhoque53353462014-04-22 08:43:45 -050032{
akmhoque31d1d4b2014-05-05 22:08:14 -050033 ndn::Name name(m_confParam.getRouterPrefix());
akmhoque674b0b12014-05-20 14:33:28 -050034 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoquefdbddb12014-05-02 18:35:19 -050035 getNlsrFace().setInterestFilter(name,
akmhoque31d1d4b2014-05-05 22:08:14 -050036 ndn::bind(&HelloProtocol::processInterest,
37 &m_helloProtocol, _1, _2),
akmhoque157b0a42014-05-13 00:26:37 -050038 ndn::bind(&Nlsr::onRegistrationSuccess, this, _1),
akmhoque31d1d4b2014-05-05 22:08:14 -050039 ndn::bind(&Nlsr::registrationFailed, this, _1));
40}
41
42void
43Nlsr::setLsaInterestFilter()
44{
akmhoque157b0a42014-05-13 00:26:37 -050045 ndn::Name name = m_confParam.getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -050046 name.append(m_confParam.getRouterPrefix());
akmhoque674b0b12014-05-20 14:33:28 -050047 _LOG_DEBUG("Setting interest filter for name: " << name);
akmhoque31d1d4b2014-05-05 22:08:14 -050048 getNlsrFace().setInterestFilter(name,
49 ndn::bind(&Lsdb::processInterest,
50 &m_nlsrLsdb, _1, _2),
akmhoque157b0a42014-05-13 00:26:37 -050051 ndn::bind(&Nlsr::onRegistrationSuccess, this, _1),
akmhoquefdbddb12014-05-02 18:35:19 -050052 ndn::bind(&Nlsr::registrationFailed, this, _1));
akmhoque53353462014-04-22 08:43:45 -050053}
54
55void
akmhoque157b0a42014-05-13 00:26:37 -050056Nlsr::registerPrefixes()
57{
58 std::string strategy("ndn:/localhost/nfd/strategy/broadcast");
59 std::list<Adjacent>& adjacents = m_adjacencyList.getAdjList();
60 for (std::list<Adjacent>::iterator it = adjacents.begin();
61 it != adjacents.end(); it++) {
62 m_fib.registerPrefix((*it).getName(), (*it).getConnectingFaceUri(),
63 (*it).getLinkCost(), 31536000); /* One Year in seconds */
64 m_fib.registerPrefix(m_confParam.getChronosyncPrefix(),
65 (*it).getConnectingFaceUri(), (*it).getLinkCost(), 31536000);
66 m_fib.registerPrefix(m_confParam.getLsaPrefix(),
67 (*it).getConnectingFaceUri(), (*it).getLinkCost(), 31536000);
68 m_fib.setStrategy((*it).getName(), strategy);
69 }
70
71 m_fib.setStrategy(m_confParam.getChronosyncPrefix(), strategy);
72 m_fib.setStrategy(m_confParam.getLsaPrefix(), strategy);
73}
74
75void
akmhoque53353462014-04-22 08:43:45 -050076Nlsr::initialize()
77{
akmhoque674b0b12014-05-20 14:33:28 -050078 _LOG_DEBUG("Initializing Nlsr");
akmhoque53353462014-04-22 08:43:45 -050079 m_confParam.buildRouterPrefix();
80 m_nlsrLsdb.setLsaRefreshTime(m_confParam.getLsaRefreshTime());
akmhoque31d1d4b2014-05-05 22:08:14 -050081 m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
akmhoque53353462014-04-22 08:43:45 -050082 m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
akmhoquec8a10f72014-04-25 18:42:55 -050083 m_sequencingManager.setSeqFileName(m_confParam.getSeqFileDir());
84 m_sequencingManager.initiateSeqNoFromFile();
akmhoque53353462014-04-22 08:43:45 -050085 /* debugging purpose start */
Yingdi Yu0c217822014-04-24 14:22:42 -070086 cout << m_confParam;
akmhoquec8a10f72014-04-25 18:42:55 -050087 m_adjacencyList.print();
88 m_namePrefixList.print();
akmhoque53353462014-04-22 08:43:45 -050089 /* debugging purpose end */
akmhoque674b0b12014-05-20 14:33:28 -050090 /* Logging start */
91 m_confParam.writeLog();
92 m_adjacencyList.writeLog();
93 m_namePrefixList.writeLog();
94 /* Logging end */
akmhoque157b0a42014-05-13 00:26:37 -050095 registerPrefixes();
akmhoque31d1d4b2014-05-05 22:08:14 -050096 setInfoInterestFilter();
97 setLsaInterestFilter();
akmhoque674b0b12014-05-20 14:33:28 -050098 m_nlsrLsdb.buildAndInstallOwnNameLsa();
99 m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
akmhoque157b0a42014-05-13 00:26:37 -0500100 m_syncLogicHandler.setSyncPrefix(m_confParam.getChronosyncPrefix().toUri());
akmhoquec8a10f72014-04-25 18:42:55 -0500101 m_syncLogicHandler.createSyncSocket(boost::ref(*this));
akmhoque31d1d4b2014-05-05 22:08:14 -0500102 //m_interestManager.scheduleInfoInterest(10);
103 m_helloProtocol.scheduleInterest(10);
akmhoque53353462014-04-22 08:43:45 -0500104}
akmhoque5a44dd42014-03-12 18:11:32 -0500105
akmhoque53353462014-04-22 08:43:45 -0500106void
107Nlsr::startEventLoop()
108{
akmhoquefdbddb12014-05-02 18:35:19 -0500109 m_nlsrFace.processEvents();
akmhoque53353462014-04-22 08:43:45 -0500110}
akmhoque5a44dd42014-03-12 18:11:32 -0500111
akmhoquefdbddb12014-05-02 18:35:19 -0500112void
akmhoque53353462014-04-22 08:43:45 -0500113Nlsr::usage(const string& progname)
114{
115 cout << "Usage: " << progname << " [OPTIONS...]" << endl;
116 cout << " NDN routing...." << endl;
117 cout << " -d, --daemon Run in daemon mode" << endl;
118 cout << " -f, --config_file Specify configuration file name" << endl;
119 cout << " -p, --api_port port where api client will connect" << endl;
120 cout << " -h, --help Display this help message" << endl;
akmhoque53353462014-04-22 08:43:45 -0500121}
akmhoque298385a2014-02-13 14:13:09 -0600122
akmhoqueb1710aa2014-02-19 17:13:36 -0600123
124} // namespace nlsr