blob: 217f7df9e703feba5882e71f81c39789e8d5da92 [file] [log] [blame]
akmhoque31d1d4b2014-05-05 22:08:14 -05001#include "nlsr.hpp"
2#include "lsdb.hpp"
3#include "hello-protocol.hpp"
4#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -05005#include "logger.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -05006
7namespace nlsr {
8
akmhoque674b0b12014-05-20 14:33:28 -05009INIT_LOGGER("HelloProtocol");
10
akmhoque157b0a42014-05-13 00:26:37 -050011const std::string HelloProtocol::INFO_COMPONENT="info";
12
akmhoque31d1d4b2014-05-05 22:08:14 -050013void
14HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
15{
16 std::cout << "Expressing Interest :" << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050017 _LOG_DEBUG("Expressing Interest :" << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -050018 ndn::Interest i(interestName);
19 i.setInterestLifetime(ndn::time::seconds(seconds));
20 i.setMustBeFresh(true);
21 m_nlsr.getNlsrFace().expressInterest(i,
22 ndn::bind(&HelloProtocol::processContent,
23 this,
24 _1, _2),
25 ndn::bind(&HelloProtocol::processInterestTimedOut,
26 this, _1));
27}
28
29void
30HelloProtocol::sendScheduledInterest(uint32_t seconds)
31{
32 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
33 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050034 ++it) {
akmhoque31d1d4b2014-05-05 22:08:14 -050035 ndn::Name interestName = (*it).getName() ;
akmhoque157b0a42014-05-13 00:26:37 -050036 interestName.append(INFO_COMPONENT);
37 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -050038 expressInterest(interestName,
39 m_nlsr.getConfParameter().getInterestResendTime());
40 }
41 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
42}
43
44void
45HelloProtocol::scheduleInterest(uint32_t seconds)
46{
47 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
48 ndn::bind(&HelloProtocol::sendScheduledInterest,
49 this, seconds));
50}
51
52void
53HelloProtocol::processInterest(const ndn::Name& name,
54 const ndn::Interest& interest)
55{
56 const ndn::Name interestName = interest.getName();
57 std::cout << "Interest Received for Name: " << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050058 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050059 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050060 return;
61 }
akmhoque157b0a42014-05-13 00:26:37 -050062 ndn::Name neighbor;
63 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque31d1d4b2014-05-05 22:08:14 -050064 std::cout << "Neighbor: " << neighbor << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050065 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050066 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque31d1d4b2014-05-05 22:08:14 -050067 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
68 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
akmhoque157b0a42014-05-13 00:26:37 -050069 data.setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
70 INFO_COMPONENT.size());
akmhoque31d1d4b2014-05-05 22:08:14 -050071 m_keyChain.sign(data);
72 std::cout << ">> D: " << data << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050073 _LOG_DEBUG("Sending out data for name: " << data.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -050074 m_nlsr.getNlsrFace().put(data);
75 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050076 if (status == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050077 ndn::Name interestName(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050078 interestName.append(INFO_COMPONENT);
akmhoque31d1d4b2014-05-05 22:08:14 -050079 interestName.append(m_nlsr.getConfParameter().getRouterPrefix());
80 expressInterest(interestName,
81 m_nlsr.getConfParameter().getInterestResendTime());
82 }
83 }
84}
85
86void
87HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
88{
89 const ndn::Name interestName(interest.getName());
90 std::cout << "Interest timed out for Name: " << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050091 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050092 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050093 return;
94 }
akmhoque157b0a42014-05-13 00:26:37 -050095 ndn::Name neighbor = interestName.getPrefix(-2);
akmhoque31d1d4b2014-05-05 22:08:14 -050096 std::cout << "Neighbor: " << neighbor << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050097 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -050098 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
99 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
100 uint32_t infoIntTimedOutCount =
101 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
102 std::cout << "Neighbor: " << neighbor << std::endl;
103 std::cout << "Status: " << status << std::endl;
104 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500105 _LOG_DEBUG("Status: " << status);
106 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
akmhoque157b0a42014-05-13 00:26:37 -0500107 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500108 ndn::Name interestName(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500109 interestName.append(INFO_COMPONENT);
110 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500111 expressInterest(interestName,
112 m_nlsr.getConfParameter().getInterestResendTime());
113 }
114 else if ((status == 1) &&
akmhoque157b0a42014-05-13 00:26:37 -0500115 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500116 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0);
117 m_nlsr.incrementAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500118 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500119 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500120 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 // event here
122 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
123 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
124 &m_nlsr.getLsdb()));
125 }
126 }
127}
128
129
130void
131HelloProtocol::processContent(const ndn::Interest& interest,
132 const ndn::Data& data)
133{
134 ndn::Name dataName = data.getName();
135 std::cout << "Data received for name: " << dataName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500136 _LOG_DEBUG("Data received for name: " << dataName);
akmhoque157b0a42014-05-13 00:26:37 -0500137 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
138 ndn::Name neighbor = dataName.getPrefix(-3);
akmhoque31d1d4b2014-05-05 22:08:14 -0500139 int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
140 int infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
141 neighbor);
142 //debugging purpose start
143 std::cout << "Before Updates: " << std::endl;
144 std::cout << "Neighbor : " << neighbor << std::endl;
145 std::cout << "Status: " << oldStatus << std::endl;
146 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
147 //debugging purpose end
148 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1);
149 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
150 int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
151 infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
152 neighbor);
153 //debugging purpose
154 std::cout << "After Updates: " << std::endl;
155 std::cout << "Neighbor : " << neighbor << std::endl;
156 std::cout << "Status: " << newStatus << std::endl;
157 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500158 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque31d1d4b2014-05-05 22:08:14 -0500159 //debugging purpose end
akmhoque157b0a42014-05-13 00:26:37 -0500160 // change in Adjacency list
161 if ((oldStatus - newStatus) != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500162 m_nlsr.incrementAdjBuildCount();
163 /* Need to schedule event for Adjacency LSA building */
akmhoque157b0a42014-05-13 00:26:37 -0500164 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500165 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500166 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500167 // event here
168 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
169 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
akmhoque157b0a42014-05-13 00:26:37 -0500170 ndn::ref(m_nlsr.getLsdb())));
akmhoque31d1d4b2014-05-05 22:08:14 -0500171 }
172 }
173 }
174}
175
176} //namespace nlsr