blob: 0bd450c2fa2d413780c29fafd7f115b67be72e76 [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
21NameLsa::getKey()
22{
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
113CorLsa::CorLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt
114 , double r, double theta)
115{
116 m_origRouter = origR;
117 m_lsType = lst;
118 m_lsSeqNo = lsn;
119 m_lifeTime = lt;
120 m_corRad = r;
121 m_corTheta = theta;
122}
123
124string
125CorLsa::getKey()
126{
127 string key;
128 key = m_origRouter + "/" + boost::lexical_cast<std::string>(3);
129 return key;
130}
131
132bool
133CorLsa::isEqual(CorLsa& clsa)
134{
135 return (std::abs(m_corRad - clsa.getCorRadius()) <
136 std::numeric_limits<double>::epsilon()) &&
137 (std::abs(m_corTheta - clsa.getCorTheta()) <
138 std::numeric_limits<double>::epsilon());
139}
140
141string
142CorLsa::getData()
143{
144 string corLsaData;
145 corLsaData = m_origRouter + "|";
146 corLsaData += (boost::lexical_cast<std::string>(3) + "|");
147 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
148 corLsaData += (boost::lexical_cast<std::string>(m_lifeTime) + "|");
149 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
150 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
151 return corLsaData;
152}
153
154bool
155CorLsa::initializeFromContent(string content)
156{
157 Tokenizer nt(content, "|");
158 m_origRouter = nt.getNextToken();
159 if (m_origRouter.empty())
160 {
161 return false;
162 }
163 try
164 {
165 m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
166 m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
167 m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
168 m_corRad = boost::lexical_cast<double>(nt.getNextToken());
169 m_corTheta = boost::lexical_cast<double>(nt.getNextToken());
170 }
171 catch (std::exception& e)
172 {
173 return false;
174 }
175 return true;
176}
177
178std::ostream&
179operator<<(std::ostream& os, CorLsa& cLsa)
180{
181 os << "Cor Lsa: " << endl;
182 os << " Origination Router: " << cLsa.getOrigRouter() << endl;
183 os << " Ls Type: " << (unsigned short)cLsa.getLsType() << endl;
184 os << " Ls Seq No: " << (unsigned int)cLsa.getLsSeqNo() << endl;
185 os << " Ls Lifetime: " << (unsigned int)cLsa.getLifeTime() << endl;
186 os << " Hyperbolic Radius: " << cLsa.getCorRadius() << endl;
187 os << " Hyperbolic Theta: " << cLsa.getCorTheta() << endl;
188 return os;
189}
190
191
192AdjLsa::AdjLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt,
193 uint32_t nl , Adl padl)
194{
195 m_origRouter = origR;
196 m_lsType = lst;
197 m_lsSeqNo = lsn;
198 m_lifeTime = lt;
199 m_noLink = nl;
200 std::list<Adjacent> al = padl.getAdjList();
201 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
202 {
203 if ((*it).getStatus() == 1)
204 {
205 addAdjacent((*it));
206 }
207 }
208}
209
210string
211AdjLsa::getKey()
212{
213 string key;
214 key = m_origRouter + "/" + boost::lexical_cast<std::string>(2);
215 return key;
216}
217
218bool
219AdjLsa::isEqual(AdjLsa& alsa)
220{
221 return m_adl.isEqual(alsa.getAdl());
222}
223
224
225string
226AdjLsa::getData()
227{
228 string adjLsaData;
229 adjLsaData = m_origRouter + "|" + boost::lexical_cast<std::string>(2) + "|"
230 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
231 + boost::lexical_cast<std::string>(m_lifeTime);
232 adjLsaData += "|";
233 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
234 std::list<Adjacent> al = m_adl.getAdjList();
235 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
236 {
237 adjLsaData += "|";
238 adjLsaData += (*it).getName();
239 adjLsaData += "|";
240 adjLsaData += boost::lexical_cast<std::string>((*it).getConnectingFace());
241 adjLsaData += "|";
242 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
243 }
244 return adjLsaData + "|";
245}
246
247bool
248AdjLsa::initializeFromContent(string content)
249{
250 uint32_t numLink = 0;
251 Tokenizer nt(content, "|");
252 m_origRouter = nt.getNextToken();
253 if (m_origRouter.empty())
254 {
255 return false;
256 }
257 try
258 {
259 m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
260 m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
261 m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
262 numLink = boost::lexical_cast<uint32_t>(nt.getNextToken());
263 }
264 catch (std::exception& e)
265 {
266 return false;
267 }
268 for (uint32_t i = 0; i < numLink; i++)
269 {
270 try
271 {
272 string adjName = nt.getNextToken();
273 int connectingFace = boost::lexical_cast<int>(nt.getNextToken());
274 double linkCost = boost::lexical_cast<double>(nt.getNextToken());
275 Adjacent adjacent(adjName, connectingFace, linkCost, 0, 0);
276 addAdjacent(adjacent);
277 }
278 catch (std::exception& e)
279 {
280 return false;
281 }
282 }
283 return true;
284}
285
286
287void
288AdjLsa::addNptEntries(Nlsr& pnlsr)
289{
290 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
291 {
292 pnlsr.getNpt().addNpteByDestName(getOrigRouter(), getOrigRouter(), pnlsr);
293 }
294}
295
296
297void
298AdjLsa::removeNptEntries(Nlsr& pnlsr)
299{
300 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
301 {
302 pnlsr.getNpt().removeNpte(getOrigRouter(), getOrigRouter(), pnlsr);
303 }
304}
305
306
307
308std::ostream&
309operator<<(std::ostream& os, AdjLsa& aLsa)
310{
311 os << "Adj Lsa: " << endl;
312 os << " Origination Router: " << aLsa.getOrigRouter() << endl;
313 os << " Ls Type: " << (unsigned short)aLsa.getLsType() << endl;
314 os << " Ls Seq No: " << (unsigned int)aLsa.getLsSeqNo() << endl;
315 os << " Ls Lifetime: " << (unsigned int)aLsa.getLifeTime() << endl;
316 os << " No Link: " << (unsigned int)aLsa.getNoLink() << endl;
317 os << " Adjacents: " << endl;
318 int i = 1;
319 std::list<Adjacent> al = aLsa.getAdl().getAdjList();
320 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
321 {
322 os << " Adjacent " << i << ": " << endl;
323 os << " Adjacent Name: " << (*it).getName() << endl;
324 os << " Connecting Face: " << (*it).getConnectingFace() << endl;
325 os << " Link Cost: " << (*it).getLinkCost() << endl;
326 }
327 return os;
328}
329
330}//namespace nlsr