blob: e2b6dcfde3fb347e04962169d4827b46aeaec196 [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"
10#include "npl.hpp"
11#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
28NameLsa::NameLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt, Npl npl)
29{
30 m_origRouter = origR;
31 m_lsType = lst;
32 m_lsSeqNo = lsn;
33 m_lifeTime = lt;
34 std::list<string> nl = npl.getNameList();
35 for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
36 {
37 addName((*it));
38 }
39}
40
41string
42NameLsa::getData()
43{
44 string nameLsaData;
45 nameLsaData = m_origRouter + "|" + boost::lexical_cast<std::string>(1) + "|"
46 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
47 + boost::lexical_cast<std::string>(m_lifeTime);
48 nameLsaData += "|";
49 nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize());
50 std::list<string> nl = m_npl.getNameList();
51 for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
52 {
53 nameLsaData += "|";
54 nameLsaData += (*it);
55 }
56 return nameLsaData + "|";
57}
58
59bool
60NameLsa::initializeFromContent(string content)
61{
62 uint32_t numName = 0;
63 Tokenizer nt(content, "|");
64 m_origRouter = nt.getNextToken();
65 if (m_origRouter.empty())
66 {
67 return false;
68 }
69 try
70 {
71 m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
72 m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
73 m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
74 numName = boost::lexical_cast<uint32_t>(nt.getNextToken());
75 }
76 catch (std::exception& e)
77 {
78 return false;
79 }
80 for (uint32_t i = 0; i < numName; i++)
81 {
82 string name = nt.getNextToken();
83 addName(name);
84 }
85 return true;
86}
87
88void
89NameLsa::writeLog()
90{
91}
92
93std::ostream&
94operator<<(std::ostream& os, NameLsa& nLsa)
95{
96 os << "Name Lsa: " << endl;
97 os << " Origination Router: " << nLsa.getOrigRouter() << endl;
98 os << " Ls Type: " << (unsigned short)nLsa.getLsType() << endl;
99 os << " Ls Seq No: " << (unsigned int)nLsa.getLsSeqNo() << endl;
100 os << " Ls Lifetime: " << (unsigned int)nLsa.getLifeTime() << endl;
101 os << " Names: " << endl;
102 int i = 1;
103 std::list<string> nl = nLsa.getNpl().getNameList();
104 for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
105 {
106 os << " Name " << i << ": " << (*it) << endl;
107 }
108 return os;
109}
110
111
112
akmhoqueb6450b12014-04-24 00:01:03 -0500113CoordinateLsa::CoordinateLsa(string origR, uint8_t lst, uint32_t lsn,
114 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
125string
akmhoqueb6450b12014-04-24 00:01:03 -0500126CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500127{
128 string key;
129 key = m_origRouter + "/" + boost::lexical_cast<std::string>(3);
130 return key;
131}
132
133bool
akmhoqueb6450b12014-04-24 00:01:03 -0500134CoordinateLsa::isEqual(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;
146 corLsaData = m_origRouter + "|";
147 corLsaData += (boost::lexical_cast<std::string>(3) + "|");
148 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
149 corLsaData += (boost::lexical_cast<std::string>(m_lifeTime) + "|");
150 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
151 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
152 return corLsaData;
153}
154
155bool
akmhoqueb6450b12014-04-24 00:01:03 -0500156CoordinateLsa::initializeFromContent(string content)
akmhoque53353462014-04-22 08:43:45 -0500157{
158 Tokenizer nt(content, "|");
159 m_origRouter = nt.getNextToken();
160 if (m_origRouter.empty())
161 {
162 return false;
163 }
164 try
165 {
166 m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
167 m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
168 m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
169 m_corRad = boost::lexical_cast<double>(nt.getNextToken());
170 m_corTheta = boost::lexical_cast<double>(nt.getNextToken());
171 }
172 catch (std::exception& e)
173 {
174 return false;
175 }
176 return true;
177}
178
179std::ostream&
akmhoqueb6450b12014-04-24 00:01:03 -0500180operator<<(std::ostream& os, const CoordinateLsa& cLsa)
akmhoque53353462014-04-22 08:43:45 -0500181{
182 os << "Cor Lsa: " << endl;
183 os << " Origination Router: " << cLsa.getOrigRouter() << endl;
184 os << " Ls Type: " << (unsigned short)cLsa.getLsType() << endl;
185 os << " Ls Seq No: " << (unsigned int)cLsa.getLsSeqNo() << endl;
186 os << " Ls Lifetime: " << (unsigned int)cLsa.getLifeTime() << endl;
187 os << " Hyperbolic Radius: " << cLsa.getCorRadius() << endl;
188 os << " Hyperbolic Theta: " << cLsa.getCorTheta() << endl;
189 return os;
190}
191
192
193AdjLsa::AdjLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt,
194 uint32_t nl , Adl padl)
195{
196 m_origRouter = origR;
197 m_lsType = lst;
198 m_lsSeqNo = lsn;
199 m_lifeTime = lt;
200 m_noLink = nl;
201 std::list<Adjacent> al = padl.getAdjList();
202 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
203 {
204 if ((*it).getStatus() == 1)
205 {
206 addAdjacent((*it));
207 }
208 }
209}
210
211string
212AdjLsa::getKey()
213{
214 string key;
215 key = m_origRouter + "/" + boost::lexical_cast<std::string>(2);
216 return key;
217}
218
219bool
220AdjLsa::isEqual(AdjLsa& alsa)
221{
222 return m_adl.isEqual(alsa.getAdl());
223}
224
225
226string
227AdjLsa::getData()
228{
229 string adjLsaData;
230 adjLsaData = m_origRouter + "|" + boost::lexical_cast<std::string>(2) + "|"
231 + 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();
236 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
237 {
238 adjLsaData += "|";
239 adjLsaData += (*it).getName();
240 adjLsaData += "|";
241 adjLsaData += boost::lexical_cast<std::string>((*it).getConnectingFace());
242 adjLsaData += "|";
243 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
244 }
245 return adjLsaData + "|";
246}
247
248bool
249AdjLsa::initializeFromContent(string content)
250{
251 uint32_t numLink = 0;
252 Tokenizer nt(content, "|");
253 m_origRouter = nt.getNextToken();
254 if (m_origRouter.empty())
255 {
256 return false;
257 }
258 try
259 {
260 m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
261 m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
262 m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
263 numLink = boost::lexical_cast<uint32_t>(nt.getNextToken());
264 }
265 catch (std::exception& e)
266 {
267 return false;
268 }
269 for (uint32_t i = 0; i < numLink; i++)
270 {
271 try
272 {
273 string adjName = nt.getNextToken();
274 int connectingFace = boost::lexical_cast<int>(nt.getNextToken());
275 double linkCost = boost::lexical_cast<double>(nt.getNextToken());
276 Adjacent adjacent(adjName, connectingFace, linkCost, 0, 0);
277 addAdjacent(adjacent);
278 }
279 catch (std::exception& e)
280 {
281 return false;
282 }
283 }
284 return true;
285}
286
287
288void
289AdjLsa::addNptEntries(Nlsr& pnlsr)
290{
291 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
292 {
293 pnlsr.getNpt().addNpteByDestName(getOrigRouter(), getOrigRouter(), pnlsr);
294 }
295}
296
297
298void
299AdjLsa::removeNptEntries(Nlsr& pnlsr)
300{
301 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
302 {
303 pnlsr.getNpt().removeNpte(getOrigRouter(), getOrigRouter(), pnlsr);
304 }
305}
306
307
308
309std::ostream&
310operator<<(std::ostream& os, AdjLsa& aLsa)
311{
312 os << "Adj Lsa: " << endl;
313 os << " Origination Router: " << aLsa.getOrigRouter() << endl;
314 os << " Ls Type: " << (unsigned short)aLsa.getLsType() << endl;
315 os << " Ls Seq No: " << (unsigned int)aLsa.getLsSeqNo() << endl;
316 os << " Ls Lifetime: " << (unsigned int)aLsa.getLifeTime() << endl;
317 os << " No Link: " << (unsigned int)aLsa.getNoLink() << endl;
318 os << " Adjacents: " << endl;
319 int i = 1;
320 std::list<Adjacent> al = aLsa.getAdl().getAdjList();
321 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
322 {
323 os << " Adjacent " << i << ": " << endl;
324 os << " Adjacent Name: " << (*it).getName() << endl;
325 os << " Connecting Face: " << (*it).getConnectingFace() << endl;
326 os << " Link Cost: " << (*it).getLinkCost() << endl;
327 }
328 return os;
329}
330
331}//namespace nlsr