blob: 2dc106ca49668255dc2904d0bae6c0a96b390b18 [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"
5
6namespace nlsr {
7
akmhoque157b0a42014-05-13 00:26:37 -05008const std::string HelloProtocol::INFO_COMPONENT="info";
9
akmhoque31d1d4b2014-05-05 22:08:14 -050010void
11HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
12{
13 std::cout << "Expressing Interest :" << interestName << std::endl;
14 ndn::Interest i(interestName);
15 i.setInterestLifetime(ndn::time::seconds(seconds));
16 i.setMustBeFresh(true);
17 m_nlsr.getNlsrFace().expressInterest(i,
18 ndn::bind(&HelloProtocol::processContent,
19 this,
20 _1, _2),
21 ndn::bind(&HelloProtocol::processInterestTimedOut,
22 this, _1));
23}
24
25void
26HelloProtocol::sendScheduledInterest(uint32_t seconds)
27{
28 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
29 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050030 ++it) {
akmhoque31d1d4b2014-05-05 22:08:14 -050031 ndn::Name interestName = (*it).getName() ;
akmhoque157b0a42014-05-13 00:26:37 -050032 interestName.append(INFO_COMPONENT);
33 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -050034 expressInterest(interestName,
35 m_nlsr.getConfParameter().getInterestResendTime());
36 }
37 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
38}
39
40void
41HelloProtocol::scheduleInterest(uint32_t seconds)
42{
43 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
44 ndn::bind(&HelloProtocol::sendScheduledInterest,
45 this, seconds));
46}
47
48void
49HelloProtocol::processInterest(const ndn::Name& name,
50 const ndn::Interest& interest)
51{
52 const ndn::Name interestName = interest.getName();
53 std::cout << "Interest Received for Name: " << interestName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050054 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050055 return;
56 }
akmhoque157b0a42014-05-13 00:26:37 -050057 ndn::Name neighbor;
58 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque31d1d4b2014-05-05 22:08:14 -050059 std::cout << "Neighbor: " << neighbor << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050060 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque31d1d4b2014-05-05 22:08:14 -050061 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
62 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
akmhoque157b0a42014-05-13 00:26:37 -050063 data.setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
64 INFO_COMPONENT.size());
akmhoque31d1d4b2014-05-05 22:08:14 -050065 m_keyChain.sign(data);
66 std::cout << ">> D: " << data << std::endl;
67 m_nlsr.getNlsrFace().put(data);
68 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050069 if (status == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050070 ndn::Name interestName(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050071 interestName.append(INFO_COMPONENT);
akmhoque31d1d4b2014-05-05 22:08:14 -050072 interestName.append(m_nlsr.getConfParameter().getRouterPrefix());
73 expressInterest(interestName,
74 m_nlsr.getConfParameter().getInterestResendTime());
75 }
76 }
77}
78
79void
80HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
81{
82 const ndn::Name interestName(interest.getName());
83 std::cout << "Interest timed out for Name: " << interestName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050084 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050085 return;
86 }
akmhoque157b0a42014-05-13 00:26:37 -050087 ndn::Name neighbor = interestName.getPrefix(-2);
akmhoque31d1d4b2014-05-05 22:08:14 -050088 std::cout << "Neighbor: " << neighbor << std::endl;
89 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
90 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
91 uint32_t infoIntTimedOutCount =
92 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
93 std::cout << "Neighbor: " << neighbor << std::endl;
94 std::cout << "Status: " << status << std::endl;
95 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -050096 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -050097 ndn::Name interestName(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050098 interestName.append(INFO_COMPONENT);
99 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 expressInterest(interestName,
101 m_nlsr.getConfParameter().getInterestResendTime());
102 }
103 else if ((status == 1) &&
akmhoque157b0a42014-05-13 00:26:37 -0500104 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500105 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0);
106 m_nlsr.incrementAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500107 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
108 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 // event here
110 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
111 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
112 &m_nlsr.getLsdb()));
113 }
114 }
115}
116
117
118void
119HelloProtocol::processContent(const ndn::Interest& interest,
120 const ndn::Data& data)
121{
122 ndn::Name dataName = data.getName();
123 std::cout << "Data received for name: " << dataName << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500124 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
125 ndn::Name neighbor = dataName.getPrefix(-3);
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
127 int infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
128 neighbor);
129 //debugging purpose start
130 std::cout << "Before Updates: " << std::endl;
131 std::cout << "Neighbor : " << neighbor << std::endl;
132 std::cout << "Status: " << oldStatus << std::endl;
133 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
134 //debugging purpose end
135 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1);
136 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
137 int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
138 infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
139 neighbor);
140 //debugging purpose
141 std::cout << "After Updates: " << std::endl;
142 std::cout << "Neighbor : " << neighbor << std::endl;
143 std::cout << "Status: " << newStatus << std::endl;
144 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
145 //debugging purpose end
akmhoque157b0a42014-05-13 00:26:37 -0500146 // change in Adjacency list
147 if ((oldStatus - newStatus) != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500148 m_nlsr.incrementAdjBuildCount();
149 /* Need to schedule event for Adjacency LSA building */
akmhoque157b0a42014-05-13 00:26:37 -0500150 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
151 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500152 // event here
153 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
154 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
akmhoque157b0a42014-05-13 00:26:37 -0500155 ndn::ref(m_nlsr.getLsdb())));
akmhoque31d1d4b2014-05-05 22:08:14 -0500156 }
157 }
158 }
159}
160
161} //namespace nlsr