blob: 1cd86ae19d67410f7943f08b9eccd373bbc1f1e4 [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
akmhoque93f1a072014-06-19 16:24:28 -050033const std::string HelloProtocol::INFO_COMPONENT = "INFO";
34const std::string HelloProtocol::NLSR_COMPONENT = "NLSR";
akmhoque157b0a42014-05-13 00:26:37 -050035
akmhoque31d1d4b2014-05-05 22:08:14 -050036void
37HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
38{
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) {
akmhoque93f1a072014-06-19 16:24:28 -050057 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -050058 ndn::Name interestName = (*it).getName() ;
akmhoque93f1a072014-06-19 16:24:28 -050059 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -050060 interestName.append(INFO_COMPONENT);
61 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -050062 expressInterest(interestName,
63 m_nlsr.getConfParameter().getInterestResendTime());
64 }
65 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
66}
67
68void
69HelloProtocol::scheduleInterest(uint32_t seconds)
70{
71 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
72 ndn::bind(&HelloProtocol::sendScheduledInterest,
73 this, seconds));
74}
75
76void
77HelloProtocol::processInterest(const ndn::Name& name,
78 const ndn::Interest& interest)
79{
akmhoque93f1a072014-06-19 16:24:28 -050080 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -050081 const ndn::Name interestName = interest.getName();
akmhoque674b0b12014-05-20 14:33:28 -050082 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050083 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050084 return;
85 }
akmhoque157b0a42014-05-13 00:26:37 -050086 ndn::Name neighbor;
87 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque674b0b12014-05-20 14:33:28 -050088 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050089 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque31d1d4b2014-05-05 22:08:14 -050090 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
91 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
akmhoque157b0a42014-05-13 00:26:37 -050092 data.setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
93 INFO_COMPONENT.size());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070094 m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName());
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) {
akmhoque93f1a072014-06-19 16:24:28 -050099 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500101 interestName.append(NLSR_COMPONENT);
akmhoque157b0a42014-05-13 00:26:37 -0500102 interestName.append(INFO_COMPONENT);
akmhoqued57f3672014-06-10 10:41:32 -0500103 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 expressInterest(interestName,
105 m_nlsr.getConfParameter().getInterestResendTime());
106 }
107 }
108}
109
110void
111HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
112{
akmhoque93f1a072014-06-19 16:24:28 -0500113 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 const ndn::Name interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500115 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500116 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500117 return;
118 }
akmhoque93f1a072014-06-19 16:24:28 -0500119 ndn::Name neighbor = interestName.getPrefix(-3);
akmhoque674b0b12014-05-20 14:33:28 -0500120 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
122 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
123 uint32_t infoIntTimedOutCount =
124 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500125 _LOG_DEBUG("Status: " << status);
126 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
akmhoque157b0a42014-05-13 00:26:37 -0500127 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque93f1a072014-06-19 16:24:28 -0500128 /* interest name: /<neighbor>/NLSR/INFO/<router> */
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 ndn::Name interestName(neighbor);
akmhoque93f1a072014-06-19 16:24:28 -0500130 interestName.append(NLSR_COMPONENT);
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{
akmhoque93f1a072014-06-19 16:24:28 -0500163 /* data name: /<neighbor>/NLSR/INFO/<router>/<version> */
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700164 ndn::Name dataName = data->getName();
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) {
akmhoque93f1a072014-06-19 16:24:28 -0500167 ndn::Name neighbor = dataName.getPrefix(-4);
akmhoque31d1d4b2014-05-05 22:08:14 -0500168 int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500169 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1);
170 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
171 int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque2f423352014-06-03 11:49:35 -0500172 _LOG_DEBUG("Neighbor : " << neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500173 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500174 // change in Adjacency list
175 if ((oldStatus - newStatus) != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500176 m_nlsr.incrementAdjBuildCount();
akmhoque2f423352014-06-03 11:49:35 -0500177 // Need to schedule event for Adjacency LSA building
akmhoque157b0a42014-05-13 00:26:37 -0500178 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500179 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500180 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500181 // event here
182 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
183 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
akmhoque157b0a42014-05-13 00:26:37 -0500184 ndn::ref(m_nlsr.getLsdb())));
akmhoque31d1d4b2014-05-05 22:08:14 -0500185 }
186 }
187 }
188}
189
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700190void
191HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
192 const std::string& msg)
193{
194 _LOG_DEBUG("Validation Error: " << msg);
195}
196
akmhoque31d1d4b2014-05-05 22:08:14 -0500197} //namespace nlsr