blob: 0b3486745744da08b0a8ebb659b7171205f096ee [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <string>
2#include <utility>
akmhoque157b0a42014-05-13 00:26:37 -05003
akmhoque53353462014-04-22 08:43:45 -05004#include "lsdb.hpp"
5#include "nlsr.hpp"
akmhoque157b0a42014-05-13 00:26:37 -05006#include "conf-parameter.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -05007#include "utility/name-helper.hpp"
akmhoque53353462014-04-22 08:43:45 -05008
9namespace nlsr {
10
11using namespace std;
12
13void
akmhoque31d1d4b2014-05-05 22:08:14 -050014Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -050015{
akmhoque31d1d4b2014-05-05 22:08:14 -050016 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050017}
18
19static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050020nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050021{
22 return nlsa1.getKey() == key;
23}
24
25
26bool
akmhoque31d1d4b2014-05-05 22:08:14 -050027Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050028{
akmhoque31d1d4b2014-05-05 22:08:14 -050029 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
30 "name",
31 m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
32 m_nlsr.getConfParameter().getRouterDeadInterval(),
33 m_nlsr.getNamePrefixList());
34 m_nlsr.getSequencingManager().increaseNameLsaSeq();
35 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -050036}
37
akmhoqueb6450b12014-04-24 00:01:03 -050038NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050039Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050040{
41 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
42 m_nameLsdb.end(),
43 bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -050044 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -050045 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050046 }
akmhoqueb6450b12014-04-24 00:01:03 -050047 return 0;
akmhoque53353462014-04-22 08:43:45 -050048}
49
50bool
akmhoque31d1d4b2014-05-05 22:08:14 -050051Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050052{
akmhoqueb6450b12014-04-24 00:01:03 -050053 NameLsa* nameLsaCheck = findNameLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -050054 if (nameLsaCheck != 0) {
55 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -050056 return true;
57 }
akmhoque157b0a42014-05-13 00:26:37 -050058 else {
akmhoque53353462014-04-22 08:43:45 -050059 return false;
60 }
61 }
62 return true;
63}
64
65ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -050066Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo, int expTime)
akmhoque53353462014-04-22 08:43:45 -050067{
akmhoque31d1d4b2014-05-05 22:08:14 -050068 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
69 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
70 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -050071}
72
73bool
akmhoque31d1d4b2014-05-05 22:08:14 -050074Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -050075{
76 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -050077 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -050078 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -050079 addNameLsa(nlsa);
80 nlsa.writeLog();
81 printNameLsdb();
akmhoque157b0a42014-05-13 00:26:37 -050082 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050083 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
84 nlsa.getOrigRouter());
85 std::list<ndn::Name> nameList = nlsa.getNpl().getNameList();
86 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -050087 it++) {
88 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050089 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -050090 }
91 }
92 }
akmhoque157b0a42014-05-13 00:26:37 -050093 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -050094 timeToExpire = nlsa.getLifeTime();
95 }
akmhoque31d1d4b2014-05-05 22:08:14 -050096 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -050097 nlsa.getLsSeqNo(),
98 timeToExpire));
99 }
akmhoque157b0a42014-05-13 00:26:37 -0500100 else {
101 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500102 chkNameLsa->writeLog();
103 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
104 chkNameLsa->setLifeTime(nlsa.getLifeTime());
105 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500106 nlsa.getNpl().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -0500107 std::list<ndn::Name> nameToAdd;
akmhoque53353462014-04-22 08:43:45 -0500108 std::set_difference(nlsa.getNpl().getNameList().begin(),
109 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500110 chkNameLsa->getNpl().getNameList().begin(),
111 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500112 std::inserter(nameToAdd, nameToAdd.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500113 for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500114 it != nameToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500115 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500116 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
117 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500118 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500119 }
120 }
121 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500122 std::list<ndn::Name> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500123 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
124 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500125 nlsa.getNpl().getNameList().begin(),
126 nlsa.getNpl().getNameList().end(),
127 std::inserter(nameToRemove, nameToRemove.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500128 for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500129 it != nameToRemove.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500130 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500131 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
132 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500133 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500134 }
135 }
136 }
akmhoque157b0a42014-05-13 00:26:37 -0500137 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500138 timeToExpire = nlsa.getLifeTime();
139 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500140 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
141 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500142 nlsa.getLsSeqNo(),
143 timeToExpire));
144 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500145 }
146 }
147 return true;
148}
149
150bool
151Lsdb::addNameLsa(NameLsa& nlsa)
152{
153 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
154 m_nameLsdb.end(),
155 bind(nameLsaCompareByKey, _1,
156 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500157 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500158 m_nameLsdb.push_back(nlsa);
159 return true;
160 }
161 return false;
162}
163
164bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500165Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500166{
167 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
168 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500169 ndn::bind(nameLsaCompareByKey, _1, key));
170 if (it != m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500171 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500172 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500173 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500174 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
175 (*it).getOrigRouter());
176 for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500177 nit != (*it).getNpl().getNameList().end(); ++nit) {
178 if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500179 m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500180 }
181 }
182 }
183 m_nameLsdb.erase(it);
184 return true;
185 }
186 return false;
187}
188
189bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500190Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500191{
192 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
193 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500194 ndn::bind(nameLsaCompareByKey, _1, key));
195 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500196 return false;
197 }
198 return true;
199}
200
201void
202Lsdb::printNameLsdb()
203{
204 cout << "---------------Name LSDB-------------------" << endl;
205 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500206 it != m_nameLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500207 cout << (*it) << endl;
208 }
209}
210
211// Cor LSA and LSDB related Functions start here
212
213
214static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500215corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500216{
217 return clsa.getKey() == key;
218}
219
220bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500221Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500222{
akmhoque31d1d4b2014-05-05 22:08:14 -0500223 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
224 "coordinate",
225 m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
226 m_nlsr.getConfParameter().getRouterDeadInterval(),
227 m_nlsr.getConfParameter().getCorR(),
228 m_nlsr.getConfParameter().getCorTheta());
229 m_nlsr.getSequencingManager().increaseCorLsaSeq();
230 installCoordinateLsa(corLsa);
akmhoque53353462014-04-22 08:43:45 -0500231 return true;
232}
233
akmhoqueb6450b12014-04-24 00:01:03 -0500234CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500235Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500236{
akmhoqueb6450b12014-04-24 00:01:03 -0500237 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
238 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500239 ndn::bind(corLsaCompareByKey, _1, key));
240 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500241 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500242 }
akmhoqueb6450b12014-04-24 00:01:03 -0500243 return 0;
akmhoque53353462014-04-22 08:43:45 -0500244}
245
246bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500247Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500248{
akmhoqueb6450b12014-04-24 00:01:03 -0500249 CoordinateLsa* clsa = findCoordinateLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500250 if (clsa != 0) {
251 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500252 return true;
253 }
akmhoque157b0a42014-05-13 00:26:37 -0500254 else {
akmhoque53353462014-04-22 08:43:45 -0500255 return false;
256 }
257 }
258 return true;
259}
260
261ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500262Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoqueb6450b12014-04-24 00:01:03 -0500263 int expTime)
akmhoque53353462014-04-22 08:43:45 -0500264{
akmhoque31d1d4b2014-05-05 22:08:14 -0500265 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
266 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
267 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500268}
269
270bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500271Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500272{
273 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500274 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500275 if (chkCorLsa == 0) {
akmhoqueb6450b12014-04-24 00:01:03 -0500276 addCoordinateLsa(clsa);
akmhoque157b0a42014-05-13 00:26:37 -0500277 //debugging purpose
278 printCorLsdb();
279 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500280 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
281 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500282 }
akmhoque157b0a42014-05-13 00:26:37 -0500283 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500284 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500285 }
akmhoque157b0a42014-05-13 00:26:37 -0500286 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500287 timeToExpire = clsa.getLifeTime();
288 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500289 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500290 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500291 }
akmhoque157b0a42014-05-13 00:26:37 -0500292 else {
293 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500294 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
295 chkCorLsa->setLifeTime(clsa.getLifeTime());
akmhoque157b0a42014-05-13 00:26:37 -0500296 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500297 chkCorLsa->setCorRadius(clsa.getCorRadius());
298 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500299 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500300 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500301 }
302 }
akmhoque157b0a42014-05-13 00:26:37 -0500303 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500304 timeToExpire = clsa.getLifeTime();
305 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500306 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
307 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500308 clsa.getLsSeqNo(),
309 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500310 }
311 }
312 return true;
313}
314
315bool
akmhoqueb6450b12014-04-24 00:01:03 -0500316Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500317{
akmhoqueb6450b12014-04-24 00:01:03 -0500318 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
319 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500320 ndn::bind(corLsaCompareByKey, _1,
321 clsa.getKey()));
322 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500323 m_corLsdb.push_back(clsa);
324 return true;
325 }
326 return false;
327}
328
329bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500330Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500331{
akmhoqueb6450b12014-04-24 00:01:03 -0500332 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
333 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500334 ndn::bind(corLsaCompareByKey,
335 _1, key));
336 if (it != m_corLsdb.end()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500337 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500338 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500339 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
340 (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500341 }
342 m_corLsdb.erase(it);
343 return true;
344 }
345 return false;
346}
347
348bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500349Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500350{
akmhoqueb6450b12014-04-24 00:01:03 -0500351 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
352 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500353 ndn::bind(corLsaCompareByKey,
354 _1, key));
355 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500356 return false;
357 }
358 return true;
359}
360
akmhoque157b0a42014-05-13 00:26:37 -0500361//debugging
akmhoque53353462014-04-22 08:43:45 -0500362void
akmhoque157b0a42014-05-13 00:26:37 -0500363Lsdb::printCorLsdb()
akmhoque53353462014-04-22 08:43:45 -0500364{
365 cout << "---------------Cor LSDB-------------------" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500366 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500367 it != m_corLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500368 cout << (*it) << endl;
369 }
370}
371
372
373// Adj LSA and LSDB related function starts here
374
375static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500376adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500377{
378 return alsa.getKey() == key;
379}
380
381
382void
akmhoque31d1d4b2014-05-05 22:08:14 -0500383Lsdb::scheduledAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500384{
385 cout << "scheduledAdjLsaBuild Called" << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500386 m_nlsr.setIsBuildAdjLsaSheduled(0);
akmhoque157b0a42014-05-13 00:26:37 -0500387 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500388 int adjBuildCount = m_nlsr.getAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500389 if (adjBuildCount > 0) {
390 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500391 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500392 }
akmhoque157b0a42014-05-13 00:26:37 -0500393 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500394 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
395 key.append("adjacency");
396 removeAdjLsa(key);
397 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500398 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500399 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500400 }
401 }
akmhoque157b0a42014-05-13 00:26:37 -0500402 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500403 m_nlsr.setIsBuildAdjLsaSheduled(1);
404 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
405 m_nlsr.getConfParameter().getInterestResendTime();
406 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
407 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
408 this));
akmhoque53353462014-04-22 08:43:45 -0500409 }
410}
411
412
413bool
414Lsdb::addAdjLsa(AdjLsa& alsa)
415{
416 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
417 m_adjLsdb.end(),
418 bind(adjLsaCompareByKey, _1,
419 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500420 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500421 m_adjLsdb.push_back(alsa);
422 return true;
423 }
424 return false;
425}
426
akmhoqueb6450b12014-04-24 00:01:03 -0500427AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500428Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500429{
430 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
431 m_adjLsdb.end(),
432 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500433 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500434 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500435 }
akmhoqueb6450b12014-04-24 00:01:03 -0500436 return 0;
akmhoque53353462014-04-22 08:43:45 -0500437}
438
439
440bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500441Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500442{
akmhoqueb6450b12014-04-24 00:01:03 -0500443 AdjLsa* adjLsaCheck = findAdjLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500444 if (adjLsaCheck != 0) {
445 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500446 return true;
447 }
akmhoque157b0a42014-05-13 00:26:37 -0500448 else {
akmhoque53353462014-04-22 08:43:45 -0500449 return false;
450 }
451 }
452 return true;
453}
454
455
456ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500457Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo, int expTime)
akmhoque53353462014-04-22 08:43:45 -0500458{
akmhoque31d1d4b2014-05-05 22:08:14 -0500459 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
460 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
461 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500462}
463
464bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500465Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500466{
467 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500468 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500469 if (chkAdjLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500470 addAdjLsa(alsa);
akmhoque31d1d4b2014-05-05 22:08:14 -0500471 alsa.addNptEntries(m_nlsr);
472 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500473 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500474 timeToExpire = alsa.getLifeTime();
475 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500476 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500477 alsa.getLsSeqNo(), timeToExpire);
478 }
akmhoque157b0a42014-05-13 00:26:37 -0500479 else {
480 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500481 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
482 chkAdjLsa->setLifeTime(alsa.getLifeTime());
akmhoque157b0a42014-05-13 00:26:37 -0500483 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500484 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500485 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500486 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500487 }
akmhoque157b0a42014-05-13 00:26:37 -0500488 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500489 timeToExpire = alsa.getLifeTime();
490 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500491 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
492 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500493 alsa.getLsSeqNo(),
494 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500495 }
496 }
497 return true;
498}
499
500bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500501Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500502{
akmhoque31d1d4b2014-05-05 22:08:14 -0500503 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
504 "adjacency",
505 m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
506 m_nlsr.getConfParameter().getRouterDeadInterval(),
507 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
508 m_nlsr.getAdjacencyList());
509 m_nlsr.getSequencingManager().increaseAdjLsaSeq();
510 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500511 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500512 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
513 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
514 lsaPrefix);
515 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500516}
517
518bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500519Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500520{
521 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
522 m_adjLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500523 ndn::bind(adjLsaCompareByKey, _1, key));
524 if (it != m_adjLsdb.end()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500525 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500526 m_adjLsdb.erase(it);
527 return true;
528 }
529 return false;
530}
531
532bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500533Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500534{
535 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
536 m_adjLsdb.end(),
537 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500538 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500539 return false;
540 }
541 return true;
542}
543
544std::list<AdjLsa>&
545Lsdb::getAdjLsdb()
546{
547 return m_adjLsdb;
548}
549
550void
551Lsdb::setLsaRefreshTime(int lrt)
552{
553 m_lsaRefreshTime = lrt;
554}
555
556void
557Lsdb::setThisRouterPrefix(string trp)
558{
559 m_thisRouterPrefix = trp;
560}
561
562void
akmhoque31d1d4b2014-05-05 22:08:14 -0500563Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500564{
565 cout << "Lsdb::exprireOrRefreshNameLsa Called " << endl;
566 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500567 NameLsa* chkNameLsa = findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500568 if (chkNameLsa != 0) {
akmhoqueb6450b12014-04-24 00:01:03 -0500569 cout << " LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << endl;
akmhoque157b0a42014-05-13 00:26:37 -0500570 if (chkNameLsa->getLsSeqNo() == seqNo) {
571 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoqueb6450b12014-04-24 00:01:03 -0500572 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500573 cout << "Own Name LSA, so refreshing name LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500574 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500575 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoqueb6450b12014-04-24 00:01:03 -0500576 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500577 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500578 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500579 chkNameLsa->getLsSeqNo(),
580 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500581 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500582 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500583 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
584 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
585 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500586 }
akmhoque157b0a42014-05-13 00:26:37 -0500587 else {
akmhoque53353462014-04-22 08:43:45 -0500588 cout << "Other's Name LSA, so removing form LSDB" << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500589 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500590 }
591 }
592 }
593}
594
595void
akmhoque31d1d4b2014-05-05 22:08:14 -0500596Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500597{
598 cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
599 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500600 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500601 if (chkAdjLsa != 0) {
akmhoqueb6450b12014-04-24 00:01:03 -0500602 cout << " LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
akmhoque157b0a42014-05-13 00:26:37 -0500603 if (chkAdjLsa->getLsSeqNo() == seqNo) {
604 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque53353462014-04-22 08:43:45 -0500605 cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500606 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500607 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquefdbddb12014-05-02 18:35:19 -0500608 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500609 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500610 chkAdjLsa->getLsSeqNo(),
611 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500612 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500613 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500614 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
615 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
616 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500617 }
akmhoque157b0a42014-05-13 00:26:37 -0500618 else {
akmhoque53353462014-04-22 08:43:45 -0500619 cout << "Other's Adj LSA, so removing form LSDB" << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500620 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500621 }
622 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500623 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500624 }
625 }
626}
627
628void
akmhoque31d1d4b2014-05-05 22:08:14 -0500629Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500630 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500631{
632 cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
633 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500634 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500635 if (chkCorLsa != 0) {
akmhoqueb6450b12014-04-24 00:01:03 -0500636 cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
akmhoque157b0a42014-05-13 00:26:37 -0500637 if (chkCorLsa->getLsSeqNo() == seqNo) {
638 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque53353462014-04-22 08:43:45 -0500639 cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500640 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500641 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquefdbddb12014-05-02 18:35:19 -0500642 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500643 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
644 chkCorLsa->getKey(),
645 chkCorLsa->getLsSeqNo(),
646 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500647 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500648 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500649 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
650 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
651 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500652 }
akmhoque157b0a42014-05-13 00:26:37 -0500653 else {
akmhoque53353462014-04-22 08:43:45 -0500654 cout << "Other's Cor LSA, so removing form LSDB" << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500655 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500656 }
akmhoque157b0a42014-05-13 00:26:37 -0500657 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500658 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500659 }
660 }
661 }
662}
663
664
665void
akmhoque31d1d4b2014-05-05 22:08:14 -0500666Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime)
667{
668 std::cout << "Expressing Interest :" << interestName << std::endl;
669 ndn::Interest interest(interestName);
670 interest.setInterestLifetime(ndn::time::seconds(interestLifeTime));
671 interest.setMustBeFresh(true);
672 m_nlsr.getNlsrFace().expressInterest(interest,
673 ndn::bind(&Lsdb::processContent,
674 this, _1, _2),
675 ndn::bind(&Lsdb::processInterestTimedOut,
676 this, _1));
677}
678
679void
680Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
681{
682 const ndn::Name& intName(interest.getName());
683 std::cout << "Interest recevied for LSA: " << intName << std::endl;
684 string chkString("LSA");
685 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(),
686 chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500687 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500688 std::string interestedLsType;
689 uint64_t interestedLsSeqNo;
690 ndn::Name origRouter = intName.getSubName(lsaPosition + 1,
691 interest.getName().size() - lsaPosition - 3);
akmhoque157b0a42014-05-13 00:26:37 -0500692 interestedLsType = intName[-2].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500693 interestedLsSeqNo = intName[-1].toNumber();
694 std::cout << "Router Name: " << origRouter << std::endl;
695 std::cout << "Ls Type : " << interestedLsType << std::endl;
696 std::cout << "Ls Seq : " << interestedLsSeqNo << endl;
697 std::cout << "Ls Type: " << interestedLsType << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500698 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500699 processInterestForNameLsa(interest,
700 origRouter.append(interestedLsType),
701 interestedLsSeqNo);
702 return;
703 }
akmhoque157b0a42014-05-13 00:26:37 -0500704 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500705 processInterestForAdjacencyLsa(interest,
706 origRouter.append(interestedLsType),
707 interestedLsSeqNo);
708 return;
709 }
akmhoque157b0a42014-05-13 00:26:37 -0500710 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500711 processInterestForCoordinateLsa(interest,
712 origRouter.append(interestedLsType),
713 interestedLsSeqNo);
714 return;
715 }
akmhoque157b0a42014-05-13 00:26:37 -0500716 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500717 cout << "Unrecognized LSA Type :(" << endl;
718 }
719 }
720}
721
722void
723Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
724 const ndn::Name& lsaKey,
725 uint32_t interestedlsSeqNo)
726{
727 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500728 if (nameLsa != 0) {
729 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500730 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
731 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
732 std::string content = nameLsa->getData();
733 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
734 content.size());
735 m_keyChain.sign(data);
736 std::cout << ">> D: " << data << std::endl;
737 m_nlsr.getNlsrFace().put(data);
738 }
739 }
740}
741
742void
743Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
744 const ndn::Name& lsaKey,
745 uint32_t interestedlsSeqNo)
746{
747 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500748 if (adjLsa != 0) {
749 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500750 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
751 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
752 std::string content = adjLsa->getData();
753 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
754 content.size());
755 m_keyChain.sign(data);
756 std::cout << ">> D: " << data << std::endl;
757 m_nlsr.getNlsrFace().put(data);
758 }
759 }
760}
761
762void
763Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
764 const ndn::Name& lsaKey,
765 uint32_t interestedlsSeqNo)
766{
767 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500768 if (corLsa != 0) {
769 if (corLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500770 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
771 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
772 std::string content = corLsa->getData();
773 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
774 content.size());
775 m_keyChain.sign(data);
776 std::cout << ">> D: " << data << std::endl;
777 m_nlsr.getNlsrFace().put(data);
778 }
779 }
780}
781
782void
783Lsdb::processContent(const ndn::Interest& interest, const ndn::Data& data)
784{
785 const ndn::Name& dataName = data.getName();
786 std::cout << "Data received for name: " << dataName << std::endl;
787 string dataContent(reinterpret_cast<const char*>(data.getContent().value()));
788 string chkString("LSA");
789 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500790 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500791 std::string interestedLsType;
792 uint64_t interestedLsSeqNo;
793 ndn::Name origRouter = dataName.getSubName(lsaPosition + 1,
794 dataName.size() - lsaPosition - 4);
akmhoque157b0a42014-05-13 00:26:37 -0500795 interestedLsType = dataName[-3].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500796 interestedLsSeqNo = dataName[-2].toNumber();
797 std::cout << "Ls Type : " << interestedLsType << std::endl;
798 std::cout << "Ls Seq : " << interestedLsSeqNo << std::endl;
799 std::cout << "Ls Type: " << interestedLsType << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500800 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500801 processContentNameLsa(origRouter.append(interestedLsType),
802 interestedLsSeqNo, dataContent);
803 return;
804 }
akmhoque157b0a42014-05-13 00:26:37 -0500805 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500806 processContentAdjacencyLsa(origRouter.append(interestedLsType),
807 interestedLsSeqNo, dataContent);
808 return;
809 }
akmhoque157b0a42014-05-13 00:26:37 -0500810 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500811 processContentCoordinateLsa(origRouter.append(interestedLsType),
812 interestedLsSeqNo, dataContent);
813 return;
814 }
akmhoque157b0a42014-05-13 00:26:37 -0500815 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500816 cout << "Unrecognized LSA Type :(" << endl;
817 }
818 }
819}
820
821void
822Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
823 uint32_t lsSeqNo, std::string& dataContent)
824{
akmhoque157b0a42014-05-13 00:26:37 -0500825 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500826 NameLsa nameLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500827 if (nameLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500828 installNameLsa(nameLsa);
829 }
akmhoque157b0a42014-05-13 00:26:37 -0500830 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500831 std::cout << "LSA data decoding error :(" << std::endl;
832 }
833 }
834}
835
836void
837Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
838 uint32_t lsSeqNo, std::string& dataContent)
839{
akmhoque157b0a42014-05-13 00:26:37 -0500840 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500841 AdjLsa adjLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500842 if (adjLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500843 installAdjLsa(adjLsa);
844 }
akmhoque157b0a42014-05-13 00:26:37 -0500845 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500846 std::cout << "LSA data decoding error :(" << std::endl;
847 }
848 }
849}
850
851void
852Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
853 uint32_t lsSeqNo, std::string& dataContent)
854{
akmhoque157b0a42014-05-13 00:26:37 -0500855 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500856 CoordinateLsa corLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500857 if (corLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500858 installCoordinateLsa(corLsa);
859 }
akmhoque157b0a42014-05-13 00:26:37 -0500860 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500861 std::cout << "LSA data decoding error :(" << std::endl;
862 }
863 }
864}
865
866void
867Lsdb::processInterestTimedOut(const ndn::Interest& interest)
868{
869 const ndn::Name& interestName(interest.getName());
870 cout << "Interest timed out for Name: " << interestName << endl;
871}
872
873
874void
akmhoque53353462014-04-22 08:43:45 -0500875Lsdb::printAdjLsdb()
876{
877 cout << "---------------Adj LSDB-------------------" << endl;
878 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500879 it != m_adjLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500880 cout << (*it) << endl;
881 }
882}
883
884//-----utility function -----
885bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500886Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -0500887{
akmhoque157b0a42014-05-13 00:26:37 -0500888 if (lsType == "name") {
akmhoque53353462014-04-22 08:43:45 -0500889 return doesNameLsaExist(key);
890 }
akmhoque157b0a42014-05-13 00:26:37 -0500891 else if (lsType == "adjacency") {
akmhoque53353462014-04-22 08:43:45 -0500892 return doesAdjLsaExist(key);
893 }
akmhoque157b0a42014-05-13 00:26:37 -0500894 else if (lsType == "coordinate") {
akmhoqueb6450b12014-04-24 00:01:03 -0500895 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -0500896 }
897 return false;
898}
899
900}//namespace nlsr
901