blob: 14d033a2a78f06719115aa1d6d2fedc837ac1332 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <string>
2#include <iostream>
3#include <sstream>
4#include <algorithm>
5#include <cmath>
6#include <limits>
akmhoque31d1d4b2014-05-05 22:08:14 -05007#include <boost/tokenizer.hpp>
8#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -05009
10#include "nlsr.hpp"
11#include "lsa.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050012#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050013#include "adjacent.hpp"
akmhoque53353462014-04-22 08:43:45 -050014
15
16namespace nlsr {
17
18using namespace std;
19
akmhoque31d1d4b2014-05-05 22:08:14 -050020const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -050021NameLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050022{
akmhoque31d1d4b2014-05-05 22:08:14 -050023 ndn::Name key = m_origRouter;
24 key.append("name");
akmhoque53353462014-04-22 08:43:45 -050025 return key;
26}
27
akmhoque31d1d4b2014-05-05 22:08:14 -050028NameLsa::NameLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
29 uint32_t lt,
akmhoquefdbddb12014-05-02 18:35:19 -050030 NamePrefixList& npl)
akmhoque53353462014-04-22 08:43:45 -050031{
32 m_origRouter = origR;
33 m_lsType = lst;
34 m_lsSeqNo = lsn;
35 m_lifeTime = lt;
akmhoque31d1d4b2014-05-05 22:08:14 -050036 std::list<ndn::Name>& nl = npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050037 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050038 addName((*it));
39 }
40}
41
42string
43NameLsa::getData()
44{
45 string nameLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -050046 nameLsaData = m_origRouter.toUri() + "|" + "name" + "|"
akmhoque53353462014-04-22 08:43:45 -050047 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
48 + boost::lexical_cast<std::string>(m_lifeTime);
49 nameLsaData += "|";
50 nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize());
akmhoque31d1d4b2014-05-05 22:08:14 -050051 std::list<ndn::Name> nl = m_npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050052 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050053 nameLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -050054 nameLsaData += (*it).toUri();
akmhoque53353462014-04-22 08:43:45 -050055 }
56 return nameLsaData + "|";
57}
58
59bool
akmhoque31d1d4b2014-05-05 22:08:14 -050060NameLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -050061{
62 uint32_t numName = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -050063 boost::char_separator<char> sep("|");
64 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
65 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
66 tokens.begin();
67 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -050068 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -050069 return false;
70 }
akmhoque31d1d4b2014-05-05 22:08:14 -050071 try {
72 m_lsType = *tok_iter++;
73 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
74 m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
75 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -050076 }
akmhoque31d1d4b2014-05-05 22:08:14 -050077 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -050078 return false;
79 }
akmhoque157b0a42014-05-13 00:26:37 -050080 for (uint32_t i = 0; i < numName; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -050081 string name(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -050082 addName(name);
83 }
84 return true;
85}
86
87void
88NameLsa::writeLog()
89{
90}
91
92std::ostream&
93operator<<(std::ostream& os, NameLsa& nLsa)
94{
95 os << "Name Lsa: " << endl;
96 os << " Origination Router: " << nLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050097 os << " Ls Type: " << nLsa.getLsType() << endl;
akmhoque53353462014-04-22 08:43:45 -050098 os << " Ls Seq No: " << (unsigned int)nLsa.getLsSeqNo() << endl;
99 os << " Ls Lifetime: " << (unsigned int)nLsa.getLifeTime() << endl;
100 os << " Names: " << endl;
101 int i = 1;
akmhoque31d1d4b2014-05-05 22:08:14 -0500102 std::list<ndn::Name> nl = nLsa.getNpl().getNameList();
103 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
akmhoque53353462014-04-22 08:43:45 -0500104 {
105 os << " Name " << i << ": " << (*it) << endl;
106 }
107 return os;
108}
109
110
111
akmhoque31d1d4b2014-05-05 22:08:14 -0500112CoordinateLsa::CoordinateLsa(const ndn::Name& origR, const string lst,
113 uint32_t lsn,
akmhoqueb6450b12014-04-24 00:01:03 -0500114 uint32_t lt
115 , double r, double theta)
akmhoque53353462014-04-22 08:43:45 -0500116{
117 m_origRouter = origR;
118 m_lsType = lst;
119 m_lsSeqNo = lsn;
120 m_lifeTime = lt;
121 m_corRad = r;
122 m_corTheta = theta;
123}
124
akmhoque31d1d4b2014-05-05 22:08:14 -0500125const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -0500126CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500127{
akmhoque31d1d4b2014-05-05 22:08:14 -0500128 ndn::Name key = m_origRouter;
129 key.append("coordinate");
akmhoque53353462014-04-22 08:43:45 -0500130 return key;
131}
132
133bool
akmhoquefdbddb12014-05-02 18:35:19 -0500134CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500135{
136 return (std::abs(m_corRad - clsa.getCorRadius()) <
137 std::numeric_limits<double>::epsilon()) &&
138 (std::abs(m_corTheta - clsa.getCorTheta()) <
139 std::numeric_limits<double>::epsilon());
140}
141
142string
akmhoqueb6450b12014-04-24 00:01:03 -0500143CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500144{
145 string corLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500146 corLsaData = m_origRouter.toUri() + "|";
147 corLsaData += "coordinate";
148 corLsaData += "|";
akmhoque53353462014-04-22 08:43:45 -0500149 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
150 corLsaData += (boost::lexical_cast<std::string>(m_lifeTime) + "|");
151 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
152 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
153 return corLsaData;
154}
155
156bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500157CoordinateLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500158{
akmhoque31d1d4b2014-05-05 22:08:14 -0500159 boost::char_separator<char> sep("|");
160 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
161 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
162 tokens.begin();
163 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500164 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500165 return false;
166 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500167 try {
168 m_lsType = *tok_iter++;
169 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
170 m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
171 m_corRad = boost::lexical_cast<double>(*tok_iter++);
172 m_corTheta = boost::lexical_cast<double>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500173 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500174 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500175 return false;
176 }
177 return true;
178}
179
180std::ostream&
akmhoqueb6450b12014-04-24 00:01:03 -0500181operator<<(std::ostream& os, const CoordinateLsa& cLsa)
akmhoque53353462014-04-22 08:43:45 -0500182{
183 os << "Cor Lsa: " << endl;
184 os << " Origination Router: " << cLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500185 os << " Ls Type: " << cLsa.getLsType() << endl;
akmhoque53353462014-04-22 08:43:45 -0500186 os << " Ls Seq No: " << (unsigned int)cLsa.getLsSeqNo() << endl;
187 os << " Ls Lifetime: " << (unsigned int)cLsa.getLifeTime() << endl;
188 os << " Hyperbolic Radius: " << cLsa.getCorRadius() << endl;
189 os << " Hyperbolic Theta: " << cLsa.getCorTheta() << endl;
190 return os;
191}
192
193
akmhoque31d1d4b2014-05-05 22:08:14 -0500194AdjLsa::AdjLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
195 uint32_t lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500196 uint32_t nl , AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -0500197{
198 m_origRouter = origR;
199 m_lsType = lst;
200 m_lsSeqNo = lsn;
201 m_lifeTime = lt;
202 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500203 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500204 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
205 if ((*it).getStatus() == 1) {
akmhoque53353462014-04-22 08:43:45 -0500206 addAdjacent((*it));
207 }
208 }
209}
210
akmhoque31d1d4b2014-05-05 22:08:14 -0500211const ndn::Name
212AdjLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500213{
akmhoque31d1d4b2014-05-05 22:08:14 -0500214 ndn::Name key = m_origRouter;
215 key.append("adjacency");
akmhoque53353462014-04-22 08:43:45 -0500216 return key;
217}
218
219bool
akmhoquefdbddb12014-05-02 18:35:19 -0500220AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500221{
akmhoquefdbddb12014-05-02 18:35:19 -0500222 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500223}
224
225
226string
227AdjLsa::getData()
228{
229 string adjLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500230 adjLsaData = m_origRouter.toUri() + "|" + "adjacency" + "|"
akmhoque53353462014-04-22 08:43:45 -0500231 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
232 + boost::lexical_cast<std::string>(m_lifeTime);
233 adjLsaData += "|";
234 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
235 std::list<Adjacent> al = m_adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500236 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500237 adjLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -0500238 adjLsaData += (*it).getName().toUri();
akmhoque53353462014-04-22 08:43:45 -0500239 adjLsaData += "|";
akmhoque157b0a42014-05-13 00:26:37 -0500240 adjLsaData += (*it).getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -0500241 adjLsaData += "|";
242 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
243 }
244 return adjLsaData + "|";
245}
246
247bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500248AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500249{
250 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500251 boost::char_separator<char> sep("|");
252 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
253 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
254 tokens.begin();
255 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500256 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500257 return false;
258 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500259 try {
260 m_lsType = *tok_iter++;
261 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
262 m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
263 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500264 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500265 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500266 return false;
267 }
akmhoque157b0a42014-05-13 00:26:37 -0500268 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500269 try {
270 string adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500271 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500272 double linkCost = boost::lexical_cast<double>(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500273 Adjacent adjacent(adjName, connectingFaceUri, linkCost, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500274 addAdjacent(adjacent);
275 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500276 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500277 return false;
278 }
279 }
280 return true;
281}
282
283
284void
285AdjLsa::addNptEntries(Nlsr& pnlsr)
286{
akmhoque157b0a42014-05-13 00:26:37 -0500287 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500288 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500289 }
290}
291
292
293void
294AdjLsa::removeNptEntries(Nlsr& pnlsr)
295{
akmhoque157b0a42014-05-13 00:26:37 -0500296 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500297 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500298 }
299}
300
301
302
303std::ostream&
304operator<<(std::ostream& os, AdjLsa& aLsa)
305{
306 os << "Adj Lsa: " << endl;
307 os << " Origination Router: " << aLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500308 os << " Ls Type: " << aLsa.getLsType() << endl;
akmhoque53353462014-04-22 08:43:45 -0500309 os << " Ls Seq No: " << (unsigned int)aLsa.getLsSeqNo() << endl;
310 os << " Ls Lifetime: " << (unsigned int)aLsa.getLifeTime() << endl;
311 os << " No Link: " << (unsigned int)aLsa.getNoLink() << endl;
312 os << " Adjacents: " << endl;
313 int i = 1;
314 std::list<Adjacent> al = aLsa.getAdl().getAdjList();
315 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
316 {
317 os << " Adjacent " << i << ": " << endl;
318 os << " Adjacent Name: " << (*it).getName() << endl;
akmhoque157b0a42014-05-13 00:26:37 -0500319 os << " Connecting FaceUri: " << (*it).getConnectingFaceUri() << endl;
akmhoque53353462014-04-22 08:43:45 -0500320 os << " Link Cost: " << (*it).getLinkCost() << endl;
321 }
322 return os;
323}
324
325}//namespace nlsr