blob: f99482d5c8ad2785dc449ad93f8cdf2ae0391b50 [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"
akmhoque674b0b12014-05-20 14:33:28 -05008#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -05009
10namespace nlsr {
11
akmhoque674b0b12014-05-20 14:33:28 -050012INIT_LOGGER("Lsdb");
13
akmhoque53353462014-04-22 08:43:45 -050014using namespace std;
15
16void
akmhoque31d1d4b2014-05-05 22:08:14 -050017Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -050018{
akmhoque31d1d4b2014-05-05 22:08:14 -050019 m_nlsr.getScheduler().cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -050020}
21
22static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050023nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050024{
25 return nlsa1.getKey() == key;
26}
27
28
29bool
akmhoque31d1d4b2014-05-05 22:08:14 -050030Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -050031{
akmhoque31d1d4b2014-05-05 22:08:14 -050032 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
33 "name",
34 m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
35 m_nlsr.getConfParameter().getRouterDeadInterval(),
36 m_nlsr.getNamePrefixList());
37 m_nlsr.getSequencingManager().increaseNameLsaSeq();
38 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -050039}
40
akmhoqueb6450b12014-04-24 00:01:03 -050041NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -050042Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -050043{
44 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
45 m_nameLsdb.end(),
46 bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -050047 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -050048 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050049 }
akmhoqueb6450b12014-04-24 00:01:03 -050050 return 0;
akmhoque53353462014-04-22 08:43:45 -050051}
52
53bool
akmhoque31d1d4b2014-05-05 22:08:14 -050054Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050055{
akmhoqueb6450b12014-04-24 00:01:03 -050056 NameLsa* nameLsaCheck = findNameLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -050057 if (nameLsaCheck != 0) {
58 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -050059 return true;
60 }
akmhoque157b0a42014-05-13 00:26:37 -050061 else {
akmhoque53353462014-04-22 08:43:45 -050062 return false;
63 }
64 }
65 return true;
66}
67
68ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -050069Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo, int expTime)
akmhoque53353462014-04-22 08:43:45 -050070{
akmhoque31d1d4b2014-05-05 22:08:14 -050071 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
72 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
73 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -050074}
75
76bool
akmhoque31d1d4b2014-05-05 22:08:14 -050077Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -050078{
79 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -050080 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -050081 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -050082 addNameLsa(nlsa);
akmhoque674b0b12014-05-20 14:33:28 -050083 _LOG_DEBUG("New Name LSA. Adding to LSDB");
akmhoque53353462014-04-22 08:43:45 -050084 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -050085
akmhoque53353462014-04-22 08:43:45 -050086 printNameLsdb();
akmhoque157b0a42014-05-13 00:26:37 -050087 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050088 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
89 nlsa.getOrigRouter());
90 std::list<ndn::Name> nameList = nlsa.getNpl().getNameList();
91 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -050092 it++) {
93 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050094 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -050095 }
96 }
97 }
akmhoque157b0a42014-05-13 00:26:37 -050098 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -050099 timeToExpire = nlsa.getLifeTime();
100 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500102 nlsa.getLsSeqNo(),
103 timeToExpire));
104 }
akmhoque157b0a42014-05-13 00:26:37 -0500105 else {
106 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500107 _LOG_DEBUG("Updated Name LSA. Updating LSDB");
108 _LOG_DEBUG("Old Name LSA ");
akmhoqueb6450b12014-04-24 00:01:03 -0500109 chkNameLsa->writeLog();
110 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
111 chkNameLsa->setLifeTime(nlsa.getLifeTime());
112 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500113 nlsa.getNpl().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -0500114 std::list<ndn::Name> nameToAdd;
akmhoque53353462014-04-22 08:43:45 -0500115 std::set_difference(nlsa.getNpl().getNameList().begin(),
116 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500117 chkNameLsa->getNpl().getNameList().begin(),
118 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500119 std::inserter(nameToAdd, nameToAdd.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500120 for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500121 it != nameToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500122 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500123 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
124 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500125 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500126 }
127 }
128 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500129 std::list<ndn::Name> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500130 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
131 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500132 nlsa.getNpl().getNameList().begin(),
133 nlsa.getNpl().getNameList().end(),
134 std::inserter(nameToRemove, nameToRemove.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500135 for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500136 it != nameToRemove.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500137 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500138 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
139 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500140 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500141 }
142 }
143 }
akmhoque157b0a42014-05-13 00:26:37 -0500144 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500145 timeToExpire = nlsa.getLifeTime();
146 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500147 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
148 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500149 nlsa.getLsSeqNo(),
150 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500151 _LOG_DEBUG("Updated Name LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500152 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500153 }
154 }
155 return true;
156}
157
158bool
159Lsdb::addNameLsa(NameLsa& nlsa)
160{
161 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
162 m_nameLsdb.end(),
163 bind(nameLsaCompareByKey, _1,
164 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500165 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500166 m_nameLsdb.push_back(nlsa);
167 return true;
168 }
169 return false;
170}
171
172bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500173Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500174{
175 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
176 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500177 ndn::bind(nameLsaCompareByKey, _1, key));
178 if (it != m_nameLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500179 _LOG_DEBUG("Removing Name LSA");
akmhoque53353462014-04-22 08:43:45 -0500180 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500181 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500182 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
184 (*it).getOrigRouter());
185 for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500186 nit != (*it).getNpl().getNameList().end(); ++nit) {
187 if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500188 m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500189 }
190 }
191 }
192 m_nameLsdb.erase(it);
193 return true;
194 }
195 return false;
196}
197
198bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500199Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500200{
201 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
202 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500203 ndn::bind(nameLsaCompareByKey, _1, key));
204 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500205 return false;
206 }
207 return true;
208}
209
210void
211Lsdb::printNameLsdb()
212{
213 cout << "---------------Name LSDB-------------------" << endl;
214 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500215 it != m_nameLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500216 cout << (*it) << endl;
217 }
218}
219
220// Cor LSA and LSDB related Functions start here
221
222
223static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500224corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500225{
226 return clsa.getKey() == key;
227}
228
229bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500230Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500231{
akmhoque31d1d4b2014-05-05 22:08:14 -0500232 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
233 "coordinate",
234 m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
235 m_nlsr.getConfParameter().getRouterDeadInterval(),
236 m_nlsr.getConfParameter().getCorR(),
237 m_nlsr.getConfParameter().getCorTheta());
238 m_nlsr.getSequencingManager().increaseCorLsaSeq();
239 installCoordinateLsa(corLsa);
akmhoque53353462014-04-22 08:43:45 -0500240 return true;
241}
242
akmhoqueb6450b12014-04-24 00:01:03 -0500243CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500244Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500245{
akmhoqueb6450b12014-04-24 00:01:03 -0500246 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
247 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500248 ndn::bind(corLsaCompareByKey, _1, key));
249 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500250 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500251 }
akmhoqueb6450b12014-04-24 00:01:03 -0500252 return 0;
akmhoque53353462014-04-22 08:43:45 -0500253}
254
255bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500256Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500257{
akmhoqueb6450b12014-04-24 00:01:03 -0500258 CoordinateLsa* clsa = findCoordinateLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500259 if (clsa != 0) {
260 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500261 return true;
262 }
akmhoque157b0a42014-05-13 00:26:37 -0500263 else {
akmhoque53353462014-04-22 08:43:45 -0500264 return false;
265 }
266 }
267 return true;
268}
269
270ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500271Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoqueb6450b12014-04-24 00:01:03 -0500272 int expTime)
akmhoque53353462014-04-22 08:43:45 -0500273{
akmhoque31d1d4b2014-05-05 22:08:14 -0500274 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
275 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
276 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500277}
278
279bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500280Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500281{
282 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500283 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500284 if (chkCorLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500285 _LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
286 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500287 addCoordinateLsa(clsa);
akmhoque157b0a42014-05-13 00:26:37 -0500288 //debugging purpose
289 printCorLsdb();
290 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500291 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
292 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500293 }
akmhoque157b0a42014-05-13 00:26:37 -0500294 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500295 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500296 }
akmhoque157b0a42014-05-13 00:26:37 -0500297 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500298 timeToExpire = clsa.getLifeTime();
299 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500300 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500301 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500302 }
akmhoque157b0a42014-05-13 00:26:37 -0500303 else {
304 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500305 _LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
306 _LOG_DEBUG("Old Coordinate LSA");
307 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500308 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
309 chkCorLsa->setLifeTime(clsa.getLifeTime());
akmhoque157b0a42014-05-13 00:26:37 -0500310 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500311 chkCorLsa->setCorRadius(clsa.getCorRadius());
312 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500313 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500314 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500315 }
316 }
akmhoque157b0a42014-05-13 00:26:37 -0500317 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500318 timeToExpire = clsa.getLifeTime();
319 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500320 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
321 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500322 clsa.getLsSeqNo(),
323 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500324 _LOG_DEBUG("Updated Coordinate LSA");
325 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500326 }
327 }
328 return true;
329}
330
331bool
akmhoqueb6450b12014-04-24 00:01:03 -0500332Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500333{
akmhoqueb6450b12014-04-24 00:01:03 -0500334 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
335 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500336 ndn::bind(corLsaCompareByKey, _1,
337 clsa.getKey()));
338 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500339 m_corLsdb.push_back(clsa);
340 return true;
341 }
342 return false;
343}
344
345bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500346Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500347{
akmhoqueb6450b12014-04-24 00:01:03 -0500348 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
349 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500350 ndn::bind(corLsaCompareByKey,
351 _1, key));
352 if (it != m_corLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500353 _LOG_DEBUG("Removing Coordinate LSA");
354 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500355 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500356 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500357 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
358 (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500359 }
360 m_corLsdb.erase(it);
361 return true;
362 }
363 return false;
364}
365
366bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500367Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500368{
akmhoqueb6450b12014-04-24 00:01:03 -0500369 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
370 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500371 ndn::bind(corLsaCompareByKey,
372 _1, key));
373 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500374 return false;
375 }
376 return true;
377}
378
akmhoque157b0a42014-05-13 00:26:37 -0500379//debugging
akmhoque53353462014-04-22 08:43:45 -0500380void
akmhoque157b0a42014-05-13 00:26:37 -0500381Lsdb::printCorLsdb()
akmhoque53353462014-04-22 08:43:45 -0500382{
383 cout << "---------------Cor LSDB-------------------" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500384 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500385 it != m_corLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500386 cout << (*it) << endl;
387 }
388}
389
390
391// Adj LSA and LSDB related function starts here
392
393static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500394adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500395{
396 return alsa.getKey() == key;
397}
398
399
400void
akmhoque31d1d4b2014-05-05 22:08:14 -0500401Lsdb::scheduledAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500402{
akmhoque674b0b12014-05-20 14:33:28 -0500403 std::cout << "scheduledAdjLsaBuild Called" << endl;
404 _LOG_DEBUG("scheduledAdjLsaBuild Called");
405 m_nlsr.setIsBuildAdjLsaSheduled(false);
akmhoque157b0a42014-05-13 00:26:37 -0500406 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500407 int adjBuildCount = m_nlsr.getAdjBuildCount();
akmhoque157b0a42014-05-13 00:26:37 -0500408 if (adjBuildCount > 0) {
409 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500410 _LOG_DEBUG("Building and installing Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500411 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500412 }
akmhoque157b0a42014-05-13 00:26:37 -0500413 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500414 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
415 key.append("adjacency");
416 removeAdjLsa(key);
417 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500418 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500419 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500420 }
421 }
akmhoque157b0a42014-05-13 00:26:37 -0500422 else {
akmhoque674b0b12014-05-20 14:33:28 -0500423 m_nlsr.setIsBuildAdjLsaSheduled(true);
akmhoque31d1d4b2014-05-05 22:08:14 -0500424 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
425 m_nlsr.getConfParameter().getInterestResendTime();
426 m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
427 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
428 this));
akmhoque53353462014-04-22 08:43:45 -0500429 }
430}
431
432
433bool
434Lsdb::addAdjLsa(AdjLsa& alsa)
435{
436 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
437 m_adjLsdb.end(),
438 bind(adjLsaCompareByKey, _1,
439 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500440 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500441 m_adjLsdb.push_back(alsa);
442 return true;
443 }
444 return false;
445}
446
akmhoqueb6450b12014-04-24 00:01:03 -0500447AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500448Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500449{
450 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
451 m_adjLsdb.end(),
452 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500453 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500454 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500455 }
akmhoqueb6450b12014-04-24 00:01:03 -0500456 return 0;
akmhoque53353462014-04-22 08:43:45 -0500457}
458
459
460bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500461Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500462{
akmhoqueb6450b12014-04-24 00:01:03 -0500463 AdjLsa* adjLsaCheck = findAdjLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500464 if (adjLsaCheck != 0) {
465 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500466 return true;
467 }
akmhoque157b0a42014-05-13 00:26:37 -0500468 else {
akmhoque53353462014-04-22 08:43:45 -0500469 return false;
470 }
471 }
472 return true;
473}
474
475
476ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500477Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo, int expTime)
akmhoque53353462014-04-22 08:43:45 -0500478{
akmhoque31d1d4b2014-05-05 22:08:14 -0500479 return m_nlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
480 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
481 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500482}
483
484bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500485Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500486{
487 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500488 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500489 if (chkAdjLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500490 _LOG_DEBUG("New Adj LSA. Adding to LSDB");
491 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500492 addAdjLsa(alsa);
akmhoque31d1d4b2014-05-05 22:08:14 -0500493 alsa.addNptEntries(m_nlsr);
494 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500495 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500496 timeToExpire = alsa.getLifeTime();
497 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500498 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500499 alsa.getLsSeqNo(), timeToExpire);
500 }
akmhoque157b0a42014-05-13 00:26:37 -0500501 else {
502 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500503 _LOG_DEBUG("Updated Adj LSA. Updating LSDB");
504 _LOG_DEBUG("Old Adj LSA");
505 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500506 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
507 chkAdjLsa->setLifeTime(alsa.getLifeTime());
akmhoque157b0a42014-05-13 00:26:37 -0500508 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500509 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500510 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500511 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500512 }
akmhoque157b0a42014-05-13 00:26:37 -0500513 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque53353462014-04-22 08:43:45 -0500514 timeToExpire = alsa.getLifeTime();
515 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500516 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
517 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500518 alsa.getLsSeqNo(),
519 timeToExpire));
akmhoque674b0b12014-05-20 14:33:28 -0500520 _LOG_DEBUG("Updated Adj LSA");
521 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500522 }
523 }
524 return true;
525}
526
527bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500528Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500529{
akmhoque31d1d4b2014-05-05 22:08:14 -0500530 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
531 "adjacency",
532 m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
533 m_nlsr.getConfParameter().getRouterDeadInterval(),
534 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
535 m_nlsr.getAdjacencyList());
536 m_nlsr.getSequencingManager().increaseAdjLsaSeq();
537 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500538 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500539 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
540 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
541 lsaPrefix);
542 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500543}
544
545bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500546Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500547{
548 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
549 m_adjLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500550 ndn::bind(adjLsaCompareByKey, _1, key));
551 if (it != m_adjLsdb.end()) {
akmhoque674b0b12014-05-20 14:33:28 -0500552 _LOG_DEBUG("Removing Adj LSA");
553 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500554 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500555 m_adjLsdb.erase(it);
556 return true;
557 }
558 return false;
559}
560
561bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500562Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500563{
564 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
565 m_adjLsdb.end(),
566 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500567 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500568 return false;
569 }
570 return true;
571}
572
573std::list<AdjLsa>&
574Lsdb::getAdjLsdb()
575{
576 return m_adjLsdb;
577}
578
579void
580Lsdb::setLsaRefreshTime(int lrt)
581{
582 m_lsaRefreshTime = lrt;
583}
584
585void
586Lsdb::setThisRouterPrefix(string trp)
587{
588 m_thisRouterPrefix = trp;
589}
590
591void
akmhoque31d1d4b2014-05-05 22:08:14 -0500592Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500593{
akmhoque674b0b12014-05-20 14:33:28 -0500594 std::cout << "Lsdb::exprireOrRefreshNameLsa Called " << std::endl;
595 std::cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << std::endl;
596 _LOG_DEBUG("Lsdb::exprireOrRefreshNameLsa Called");
597 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500598 NameLsa* chkNameLsa = findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500599 if (chkNameLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500600 std::cout << "LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << std::endl;
601 _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500602 if (chkNameLsa->getLsSeqNo() == seqNo) {
603 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoqueb6450b12014-04-24 00:01:03 -0500604 chkNameLsa->writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500605 std::cout << "Own Name LSA, so refreshing name LSA" << std::endl;
606 _LOG_DEBUG("Own Name LSA, so refreshing name LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500607 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500608 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoqueb6450b12014-04-24 00:01:03 -0500609 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500610 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500611 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500612 chkNameLsa->getLsSeqNo(),
613 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500614 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500615 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500616 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
617 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
618 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500619 }
akmhoque157b0a42014-05-13 00:26:37 -0500620 else {
akmhoque674b0b12014-05-20 14:33:28 -0500621 std::cout << "Other's Name LSA, so removing form LSDB" << std::endl;
622 _LOG_DEBUG("Other's Name LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500623 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500624 }
625 }
626 }
627}
628
629void
akmhoque31d1d4b2014-05-05 22:08:14 -0500630Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500631{
632 cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
633 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500634 _LOG_DEBUG("Lsdb::exprireOrRefreshAdjLsa Called");
635 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500636 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500637 if (chkAdjLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500638 cout << "LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
639 _LOG_DEBUG("LSA Exists with seq no: ");
akmhoque157b0a42014-05-13 00:26:37 -0500640 if (chkAdjLsa->getLsSeqNo() == seqNo) {
641 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque53353462014-04-22 08:43:45 -0500642 cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500643 _LOG_DEBUG("Own Adj LSA, so refreshing Adj LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500644 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500645 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquefdbddb12014-05-02 18:35:19 -0500646 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500647 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500648 chkAdjLsa->getLsSeqNo(),
649 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500650 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500651 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500652 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
653 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
654 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500655 }
akmhoque157b0a42014-05-13 00:26:37 -0500656 else {
akmhoque53353462014-04-22 08:43:45 -0500657 cout << "Other's Adj LSA, so removing form LSDB" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500658 _LOG_DEBUG("Other's Adj LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500659 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500660 }
661 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500662 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500663 }
664 }
665}
666
667void
akmhoque31d1d4b2014-05-05 22:08:14 -0500668Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500669 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500670{
671 cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
672 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500673 _LOG_DEBUG("Lsdb::exprireOrRefreshCorLsa Called ");
674 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500675 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500676 if (chkCorLsa != 0) {
akmhoqueb6450b12014-04-24 00:01:03 -0500677 cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500678 _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500679 if (chkCorLsa->getLsSeqNo() == seqNo) {
680 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque53353462014-04-22 08:43:45 -0500681 cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500682 _LOG_DEBUG("Own Cor LSA, so refreshing Cor LSA");
akmhoqueb6450b12014-04-24 00:01:03 -0500683 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500684 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoquefdbddb12014-05-02 18:35:19 -0500685 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500686 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
687 chkCorLsa->getKey(),
688 chkCorLsa->getLsSeqNo(),
689 m_lsaRefreshTime));
akmhoque53353462014-04-22 08:43:45 -0500690 // publish routing update
akmhoque157b0a42014-05-13 00:26:37 -0500691 ndn::Name lsaPrefix = m_nlsr.getConfParameter().getLsaPrefix();
akmhoque31d1d4b2014-05-05 22:08:14 -0500692 lsaPrefix.append(m_nlsr.getConfParameter().getRouterPrefix());
693 m_nlsr.getSyncLogicHandler().publishRoutingUpdate(m_nlsr.getSequencingManager(),
694 lsaPrefix);
akmhoque53353462014-04-22 08:43:45 -0500695 }
akmhoque157b0a42014-05-13 00:26:37 -0500696 else {
akmhoque53353462014-04-22 08:43:45 -0500697 cout << "Other's Cor LSA, so removing form LSDB" << endl;
akmhoque674b0b12014-05-20 14:33:28 -0500698 _LOG_DEBUG("Other's Cor LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500699 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500700 }
akmhoque157b0a42014-05-13 00:26:37 -0500701 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500702 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500703 }
704 }
705 }
706}
707
708
709void
akmhoque31d1d4b2014-05-05 22:08:14 -0500710Lsdb::expressInterest(const ndn::Name& interestName, uint32_t interestLifeTime)
711{
712 std::cout << "Expressing Interest :" << interestName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500713 _LOG_DEBUG("Expressing Interest for LSA(name): " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500714 ndn::Interest interest(interestName);
715 interest.setInterestLifetime(ndn::time::seconds(interestLifeTime));
716 interest.setMustBeFresh(true);
717 m_nlsr.getNlsrFace().expressInterest(interest,
718 ndn::bind(&Lsdb::processContent,
719 this, _1, _2),
720 ndn::bind(&Lsdb::processInterestTimedOut,
721 this, _1));
722}
723
724void
725Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
726{
727 const ndn::Name& intName(interest.getName());
728 std::cout << "Interest recevied for LSA: " << intName << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -0500729 _LOG_DEBUG("Interest recevied for LSA(name): " << intName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500730 string chkString("LSA");
731 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(),
732 chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500733 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500734 std::string interestedLsType;
735 uint64_t interestedLsSeqNo;
736 ndn::Name origRouter = intName.getSubName(lsaPosition + 1,
737 interest.getName().size() - lsaPosition - 3);
akmhoque157b0a42014-05-13 00:26:37 -0500738 interestedLsType = intName[-2].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500739 interestedLsSeqNo = intName[-1].toNumber();
740 std::cout << "Router Name: " << origRouter << std::endl;
741 std::cout << "Ls Type : " << interestedLsType << std::endl;
742 std::cout << "Ls Seq : " << interestedLsSeqNo << endl;
743 std::cout << "Ls Type: " << interestedLsType << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500744 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500745 processInterestForNameLsa(interest,
746 origRouter.append(interestedLsType),
747 interestedLsSeqNo);
748 return;
749 }
akmhoque157b0a42014-05-13 00:26:37 -0500750 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500751 processInterestForAdjacencyLsa(interest,
752 origRouter.append(interestedLsType),
753 interestedLsSeqNo);
754 return;
755 }
akmhoque157b0a42014-05-13 00:26:37 -0500756 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500757 processInterestForCoordinateLsa(interest,
758 origRouter.append(interestedLsType),
759 interestedLsSeqNo);
760 return;
761 }
akmhoque157b0a42014-05-13 00:26:37 -0500762 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500763 cout << "Unrecognized LSA Type :(" << endl;
764 }
765 }
766}
767
768void
769Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
770 const ndn::Name& lsaKey,
771 uint32_t interestedlsSeqNo)
772{
773 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500774 if (nameLsa != 0) {
775 if (nameLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500776 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500777 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500778 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
779 std::string content = nameLsa->getData();
780 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
781 content.size());
782 m_keyChain.sign(data);
akmhoque674b0b12014-05-20 14:33:28 -0500783 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500784 m_nlsr.getNlsrFace().put(data);
785 }
786 }
787}
788
789void
790Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
791 const ndn::Name& lsaKey,
792 uint32_t interestedlsSeqNo)
793{
794 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500795 if (adjLsa != 0) {
796 if (adjLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500797 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500798 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500799 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
800 std::string content = adjLsa->getData();
801 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
802 content.size());
803 m_keyChain.sign(data);
akmhoque674b0b12014-05-20 14:33:28 -0500804 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500805 m_nlsr.getNlsrFace().put(data);
806 }
807 }
808}
809
810void
811Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
812 const ndn::Name& lsaKey,
813 uint32_t interestedlsSeqNo)
814{
815 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500816 if (corLsa != 0) {
817 if (corLsa->getLsSeqNo() >= interestedlsSeqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500818 ndn::Data data(ndn::Name(interest.getName()).appendVersion());
akmhoque674b0b12014-05-20 14:33:28 -0500819 _LOG_DEBUG("Sending data for LSA(name): " << interest.getName());
akmhoque31d1d4b2014-05-05 22:08:14 -0500820 data.setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
821 std::string content = corLsa->getData();
822 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()),
823 content.size());
824 m_keyChain.sign(data);
akmhoque674b0b12014-05-20 14:33:28 -0500825 //std::cout << ">> D: " << data << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500826 m_nlsr.getNlsrFace().put(data);
827 }
828 }
829}
830
831void
832Lsdb::processContent(const ndn::Interest& interest, const ndn::Data& data)
833{
834 const ndn::Name& dataName = data.getName();
akmhoque674b0b12014-05-20 14:33:28 -0500835 std::cout << "Data received for LSA(name): " << dataName << std::endl;
836 _LOG_DEBUG("Data received for LSA(name): " << dataName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500837 string dataContent(reinterpret_cast<const char*>(data.getContent().value()));
838 string chkString("LSA");
839 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
akmhoque157b0a42014-05-13 00:26:37 -0500840 if (lsaPosition >= 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500841 std::string interestedLsType;
842 uint64_t interestedLsSeqNo;
843 ndn::Name origRouter = dataName.getSubName(lsaPosition + 1,
844 dataName.size() - lsaPosition - 4);
akmhoque157b0a42014-05-13 00:26:37 -0500845 interestedLsType = dataName[-3].toUri();
akmhoque31d1d4b2014-05-05 22:08:14 -0500846 interestedLsSeqNo = dataName[-2].toNumber();
847 std::cout << "Ls Type : " << interestedLsType << std::endl;
848 std::cout << "Ls Seq : " << interestedLsSeqNo << std::endl;
849 std::cout << "Ls Type: " << interestedLsType << std::endl;
akmhoque157b0a42014-05-13 00:26:37 -0500850 if (interestedLsType == "name") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500851 processContentNameLsa(origRouter.append(interestedLsType),
852 interestedLsSeqNo, dataContent);
853 return;
854 }
akmhoque157b0a42014-05-13 00:26:37 -0500855 else if (interestedLsType == "adjacency") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500856 processContentAdjacencyLsa(origRouter.append(interestedLsType),
857 interestedLsSeqNo, dataContent);
858 return;
859 }
akmhoque157b0a42014-05-13 00:26:37 -0500860 else if (interestedLsType == "coordinate") {
akmhoque31d1d4b2014-05-05 22:08:14 -0500861 processContentCoordinateLsa(origRouter.append(interestedLsType),
862 interestedLsSeqNo, dataContent);
863 return;
864 }
akmhoque157b0a42014-05-13 00:26:37 -0500865 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500866 cout << "Unrecognized LSA Type :(" << endl;
867 }
868 }
869}
870
871void
872Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
873 uint32_t lsSeqNo, std::string& dataContent)
874{
akmhoque157b0a42014-05-13 00:26:37 -0500875 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500876 NameLsa nameLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500877 if (nameLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500878 installNameLsa(nameLsa);
879 }
akmhoque157b0a42014-05-13 00:26:37 -0500880 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500881 std::cout << "LSA data decoding error :(" << std::endl;
882 }
883 }
884}
885
886void
887Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
888 uint32_t lsSeqNo, std::string& dataContent)
889{
akmhoque157b0a42014-05-13 00:26:37 -0500890 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500891 AdjLsa adjLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500892 if (adjLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500893 installAdjLsa(adjLsa);
894 }
akmhoque157b0a42014-05-13 00:26:37 -0500895 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500896 std::cout << "LSA data decoding error :(" << std::endl;
897 }
898 }
899}
900
901void
902Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
903 uint32_t lsSeqNo, std::string& dataContent)
904{
akmhoque157b0a42014-05-13 00:26:37 -0500905 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500906 CoordinateLsa corLsa;
akmhoque157b0a42014-05-13 00:26:37 -0500907 if (corLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500908 installCoordinateLsa(corLsa);
909 }
akmhoque157b0a42014-05-13 00:26:37 -0500910 else {
akmhoque31d1d4b2014-05-05 22:08:14 -0500911 std::cout << "LSA data decoding error :(" << std::endl;
912 }
913 }
914}
915
916void
917Lsdb::processInterestTimedOut(const ndn::Interest& interest)
918{
919 const ndn::Name& interestName(interest.getName());
akmhoque674b0b12014-05-20 14:33:28 -0500920 cout << "Interest timed out for LSA(name): " << interestName << endl;
921 _LOG_DEBUG("Interest timed out for LSA(name): " << interestName);
akmhoque31d1d4b2014-05-05 22:08:14 -0500922}
923
924
925void
akmhoque53353462014-04-22 08:43:45 -0500926Lsdb::printAdjLsdb()
927{
928 cout << "---------------Adj LSDB-------------------" << endl;
929 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500930 it != m_adjLsdb.end() ; it++) {
akmhoque53353462014-04-22 08:43:45 -0500931 cout << (*it) << endl;
932 }
933}
934
935//-----utility function -----
936bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500937Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -0500938{
akmhoque157b0a42014-05-13 00:26:37 -0500939 if (lsType == "name") {
akmhoque53353462014-04-22 08:43:45 -0500940 return doesNameLsaExist(key);
941 }
akmhoque157b0a42014-05-13 00:26:37 -0500942 else if (lsType == "adjacency") {
akmhoque53353462014-04-22 08:43:45 -0500943 return doesAdjLsaExist(key);
944 }
akmhoque157b0a42014-05-13 00:26:37 -0500945 else if (lsType == "coordinate") {
akmhoqueb6450b12014-04-24 00:01:03 -0500946 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -0500947 }
948 return false;
949}
950
951}//namespace nlsr
952