Skip to content

Commit

Permalink
lsa: cleanup operator<<
Browse files Browse the repository at this point in the history
Delete .toString() virtual function in favor of ostream output.

Delete Lsa::getOriginRouterCopy() in favor of a custom Boost.Multi-Index
key extractor.

Mark single-parameter constructor as 'explicit'.

Rewrite class-level Doxygen with ABNF syntax.

refs #5308

Change-Id: I3c43395ce86f9a1a52da186fcf8c5a15cf35fe40
  • Loading branch information
yoursunny committed Jan 10, 2024
1 parent 18c6430 commit 153fbc1
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 126 deletions.
15 changes: 2 additions & 13 deletions src/lsa/adj-lsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,18 @@ AdjLsa::wireDecode(const ndn::Block& wire)
m_adl = adl;
}

std::string
AdjLsa::toString() const
void
AdjLsa::print(std::ostream& os) const
{
std::ostringstream os;
os << getString();
os << " Adjacent(s):\n";

int adjacencyIndex = 0;

for (const auto& adjacency : m_adl) {
os << " Adjacent " << adjacencyIndex++
<< ": (name=" << adjacency.getName()
<< ", uri=" << adjacency.getFaceUri()
<< ", cost=" << adjacency.getLinkCost() << ")\n";
}

return os.str();
}

std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
Expand All @@ -145,10 +140,4 @@ AdjLsa::update(const std::shared_ptr<Lsa>& lsa)
return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
}

std::ostream&
operator<<(std::ostream& os, const AdjLsa& lsa)
{
return os << lsa.toString();
}

} // namespace nlsr
28 changes: 15 additions & 13 deletions src/lsa/adj-lsa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,27 @@

namespace nlsr {

/*!
\brief Data abstraction for AdjLsa
AdjacencyLsa := ADJACENCY-LSA-TYPE TLV-LENGTH
Lsa
Adjacency*
/**
* @brief Represents an LSA of adjacencies of the origin router in link-state mode.
*
* AdjLsa is encoded as:
* @code{.abnf}
* AdjLsa = ADJACENCY-LSA-TYPE TLV-LENGTH
* Lsa
* *Adjacency
* @endcode
*/
class AdjLsa : public Lsa, private boost::equality_comparable<AdjLsa>
{
public:
typedef AdjacencyList::const_iterator const_iterator;
using const_iterator = AdjacencyList::const_iterator;

AdjLsa() = default;

AdjLsa(const ndn::Name& originR, uint64_t seqNo,
const ndn::time::system_clock::time_point& timepoint, AdjacencyList& adl);

explicit
AdjLsa(const ndn::Block& block);

Lsa::Type
Expand Down Expand Up @@ -103,12 +107,13 @@ class AdjLsa : public Lsa, private boost::equality_comparable<AdjLsa>
void
wireDecode(const ndn::Block& wire);

std::string
toString() const override;

std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
update(const std::shared_ptr<Lsa>& lsa) override;

private:
void
print(std::ostream& os) const override;

private: // non-member operators
// NOTE: the following "hidden friend" operators are available via
// argument-dependent lookup only and must be defined inline.
Expand All @@ -126,9 +131,6 @@ class AdjLsa : public Lsa, private boost::equality_comparable<AdjLsa>

NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(AdjLsa);

std::ostream&
operator<<(std::ostream& os, const AdjLsa& lsa);

} // namespace nlsr

#endif // NLSR_LSA_ADJ_LSA_HPP
14 changes: 2 additions & 12 deletions src/lsa/coordinate-lsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,14 @@ CoordinateLsa::wireDecode(const ndn::Block& wire)
m_hyperbolicAngles = angles;
}

std::string
CoordinateLsa::toString() const
void
CoordinateLsa::print(std::ostream& os) const
{
std::ostringstream os;
os << getString();
os << " Hyperbolic Radius : " << m_hyperbolicRadius << "\n";
int i = 0;
for (const auto& value : m_hyperbolicAngles) {
os << " Hyperbolic Theta " << i++ << " : " << value << "\n";
}

return os.str();
}

std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
Expand All @@ -148,10 +144,4 @@ CoordinateLsa::update(const std::shared_ptr<Lsa>& lsa)
return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
}

std::ostream&
operator<<(std::ostream& os, const CoordinateLsa& lsa)
{
return os << lsa.toString();
}

} // namespace nlsr
33 changes: 21 additions & 12 deletions src/lsa/coordinate-lsa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@

namespace nlsr {

/*!
\brief Data abstraction for CoordinateLsa
CoordinateLsa := COORDINATE-LSA-TYPE TLV-LENGTH
Lsa
HyperbolicRadius
HyperbolicAngle+
/**
* @brief Represents an LSA of hyperbolic coordinates of the origin router.
*
* CoordinateLsa is encoded as:
* @code{.abnf}
* CoordinateLsa = COORDINATE-LSA-TYPE TLV-LENGTH
* Lsa
* HyperbolicRadius
* 1*HyperbolicAngle ; theta
*
* HyperbolicRadius = HYPERBOLIC-RADIUS-TYPE TLV-LENGTH
* Double ; IEEE754 double precision
*
* HyperbolicAngle = HYPERBOLIC-ANGLE-TYPE TLV-LENGTH
* Double ; IEEE754 double precision
* @endcode
*/
class CoordinateLsa : public Lsa, private boost::equality_comparable<CoordinateLsa>
{
Expand All @@ -45,6 +55,7 @@ class CoordinateLsa : public Lsa, private boost::equality_comparable<CoordinateL
const ndn::time::system_clock::time_point& timepoint,
double radius, std::vector<double> angles);

explicit
CoordinateLsa(const ndn::Block& block);

Lsa::Type
Expand Down Expand Up @@ -95,12 +106,13 @@ class CoordinateLsa : public Lsa, private boost::equality_comparable<CoordinateL
void
wireDecode(const ndn::Block& wire);

std::string
toString() const override;

std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
update(const std::shared_ptr<Lsa>& lsa) override;

private:
void
print(std::ostream& os) const override;

private: // non-member operators
// NOTE: the following "hidden friend" operators are available via
// argument-dependent lookup only and must be defined inline.
Expand All @@ -122,9 +134,6 @@ class CoordinateLsa : public Lsa, private boost::equality_comparable<CoordinateL

NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(CoordinateLsa);

std::ostream&
operator<<(std::ostream& os, const CoordinateLsa& lsa);

} // namespace nlsr

#endif // NLSR_LSA_COORDINATE_LSA_HPP
28 changes: 14 additions & 14 deletions src/lsa/lsa.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -97,6 +97,19 @@ Lsa::wireDecode(const ndn::Block& wire)
}
}

std::ostream&
operator<<(std::ostream& os, const Lsa& lsa)
{
auto duration = lsa.m_expirationTimePoint - ndn::time::system_clock::now();
os << " " << lsa.getType() << " LSA:\n"
<< " Origin Router : " << lsa.m_originRouter << "\n"
<< " Sequence Number : " << lsa.m_seqNo << "\n"
<< " Expires in : " << ndn::time::duration_cast<ndn::time::milliseconds>(duration)
<< "\n";
lsa.print(os);
return os;
}

std::ostream&
operator<<(std::ostream& os, const Lsa::Type& type)
{
Expand Down Expand Up @@ -137,17 +150,4 @@ operator>>(std::istream& is, Lsa::Type& type)
return is;
}

std::string
Lsa::getString() const
{
std::ostringstream os;
auto duration = m_expirationTimePoint - ndn::time::system_clock::now();
os << " " << getType() << " LSA:\n"
<< " Origin Router : " << m_originRouter << "\n"
<< " Sequence Number : " << m_seqNo << "\n"
<< " Expires in : " << ndn::time::duration_cast<ndn::time::milliseconds>(duration)
<< "\n";
return os.str();
}

} // namespace nlsr
42 changes: 20 additions & 22 deletions src/lsa/lsa.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -31,12 +31,16 @@

namespace nlsr {

/*!
\brief Data abstraction for Lsa
Lsa := LSA-TYPE TLV-LENGTH
Name
SequenceNumber
ExpirationTimePoint
/**
* @brief Represents a Link State Announcement (LSA).
*
* The base level LSA is encoded as:
* @code{.abnf}
* Lsa = LSA-TYPE TLV-LENGTH
* Name ; origin router
* SequenceNumber
* ExpirationTime
* @endcode
*/
class Lsa
{
Expand All @@ -55,11 +59,12 @@ class Lsa
};

protected:
Lsa() = default;

Lsa(const ndn::Name& originRouter, uint64_t seqNo,
ndn::time::system_clock::time_point expirationTimePoint);

Lsa() = default;

explicit
Lsa(const Lsa& lsa);

public:
Expand Down Expand Up @@ -88,12 +93,6 @@ class Lsa
return m_originRouter;
}

ndn::Name
getOriginRouterCopy() const
{
return m_originRouter;
}

const ndn::time::system_clock::time_point&
getExpirationTimePoint() const
{
Expand All @@ -113,11 +112,6 @@ class Lsa
m_expiringEventId = eid;
}

/*! Get data common to all LSA types for printing purposes.
*/
virtual std::string
toString() const = 0;

virtual std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
update(const std::shared_ptr<Lsa>& lsa) = 0;

Expand All @@ -132,8 +126,12 @@ class Lsa
void
wireDecode(const ndn::Block& wire);

std::string
getString() const;
private:
virtual void
print(std::ostream& os) const = 0;

friend std::ostream&
operator<<(std::ostream& os, const Lsa& lsa);

PUBLIC_WITH_TESTS_ELSE_PROTECTED:
ndn::Name m_originRouter;
Expand Down
16 changes: 3 additions & 13 deletions src/lsa/name-lsa.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -112,18 +112,14 @@ NameLsa::wireDecode(const ndn::Block& wire)
m_npl = npl;
}

std::string
NameLsa::toString() const
void
NameLsa::print(std::ostream& os) const
{
std::ostringstream os;
os << getString();
os << " Names:\n";
int i = 0;
for (const auto& name : m_npl.getNames()) {
os << " Name " << i++ << ": " << name << "\n";
}

return os.str();
}

std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
Expand Down Expand Up @@ -156,10 +152,4 @@ NameLsa::update(const std::shared_ptr<Lsa>& lsa)
return {updated, namesToAdd, namesToRemove};
}

std::ostream&
operator<<(std::ostream& os, const NameLsa& lsa)
{
return os << lsa.toString();
}

} // namespace nlsr
Loading

0 comments on commit 153fbc1

Please sign in to comment.