blob: ce1f3918350e9a026306eb76d43c9dc454af737d [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{
akmhoque674b0b12014-05-20 14:33:28 -050038 _LOG_DEBUG("Expressing Interest :" << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -050039 ndn::Interest i(interestName);
40 i.setInterestLifetime(ndn::time::seconds(seconds));
41 i.setMustBeFresh(true);
42 m_nlsr.getNlsrFace().expressInterest(i,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070043 ndn::bind(&HelloProtocol::onContent,
akmhoque31d1d4b2014-05-05 22:08:14 -050044 this,
45 _1, _2),
46 ndn::bind(&HelloProtocol::processInterestTimedOut,
47 this, _1));
48}
49
50void
51HelloProtocol::sendScheduledInterest(uint32_t seconds)
52{
53 std::list<Adjacent> adjList = m_nlsr.getAdjacencyList().getAdjList();
54 for (std::list<Adjacent>::iterator it = adjList.begin(); it != adjList.end();
akmhoque157b0a42014-05-13 00:26:37 -050055 ++it) {
akmhoque31d1d4b2014-05-05 22:08:14 -050056 ndn::Name interestName = (*it).getName() ;
akmhoque157b0a42014-05-13 00:26:37 -050057 interestName.append(INFO_COMPONENT);
58 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -050059 expressInterest(interestName,
60 m_nlsr.getConfParameter().getInterestResendTime());
61 }
62 scheduleInterest(m_nlsr.getConfParameter().getInfoInterestInterval());
63}
64
65void
66HelloProtocol::scheduleInterest(uint32_t seconds)
67{
68 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(seconds),
69 ndn::bind(&HelloProtocol::sendScheduledInterest,
70 this, seconds));
71}
72
73void
74HelloProtocol::processInterest(const ndn::Name& name,
75 const ndn::Interest& interest)
76{
77 const ndn::Name interestName = interest.getName();
akmhoque674b0b12014-05-20 14:33:28 -050078 _LOG_DEBUG("Interest Received for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -050079 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -050080 return;
81 }
akmhoque157b0a42014-05-13 00:26:37 -050082 ndn::Name neighbor;
83 neighbor.wireDecode(interestName.get(-1).blockFromValue());
akmhoque674b0b12014-05-20 14:33:28 -050084 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050085 if (m_nlsr.getAdjacencyList().isNeighbor(neighbor)) {
akmhoque31d1d4b2014-05-05 22:08:14 -050086 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
87 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
akmhoque157b0a42014-05-13 00:26:37 -050088 data.setContent(reinterpret_cast<const uint8_t*>(INFO_COMPONENT.c_str()),
89 INFO_COMPONENT.size());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -070090 m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName());
akmhoque674b0b12014-05-20 14:33:28 -050091 _LOG_DEBUG("Sending out data for name: " << data.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -050092 m_nlsr.getNlsrFace().put(data);
93 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050094 if (status == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050095 ndn::Name interestName(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050096 interestName.append(INFO_COMPONENT);
akmhoqued57f3672014-06-10 10:41:32 -050097 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -050098 expressInterest(interestName,
99 m_nlsr.getConfParameter().getInterestResendTime());
100 }
101 }
102}
103
104void
105HelloProtocol::processInterestTimedOut(const ndn::Interest& interest)
106{
107 const ndn::Name interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500108 _LOG_DEBUG("Interest timed out for Name: " << interestName);
akmhoque157b0a42014-05-13 00:26:37 -0500109 if (interestName.get(-2).toUri() != INFO_COMPONENT) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500110 return;
111 }
akmhoque157b0a42014-05-13 00:26:37 -0500112 ndn::Name neighbor = interestName.getPrefix(-2);
akmhoque674b0b12014-05-20 14:33:28 -0500113 _LOG_DEBUG("Neighbor: " << neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 m_nlsr.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
115 int status = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
116 uint32_t infoIntTimedOutCount =
117 m_nlsr.getAdjacencyList().getTimedOutInterestCount(neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500118 _LOG_DEBUG("Status: " << status);
119 _LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
akmhoque157b0a42014-05-13 00:26:37 -0500120 if ((infoIntTimedOutCount < m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 ndn::Name interestName(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500122 interestName.append(INFO_COMPONENT);
123 interestName.append(m_nlsr.getConfParameter().getRouterPrefix().wireEncode());
akmhoque31d1d4b2014-05-05 22:08:14 -0500124 expressInterest(interestName,
125 m_nlsr.getConfParameter().getInterestResendTime());
126 }
127 else if ((status == 1) &&
akmhoque157b0a42014-05-13 00:26:37 -0500128 (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 0);
130 m_nlsr.incrementAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500131 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500132 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500133 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500134 // event here
135 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
136 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
137 &m_nlsr.getLsdb()));
138 }
139 }
140}
141
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700142void
143HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
144{
145 m_nlsr.getValidator().validate(data,
146 ndn::bind(&HelloProtocol::onContentValidated, this, _1),
147 ndn::bind(&HelloProtocol::onContentValidationFailed,
148 this, _1, _2));
149}
akmhoque31d1d4b2014-05-05 22:08:14 -0500150
151void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700152HelloProtocol::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500153{
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700154 ndn::Name dataName = data->getName();
akmhoque674b0b12014-05-20 14:33:28 -0500155 _LOG_DEBUG("Data received for name: " << dataName);
akmhoque157b0a42014-05-13 00:26:37 -0500156 if (dataName.get(-3).toUri() == INFO_COMPONENT) {
157 ndn::Name neighbor = dataName.getPrefix(-3);
akmhoque31d1d4b2014-05-05 22:08:14 -0500158 int oldStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque31d1d4b2014-05-05 22:08:14 -0500159 m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, 1);
160 m_nlsr.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
161 int newStatus = m_nlsr.getAdjacencyList().getStatusOfNeighbor(neighbor);
akmhoque2f423352014-06-03 11:49:35 -0500162 _LOG_DEBUG("Neighbor : " << neighbor);
akmhoque674b0b12014-05-20 14:33:28 -0500163 _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
akmhoque157b0a42014-05-13 00:26:37 -0500164 // change in Adjacency list
165 if ((oldStatus - newStatus) != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500166 m_nlsr.incrementAdjBuildCount();
akmhoque2f423352014-06-03 11:49:35 -0500167 // Need to schedule event for Adjacency LSA building
akmhoque157b0a42014-05-13 00:26:37 -0500168 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
akmhoque674b0b12014-05-20 14:33:28 -0500169 _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
akmhoque157b0a42014-05-13 00:26:37 -0500170 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500171 // event here
172 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(5),
173 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
akmhoque157b0a42014-05-13 00:26:37 -0500174 ndn::ref(m_nlsr.getLsdb())));
akmhoque31d1d4b2014-05-05 22:08:14 -0500175 }
176 }
177 }
178}
179
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700180void
181HelloProtocol::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
182 const std::string& msg)
183{
184 _LOG_DEBUG("Validation Error: " << msg);
185}
186
akmhoque31d1d4b2014-05-05 22:08:14 -0500187} //namespace nlsr