blob: 91b68a93148670f3af2e5faa5ddf5928ef434c62 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque31d1d4b2014-05-05 22:08:14 -050023#include "nlsr.hpp"
24#include "lsdb.hpp"
25#include "hello-protocol.hpp"
26#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050027#include "logger.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050028
29namespace nlsr {
30
akmhoque674b0b12014-05-20 14:33:28 -050031INIT_LOGGER("HelloProtocol");
32
akmhoque157b0a42014-05-13 00:26:37 -050033const std::string HelloProtocol::INFO_COMPONENT="info";
34
akmhoque31d1d4b2014-05-05 22:08:14 -050035void
36HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
37{
38 std::cout << "Expressing Interest :" << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050039 _LOG_DEBUG("Expressing Interest :" << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -050040 ndn::Interest i(interestName);
41 i.setInterestLifetime(ndn::time::seconds(seconds));
42 i.setMustBeFresh(true);
43 m_nlsr.getNlsrFace().expressInterest(i,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070044 ndn::bind(&HelloProtocol::onContent,
akmhoque31d1d4b2014-05-05 22:08:14 -050045 this,
46 _1, _2),
47 ndn::bind(&HelloProtocol::processInterestTimedOut,
48 this, _1));
49}
50
51void
52HelloProtocol::sendScheduledInterest(uint32_t seconds)
53{
54 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
55 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050056 ++it) {
akmhoque31d1d4b2014-05-05 22:08:14 -050057 ndn::Name interestName = (*it).getName() ;
akmhoque157b0a42014-05-13 00:26:37 -050058 interestName.append(INFO_COMPONENT);
59 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -050060 expressInterest(interestName,
61 m_nlsr.getConfParameter().getInterestResendTime());
62 }
63 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
64}
65
66void
67HelloProtocol::scheduleInterest(uint32_t seconds)
68{
69 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
70 ndn::bind(&HelloProtocol::sendScheduledInterest,
71 this, seconds));
72}
73
74void
75HelloProtocol::processInterest(const ndn::Name& name,
76 const ndn::Interest& interest)
77{
78 const ndn::Name interestName = interest.getName();
79 std::cout << "Interest Received for Name: " << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050080 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050081 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050082 return;
83 }
akmhoque157b0a42014-05-13 00:26:37 -050084 ndn::Name neighbor;
85 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque31d1d4b2014-05-05 22:08:14 -050086 std::cout << "Neighbor: " << neighbor << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050087 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050088 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque31d1d4b2014-05-05 22:08:14 -050089 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
90 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
akmhoque157b0a42014-05-13 00:26:37 -050091 data.setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
92 INFO_COMPONENT.size());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070093 m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName());
akmhoque31d1d4b2014-05-05 22:08:14 -050094 std::cout << ">> D: " << data << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050095 _LOG_DEBUG("Sending out data for name: " << data.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -050096 m_nlsr.getNlsrFace().put(data);
97 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050098 if (status == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050099 ndn::Name interestName(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500100 interestName.append(INFO_COMPONENT);
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 interestName.append(m_nlsr.getConfParameter().getRouterPrefix());
102 expressInterest(interestName,
103 m_nlsr.getConfParameter().getInterestResendTime());
104 }
105 }
106}
107
108void
109HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
110{
111 const ndn::Name interestName(interest.getName());
112 std::cout << "Interest timed out for Name: " << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500113 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500114 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500115 return;
116 }
akmhoque157b0a42014-05-13 00:26:37 -0500117 ndn::Name neighbor = interestName.getPrefix(-2);
akmhoque31d1d4b2014-05-05 22:08:14 -0500118 std::cout << "Neighbor: " << neighbor << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500119 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500120 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
121 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
122 uint32_t infoIntTimedOutCount =
123 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
124 std::cout << "Neighbor: " << neighbor << std::endl;
125 std::cout << "Status: " << status << std::endl;
126 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500127 _LOG_DEBUG("Status: " << status);
128 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
akmhoque157b0a42014-05-13 00:26:37 -0500129 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500130 ndn::Name interestName(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500131 interestName.append(INFO_COMPONENT);
132 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500133 expressInterest(interestName,
134 m_nlsr.getConfParameter().getInterestResendTime());
135 }
136 else if ((status == 1) &&
akmhoque157b0a42014-05-13 00:26:37 -0500137 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500138 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0);
139 m_nlsr.incrementAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500140 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500141 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500142 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 // event here
144 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
145 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
146 &m_nlsr.getLsdb()));
147 }
148 }
149}
150
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700151void
152HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
153{
154 m_nlsr.getValidator().validate(data,
155 ndn::bind(&HelloProtocol::onContentValidated, this, _1),
156 ndn::bind(&HelloProtocol::onContentValidationFailed,
157 this, _1, _2));
158}
akmhoque31d1d4b2014-05-05 22:08:14 -0500159
160void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700161HelloProtocol::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500162{
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700163 ndn::Name dataName = data->getName();
akmhoque31d1d4b2014-05-05 22:08:14 -0500164 std::cout << "Data received for name: " << dataName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500165 _LOG_DEBUG("Data received for name: " << dataName);
akmhoque157b0a42014-05-13 00:26:37 -0500166 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
167 ndn::Name neighbor = dataName.getPrefix(-3);
akmhoque31d1d4b2014-05-05 22:08:14 -0500168 int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
169 int infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
170 neighbor);
171 //debugging purpose start
172 std::cout << "Before Updates: " << std::endl;
173 std::cout << "Neighbor : " << neighbor << std::endl;
174 std::cout << "Status: " << oldStatus << std::endl;
175 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
176 //debugging purpose end
177 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1);
178 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
179 int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
180 infoIntTimedOutCount = m_nlsr.getAdjacencyList().getTimedOutInterestCount(
181 neighbor);
182 //debugging purpose
183 std::cout << "After Updates: " << std::endl;
184 std::cout << "Neighbor : " << neighbor << std::endl;
185 std::cout << "Status: " << newStatus << std::endl;
186 std::cout << "Info Interest Timed out: " << infoIntTimedOutCount << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500187 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque31d1d4b2014-05-05 22:08:14 -0500188 //debugging purpose end
akmhoque157b0a42014-05-13 00:26:37 -0500189 // change in Adjacency list
190 if ((oldStatus - newStatus) != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500191 m_nlsr.incrementAdjBuildCount();
192 /* Need to schedule event for Adjacency LSA building */
akmhoque157b0a42014-05-13 00:26:37 -0500193 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500194 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500195 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500196 // event here
197 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
198 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
akmhoque157b0a42014-05-13 00:26:37 -0500199 ndn::ref(m_nlsr.getLsdb())));
akmhoque31d1d4b2014-05-05 22:08:14 -0500200 }
201 }
202 }
203}
204
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700205void
206HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
207 const std::string& msg)
208{
209 _LOG_DEBUG("Validation Error: " << msg);
210}
211
akmhoque31d1d4b2014-05-05 22:08:14 -0500212} //namespace nlsr