src: Replace Tokenizer with the methods of ndn::Name
Purpose of this commit is to replace Tokenizer in all
places except conf-file-processor.*pp with the methods
of ndn::Name. One static method in class NameHelper
is added. Tokenizer will be removed finally when
configuration file parsing commit is complete.
Refs: 1533
Change-Id: I14a67655eb963c5cc5b045c3c9f7d516e405ff19
diff --git a/src/lsa.cpp b/src/lsa.cpp
index 5f76eda..2ad95c5 100644
--- a/src/lsa.cpp
+++ b/src/lsa.cpp
@@ -4,6 +4,8 @@
#include <algorithm>
#include <cmath>
#include <limits>
+#include <boost/tokenizer.hpp>
+#include <boost/algorithm/string.hpp>
#include "nlsr.hpp"
#include "lsa.hpp"
@@ -17,23 +19,24 @@
using namespace std;
-string
+const ndn::Name
NameLsa::getKey() const
{
- string key;
- key = m_origRouter + "/" + boost::lexical_cast<std::string>(1);
+ ndn::Name key = m_origRouter;
+ key.append("name");
return key;
}
-NameLsa::NameLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt,
+NameLsa::NameLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
+ uint32_t lt,
NamePrefixList& npl)
{
m_origRouter = origR;
m_lsType = lst;
m_lsSeqNo = lsn;
m_lifeTime = lt;
- std::list<string> nl = npl.getNameList();
- for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
+ std::list<ndn::Name>& nl = npl.getNameList();
+ for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
{
addName((*it));
}
@@ -43,44 +46,45 @@
NameLsa::getData()
{
string nameLsaData;
- nameLsaData = m_origRouter + "|" + boost::lexical_cast<std::string>(1) + "|"
+ nameLsaData = m_origRouter.toUri() + "|" + "name" + "|"
+ boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
+ boost::lexical_cast<std::string>(m_lifeTime);
nameLsaData += "|";
nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize());
- std::list<string> nl = m_npl.getNameList();
- for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
+ std::list<ndn::Name> nl = m_npl.getNameList();
+ for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
{
nameLsaData += "|";
- nameLsaData += (*it);
+ nameLsaData += (*it).toUri();
}
return nameLsaData + "|";
}
bool
-NameLsa::initializeFromContent(string content)
+NameLsa::initializeFromContent(const std::string& content)
{
uint32_t numName = 0;
- Tokenizer nt(content, "|");
- m_origRouter = nt.getNextToken();
- if (m_origRouter.empty())
+ boost::char_separator<char> sep("|");
+ boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
+ boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
+ tokens.begin();
+ m_origRouter = ndn::Name(*tok_iter++);
+ if (!(m_origRouter.size() > 0))
{
return false;
}
- try
- {
- m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
- m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
- m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
- numName = boost::lexical_cast<uint32_t>(nt.getNextToken());
+ try {
+ m_lsType = *tok_iter++;
+ m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
+ m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
+ numName = boost::lexical_cast<uint32_t>(*tok_iter++);
}
- catch (std::exception& e)
- {
+ catch (std::exception& e) {
return false;
}
for (uint32_t i = 0; i < numName; i++)
{
- string name = nt.getNextToken();
+ string name(*tok_iter++);
addName(name);
}
return true;
@@ -96,13 +100,13 @@
{
os << "Name Lsa: " << endl;
os << " Origination Router: " << nLsa.getOrigRouter() << endl;
- os << " Ls Type: " << (unsigned short)nLsa.getLsType() << endl;
+ os << " Ls Type: " << nLsa.getLsType() << endl;
os << " Ls Seq No: " << (unsigned int)nLsa.getLsSeqNo() << endl;
os << " Ls Lifetime: " << (unsigned int)nLsa.getLifeTime() << endl;
os << " Names: " << endl;
int i = 1;
- std::list<string> nl = nLsa.getNpl().getNameList();
- for (std::list<string>::iterator it = nl.begin(); it != nl.end(); it++)
+ std::list<ndn::Name> nl = nLsa.getNpl().getNameList();
+ for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
{
os << " Name " << i << ": " << (*it) << endl;
}
@@ -111,7 +115,8 @@
-CoordinateLsa::CoordinateLsa(string origR, uint8_t lst, uint32_t lsn,
+CoordinateLsa::CoordinateLsa(const ndn::Name& origR, const string lst,
+ uint32_t lsn,
uint32_t lt
, double r, double theta)
{
@@ -123,11 +128,11 @@
m_corTheta = theta;
}
-string
+const ndn::Name
CoordinateLsa::getKey() const
{
- string key;
- key = m_origRouter + "/" + boost::lexical_cast<std::string>(3);
+ ndn::Name key = m_origRouter;
+ key.append("coordinate");
return key;
}
@@ -144,8 +149,9 @@
CoordinateLsa::getData()
{
string corLsaData;
- corLsaData = m_origRouter + "|";
- corLsaData += (boost::lexical_cast<std::string>(3) + "|");
+ corLsaData = m_origRouter.toUri() + "|";
+ corLsaData += "coordinate";
+ corLsaData += "|";
corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
corLsaData += (boost::lexical_cast<std::string>(m_lifeTime) + "|");
corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
@@ -154,24 +160,25 @@
}
bool
-CoordinateLsa::initializeFromContent(string content)
+CoordinateLsa::initializeFromContent(const std::string& content)
{
- Tokenizer nt(content, "|");
- m_origRouter = nt.getNextToken();
- if (m_origRouter.empty())
+ boost::char_separator<char> sep("|");
+ boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
+ boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
+ tokens.begin();
+ m_origRouter = ndn::Name(*tok_iter++);
+ if (!(m_origRouter.size() > 0))
{
return false;
}
- try
- {
- m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
- m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
- m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
- m_corRad = boost::lexical_cast<double>(nt.getNextToken());
- m_corTheta = boost::lexical_cast<double>(nt.getNextToken());
+ try {
+ m_lsType = *tok_iter++;
+ m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
+ m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
+ m_corRad = boost::lexical_cast<double>(*tok_iter++);
+ m_corTheta = boost::lexical_cast<double>(*tok_iter++);
}
- catch (std::exception& e)
- {
+ catch (std::exception& e) {
return false;
}
return true;
@@ -182,7 +189,7 @@
{
os << "Cor Lsa: " << endl;
os << " Origination Router: " << cLsa.getOrigRouter() << endl;
- os << " Ls Type: " << (unsigned short)cLsa.getLsType() << endl;
+ os << " Ls Type: " << cLsa.getLsType() << endl;
os << " Ls Seq No: " << (unsigned int)cLsa.getLsSeqNo() << endl;
os << " Ls Lifetime: " << (unsigned int)cLsa.getLifeTime() << endl;
os << " Hyperbolic Radius: " << cLsa.getCorRadius() << endl;
@@ -191,7 +198,8 @@
}
-AdjLsa::AdjLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt,
+AdjLsa::AdjLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
+ uint32_t lt,
uint32_t nl , AdjacencyList& adl)
{
m_origRouter = origR;
@@ -209,11 +217,11 @@
}
}
-string
-AdjLsa::getKey()
+const ndn::Name
+AdjLsa::getKey() const
{
- string key;
- key = m_origRouter + "/" + boost::lexical_cast<std::string>(2);
+ ndn::Name key = m_origRouter;
+ key.append("adjacency");
return key;
}
@@ -228,7 +236,7 @@
AdjLsa::getData()
{
string adjLsaData;
- adjLsaData = m_origRouter + "|" + boost::lexical_cast<std::string>(2) + "|"
+ adjLsaData = m_origRouter.toUri() + "|" + "adjacency" + "|"
+ boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
+ boost::lexical_cast<std::string>(m_lifeTime);
adjLsaData += "|";
@@ -237,7 +245,7 @@
for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
{
adjLsaData += "|";
- adjLsaData += (*it).getName();
+ adjLsaData += (*it).getName().toUri();
adjLsaData += "|";
adjLsaData += boost::lexical_cast<std::string>((*it).getConnectingFace());
adjLsaData += "|";
@@ -247,38 +255,37 @@
}
bool
-AdjLsa::initializeFromContent(string content)
+AdjLsa::initializeFromContent(const std::string& content)
{
uint32_t numLink = 0;
- Tokenizer nt(content, "|");
- m_origRouter = nt.getNextToken();
- if (m_origRouter.empty())
+ boost::char_separator<char> sep("|");
+ boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
+ boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
+ tokens.begin();
+ m_origRouter = ndn::Name(*tok_iter++);
+ if (!(m_origRouter.size() > 0))
{
return false;
}
- try
- {
- m_lsType = boost::lexical_cast<uint8_t>(nt.getNextToken());
- m_lsSeqNo = boost::lexical_cast<uint32_t>(nt.getNextToken());
- m_lifeTime = boost::lexical_cast<uint32_t>(nt.getNextToken());
- numLink = boost::lexical_cast<uint32_t>(nt.getNextToken());
+ try {
+ m_lsType = *tok_iter++;
+ m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
+ m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
+ numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
}
- catch (std::exception& e)
- {
+ catch (std::exception& e) {
return false;
}
for (uint32_t i = 0; i < numLink; i++)
{
- try
- {
- string adjName = nt.getNextToken();
- int connectingFace = boost::lexical_cast<int>(nt.getNextToken());
- double linkCost = boost::lexical_cast<double>(nt.getNextToken());
+ try {
+ string adjName(*tok_iter++);
+ int connectingFace = boost::lexical_cast<int>(*tok_iter++);
+ double linkCost = boost::lexical_cast<double>(*tok_iter++);
Adjacent adjacent(adjName, connectingFace, linkCost, 0, 0);
addAdjacent(adjacent);
}
- catch (std::exception& e)
- {
+ catch (std::exception& e) {
return false;
}
}
@@ -291,8 +298,7 @@
{
if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
{
- pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter(),
- pnlsr);
+ pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
}
}
@@ -302,7 +308,7 @@
{
if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
{
- pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter(), pnlsr);
+ pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
}
}
@@ -313,7 +319,7 @@
{
os << "Adj Lsa: " << endl;
os << " Origination Router: " << aLsa.getOrigRouter() << endl;
- os << " Ls Type: " << (unsigned short)aLsa.getLsType() << endl;
+ os << " Ls Type: " << aLsa.getLsType() << endl;
os << " Ls Seq No: " << (unsigned int)aLsa.getLsSeqNo() << endl;
os << " Ls Lifetime: " << (unsigned int)aLsa.getLifeTime() << endl;
os << " No Link: " << (unsigned int)aLsa.getNoLink() << endl;