blob: 16e7d1a9815a620889dbc79c6a4a51c2d900c5f9 [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 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <string>
24#include <utility>
akmhoque157b0a42014-05-13 00:26:37 -050025
akmhoque53353462014-04-22 08:43:45 -050026#include "lsdb.hpp"
27#include "nlsr.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050028#include "conf-parameter.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050029#include "utility/name-helper.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050030#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050031
32namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("Lsdb");
35
akmhoque53353462014-04-22 08:43:45 -050036using namespace std;
37
38void
akmhoque31d1d4b2014-05-05 22:08:14 -050039Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -050040{
akmhoque31d1d4b2014-05-05 22:08:14 -050041 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050042}
43
44static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050045nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050046{
47 return nlsa1.getKey() == key;
48}
49
50
51bool
akmhoque31d1d4b2014-05-05 22:08:14 -050052Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050053{
akmhoque31d1d4b2014-05-05 22:08:14 -050054 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
55 "name",
56 m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -050057 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -050058 m_nlsr.getNamePrefixList());
59 m_nlsr.getSequencingManager().increaseNameLsaSeq();
60 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -050061}
62
akmhoqueb6450b12014-04-24 00:01:03 -050063NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050064Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050065{
66 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
67 m_nameLsdb.end(),
68 bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -050069 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -050070 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050071 }
akmhoqueb6450b12014-04-24 00:01:03 -050072 return 0;
akmhoque53353462014-04-22 08:43:45 -050073}
74
75bool
akmhoque31d1d4b2014-05-05 22:08:14 -050076Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050077{
akmhoqueb6450b12014-04-24 00:01:03 -050078 NameLsa* nameLsaCheck = findNameLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -050079 if (nameLsaCheck != 0) {
80 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -050081 return true;
82 }
akmhoque157b0a42014-05-13 00:26:37 -050083 else {
akmhoque53353462014-04-22 08:43:45 -050084 return false;
85 }
86 }
87 return true;
88}
89
90ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -050091Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
92 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -050093{
akmhoquec7a79b22014-05-26 08:06:19 -050094 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -050095 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
96 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -050097}
98
99bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500100Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500101{
akmhoquec7a79b22014-05-26 08:06:19 -0500102 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -0500103 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500104 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500105 addNameLsa(nlsa);
akmhoque2f423352014-06-03 11:49:35 -0500106 _LOG_DEBUG("New Name LSA");
107 _LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500108 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500109
akmhoque157b0a42014-05-13 00:26:37 -0500110 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500111 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
112 nlsa.getOrigRouter());
113 std::list<ndn::Name> nameList = nlsa.getNpl().getNameList();
114 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -0500115 it++) {
116 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500117 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500118 }
119 }
120 }
akmhoque157b0a42014-05-13 00:26:37 -0500121 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500122 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
123 ndn::time::system_clock::now();
124 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500125 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500127 nlsa.getLsSeqNo(),
128 timeToExpire));
129 }
akmhoque157b0a42014-05-13 00:26:37 -0500130 else {
131 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500132 _LOG_DEBUG("Updated Name LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500133 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500134 chkNameLsa->writeLog();
135 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500136 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500137 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500138 nlsa.getNpl().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -0500139 std::list<ndn::Name> nameToAdd;
akmhoque53353462014-04-22 08:43:45 -0500140 std::set_difference(nlsa.getNpl().getNameList().begin(),
141 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500142 chkNameLsa->getNpl().getNameList().begin(),
143 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500144 std::inserter(nameToAdd, nameToAdd.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500145 for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500146 it != nameToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500147 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500148 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
149 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500151 }
152 }
153 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500154 std::list<ndn::Name> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500155 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
156 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500157 nlsa.getNpl().getNameList().begin(),
158 nlsa.getNpl().getNameList().end(),
159 std::inserter(nameToRemove, nameToRemove.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500160 for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500161 it != nameToRemove.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500162 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500163 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
164 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500165 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500166 }
167 }
168 }
akmhoque157b0a42014-05-13 00:26:37 -0500169 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500170 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
171 ndn::time::system_clock::now();
172 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500173 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500174 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
175 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500176 nlsa.getLsSeqNo(),
177 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500178 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500179 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500180 }
181 }
182 return true;
183}
184
185bool
186Lsdb::addNameLsa(NameLsa& nlsa)
187{
188 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
189 m_nameLsdb.end(),
190 bind(nameLsaCompareByKey, _1,
191 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500192 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500193 m_nameLsdb.push_back(nlsa);
194 return true;
195 }
196 return false;
197}
198
199bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500200Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500201{
202 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
203 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500204 ndn::bind(nameLsaCompareByKey, _1, key));
205 if (it != m_nameLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500206 _LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500207 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500208 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500209 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500210 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
211 (*it).getOrigRouter());
212 for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500213 nit != (*it).getNpl().getNameList().end(); ++nit) {
214 if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500215 m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500216 }
217 }
218 }
219 m_nameLsdb.erase(it);
220 return true;
221 }
222 return false;
223}
224
225bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500226Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500227{
228 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
229 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500230 ndn::bind(nameLsaCompareByKey, _1, key));
231 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500232 return false;
233 }
234 return true;
235}
236
237void
akmhoque2f423352014-06-03 11:49:35 -0500238Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500239{
akmhoque2f423352014-06-03 11:49:35 -0500240 _LOG_DEBUG("---------------Name LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500241 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500242 it != m_nameLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500243 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500244 }
245}
246
247// Cor LSA and LSDB related Functions start here
248
akmhoque53353462014-04-22 08:43:45 -0500249static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500250corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500251{
252 return clsa.getKey() == key;
253}
254
255bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500256Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500257{
akmhoque31d1d4b2014-05-05 22:08:14 -0500258 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
259 "coordinate",
260 m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500261 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500262 m_nlsr.getConfParameter().getCorR(),
263 m_nlsr.getConfParameter().getCorTheta());
264 m_nlsr.getSequencingManager().increaseCorLsaSeq();
265 installCoordinateLsa(corLsa);
akmhoque53353462014-04-22 08:43:45 -0500266 return true;
267}
268
akmhoqueb6450b12014-04-24 00:01:03 -0500269CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500270Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500271{
akmhoqueb6450b12014-04-24 00:01:03 -0500272 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
273 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500274 ndn::bind(corLsaCompareByKey, _1, key));
275 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500276 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500277 }
akmhoqueb6450b12014-04-24 00:01:03 -0500278 return 0;
akmhoque53353462014-04-22 08:43:45 -0500279}
280
281bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500282Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500283{
akmhoqueb6450b12014-04-24 00:01:03 -0500284 CoordinateLsa* clsa = findCoordinateLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500285 if (clsa != 0) {
286 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500287 return true;
288 }
akmhoque157b0a42014-05-13 00:26:37 -0500289 else {
akmhoque53353462014-04-22 08:43:45 -0500290 return false;
291 }
292 }
293 return true;
294}
295
296ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500297Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500298 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500299{
akmhoquec7a79b22014-05-26 08:06:19 -0500300 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -0500301 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
302 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500303}
304
305bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500306Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500307{
akmhoquec7a79b22014-05-26 08:06:19 -0500308 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -0500309 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500310 if (chkCorLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500311 _LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500312 _LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500313 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500314 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500315
akmhoque157b0a42014-05-13 00:26:37 -0500316 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500317 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
318 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500319 }
akmhoque157b0a42014-05-13 00:26:37 -0500320 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500321 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500322 }
akmhoque157b0a42014-05-13 00:26:37 -0500323 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500324 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
325 ndn::time::system_clock::now();
326 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500327 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500328 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500329 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500330 }
akmhoque157b0a42014-05-13 00:26:37 -0500331 else {
332 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500333 _LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500334 _LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500335 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500336 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500337 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500338 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500339 chkCorLsa->setCorRadius(clsa.getCorRadius());
340 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500341 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500342 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500343 }
344 }
akmhoque157b0a42014-05-13 00:26:37 -0500345 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500346 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
347 ndn::time::system_clock::now();
348 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500349 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500350 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
351 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500352 clsa.getLsSeqNo(),
353 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500354 _LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500355 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500356 }
357 }
358 return true;
359}
360
361bool
akmhoqueb6450b12014-04-24 00:01:03 -0500362Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500363{
akmhoqueb6450b12014-04-24 00:01:03 -0500364 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
365 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500366 ndn::bind(corLsaCompareByKey, _1,
367 clsa.getKey()));
368 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500369 m_corLsdb.push_back(clsa);
370 return true;
371 }
372 return false;
373}
374
375bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500376Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500377{
akmhoqueb6450b12014-04-24 00:01:03 -0500378 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
379 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500380 ndn::bind(corLsaCompareByKey,
381 _1, key));
382 if (it != m_corLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500383 _LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500384 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500385 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500386 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500387 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
388 (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500389 }
390 m_corLsdb.erase(it);
391 return true;
392 }
393 return false;
394}
395
396bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500397Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500398{
akmhoqueb6450b12014-04-24 00:01:03 -0500399 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
400 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500401 ndn::bind(corLsaCompareByKey,
402 _1, key));
403 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500404 return false;
405 }
406 return true;
407}
408
409void
akmhoque2f423352014-06-03 11:49:35 -0500410Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500411{
akmhoque2f423352014-06-03 11:49:35 -0500412 _LOG_DEBUG("---------------Cor LSDB-------------------");
akmhoqueb6450b12014-04-24 00:01:03 -0500413 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500414 it != m_corLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500415 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500416 }
417}
418
akmhoque53353462014-04-22 08:43:45 -0500419// Adj LSA and LSDB related function starts here
420
421static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500422adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500423{
424 return alsa.getKey() == key;
425}
426
akmhoque53353462014-04-22 08:43:45 -0500427void
akmhoque31d1d4b2014-05-05 22:08:14 -0500428Lsdb::scheduledAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500429{
akmhoque674b0b12014-05-20 14:33:28 -0500430 _LOG_DEBUG("scheduledAdjLsaBuild Called");
431 m_nlsr.setIsBuildAdjLsaSheduled(false);
akmhoque157b0a42014-05-13 00:26:37 -0500432 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500433 int adjBuildCount = m_nlsr.getAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500434 if (adjBuildCount > 0) {
435 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500436 _LOG_DEBUG("Building and installing Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500437 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500438 }
akmhoque157b0a42014-05-13 00:26:37 -0500439 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500440 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
441 key.append("adjacency");
442 removeAdjLsa(key);
443 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500444 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500445 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500446 }
447 }
akmhoque157b0a42014-05-13 00:26:37 -0500448 else {
akmhoque674b0b12014-05-20 14:33:28 -0500449 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500450 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
451 m_nlsr.getConfParameter().getInterestResendTime();
452 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
453 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
454 this));
akmhoque53353462014-04-22 08:43:45 -0500455 }
456}
457
458
459bool
460Lsdb::addAdjLsa(AdjLsa& alsa)
461{
462 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
463 m_adjLsdb.end(),
464 bind(adjLsaCompareByKey, _1,
465 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500466 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500467 m_adjLsdb.push_back(alsa);
468 return true;
469 }
470 return false;
471}
472
akmhoqueb6450b12014-04-24 00:01:03 -0500473AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500474Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500475{
476 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
477 m_adjLsdb.end(),
478 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500479 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500480 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500481 }
akmhoqueb6450b12014-04-24 00:01:03 -0500482 return 0;
akmhoque53353462014-04-22 08:43:45 -0500483}
484
485
486bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500487Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500488{
akmhoqueb6450b12014-04-24 00:01:03 -0500489 AdjLsa* adjLsaCheck = findAdjLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500490 if (adjLsaCheck != 0) {
491 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500492 return true;
493 }
akmhoque157b0a42014-05-13 00:26:37 -0500494 else {
akmhoque53353462014-04-22 08:43:45 -0500495 return false;
496 }
497 }
498 return true;
499}
500
501
502ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500503Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
504 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500505{
akmhoquec7a79b22014-05-26 08:06:19 -0500506 return m_nlsr.getScheduler().scheduleEvent(expTime,
akmhoque31d1d4b2014-05-05 22:08:14 -0500507 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
508 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500509}
510
511bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500512Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500513{
akmhoquec7a79b22014-05-26 08:06:19 -0500514 ndn::time::seconds timeToExpire = ndn::time::seconds(m_lsaRefreshTime);
akmhoqueb6450b12014-04-24 00:01:03 -0500515 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500516 if (chkAdjLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500517 _LOG_DEBUG("New Adj LSA. Adding to LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500518 _LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500519 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500520 addAdjLsa(alsa);
akmhoque31d1d4b2014-05-05 22:08:14 -0500521 alsa.addNptEntries(m_nlsr);
522 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500523 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500524 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
525 ndn::time::system_clock::now();
526 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500527 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500528 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500529 alsa.getLsSeqNo(), timeToExpire);
530 }
akmhoque157b0a42014-05-13 00:26:37 -0500531 else {
532 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500533 _LOG_DEBUG("Updated Adj LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500534 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500535 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500536 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500537 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500538 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500539 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500540 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500541 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500542 }
akmhoque157b0a42014-05-13 00:26:37 -0500543 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500544 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
545 ndn::time::system_clock::now();
546 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500547 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500548 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
549 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500550 alsa.getLsSeqNo(),
551 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500552 _LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500553 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500554 }
555 }
556 return true;
557}
558
559bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500560Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500561{
akmhoque31d1d4b2014-05-05 22:08:14 -0500562 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
563 "adjacency",
564 m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500565 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500566 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
567 m_nlsr.getAdjacencyList());
568 m_nlsr.getSequencingManager().increaseAdjLsaSeq();
569 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500570 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500571 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
572 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
573 lsaPrefix);
574 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500575}
576
577bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500578Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500579{
580 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
581 m_adjLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500582 ndn::bind(adjLsaCompareByKey, _1, key));
583 if (it != m_adjLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500584 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500585 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500586 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500587 m_adjLsdb.erase(it);
588 return true;
589 }
590 return false;
591}
592
593bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500594Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500595{
596 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
597 m_adjLsdb.end(),
598 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500599 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500600 return false;
601 }
602 return true;
603}
604
605std::list<AdjLsa>&
606Lsdb::getAdjLsdb()
607{
608 return m_adjLsdb;
609}
610
611void
612Lsdb::setLsaRefreshTime(int lrt)
613{
614 m_lsaRefreshTime = lrt;
615}
616
617void
618Lsdb::setThisRouterPrefix(string trp)
619{
620 m_thisRouterPrefix = trp;
621}
622
623void
akmhoque31d1d4b2014-05-05 22:08:14 -0500624Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500625{
akmhoque674b0b12014-05-20 14:33:28 -0500626 _LOG_DEBUG("Lsdb::exprireOrRefreshNameLsa Called");
627 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500628 NameLsa* chkNameLsa = findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500629 if (chkNameLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500630 _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500631 if (chkNameLsa->getLsSeqNo() == seqNo) {
632 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500633 _LOG_DEBUG("Own Name LSA, so refreshing it");
634 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500635 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500636 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500637 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500638 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500639 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500640 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500641 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500642 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500643 chkNameLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500644 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500645 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500646 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500647 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
648 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
649 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500650 }
akmhoque157b0a42014-05-13 00:26:37 -0500651 else {
akmhoque674b0b12014-05-20 14:33:28 -0500652 _LOG_DEBUG("Other's Name LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500653 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500654 }
655 }
656 }
657}
658
659void
akmhoque31d1d4b2014-05-05 22:08:14 -0500660Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500661{
akmhoque674b0b12014-05-20 14:33:28 -0500662 _LOG_DEBUG("Lsdb::exprireOrRefreshAdjLsa Called");
663 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500664 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500665 if (chkAdjLsa != 0) {
akmhoque2f423352014-06-03 11:49:35 -0500666 _LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500667 if (chkAdjLsa->getLsSeqNo() == seqNo) {
668 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500669 _LOG_DEBUG("Own Adj LSA, so refreshing it");
670 _LOG_DEBUG("Deleting Adj Lsa");
671 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500672 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500673 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500674 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500675 _LOG_DEBUG("Adding Adj Lsa");
676 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500677 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500678 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500679 chkAdjLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500680 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500681 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500682 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500683 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
684 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
685 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500686 }
akmhoque157b0a42014-05-13 00:26:37 -0500687 else {
akmhoque674b0b12014-05-20 14:33:28 -0500688 _LOG_DEBUG("Other's Adj LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500689 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500690 }
691 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500692 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500693 }
694 }
695}
696
697void
akmhoque31d1d4b2014-05-05 22:08:14 -0500698Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500699 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500700{
akmhoque674b0b12014-05-20 14:33:28 -0500701 _LOG_DEBUG("Lsdb::exprireOrRefreshCorLsa Called ");
702 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500703 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500704 if (chkCorLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500705 _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500706 if (chkCorLsa->getLsSeqNo() == seqNo) {
707 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500708 _LOG_DEBUG("Own Cor LSA, so refreshing it");
709 _LOG_DEBUG("Deleting Coordinate Lsa");
710 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500711 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500712 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500713 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500714 _LOG_DEBUG("Adding Coordinate Lsa");
715 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500716 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500717 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
718 chkCorLsa->getKey(),
719 chkCorLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500720 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500721 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500722 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500723 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
724 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
725 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500726 }
akmhoque157b0a42014-05-13 00:26:37 -0500727 else {
akmhoque674b0b12014-05-20 14:33:28 -0500728 _LOG_DEBUG("Other's Cor LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500729 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500730 }
akmhoque157b0a42014-05-13 00:26:37 -0500731 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500732 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500733 }
734 }
735 }
736}
737
738
739void
akmhoque06986672014-05-27 13:55:53 -0500740Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime,
741 uint32_t timeoutCount)
akmhoque31d1d4b2014-05-05 22:08:14 -0500742{
akmhoque674b0b12014-05-20 14:33:28 -0500743 _LOG_DEBUG("Expressing Interest for LSA(name): " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500744 ndn::Interest interest(interestName);
745 interest.setInterestLifetime(ndn::time::seconds(interestLifeTime));
746 interest.setMustBeFresh(true);
747 m_nlsr.getNlsrFace().expressInterest(interest,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700748 ndn::bind(&Lsdb::onContent,
akmhoque31d1d4b2014-05-05 22:08:14 -0500749 this, _1, _2),
750 ndn::bind(&Lsdb::processInterestTimedOut,
akmhoque06986672014-05-27 13:55:53 -0500751 this, _1, timeoutCount));
akmhoque31d1d4b2014-05-05 22:08:14 -0500752}
753
754void
755Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
756{
757 const ndn::Name& intName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500758 _LOG_DEBUG("Interest recevied for LSA(name): " << intName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500759 string chkString("LSA");
760 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(),
761 chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500762 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500763 std::string interestedLsType;
764 uint64_t interestedLsSeqNo;
765 ndn::Name origRouter = intName.getSubName(lsaPosition + 1,
766 interest.getName().size() - lsaPosition - 3);
akmhoque157b0a42014-05-13 00:26:37 -0500767 interestedLsType = intName[-2].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500768 interestedLsSeqNo = intName[-1].toNumber();
akmhoque157b0a42014-05-13 00:26:37 -0500769 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500770 processInterestForNameLsa(interest,
771 origRouter.append(interestedLsType),
772 interestedLsSeqNo);
773 return;
774 }
akmhoque157b0a42014-05-13 00:26:37 -0500775 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500776 processInterestForAdjacencyLsa(interest,
777 origRouter.append(interestedLsType),
778 interestedLsSeqNo);
779 return;
780 }
akmhoque157b0a42014-05-13 00:26:37 -0500781 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500782 processInterestForCoordinateLsa(interest,
783 origRouter.append(interestedLsType),
784 interestedLsSeqNo);
785 return;
786 }
akmhoque157b0a42014-05-13 00:26:37 -0500787 else {
akmhoque2f423352014-06-03 11:49:35 -0500788 _LOG_DEBUG("Unrecognized LSA Type :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500789 }
790 }
791}
792
793void
794Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
795 const ndn::Name& lsaKey,
796 uint32_t interestedlsSeqNo)
797{
798 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500799 if (nameLsa != 0) {
800 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500801 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500802 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500803 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
804 std::string content = nameLsa->getData();
805 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
806 content.size());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700807 m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500808 m_nlsr.getNlsrFace().put(data);
809 }
810 }
811}
812
813void
814Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
815 const ndn::Name& lsaKey,
816 uint32_t interestedlsSeqNo)
817{
818 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500819 if (adjLsa != 0) {
820 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500821 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500822 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500823 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
824 std::string content = adjLsa->getData();
825 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
826 content.size());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700827 m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500828 m_nlsr.getNlsrFace().put(data);
829 }
830 }
831}
832
833void
834Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
835 const ndn::Name& lsaKey,
836 uint32_t interestedlsSeqNo)
837{
838 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500839 if (corLsa != 0) {
840 if (corLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500841 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500842 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500843 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
844 std::string content = corLsa->getData();
845 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
846 content.size());
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700847 m_nlsr.getKeyChain().sign(data, m_nlsr.getDefaultCertName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500848 m_nlsr.getNlsrFace().put(data);
849 }
850 }
851}
852
853void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700854Lsdb::onContent(const ndn::Interest& interest, const ndn::Data& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500855{
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700856 m_nlsr.getValidator().validate(data,
857 ndn::bind(&Lsdb::onContentValidated, this, _1),
858 ndn::bind(&Lsdb::onContentValidationFailed, this, _1, _2));
859
860}
861
862void
863Lsdb::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
864{
865 const ndn::Name& dataName = data->getName();
akmhoque674b0b12014-05-20 14:33:28 -0500866 _LOG_DEBUG("Data received for LSA(name): " << dataName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700867 string dataContent(reinterpret_cast<const char*>(data->getContent().value()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500868 string chkString("LSA");
869 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500870 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500871 std::string interestedLsType;
872 uint64_t interestedLsSeqNo;
873 ndn::Name origRouter = dataName.getSubName(lsaPosition + 1,
874 dataName.size() - lsaPosition - 4);
akmhoque157b0a42014-05-13 00:26:37 -0500875 interestedLsType = dataName[-3].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500876 interestedLsSeqNo = dataName[-2].toNumber();
akmhoque157b0a42014-05-13 00:26:37 -0500877 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500878 processContentNameLsa(origRouter.append(interestedLsType),
879 interestedLsSeqNo, dataContent);
880 return;
881 }
akmhoque157b0a42014-05-13 00:26:37 -0500882 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500883 processContentAdjacencyLsa(origRouter.append(interestedLsType),
884 interestedLsSeqNo, dataContent);
885 return;
886 }
akmhoque157b0a42014-05-13 00:26:37 -0500887 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500888 processContentCoordinateLsa(origRouter.append(interestedLsType),
889 interestedLsSeqNo, dataContent);
890 return;
891 }
akmhoque157b0a42014-05-13 00:26:37 -0500892 else {
akmhoque2f423352014-06-03 11:49:35 -0500893 _LOG_DEBUG("Unrecognized LSA Type :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500894 }
895 }
896}
897
898void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700899Lsdb::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const std::string& msg)
900{
akmhoque2f423352014-06-03 11:49:35 -0500901 _LOG_DEBUG("Validation Error: " << msg);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700902}
903
904void
akmhoque31d1d4b2014-05-05 22:08:14 -0500905Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
906 uint32_t lsSeqNo, std::string& dataContent)
907{
akmhoque157b0a42014-05-13 00:26:37 -0500908 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500909 NameLsa nameLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500910 if (nameLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500911 installNameLsa(nameLsa);
912 }
akmhoque157b0a42014-05-13 00:26:37 -0500913 else {
akmhoque2f423352014-06-03 11:49:35 -0500914 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500915 }
916 }
917}
918
919void
920Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
921 uint32_t lsSeqNo, std::string& dataContent)
922{
akmhoque157b0a42014-05-13 00:26:37 -0500923 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500924 AdjLsa adjLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500925 if (adjLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500926 installAdjLsa(adjLsa);
927 }
akmhoque157b0a42014-05-13 00:26:37 -0500928 else {
akmhoque2f423352014-06-03 11:49:35 -0500929 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500930 }
931 }
932}
933
934void
935Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
936 uint32_t lsSeqNo, std::string& dataContent)
937{
akmhoque157b0a42014-05-13 00:26:37 -0500938 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500939 CoordinateLsa corLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500940 if (corLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500941 installCoordinateLsa(corLsa);
942 }
akmhoque157b0a42014-05-13 00:26:37 -0500943 else {
akmhoque2f423352014-06-03 11:49:35 -0500944 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500945 }
946 }
947}
948
949void
akmhoque06986672014-05-27 13:55:53 -0500950Lsdb::processInterestTimedOut(const ndn::Interest& interest, uint32_t timeoutCount)
akmhoque31d1d4b2014-05-05 22:08:14 -0500951{
952 const ndn::Name& interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500953 _LOG_DEBUG("Interest timed out for LSA(name): " << interestName);
akmhoque06986672014-05-27 13:55:53 -0500954 if ((timeoutCount + 1) <= m_nlsr.getConfParameter().getInterestRetryNumber()) {
955 _LOG_DEBUG("Interest timeoutCount: " << (timeoutCount + 1));
956 _LOG_DEBUG("Need to express interest again for LSA(name): " << interestName);
957 expressInterest(interestName,
958 m_nlsr.getConfParameter().getInterestResendTime(),
959 timeoutCount + 1);
960 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500961}
962
akmhoquec7a79b22014-05-26 08:06:19 -0500963ndn::time::system_clock::TimePoint
964Lsdb::getLsaExpirationTimePoint()
965{
966 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
967 expirationTimePoint = expirationTimePoint +
968 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
969 return expirationTimePoint;
970}
akmhoque31d1d4b2014-05-05 22:08:14 -0500971
972void
akmhoque2f423352014-06-03 11:49:35 -0500973Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500974{
akmhoque2f423352014-06-03 11:49:35 -0500975 _LOG_DEBUG("---------------Adj LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500976 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500977 it != m_adjLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500978 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500979 }
980}
981
982//-----utility function -----
983bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500984Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -0500985{
akmhoque157b0a42014-05-13 00:26:37 -0500986 if (lsType == "name") {
akmhoque53353462014-04-22 08:43:45 -0500987 return doesNameLsaExist(key);
988 }
akmhoque157b0a42014-05-13 00:26:37 -0500989 else if (lsType == "adjacency") {
akmhoque53353462014-04-22 08:43:45 -0500990 return doesAdjLsaExist(key);
991 }
akmhoque157b0a42014-05-13 00:26:37 -0500992 else if (lsType == "coordinate") {
akmhoqueb6450b12014-04-24 00:01:03 -0500993 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -0500994 }
995 return false;
996}
997
998}//namespace nlsr