blob: 5076f5e69b08eb732fea21e78a044e5f421983f5 [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
akmhoque50125a92014-06-30 08:54:17 -0500570 //ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
571 //lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
akmhoque157b0a42014-05-13 00:26:37 -0500572 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500573 lsaPrefix.append(m_nlsr.getConfParameter().getSiteName());
574 lsaPrefix.append(m_nlsr.getConfParameter().getRouterName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500575 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
576 lsaPrefix);
577 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500578}
579
580bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500581Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500582{
583 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
584 m_adjLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500585 ndn::bind(adjLsaCompareByKey, _1, key));
586 if (it != m_adjLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500587 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500588 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500589 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500590 m_adjLsdb.erase(it);
591 return true;
592 }
593 return false;
594}
595
596bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500597Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500598{
599 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
600 m_adjLsdb.end(),
601 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500602 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500603 return false;
604 }
605 return true;
606}
607
608std::list<AdjLsa>&
609Lsdb::getAdjLsdb()
610{
611 return m_adjLsdb;
612}
613
614void
615Lsdb::setLsaRefreshTime(int lrt)
616{
617 m_lsaRefreshTime = lrt;
618}
619
620void
621Lsdb::setThisRouterPrefix(string trp)
622{
623 m_thisRouterPrefix = trp;
624}
625
626void
akmhoque31d1d4b2014-05-05 22:08:14 -0500627Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500628{
akmhoque674b0b12014-05-20 14:33:28 -0500629 _LOG_DEBUG("Lsdb::exprireOrRefreshNameLsa Called");
630 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500631 NameLsa* chkNameLsa = findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500632 if (chkNameLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500633 _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500634 if (chkNameLsa->getLsSeqNo() == seqNo) {
635 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500636 _LOG_DEBUG("Own Name LSA, so refreshing it");
637 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500638 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500639 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500640 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500641 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500642 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500643 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500644 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500645 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500646 chkNameLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500647 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500648 // publish routing update
akmhoque50125a92014-06-30 08:54:17 -0500649 //ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
650 //lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
akmhoque157b0a42014-05-13 00:26:37 -0500651 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500652 lsaPrefix.append(m_nlsr.getConfParameter().getSiteName());
653 lsaPrefix.append(m_nlsr.getConfParameter().getRouterName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500654 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
655 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500656 }
akmhoque157b0a42014-05-13 00:26:37 -0500657 else {
akmhoque674b0b12014-05-20 14:33:28 -0500658 _LOG_DEBUG("Other's Name LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500659 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500660 }
661 }
662 }
663}
664
665void
akmhoque31d1d4b2014-05-05 22:08:14 -0500666Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500667{
akmhoque674b0b12014-05-20 14:33:28 -0500668 _LOG_DEBUG("Lsdb::exprireOrRefreshAdjLsa Called");
669 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500670 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500671 if (chkAdjLsa != 0) {
akmhoque2f423352014-06-03 11:49:35 -0500672 _LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500673 if (chkAdjLsa->getLsSeqNo() == seqNo) {
674 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500675 _LOG_DEBUG("Own Adj LSA, so refreshing it");
676 _LOG_DEBUG("Deleting Adj Lsa");
677 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500678 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500679 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500680 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500681 _LOG_DEBUG("Adding Adj Lsa");
682 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500683 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500684 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500685 chkAdjLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500686 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500687 // publish routing update
akmhoque50125a92014-06-30 08:54:17 -0500688 //ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
689 //lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
akmhoque157b0a42014-05-13 00:26:37 -0500690 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500691 lsaPrefix.append(m_nlsr.getConfParameter().getSiteName());
692 lsaPrefix.append(m_nlsr.getConfParameter().getRouterName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500693 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
694 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500695 }
akmhoque157b0a42014-05-13 00:26:37 -0500696 else {
akmhoque674b0b12014-05-20 14:33:28 -0500697 _LOG_DEBUG("Other's Adj LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500698 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500699 }
700 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500701 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500702 }
703 }
704}
705
706void
akmhoque31d1d4b2014-05-05 22:08:14 -0500707Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500708 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500709{
akmhoque674b0b12014-05-20 14:33:28 -0500710 _LOG_DEBUG("Lsdb::exprireOrRefreshCorLsa Called ");
711 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500712 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500713 if (chkCorLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500714 _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500715 if (chkCorLsa->getLsSeqNo() == seqNo) {
716 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500717 _LOG_DEBUG("Own Cor LSA, so refreshing it");
718 _LOG_DEBUG("Deleting Coordinate Lsa");
719 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500720 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500721 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500722 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500723 _LOG_DEBUG("Adding Coordinate Lsa");
724 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500725 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500726 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
727 chkCorLsa->getKey(),
728 chkCorLsa->getLsSeqNo(),
akmhoquec7a79b22014-05-26 08:06:19 -0500729 ndn::time::seconds(m_lsaRefreshTime)));
akmhoque53353462014-04-22 08:43:45 -0500730 // publish routing update
akmhoque50125a92014-06-30 08:54:17 -0500731 //ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
732 //lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
akmhoque157b0a42014-05-13 00:26:37 -0500733 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque50125a92014-06-30 08:54:17 -0500734 lsaPrefix.append(m_nlsr.getConfParameter().getSiteName());
735 lsaPrefix.append(m_nlsr.getConfParameter().getRouterName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500736 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
737 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500738 }
akmhoque157b0a42014-05-13 00:26:37 -0500739 else {
akmhoque674b0b12014-05-20 14:33:28 -0500740 _LOG_DEBUG("Other's Cor LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500741 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500742 }
akmhoque157b0a42014-05-13 00:26:37 -0500743 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500744 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500745 }
746 }
747 }
748}
749
750
751void
akmhoque06986672014-05-27 13:55:53 -0500752Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime,
753 uint32_t timeoutCount)
akmhoque31d1d4b2014-05-05 22:08:14 -0500754{
akmhoque674b0b12014-05-20 14:33:28 -0500755 _LOG_DEBUG("Expressing Interest for LSA(name): " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500756 ndn::Interest interest(interestName);
757 interest.setInterestLifetime(ndn::time::seconds(interestLifeTime));
758 interest.setMustBeFresh(true);
759 m_nlsr.getNlsrFace().expressInterest(interest,
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700760 ndn::bind(&Lsdb::onContent,
akmhoque31d1d4b2014-05-05 22:08:14 -0500761 this, _1, _2),
762 ndn::bind(&Lsdb::processInterestTimedOut,
akmhoque06986672014-05-27 13:55:53 -0500763 this, _1, timeoutCount));
akmhoque31d1d4b2014-05-05 22:08:14 -0500764}
765
766void
767Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
768{
769 const ndn::Name& intName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500770 _LOG_DEBUG("Interest recevied for LSA(name): " << intName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500771 string chkString("LSA");
772 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(),
773 chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500774 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500775 std::string interestedLsType;
776 uint64_t interestedLsSeqNo;
akmhoque50125a92014-06-30 08:54:17 -0500777 ndn::Name origRouter = m_nlsr.getConfParameter().getNetwork();
778 origRouter.append(intName.getSubName(lsaPosition + 1,
779 interest.getName().size() - lsaPosition - 3));
akmhoque157b0a42014-05-13 00:26:37 -0500780 interestedLsType = intName[-2].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500781 interestedLsSeqNo = intName[-1].toNumber();
akmhoque157b0a42014-05-13 00:26:37 -0500782 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500783 processInterestForNameLsa(interest,
784 origRouter.append(interestedLsType),
785 interestedLsSeqNo);
786 return;
787 }
akmhoque157b0a42014-05-13 00:26:37 -0500788 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500789 processInterestForAdjacencyLsa(interest,
790 origRouter.append(interestedLsType),
791 interestedLsSeqNo);
792 return;
793 }
akmhoque157b0a42014-05-13 00:26:37 -0500794 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500795 processInterestForCoordinateLsa(interest,
796 origRouter.append(interestedLsType),
797 interestedLsSeqNo);
798 return;
799 }
akmhoque157b0a42014-05-13 00:26:37 -0500800 else {
akmhoque2f423352014-06-03 11:49:35 -0500801 _LOG_DEBUG("Unrecognized LSA Type :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500802 }
803 }
804}
805
806void
akmhoque69c9aa92014-07-23 15:15:05 -0500807Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
808{
809 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>();
810 data->setName(ndn::Name(interest.getName()).appendVersion());
811 data->setFreshnessPeriod(ndn::time::seconds(10));
812 data->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
813 m_nlsr.getKeyChain().sign(*data, m_nlsr.getDefaultCertName());
814 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
815 m_nlsr.getNlsrFace().put(*data);
816}
817
818void
akmhoque31d1d4b2014-05-05 22:08:14 -0500819Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
820 const ndn::Name& lsaKey,
821 uint32_t interestedlsSeqNo)
822{
823 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500824 if (nameLsa != 0) {
825 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500826 std::string content = nameLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500827 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500828 }
829 }
830}
831
832void
833Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
834 const ndn::Name& lsaKey,
835 uint32_t interestedlsSeqNo)
836{
837 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500838 if (adjLsa != 0) {
839 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500840 std::string content = adjLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500841 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500842 }
843 }
844}
845
846void
847Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
848 const ndn::Name& lsaKey,
849 uint32_t interestedlsSeqNo)
850{
851 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500852 if (corLsa != 0) {
853 if (corLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500854 std::string content = corLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500855 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500856 }
857 }
858}
859
860void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700861Lsdb::onContent(const ndn::Interest& interest, const ndn::Data& data)
akmhoque31d1d4b2014-05-05 22:08:14 -0500862{
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700863 m_nlsr.getValidator().validate(data,
864 ndn::bind(&Lsdb::onContentValidated, this, _1),
865 ndn::bind(&Lsdb::onContentValidationFailed, this, _1, _2));
866
867}
868
869void
870Lsdb::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
871{
872 const ndn::Name& dataName = data->getName();
akmhoque674b0b12014-05-20 14:33:28 -0500873 _LOG_DEBUG("Data received for LSA(name): " << dataName);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700874 string dataContent(reinterpret_cast<const char*>(data->getContent().value()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500875 string chkString("LSA");
876 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500877 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500878 std::string interestedLsType;
879 uint64_t interestedLsSeqNo;
akmhoque50125a92014-06-30 08:54:17 -0500880 ndn::Name origRouter = m_nlsr.getConfParameter().getNetwork();
881 origRouter.append(dataName.getSubName(lsaPosition + 1,
882 dataName.size() - lsaPosition - 4));
akmhoque157b0a42014-05-13 00:26:37 -0500883 interestedLsType = dataName[-3].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500884 interestedLsSeqNo = dataName[-2].toNumber();
akmhoque157b0a42014-05-13 00:26:37 -0500885 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500886 processContentNameLsa(origRouter.append(interestedLsType),
887 interestedLsSeqNo, dataContent);
888 return;
889 }
akmhoque157b0a42014-05-13 00:26:37 -0500890 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500891 processContentAdjacencyLsa(origRouter.append(interestedLsType),
892 interestedLsSeqNo, dataContent);
893 return;
894 }
akmhoque157b0a42014-05-13 00:26:37 -0500895 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500896 processContentCoordinateLsa(origRouter.append(interestedLsType),
897 interestedLsSeqNo, dataContent);
898 return;
899 }
akmhoque157b0a42014-05-13 00:26:37 -0500900 else {
akmhoque2f423352014-06-03 11:49:35 -0500901 _LOG_DEBUG("Unrecognized LSA Type :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500902 }
903 }
904}
905
906void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700907Lsdb::onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data, const std::string& msg)
908{
akmhoque2f423352014-06-03 11:49:35 -0500909 _LOG_DEBUG("Validation Error: " << msg);
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700910}
911
912void
akmhoque31d1d4b2014-05-05 22:08:14 -0500913Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
914 uint32_t lsSeqNo, std::string& dataContent)
915{
akmhoque157b0a42014-05-13 00:26:37 -0500916 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500917 NameLsa nameLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500918 if (nameLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500919 installNameLsa(nameLsa);
920 }
akmhoque157b0a42014-05-13 00:26:37 -0500921 else {
akmhoque2f423352014-06-03 11:49:35 -0500922 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500923 }
924 }
925}
926
927void
928Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
929 uint32_t lsSeqNo, std::string& dataContent)
930{
akmhoque157b0a42014-05-13 00:26:37 -0500931 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500932 AdjLsa adjLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500933 if (adjLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500934 installAdjLsa(adjLsa);
935 }
akmhoque157b0a42014-05-13 00:26:37 -0500936 else {
akmhoque2f423352014-06-03 11:49:35 -0500937 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500938 }
939 }
940}
941
942void
943Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
944 uint32_t lsSeqNo, std::string& dataContent)
945{
akmhoque157b0a42014-05-13 00:26:37 -0500946 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500947 CoordinateLsa corLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500948 if (corLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500949 installCoordinateLsa(corLsa);
950 }
akmhoque157b0a42014-05-13 00:26:37 -0500951 else {
akmhoque2f423352014-06-03 11:49:35 -0500952 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -0500953 }
954 }
955}
956
957void
akmhoque06986672014-05-27 13:55:53 -0500958Lsdb::processInterestTimedOut(const ndn::Interest& interest, uint32_t timeoutCount)
akmhoque31d1d4b2014-05-05 22:08:14 -0500959{
960 const ndn::Name& interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500961 _LOG_DEBUG("Interest timed out for LSA(name): " << interestName);
akmhoque06986672014-05-27 13:55:53 -0500962 if ((timeoutCount + 1) <= m_nlsr.getConfParameter().getInterestRetryNumber()) {
963 _LOG_DEBUG("Interest timeoutCount: " << (timeoutCount + 1));
964 _LOG_DEBUG("Need to express interest again for LSA(name): " << interestName);
965 expressInterest(interestName,
966 m_nlsr.getConfParameter().getInterestResendTime(),
967 timeoutCount + 1);
968 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500969}
970
akmhoquec7a79b22014-05-26 08:06:19 -0500971ndn::time::system_clock::TimePoint
972Lsdb::getLsaExpirationTimePoint()
973{
974 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
975 expirationTimePoint = expirationTimePoint +
976 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
977 return expirationTimePoint;
978}
akmhoque31d1d4b2014-05-05 22:08:14 -0500979
980void
akmhoque2f423352014-06-03 11:49:35 -0500981Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500982{
akmhoque2f423352014-06-03 11:49:35 -0500983 _LOG_DEBUG("---------------Adj LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500984 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500985 it != m_adjLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500986 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500987 }
988}
989
990//-----utility function -----
991bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500992Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -0500993{
akmhoque157b0a42014-05-13 00:26:37 -0500994 if (lsType == "name") {
akmhoque53353462014-04-22 08:43:45 -0500995 return doesNameLsaExist(key);
996 }
akmhoque157b0a42014-05-13 00:26:37 -0500997 else if (lsType == "adjacency") {
akmhoque53353462014-04-22 08:43:45 -0500998 return doesAdjLsaExist(key);
999 }
akmhoque157b0a42014-05-13 00:26:37 -05001000 else if (lsType == "coordinate") {
akmhoqueb6450b12014-04-24 00:01:03 -05001001 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -05001002 }
1003 return false;
1004}
1005
1006}//namespace nlsr