lsa: simplify initializeFromContent and improve naming
refs: #4354 Change-Id: I5ddd5a9f482f59e6ca52061ac92514feab8c3070
This commit is contained in:
1 parent
22cc1a8d17
commit
0fa4c77987
6 files changed
+58
-78
No files matched your search
+39
-56
@@ -31,7 +31,6 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
namespace nlsr {
|
||||
@@ -53,6 +52,19 @@ Lsa::getKey() const
|
||||
return ndn::Name(m_origRouter).append(std::to_string(getType()));
|
||||
}
|
||||
|
||||
bool
|
||||
Lsa::deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator)
|
||||
{
|
||||
m_origRouter = ndn::Name(*iterator++);
|
||||
if (m_origRouter.size() <= 0)
|
||||
return false;
|
||||
if (*iterator++ != std::to_string(getType()))
|
||||
return false;
|
||||
m_lsSeqNo = boost::lexical_cast<uint32_t>(*iterator++);
|
||||
m_expirationTimePoint = ndn::time::fromIsoString(*iterator++);
|
||||
return true;
|
||||
}
|
||||
|
||||
NameLsa::NameLsa(const ndn::Name& origR, uint32_t lsn,
|
||||
const ndn::time::system_clock::TimePoint& lt,
|
||||
NamePrefixList& npl)
|
||||
@@ -78,34 +90,27 @@ NameLsa::serialize() const
|
||||
}
|
||||
|
||||
bool
|
||||
NameLsa::initializeFromContent(const std::string& content)
|
||||
NameLsa::deserialize(const std::string& content)
|
||||
{
|
||||
uint32_t numName = 0;
|
||||
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 {
|
||||
if (*tok_iter++ != std::to_string(Lsa::Type::NAME)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
|
||||
m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
|
||||
try {
|
||||
if (!deserializeCommon(tok_iter))
|
||||
return false;
|
||||
numName = boost::lexical_cast<uint32_t>(*tok_iter++);
|
||||
for (uint32_t i = 0; i < numName; i++) {
|
||||
ndn::Name name(*tok_iter++);
|
||||
addName(name);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_LOG_ERROR(e.what());
|
||||
_LOG_ERROR("Could not deserialize from content: " << e.what());
|
||||
return false;
|
||||
}
|
||||
for (uint32_t i = 0; i < numName; i++) {
|
||||
ndn::Name name(*tok_iter++);
|
||||
addName(name);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -174,33 +179,24 @@ CoordinateLsa::serialize() const
|
||||
}
|
||||
|
||||
bool
|
||||
CoordinateLsa::initializeFromContent(const std::string& content)
|
||||
CoordinateLsa::deserialize(const std::string& content)
|
||||
{
|
||||
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 {
|
||||
if (*tok_iter++ != std::to_string(Lsa::Type::COORDINATE)) {
|
||||
if (!deserializeCommon(tok_iter))
|
||||
return false;
|
||||
}
|
||||
|
||||
m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
|
||||
m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
|
||||
m_corRad = boost::lexical_cast<double>(*tok_iter++);
|
||||
m_corRad = boost::lexical_cast<double>(*tok_iter++);
|
||||
int numAngles = boost::lexical_cast<uint32_t>(*tok_iter++);
|
||||
|
||||
for (int i = 0; i < numAngles; i++) {
|
||||
m_angles.push_back(boost::lexical_cast<double>(*tok_iter++));
|
||||
}
|
||||
for (int i = 0; i < numAngles; i++) {
|
||||
m_angles.push_back(boost::lexical_cast<double>(*tok_iter++));
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_LOG_ERROR(e.what());
|
||||
_LOG_ERROR("Could not deserialize from content: " << e.what());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -257,32 +253,19 @@ AdjLsa::serialize() const
|
||||
}
|
||||
|
||||
bool
|
||||
AdjLsa::initializeFromContent(const std::string& content)
|
||||
AdjLsa::deserialize(const std::string& content)
|
||||
{
|
||||
uint32_t numLink = 0;
|
||||
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 {
|
||||
if (*tok_iter++ != std::to_string(Lsa::Type::ADJACENCY)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
|
||||
m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
|
||||
numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_LOG_ERROR(e.what());
|
||||
return false;
|
||||
}
|
||||
for (uint32_t i = 0; i < numLink; i++) {
|
||||
try {
|
||||
try {
|
||||
if (!deserializeCommon(tok_iter))
|
||||
return false;
|
||||
numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
|
||||
for (uint32_t i = 0; i < numLink; i++) {
|
||||
ndn::Name adjName(*tok_iter++);
|
||||
std::string connectingFaceUri(*tok_iter++);
|
||||
double linkCost = boost::lexical_cast<double>(*tok_iter++);
|
||||
@@ -290,10 +273,10 @@ AdjLsa::initializeFromContent(const std::string& content)
|
||||
Adjacent::STATUS_INACTIVE, 0, 0);
|
||||
addAdjacent(adjacent);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_LOG_ERROR(e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_LOG_ERROR("Could not deserialize from content: " << e.what());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
+8
-4
@@ -29,6 +29,7 @@
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <ndn-cxx/util/scheduler.hpp>
|
||||
#include <ndn-cxx/util/time.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
|
||||
namespace nlsr {
|
||||
|
||||
@@ -120,7 +121,7 @@ public:
|
||||
getKey() const;
|
||||
|
||||
virtual bool
|
||||
initializeFromContent(const std::string& content) = 0;
|
||||
deserialize(const std::string& content) = 0;
|
||||
|
||||
protected:
|
||||
/*! Get data common to all LSA types.
|
||||
@@ -131,6 +132,9 @@ protected:
|
||||
std::string
|
||||
getData() const;
|
||||
|
||||
bool
|
||||
deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator);
|
||||
|
||||
protected:
|
||||
ndn::Name m_origRouter;
|
||||
uint32_t m_lsSeqNo;
|
||||
@@ -188,7 +192,7 @@ public:
|
||||
getData(); getData() returns data of this format, in other words.
|
||||
*/
|
||||
bool
|
||||
initializeFromContent(const std::string& content) override;
|
||||
deserialize(const std::string& content) override;
|
||||
|
||||
bool
|
||||
isEqualContent(const NameLsa& other) const;
|
||||
@@ -252,7 +256,7 @@ public:
|
||||
according to getData().
|
||||
*/
|
||||
bool
|
||||
initializeFromContent(const std::string& content) override;
|
||||
deserialize(const std::string& content) override;
|
||||
|
||||
uint32_t
|
||||
getNoLink()
|
||||
@@ -331,7 +335,7 @@ public:
|
||||
same as for getData();
|
||||
*/
|
||||
bool
|
||||
initializeFromContent(const std::string& content) override;
|
||||
deserialize(const std::string& content) override;
|
||||
|
||||
double
|
||||
getCorRadius() const
|
||||
|
||||
+3
-3
@@ -1230,7 +1230,7 @@ Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
|
||||
lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
|
||||
if (isNameLsaNew(lsaKey, lsSeqNo)) {
|
||||
NameLsa nameLsa;
|
||||
if (nameLsa.initializeFromContent(dataContent)) {
|
||||
if (nameLsa.deserialize(dataContent)) {
|
||||
installNameLsa(nameLsa);
|
||||
}
|
||||
else {
|
||||
@@ -1247,7 +1247,7 @@ Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
|
||||
lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
|
||||
if (isAdjLsaNew(lsaKey, lsSeqNo)) {
|
||||
AdjLsa adjLsa;
|
||||
if (adjLsa.initializeFromContent(dataContent)) {
|
||||
if (adjLsa.deserialize(dataContent)) {
|
||||
installAdjLsa(adjLsa);
|
||||
}
|
||||
else {
|
||||
@@ -1264,7 +1264,7 @@ Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
|
||||
lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
|
||||
if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
|
||||
CoordinateLsa corLsa;
|
||||
if (corLsa.initializeFromContent(dataContent)) {
|
||||
if (corLsa.deserialize(dataContent)) {
|
||||
installCoordinateLsa(corLsa);
|
||||
}
|
||||
else {
|
||||
|
||||
+3
-10
@@ -31,22 +31,15 @@ MockLsa::serialize() const
|
||||
}
|
||||
|
||||
bool
|
||||
MockLsa::initializeFromContent(const std::string& content)
|
||||
MockLsa::deserialize(const std::string& content)
|
||||
{
|
||||
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 {
|
||||
if (*tok_iter++ != std::to_string(Lsa::Type::MOCK)) {
|
||||
return false;
|
||||
}
|
||||
m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
|
||||
m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
|
||||
deserializeCommon(tok_iter);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
return false;
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ public:
|
||||
serialize() const override;
|
||||
|
||||
bool
|
||||
initializeFromContent(const std::string& content) override;
|
||||
deserialize(const std::string& content) override;
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
|
||||
+4
-4
@@ -163,7 +163,7 @@ BOOST_AUTO_TEST_CASE(TestInitializeFromContent)
|
||||
|
||||
//If we don't do this the test will fail
|
||||
//Adjacent has default cost of 10 but no default
|
||||
//connecting face URI, so initializeFromContent fails
|
||||
//connecting face URI, so deserialize fails
|
||||
adj1.setFaceUri(ndn::util::FaceUri("udp://10.0.0.1"));
|
||||
adj2.setFaceUri(ndn::util::FaceUri("udp://10.0.0.2"));
|
||||
|
||||
@@ -176,7 +176,7 @@ BOOST_AUTO_TEST_CASE(TestInitializeFromContent)
|
||||
AdjLsa adjlsa1("router1", 1, testTimePoint, adjList.size(), adjList);
|
||||
AdjLsa adjlsa2;
|
||||
|
||||
BOOST_CHECK(adjlsa2.initializeFromContent(adjlsa1.serialize()));
|
||||
BOOST_CHECK(adjlsa2.deserialize(adjlsa1.serialize()));
|
||||
|
||||
BOOST_CHECK(adjlsa1.isEqualContent(adjlsa2));
|
||||
|
||||
@@ -188,7 +188,7 @@ BOOST_AUTO_TEST_CASE(TestInitializeFromContent)
|
||||
NameLsa nlsa1("router1", 1, testTimePoint, npl1);
|
||||
NameLsa nlsa2;
|
||||
|
||||
BOOST_CHECK(nlsa2.initializeFromContent(nlsa1.serialize()));
|
||||
BOOST_CHECK(nlsa2.deserialize(nlsa1.serialize()));
|
||||
|
||||
BOOST_CHECK_EQUAL(nlsa1.serialize(), nlsa2.serialize());
|
||||
|
||||
@@ -197,7 +197,7 @@ BOOST_AUTO_TEST_CASE(TestInitializeFromContent)
|
||||
CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles);
|
||||
CoordinateLsa clsa2;
|
||||
|
||||
BOOST_CHECK(clsa2.initializeFromContent(clsa1.serialize()));
|
||||
BOOST_CHECK(clsa2.deserialize(clsa1.serialize()));
|
||||
|
||||
BOOST_CHECK_EQUAL(clsa1.serialize(), clsa2.serialize());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user