blob: 854d89f58cc299d3ca9b9a8c961a8a743f8d6e88 [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 <iostream>
25#include <sstream>
26#include <algorithm>
27#include <cmath>
28#include <limits>
akmhoque31d1d4b2014-05-05 22:08:14 -050029#include <boost/tokenizer.hpp>
30#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -050031
32#include "nlsr.hpp"
33#include "lsa.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050034#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050035#include "adjacent.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050036#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050037
38
39namespace nlsr {
40
akmhoque674b0b12014-05-20 14:33:28 -050041INIT_LOGGER("Lsa");
42
akmhoque53353462014-04-22 08:43:45 -050043using namespace std;
44
akmhoque31d1d4b2014-05-05 22:08:14 -050045const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -050046NameLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050047{
akmhoque31d1d4b2014-05-05 22:08:14 -050048 ndn::Name key = m_origRouter;
49 key.append("name");
akmhoque53353462014-04-22 08:43:45 -050050 return key;
51}
52
akmhoque31d1d4b2014-05-05 22:08:14 -050053NameLsa::NameLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -050054 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -050055 NamePrefixList& npl)
akmhoque53353462014-04-22 08:43:45 -050056{
57 m_origRouter = origR;
58 m_lsType = lst;
59 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -050060 m_expirationTimePoint = lt;
akmhoque31d1d4b2014-05-05 22:08:14 -050061 std::list<ndn::Name>& nl = npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050062 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050063 addName((*it));
64 }
65}
66
67string
68NameLsa::getData()
69{
70 string nameLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -050071 nameLsaData = m_origRouter.toUri() + "|" + "name" + "|"
akmhoque53353462014-04-22 08:43:45 -050072 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -050073 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -050074 nameLsaData += "|";
75 nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize());
akmhoque31d1d4b2014-05-05 22:08:14 -050076 std::list<ndn::Name> nl = m_npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050077 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050078 nameLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -050079 nameLsaData += (*it).toUri();
akmhoque53353462014-04-22 08:43:45 -050080 }
81 return nameLsaData + "|";
82}
83
84bool
akmhoque31d1d4b2014-05-05 22:08:14 -050085NameLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -050086{
87 uint32_t numName = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -050088 boost::char_separator<char> sep("|");
89 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
90 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
91 tokens.begin();
92 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -050093 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -050094 return false;
95 }
akmhoque31d1d4b2014-05-05 22:08:14 -050096 try {
97 m_lsType = *tok_iter++;
98 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -050099 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500101 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500102 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500103 return false;
104 }
akmhoque157b0a42014-05-13 00:26:37 -0500105 for (uint32_t i = 0; i < numName; i++) {
akmhoque778f81b2014-06-27 10:07:56 -0500106 ndn::Name name(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500107 addName(name);
108 }
109 return true;
110}
111
112void
113NameLsa::writeLog()
114{
akmhoque674b0b12014-05-20 14:33:28 -0500115 _LOG_DEBUG("Name Lsa: ");
116 _LOG_DEBUG(" Origination Router: " << m_origRouter);
117 _LOG_DEBUG(" Ls Type: " << m_lsType);
118 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500119 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500120 _LOG_DEBUG(" Names: ");
121 int i = 1;
122 std::list<ndn::Name> nl = m_npl.getNameList();
123 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
124 {
125 _LOG_DEBUG(" Name " << i << ": " << (*it));
126 }
akmhoque2f423352014-06-03 11:49:35 -0500127 _LOG_DEBUG("name_lsa_end");
akmhoque53353462014-04-22 08:43:45 -0500128}
129
akmhoque31d1d4b2014-05-05 22:08:14 -0500130CoordinateLsa::CoordinateLsa(const ndn::Name& origR, const string lst,
131 uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500132 const ndn::time::system_clock::TimePoint& lt,
133 double r, double theta)
akmhoque53353462014-04-22 08:43:45 -0500134{
135 m_origRouter = origR;
136 m_lsType = lst;
137 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500138 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500139 m_corRad = r;
140 m_corTheta = theta;
141}
142
akmhoque31d1d4b2014-05-05 22:08:14 -0500143const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -0500144CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500145{
akmhoque31d1d4b2014-05-05 22:08:14 -0500146 ndn::Name key = m_origRouter;
147 key.append("coordinate");
akmhoque53353462014-04-22 08:43:45 -0500148 return key;
149}
150
151bool
akmhoquefdbddb12014-05-02 18:35:19 -0500152CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500153{
154 return (std::abs(m_corRad - clsa.getCorRadius()) <
155 std::numeric_limits<double>::epsilon()) &&
156 (std::abs(m_corTheta - clsa.getCorTheta()) <
157 std::numeric_limits<double>::epsilon());
158}
159
160string
akmhoqueb6450b12014-04-24 00:01:03 -0500161CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500162{
163 string corLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500164 corLsaData = m_origRouter.toUri() + "|";
165 corLsaData += "coordinate";
166 corLsaData += "|";
akmhoque53353462014-04-22 08:43:45 -0500167 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
akmhoquec7a79b22014-05-26 08:06:19 -0500168 corLsaData += (ndn::time::toIsoString(m_expirationTimePoint) + "|");
akmhoque53353462014-04-22 08:43:45 -0500169 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
170 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
171 return corLsaData;
172}
173
174bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500175CoordinateLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500176{
akmhoque31d1d4b2014-05-05 22:08:14 -0500177 boost::char_separator<char> sep("|");
178 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
179 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
180 tokens.begin();
181 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500182 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500183 return false;
184 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500185 try {
186 m_lsType = *tok_iter++;
187 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500188 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500189 m_corRad = boost::lexical_cast<double>(*tok_iter++);
190 m_corTheta = boost::lexical_cast<double>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500191 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500192 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500193 return false;
194 }
195 return true;
196}
197
akmhoque674b0b12014-05-20 14:33:28 -0500198void
199CoordinateLsa::writeLog()
200{
201 _LOG_DEBUG("Cor Lsa: ");
202 _LOG_DEBUG(" Origination Router: " << m_origRouter);
203 _LOG_DEBUG(" Ls Type: " << m_lsType);
204 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500205 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500206 _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad);
207 _LOG_DEBUG(" Hyperbolic Theta: " << m_corRad);
208}
209
akmhoque31d1d4b2014-05-05 22:08:14 -0500210AdjLsa::AdjLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500211 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500212 uint32_t nl , AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -0500213{
214 m_origRouter = origR;
215 m_lsType = lst;
216 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500217 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500218 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500219 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500220 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
221 if ((*it).getStatus() == 1) {
akmhoque53353462014-04-22 08:43:45 -0500222 addAdjacent((*it));
223 }
224 }
225}
226
akmhoque31d1d4b2014-05-05 22:08:14 -0500227const ndn::Name
228AdjLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500229{
akmhoque31d1d4b2014-05-05 22:08:14 -0500230 ndn::Name key = m_origRouter;
231 key.append("adjacency");
akmhoque53353462014-04-22 08:43:45 -0500232 return key;
233}
234
235bool
akmhoquefdbddb12014-05-02 18:35:19 -0500236AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500237{
akmhoquefdbddb12014-05-02 18:35:19 -0500238 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500239}
240
241
242string
243AdjLsa::getData()
244{
245 string adjLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500246 adjLsaData = m_origRouter.toUri() + "|" + "adjacency" + "|"
akmhoque53353462014-04-22 08:43:45 -0500247 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -0500248 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500249 adjLsaData += "|";
250 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
251 std::list<Adjacent> al = m_adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500252 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500253 adjLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -0500254 adjLsaData += (*it).getName().toUri();
akmhoque53353462014-04-22 08:43:45 -0500255 adjLsaData += "|";
akmhoque157b0a42014-05-13 00:26:37 -0500256 adjLsaData += (*it).getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -0500257 adjLsaData += "|";
258 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
259 }
260 return adjLsaData + "|";
261}
262
263bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500264AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500265{
266 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500267 boost::char_separator<char> sep("|");
268 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
269 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
270 tokens.begin();
271 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500272 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500273 return false;
274 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500275 try {
276 m_lsType = *tok_iter++;
277 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500278 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500279 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500280 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500281 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500282 return false;
283 }
akmhoque157b0a42014-05-13 00:26:37 -0500284 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500285 try {
akmhoque778f81b2014-06-27 10:07:56 -0500286 ndn::Name adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500287 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500288 double linkCost = boost::lexical_cast<double>(*tok_iter++);
akmhoquec04e7272014-07-02 11:00:14 -0500289 Adjacent adjacent(adjName, connectingFaceUri, linkCost, 0, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500290 addAdjacent(adjacent);
291 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500292 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500293 return false;
294 }
295 }
296 return true;
297}
298
299
300void
301AdjLsa::addNptEntries(Nlsr& pnlsr)
302{
akmhoque157b0a42014-05-13 00:26:37 -0500303 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500304 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500305 }
306}
307
308
309void
310AdjLsa::removeNptEntries(Nlsr& pnlsr)
311{
akmhoque157b0a42014-05-13 00:26:37 -0500312 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500313 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500314 }
315}
316
akmhoque674b0b12014-05-20 14:33:28 -0500317void
318AdjLsa::writeLog()
319{
320 _LOG_DEBUG("Adj Lsa: ");
321 _LOG_DEBUG(" Origination Router: " << m_origRouter);
322 _LOG_DEBUG(" Ls Type: " << m_lsType);
323 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500324 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500325 _LOG_DEBUG(" Adjacents: ");
326 int i = 1;
327 std::list<Adjacent> al = m_adl.getAdjList();
328 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
329 {
330 _LOG_DEBUG(" Adjacent " << i << ": ");;
331 _LOG_DEBUG(" Adjacent Name: " << (*it).getName());
332 _LOG_DEBUG(" Connecting FaceUri: " << (*it).getConnectingFaceUri());
333 _LOG_DEBUG(" Link Cost: " << (*it).getLinkCost());
334 }
akmhoque2f423352014-06-03 11:49:35 -0500335 _LOG_DEBUG("adj_lsa_end");
akmhoque53353462014-04-22 08:43:45 -0500336}
337
338}//namespace nlsr