diff --git a/src/adjacency-list.cpp b/src/adjacency-list.cpp index 6f83b1a..b82346f 100644 --- a/src/adjacency-list.cpp +++ b/src/adjacency-list.cpp @@ -194,6 +194,12 @@ AdjacencyList::getAdjList() return m_adjList; } +const std::list& +AdjacencyList::getAdjList() const +{ + return m_adjList; +} + bool AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const { diff --git a/src/adjacency-list.hpp b/src/adjacency-list.hpp index dcb1539..c21e70b 100644 --- a/src/adjacency-list.hpp +++ b/src/adjacency-list.hpp @@ -70,6 +70,9 @@ public: std::list& getAdjList(); + const std::list& + getAdjList() const; + bool isNeighbor(const ndn::Name& adjName); diff --git a/src/common.hpp b/src/common.hpp index 4e82370..470d8b1 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -34,6 +34,22 @@ using std::function; const ndn::time::seconds TIME_ALLOWED_FOR_CANONIZATION = ndn::time::seconds(4); +template +struct is_iterator +{ + static constexpr bool value = false; +}; + +/*! Use C++11 iterator_traits to check if some type is an iterator + */ +template +struct is_iterator::value_type, + void>::value>::type> +{ + static constexpr bool value = true; +}; + } // namespace nlsr #endif // NLSR_COMMON_HPP diff --git a/src/lsa.hpp b/src/lsa.hpp index 4840c34..d27ddd6 100644 --- a/src/lsa.hpp +++ b/src/lsa.hpp @@ -22,14 +22,14 @@ #ifndef NLSR_LSA_HPP #define NLSR_LSA_HPP +#include "name-prefix-list.hpp" +#include "adjacent.hpp" +#include "adjacency-list.hpp" + #include #include #include -#include "adjacent.hpp" -#include "name-prefix-list.hpp" -#include "adjacency-list.hpp" - namespace nlsr { class Nlsr; @@ -205,6 +205,12 @@ public: return m_adl; } + const AdjacencyList& + getAdl() const + { + return m_adl; + } + void addAdjacent(Adjacent adj) { diff --git a/src/route/map.cpp b/src/route/map.cpp index a97be11..544976e 100644 --- a/src/route/map.cpp +++ b/src/route/map.cpp @@ -16,19 +16,19 @@ * * You should have received a copy of the GNU General Public License along with * NLSR, e.g., in COPYING.md file. If not, see . - * - * \author A K M Mahmudul Hoque - * **/ -#include -#include +#include "map.hpp" #include "nlsr.hpp" #include "adjacent.hpp" #include "lsa.hpp" #include "lsdb.hpp" -#include "map.hpp" + #include "logger.hpp" + +#include +#include + namespace nlsr { INIT_LOGGER("Map"); @@ -97,29 +97,6 @@ Map::getMappingNoByRouterName(const ndn::Name& rName) return -1; } -void -Map::createFromAdjLsdb(Nlsr& pnlsr) -{ - std::list adjLsdb = pnlsr.getLsdb().getAdjLsdb(); - for (std::list::iterator it = adjLsdb.begin(); - it != adjLsdb.end() ; it++) { - addEntry((*it).getOrigRouter()); - std::list adl = (*it).getAdl().getAdjList(); - for (std::list::iterator itAdl = adl.begin(); - itAdl != adl.end() ; itAdl++) { - addEntry((*itAdl).getName()); - } - } -} - -void -Map::createFromCoordinateLsdb(Nlsr& nlsr) -{ - for (CoordinateLsa lsa : nlsr.getLsdb().getCoordinateLsdb()) { - addEntry(lsa.getOrigRouter()); - } -} - void Map::reset() { diff --git a/src/route/map.hpp b/src/route/map.hpp index 45e6b96..262fe52 100644 --- a/src/route/map.hpp +++ b/src/route/map.hpp @@ -16,21 +16,18 @@ * * You should have received a copy of the GNU General Public License along with * NLSR, e.g., in COPYING.md file. If not, see . - * - * \author A K M Mahmudul Hoque - * **/ + #ifndef NLSR_MAP_HPP #define NLSR_MAP_HPP +#include "common.hpp" +#include "map-entry.hpp" + #include #include #include -#include - -#include "map-entry.hpp" - namespace nlsr { class Nlsr; @@ -52,11 +49,36 @@ public: void addEntry(const ndn::Name& rtrName); - void - createFromAdjLsdb(Nlsr& pnlsr); + /*! Populates the Map with AdjacencyLsas. + \note IteratorType must an iterator type, and begin to end must represent a valid range. + */ + template void - createFromCoordinateLsdb(Nlsr& nlsr); + createFromAdjLsdb(IteratorType begin, IteratorType end) + { + BOOST_STATIC_ASSERT_MSG(is_iterator::value, "IteratorType must be an iterator!"); + for (auto lsa = begin; lsa != end; lsa++) { + addEntry(lsa->getOrigRouter()); + for (const auto& adjacent : lsa->getAdl().getAdjList()) { + addEntry(adjacent.getName()); + } + } + } + + /*! Populates the Map with CoordinateLsas. + + \note IteratorType must an iterator type, and begin to end must represent a valid range. + */ + template + void + createFromCoordinateLsdb(IteratorType begin, IteratorType end) + { + BOOST_STATIC_ASSERT_MSG(is_iterator::value, "IteratorType must be an iterator!"); + for (auto lsa = begin; lsa != end; lsa++) { + addEntry(lsa->getOrigRouter()); + } + } const ndn::Name getRouterNameByMappingNo(int32_t mn); @@ -91,4 +113,5 @@ private: }; } // namespace nlsr -#endif //NLSR_MAP_HPP + +#endif // NLSR_MAP_HPP diff --git a/src/route/routing-table.cpp b/src/route/routing-table.cpp index b1e02dc..708e3dd 100644 --- a/src/route/routing-table.cpp +++ b/src/route/routing-table.cpp @@ -16,13 +16,7 @@ * * You should have received a copy of the GNU General Public License along with * NLSR, e.g., in COPYING.md file. If not, see . - * - * \author A K M Mahmudul Hoque - * **/ -#include -#include -#include #include "routing-table.hpp" #include "nlsr.hpp" @@ -33,6 +27,10 @@ #include "name-prefix-table.hpp" #include "logger.hpp" +#include +#include +#include + namespace nlsr { INIT_LOGGER("RoutingTable"); @@ -123,7 +121,7 @@ RoutingTable::calculateLsRoutingTable(Nlsr& nlsr) _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called"); Map map; - map.createFromAdjLsdb(nlsr); + map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end()); map.writeLog(); size_t nRouters = map.getMapSize(); @@ -137,7 +135,8 @@ void RoutingTable::calculateHypRoutingTable(Nlsr& nlsr) { Map map; - map.createFromCoordinateLsdb(nlsr); + map.createFromCoordinateLsdb(nlsr.getLsdb().getCoordinateLsdb().begin(), + nlsr.getLsdb().getCoordinateLsdb().end()); map.writeLog(); size_t nRouters = map.getMapSize(); @@ -153,7 +152,7 @@ void RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr) { Map map; - map.createFromAdjLsdb(nlsr); + map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end()); map.writeLog(); size_t nRouters = map.getMapSize(); diff --git a/tests/test-hyperbolic-calculator.cpp b/tests/test-hyperbolic-calculator.cpp index 030fe25..c894c98 100644 --- a/tests/test-hyperbolic-calculator.cpp +++ b/tests/test-hyperbolic-calculator.cpp @@ -102,7 +102,7 @@ public: CoordinateLsa coordC(adjC.getOrigRouter(), 1, MAX_TIME, 14.11, anglesC); lsdb.installCoordinateLsa(coordC); - map.createFromAdjLsdb(nlsr); + map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end()); } void runTest(const double& expectedCost) diff --git a/tests/test-link-state-calculator.cpp b/tests/test-link-state-calculator.cpp index 50063a2..8aee304 100644 --- a/tests/test-link-state-calculator.cpp +++ b/tests/test-link-state-calculator.cpp @@ -97,7 +97,7 @@ public: AdjLsa adjC(c.getName(), 1, MAX_TIME, 2, adjacencyListC); lsdb.installAdjLsa(adjC); - map.createFromAdjLsdb(nlsr); + map.createFromAdjLsdb(lsdb.getAdjLsdb().begin(), lsdb.getAdjLsdb().end()); } public: