blob: 5f76edaea96dca3def84dc41f8ce61df319fafb3 [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>
7
8#include "nlsr.hpp"
9#include "lsa.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050010#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050011#include "adjacent.hpp"
12#include "utility/tokenizer.hpp"
13
14
15namespace nlsr {
16
17using namespace std;
18
19
20string
akmhoqueb6450b12014-04-24 00:01:03 -050021NameLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050022{
23 string key;
24 key = m_origRouter + "/" + boost::lexical_cast<std::string>(1);
25 return key;
26}
27
akmhoquec8a10f72014-04-25 18:42:55 -050028NameLsa::NameLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt,
akmhoquefdbddb12014-05-02 18:35:19 -050029 NamePrefixList& npl)
akmhoque53353462014-04-22 08:43:45 -050030{
31 m_origRouter = origR;
32 m_lsType = lst;
33 m_lsSeqNo = lsn;
34 m_lifeTime = lt;
35 std::list<string> nl = npl.getNameList();
36 for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
37 {
38 addName((*it));
39 }
40}
41
42string
43NameLsa::getData()
44{
45 string nameLsaData;
46 nameLsaData = m_origRouter + "|" + boost::lexical_cast<std::string>(1) + "|"
47 + 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());
51 std::list<string> nl = m_npl.getNameList();
52 for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
53 {
54 nameLsaData += "|";
55 nameLsaData += (*it);
56 }
57 return nameLsaData + "|";
58}
59
60bool
61NameLsa::initializeFromContent(string content)
62{
63 uint32_t numName = 0;
64 Tokenizer nt(content, "|");
65 m_origRouter = nt.getNextToken();
66 if (m_origRouter.empty())
67 {
68 return false;
69 }
70 try
71 {
72 m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
73 m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
74 m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
75 numName = boost::lexical_cast<uint32_t>(nt.getNextToken());
76 }
77 catch (std::exception& e)
78 {
79 return false;
80 }
81 for (uint32_t i = 0; i < numName; i++)
82 {
83 string name = nt.getNextToken();
84 addName(name);
85 }
86 return true;
87}
88
89void
90NameLsa::writeLog()
91{
92}
93
94std::ostream&
95operator<<(std::ostream& os, NameLsa& nLsa)
96{
97 os << "Name Lsa: " << endl;
98 os << " Origination Router: " << nLsa.getOrigRouter() << endl;
99 os << " Ls Type: " << (unsigned short)nLsa.getLsType() << endl;
100 os << " Ls Seq No: " << (unsigned int)nLsa.getLsSeqNo() << endl;
101 os << " Ls Lifetime: " << (unsigned int)nLsa.getLifeTime() << endl;
102 os << " Names: " << endl;
103 int i = 1;
104 std::list<string> nl = nLsa.getNpl().getNameList();
105 for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
106 {
107 os << " Name " << i << ": " << (*it) << endl;
108 }
109 return os;
110}
111
112
113
akmhoqueb6450b12014-04-24 00:01:03 -0500114CoordinateLsa::CoordinateLsa(string origR, uint8_t lst, uint32_t lsn,
115 uint32_t lt
116 , double r, double theta)
akmhoque53353462014-04-22 08:43:45 -0500117{
118 m_origRouter = origR;
119 m_lsType = lst;
120 m_lsSeqNo = lsn;
121 m_lifeTime = lt;
122 m_corRad = r;
123 m_corTheta = theta;
124}
125
126string
akmhoqueb6450b12014-04-24 00:01:03 -0500127CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500128{
129 string key;
130 key = m_origRouter + "/" + boost::lexical_cast<std::string>(3);
131 return key;
132}
133
134bool
akmhoquefdbddb12014-05-02 18:35:19 -0500135CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500136{
137 return (std::abs(m_corRad - clsa.getCorRadius()) <
138 std::numeric_limits<double>::epsilon()) &&
139 (std::abs(m_corTheta - clsa.getCorTheta()) <
140 std::numeric_limits<double>::epsilon());
141}
142
143string
akmhoqueb6450b12014-04-24 00:01:03 -0500144CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500145{
146 string corLsaData;
147 corLsaData = m_origRouter + "|";
148 corLsaData += (boost::lexical_cast<std::string>(3) + "|");
149 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
akmhoqueb6450b12014-04-24 00:01:03 -0500157CoordinateLsa::initializeFromContent(string content)
akmhoque53353462014-04-22 08:43:45 -0500158{
159 Tokenizer nt(content, "|");
160 m_origRouter = nt.getNextToken();
161 if (m_origRouter.empty())
162 {
163 return false;
164 }
165 try
166 {
167 m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
168 m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
169 m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
170 m_corRad = boost::lexical_cast<double>(nt.getNextToken());
171 m_corTheta = boost::lexical_cast<double>(nt.getNextToken());
172 }
173 catch (std::exception& e)
174 {
175 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;
185 os << " Ls Type: " << (unsigned short)cLsa.getLsType() << endl;
186 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
194AdjLsa::AdjLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500195 uint32_t nl , AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -0500196{
197 m_origRouter = origR;
198 m_lsType = lst;
199 m_lsSeqNo = lsn;
200 m_lifeTime = lt;
201 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500202 std::list<Adjacent> al = adl.getAdjList();
akmhoque53353462014-04-22 08:43:45 -0500203 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
204 {
205 if ((*it).getStatus() == 1)
206 {
207 addAdjacent((*it));
208 }
209 }
210}
211
212string
213AdjLsa::getKey()
214{
215 string key;
216 key = m_origRouter + "/" + boost::lexical_cast<std::string>(2);
217 return key;
218}
219
220bool
akmhoquefdbddb12014-05-02 18:35:19 -0500221AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500222{
akmhoquefdbddb12014-05-02 18:35:19 -0500223 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500224}
225
226
227string
228AdjLsa::getData()
229{
230 string adjLsaData;
231 adjLsaData = m_origRouter + "|" + boost::lexical_cast<std::string>(2) + "|"
232 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
233 + boost::lexical_cast<std::string>(m_lifeTime);
234 adjLsaData += "|";
235 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
236 std::list<Adjacent> al = m_adl.getAdjList();
237 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
238 {
239 adjLsaData += "|";
240 adjLsaData += (*it).getName();
241 adjLsaData += "|";
242 adjLsaData += boost::lexical_cast<std::string>((*it).getConnectingFace());
243 adjLsaData += "|";
244 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
245 }
246 return adjLsaData + "|";
247}
248
249bool
250AdjLsa::initializeFromContent(string content)
251{
252 uint32_t numLink = 0;
253 Tokenizer nt(content, "|");
254 m_origRouter = nt.getNextToken();
255 if (m_origRouter.empty())
256 {
257 return false;
258 }
259 try
260 {
261 m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
262 m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
263 m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
264 numLink = boost::lexical_cast<uint32_t>(nt.getNextToken());
265 }
266 catch (std::exception& e)
267 {
268 return false;
269 }
270 for (uint32_t i = 0; i < numLink; i++)
271 {
272 try
273 {
274 string adjName = nt.getNextToken();
275 int connectingFace = boost::lexical_cast<int>(nt.getNextToken());
276 double linkCost = boost::lexical_cast<double>(nt.getNextToken());
277 Adjacent adjacent(adjName, connectingFace, linkCost, 0, 0);
278 addAdjacent(adjacent);
279 }
280 catch (std::exception& e)
281 {
282 return false;
283 }
284 }
285 return true;
286}
287
288
289void
290AdjLsa::addNptEntries(Nlsr& pnlsr)
291{
292 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
293 {
akmhoquefdbddb12014-05-02 18:35:19 -0500294 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter(),
akmhoquec8a10f72014-04-25 18:42:55 -0500295 pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500296 }
297}
298
299
300void
301AdjLsa::removeNptEntries(Nlsr& pnlsr)
302{
303 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
304 {
akmhoquefdbddb12014-05-02 18:35:19 -0500305 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter(), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500306 }
307}
308
309
310
311std::ostream&
312operator<<(std::ostream& os, AdjLsa& aLsa)
313{
314 os << "Adj Lsa: " << endl;
315 os << " Origination Router: " << aLsa.getOrigRouter() << endl;
316 os << " Ls Type: " << (unsigned short)aLsa.getLsType() << endl;
317 os << " Ls Seq No: " << (unsigned int)aLsa.getLsSeqNo() << endl;
318 os << " Ls Lifetime: " << (unsigned int)aLsa.getLifeTime() << endl;
319 os << " No Link: " << (unsigned int)aLsa.getNoLink() << endl;
320 os << " Adjacents: " << endl;
321 int i = 1;
322 std::list<Adjacent> al = aLsa.getAdl().getAdjList();
323 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
324 {
325 os << " Adjacent " << i << ": " << endl;
326 os << " Adjacent Name: " << (*it).getName() << endl;
327 os << " Connecting Face: " << (*it).getConnectingFace() << endl;
328 os << " Link Cost: " << (*it).getLinkCost() << endl;
329 }
330 return os;
331}
332
333}//namespace nlsr