**breaking** route: consolidate routing tlv into route
refs: #5116
Plus some misc improvements
Change-Id: Id0902fec65160b4368b1b5066f460433aced98ed
diff --git a/src/route/fib-entry.cpp b/src/route/fib-entry.cpp
index 0091245..bc11634 100644
--- a/src/route/fib-entry.cpp
+++ b/src/route/fib-entry.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018, The University of Memphis,
+/*
+ * Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
@@ -16,7 +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 <http://www.gnu.org/licenses/>.
- **/
+ */
#include "fib-entry.hpp"
#include "logger.hpp"
@@ -30,7 +30,7 @@
{
NLSR_LOG_DEBUG("Name Prefix: " << m_name);
NLSR_LOG_DEBUG("Seq No: " << m_seqNo);
- m_nexthopList.writeLog();
+ NLSR_LOG_DEBUG(m_nexthopList);
}
} // namespace nlsr
diff --git a/src/route/fib.hpp b/src/route/fib.hpp
index 3e4ef68..27891eb 100644
--- a/src/route/fib.hpp
+++ b/src/route/fib.hpp
@@ -87,9 +87,6 @@
* strictly necessary, because eventually those prefix registrations
* will expire, but cleaning up after ourselves improves
* performance.
- *
- * \sa NlsrRunner::run
- *
*/
void
clean();
@@ -212,8 +209,6 @@
* an entry is removed. Typically this happens when NLSR is
* terminated or crashes, and we don't want the scheduler to crash
* because it's referencing memory that has no valid function.
- *
- * \sa NlsrRunner::run
*/
void
cancelEntryRefresh(const FibEntry& entry);
diff --git a/src/route/map-entry.hpp b/src/route/map-entry.hpp
deleted file mode 100644
index e4c998c..0000000
--- a/src/route/map-entry.hpp
+++ /dev/null
@@ -1,71 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017, The University of Memphis,
- * Regents of the University of California
- *
- * This file is part of NLSR (Named-data Link State Routing).
- * See AUTHORS.md for complete list of NLSR authors and contributors.
- *
- * NLSR is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- *
- **/
-
-#ifndef NLSR_MAP_ENTRY_HPP
-#define NLSR_MAP_ENTRY_HPP
-
-#include <boost/cstdint.hpp>
-#include <ndn-cxx/name.hpp>
-
-namespace nlsr {
-
-class MapEntry
-{
-public:
- MapEntry()
- : m_router()
- , m_mappingNumber(-1)
- {
- }
-
- ~MapEntry()
- {
- }
-
- MapEntry(const ndn::Name& rtr, int32_t mn)
- {
- m_router = rtr;
- m_mappingNumber = mn;
- }
-
- const ndn::Name&
- getRouter() const
- {
- return m_router;
- }
-
- int32_t
- getMappingNumber() const
- {
- return m_mappingNumber;
- }
-
- void
- reset();
-
-private:
- ndn::Name m_router;
- int32_t m_mappingNumber;
-};
-
-} // namespace nlsr
-
-#endif // NLSR_MAP_ENTRY_HPP
diff --git a/src/route/map.cpp b/src/route/map.cpp
index 3f6513c..0aab84f 100644
--- a/src/route/map.cpp
+++ b/src/route/map.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
@@ -16,7 +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 <http://www.gnu.org/licenses/>.
- **/
+ */
#include "map.hpp"
#include "nlsr.hpp"
@@ -32,7 +32,7 @@
void
Map::addEntry(const ndn::Name& rtrName)
{
- MapEntry me(rtrName, m_mappingIndex);
+ MapEntry me {rtrName, m_mappingIndex};
if (addEntry(me)) {
m_mappingIndex++;
}
@@ -48,43 +48,25 @@
Map::getRouterNameByMappingNo(int32_t mn) const
{
auto&& mappingNumberView = m_entries.get<detail::byMappingNumber>();
- auto iterator = mappingNumberView.find(mn);
- if (iterator == mappingNumberView.end()) {
- return {};
- }
- else {
- return {iterator->getRouter()};
- }
+ auto it = mappingNumberView.find(mn);
+ return it == mappingNumberView.end() ? ndn::nullopt : ndn::optional<ndn::Name>(it->router);
}
ndn::optional<int32_t>
Map::getMappingNoByRouterName(const ndn::Name& rName)
{
auto&& routerNameView = m_entries.get<detail::byRouterName>();
- auto iterator = routerNameView.find(rName);
- if (iterator == routerNameView.end()) {
- return {};
- }
- else {
- return {iterator->getMappingNumber()};
- }
-}
-
-void
-Map::reset()
-{
- m_entries = detail::entryContainer{};
- m_mappingIndex = 0;
+ auto it = routerNameView.find(rName);
+ return it == routerNameView.end() ? ndn::nullopt : ndn::optional<int32_t>(it->mappingNumber);
}
void
Map::writeLog()
{
NLSR_LOG_DEBUG("---------------Map----------------------");
- auto&& routerNameView = m_entries.get<detail::byRouterName>();
- for (auto entry = routerNameView.begin(); entry != routerNameView.end(); entry++) {
- NLSR_LOG_DEBUG("MapEntry: ( Router: " << entry->getRouter() << " Mapping No: "
- << entry->getMappingNumber() << " )");
+ for (const auto& entry : m_entries.get<detail::byRouterName>()) {
+ NLSR_LOG_DEBUG("MapEntry: ( Router: " << entry.router << " Mapping No: " <<
+ entry.mappingNumber << " )");
}
}
diff --git a/src/route/map.hpp b/src/route/map.hpp
index c9511bf..ae92d25 100644
--- a/src/route/map.hpp
+++ b/src/route/map.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
@@ -16,24 +16,25 @@
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#ifndef NLSR_MAP_HPP
#define NLSR_MAP_HPP
#include "common.hpp"
-#include "map-entry.hpp"
-
-#include <list>
-#include <boost/cstdint.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
-#include <boost/multi_index/mem_fun.hpp>
+#include <boost/multi_index/member.hpp>
#include <boost/multi_index/tag.hpp>
namespace nlsr {
+struct MapEntry {
+ ndn::Name router;
+ int32_t mappingNumber = -1;
+};
+
namespace detail {
using namespace boost::multi_index;
@@ -44,10 +45,10 @@
MapEntry,
indexed_by<
hashed_unique<tag<byRouterName>,
- const_mem_fun<MapEntry, const ndn::Name&, &MapEntry::getRouter>,
+ member<MapEntry, ndn::Name, &MapEntry::router>,
std::hash<ndn::Name>>,
hashed_unique<tag<byMappingNumber>,
- const_mem_fun<MapEntry, int32_t, &MapEntry::getMappingNumber>>
+ member<MapEntry, int32_t, &MapEntry::mappingNumber>>
>
>;
@@ -107,9 +108,6 @@
ndn::optional<int32_t>
getMappingNoByRouterName(const ndn::Name& rName);
- void
- reset();
-
size_t
getMapSize() const
{
diff --git a/src/route/name-prefix-table-entry.cpp b/src/route/name-prefix-table-entry.cpp
index 9890101..368cc08 100644
--- a/src/route/name-prefix-table-entry.cpp
+++ b/src/route/name-prefix-table-entry.cpp
@@ -17,7 +17,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 <http://www.gnu.org/licenses/>.
- **/
+ */
#include "name-prefix-table-entry.hpp"
@@ -32,7 +32,7 @@
void
NamePrefixTableEntry::generateNhlfromRteList()
{
- m_nexthopList.reset();
+ m_nexthopList.clear();
for (auto iterator = m_rteList.begin(); iterator != m_rteList.end(); ++iterator) {
for (auto nhItr = (*iterator)->getNexthopList().getNextHops().begin();
nhItr != (*iterator)->getNexthopList().getNextHops().end();
@@ -76,18 +76,6 @@
// be updated there.
}
-void
-NamePrefixTableEntry::writeLog()
-{
- NLSR_LOG_DEBUG("Name: " << m_namePrefix);
- for (auto it = m_rteList.begin(); it != m_rteList.end(); ++it) {
- NLSR_LOG_DEBUG("Destination: " << (*it)->getDestination());
- NLSR_LOG_DEBUG("Nexthops: ");
- (*it)->getNexthopList().writeLog();
- }
- m_nexthopList.writeLog();
-}
-
bool
operator==(const NamePrefixTableEntry& lhs, const NamePrefixTableEntry& rhs)
{
@@ -104,8 +92,10 @@
operator<<(std::ostream& os, const NamePrefixTableEntry& entry)
{
os << "Name: " << entry.getNamePrefix() << "\n";
+
for (const auto& entryPtr : entry.getRteList()) {
- os << "Destination: " << entryPtr->getDestination() << "\n";
+ os << " Destination: " << entryPtr->getDestination() << "\n";
+ os << entryPtr->getNexthopList();
}
return os;
}
diff --git a/src/route/name-prefix-table-entry.hpp b/src/route/name-prefix-table-entry.hpp
index 828d64a..c298e97 100644
--- a/src/route/name-prefix-table-entry.hpp
+++ b/src/route/name-prefix-table-entry.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018, The University of Memphis,
+/*
+ * Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
@@ -17,13 +17,12 @@
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#ifndef NLSR_NAME_PREFIX_TABLE_ENTRY_HPP
#define NLSR_NAME_PREFIX_TABLE_ENTRY_HPP
#include "routing-table-pool-entry.hpp"
-
#include "test-access-control.hpp"
#include <list>
@@ -64,7 +63,7 @@
{
if (m_rteList.size() > 0) {
for (auto it = m_rteList.begin(); it != m_rteList.end(); ++it) {
- (*it)->getNexthopList().reset();
+ (*it)->getNexthopList().clear();
}
}
}
@@ -112,7 +111,6 @@
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
std::list<std::shared_ptr<RoutingTablePoolEntry>> m_rteList;
NexthopList m_nexthopList;
-
};
bool
diff --git a/src/route/name-prefix-table.cpp b/src/route/name-prefix-table.cpp
index b38bbea..55f0c54 100644
--- a/src/route/name-prefix-table.cpp
+++ b/src/route/name-prefix-table.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
@@ -17,7 +17,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 <http://www.gnu.org/licenses/>.
- **/
+ */
#include "name-prefix-table.hpp"
@@ -228,7 +228,7 @@
}
else if (sourceEntry == entries.end()) {
NLSR_LOG_DEBUG("Routing entry: " << poolEntry->getDestination() << " now has no next-hops.");
- poolEntry->getNexthopList().reset();
+ poolEntry->getNexthopList().clear();
for (const auto& nameEntry : poolEntry->namePrefixTableEntries) {
auto nameEntryFullPtr = nameEntry.second.lock();
addEntry(nameEntryFullPtr->getNamePrefix(), poolEntry->getDestination());
diff --git a/src/route/nexthop-list.cpp b/src/route/nexthop-list.cpp
index 8b9366e..7ab1352 100644
--- a/src/route/nexthop-list.cpp
+++ b/src/route/nexthop-list.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018, The University of Memphis,
+/*
+ * Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
@@ -17,17 +17,16 @@
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#include "nexthop-list.hpp"
#include "common.hpp"
#include "nexthop.hpp"
-#include "logger.hpp"
+
+#include <ndn-cxx/util/ostream-joiner.hpp>
namespace nlsr {
-INIT_LOGGER(route.NexthopList);
-
static bool
nexthopAddCompare(const NextHop& nh1, const NextHop& nh2)
{
@@ -71,21 +70,16 @@
std::ostream&
operator<<(std::ostream& os, const NexthopList& nhl)
{
- NexthopList& ucnhl = const_cast<NexthopList&>(nhl);
- os << "NexthopList(\nNext hops: ";
- for (auto&& nh : ucnhl.getNextHops()) {
- os << nh;
- }
- os << ")";
+ os << " ";
+ std::copy(nhl.cbegin(), nhl.cend(), ndn::make_ostream_joiner(os, "\n "));
return os;
}
void
NexthopList::addNextHop(const NextHop& nh)
{
- std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
- m_nexthopList.end(),
- std::bind(&nexthopAddCompare, _1, nh));
+ auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
+ std::bind(&nexthopAddCompare, _1, nh));
if (it == m_nexthopList.end()) {
m_nexthopList.insert(nh);
}
@@ -98,23 +92,11 @@
void
NexthopList::removeNextHop(const NextHop& nh)
{
- std::set<NextHop, NextHopComparator>::iterator it = std::find_if(m_nexthopList.begin(),
- m_nexthopList.end(),
- std::bind(&nexthopRemoveCompare, _1, nh));
+ auto it = std::find_if(m_nexthopList.begin(), m_nexthopList.end(),
+ std::bind(&nexthopRemoveCompare, _1, nh));
if (it != m_nexthopList.end()) {
m_nexthopList.erase(it);
}
}
-void
-NexthopList::writeLog() const
-{
- int i = 1;
-
- for (const auto& nexthop : m_nexthopList) {
- NLSR_LOG_DEBUG("Nexthop " << i++ << ": " << nexthop.getConnectingFaceUri() <<
- " Route Cost: " << nexthop.getRouteCost());
- }
-}
-
} // namespace nlsr
diff --git a/src/route/nexthop-list.hpp b/src/route/nexthop-list.hpp
index e405889..537448e 100644
--- a/src/route/nexthop-list.hpp
+++ b/src/route/nexthop-list.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
@@ -16,7 +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 <http://www.gnu.org/licenses/>.
- **/
+ */
#ifndef NLSR_NEXTHOP_LIST_HPP
#define NLSR_NEXTHOP_LIST_HPP
@@ -47,13 +47,7 @@
class NexthopList
{
public:
- NexthopList()
- {
- }
-
- ~NexthopList()
- {
- }
+ NexthopList() = default;
/*! \brief Adds a next hop to the list.
\param nh The next hop.
@@ -80,7 +74,7 @@
}
void
- reset()
+ clear()
{
m_nexthopList.clear();
}
@@ -93,6 +87,7 @@
typedef std::set<NextHop, NextHopComparator>::iterator iterator;
typedef std::set<NextHop, NextHopComparator>::const_iterator const_iterator;
+ typedef std::set<NextHop, NextHopComparator>::reverse_iterator reverse_iterator;
iterator
begin()
@@ -118,8 +113,17 @@
return m_nexthopList.end();
}
- void
- writeLog() const;
+ reverse_iterator
+ rbegin() const
+ {
+ return m_nexthopList.rbegin();
+ }
+
+ reverse_iterator
+ rend() const
+ {
+ return m_nexthopList.rend();
+ }
private:
std::set<NextHop, NextHopComparator> m_nexthopList;
diff --git a/src/route/nexthop.cpp b/src/route/nexthop.cpp
index 14bfb43..11399aa 100644
--- a/src/route/nexthop.cpp
+++ b/src/route/nexthop.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017, The University of Memphis,
+/*
+ * Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
@@ -16,29 +16,91 @@
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- *
- *
- **/
+ */
#include "nexthop.hpp"
+#include "tlv-nlsr.hpp"
+
+#include <ndn-cxx/encoding/block-helpers.hpp>
namespace nlsr {
+template<ndn::encoding::Tag TAG>
+size_t
+NextHop::wireEncode(ndn::EncodingImpl<TAG>& block) const
+{
+ size_t totalLength = 0;
+
+ totalLength += ndn::encoding::prependDoubleBlock(block, ndn::tlv::nlsr::CostDouble, m_routeCost);
+ totalLength += ndn::encoding::prependStringBlock(block, ndn::tlv::nlsr::Uri, m_connectingFaceUri);
+
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(ndn::tlv::nlsr::NextHop);
+
+ return totalLength;
+}
+
+NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NextHop);
+
+const ndn::Block&
+NextHop::wireEncode() const
+{
+ if (m_wire.hasWire()) {
+ return m_wire;
+ }
+
+ ndn::EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ ndn::EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ m_wire = buffer.block();
+
+ return m_wire;
+}
+
+void
+NextHop::wireDecode(const ndn::Block& wire)
+{
+ m_connectingFaceUri = "";
+ m_routeCost = 0;
+
+ m_wire = wire;
+
+ if (m_wire.type() != ndn::tlv::nlsr::NextHop) {
+ std::stringstream error;
+ error << "Expected NextHop Block, but Block is of a different type: #"
+ << m_wire.type();
+ BOOST_THROW_EXCEPTION(Error(error.str()));
+ }
+
+ m_wire.parse();
+
+ ndn::Block::element_const_iterator val = m_wire.elements_begin();
+
+ if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) {
+ m_connectingFaceUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
+ ++val;
+ }
+ else {
+ BOOST_THROW_EXCEPTION(Error("Missing required Uri field"));
+ }
+
+ m_routeCost = ndn::encoding::readDouble(*val);
+}
+
bool
operator==(const NextHop& lhs, const NextHop& rhs)
{
- return ((lhs.getRouteCostAsAdjustedInteger() == rhs.getRouteCostAsAdjustedInteger())
- &&
- (lhs.getConnectingFaceUri() == rhs.getConnectingFaceUri()));
+ return (lhs.getRouteCostAsAdjustedInteger() == rhs.getRouteCostAsAdjustedInteger()) &&
+ (lhs.getConnectingFaceUri() == rhs.getConnectingFaceUri());
}
std::ostream&
operator<<(std::ostream& os, const NextHop& hop)
{
- os << "Nexthop("
- << "face-uri: " << hop.getConnectingFaceUri()
- << ", cost: " << hop.getRouteCost() << ")";
-
+ os << "NextHop(Uri: " << hop.getConnectingFaceUri() << ", Cost: " << hop.getRouteCost() << ")";
return os;
}
diff --git a/src/route/nexthop.hpp b/src/route/nexthop.hpp
index e24df7b..7f91cfe 100644
--- a/src/route/nexthop.hpp
+++ b/src/route/nexthop.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
@@ -16,22 +16,36 @@
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#ifndef NLSR_ROUTE_NEXTHOP_HPP
#define NLSR_ROUTE_NEXTHOP_HPP
#include "test-access-control.hpp"
+#include <ndn-cxx/encoding/block.hpp>
+#include <ndn-cxx/encoding/encoding-buffer.hpp>
+#include <ndn-cxx/encoding/tlv.hpp>
+
#include <iostream>
#include <cmath>
#include <boost/cstdint.hpp>
namespace nlsr {
+/*! \brief Data abstraction for Nexthop
+ *
+ * NextHop := NEXTHOP-TYPE TLV-LENGTH
+ * Uri
+ * Cost
+ *
+ * \sa https://redmine.named-data.net/projects/nlsr/wiki/Routing_Table_Dataset
+ */
class NextHop
{
public:
+ using Error = ndn::tlv::Error;
+
NextHop()
: m_connectingFaceUri()
, m_routeCost(0)
@@ -40,10 +54,15 @@
}
NextHop(const std::string& cfu, double rc)
- : m_isHyperbolic(false)
+ : m_connectingFaceUri(cfu)
+ , m_routeCost(rc)
+ , m_isHyperbolic(false)
{
- m_connectingFaceUri = cfu;
- m_routeCost = rc;
+ }
+
+ NextHop(const ndn::Block& block)
+ {
+ wireDecode(block);
}
const std::string&
@@ -96,11 +115,23 @@
return m_isHyperbolic;
}
+ template<ndn::encoding::Tag TAG>
+ size_t
+ wireEncode(ndn::EncodingImpl<TAG>& block) const;
+
+ const ndn::Block&
+ wireEncode() const;
+
+ void
+ wireDecode(const ndn::Block& wire);
+
private:
std::string m_connectingFaceUri;
double m_routeCost;
bool m_isHyperbolic;
+ mutable ndn::Block m_wire;
+
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
/*! \brief Used to adjust floating point route costs to integers
Since NFD uses integer route costs in the FIB, hyperbolic paths with similar route costs
@@ -115,6 +146,8 @@
static const uint64_t HYPERBOLIC_COST_ADJUSTMENT_FACTOR = 1000;
};
+NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(NextHop);
+
bool
operator==(const NextHop& lhs, const NextHop& rhs);
diff --git a/src/route/routing-table-entry.cpp b/src/route/routing-table-entry.cpp
index 750dc79..7320d91 100644
--- a/src/route/routing-table-entry.cpp
+++ b/src/route/routing-table-entry.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017, The University of Memphis,
+/*
+ * Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
* This file is part of NLSR (Named-data Link State Routing).
@@ -16,19 +16,96 @@
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#include "routing-table-entry.hpp"
#include "nexthop-list.hpp"
+#include "tlv-nlsr.hpp"
namespace nlsr {
+template<ndn::encoding::Tag TAG>
+size_t
+RoutingTableEntry::wireEncode(ndn::EncodingImpl<TAG>& block) const
+{
+ size_t totalLength = 0;
+
+ for (auto it = m_nexthopList.rbegin(); it != m_nexthopList.rend(); ++it) {
+ totalLength += it->wireEncode(block);
+ }
+
+ totalLength += m_destination.wireEncode(block);
+
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTableEntry);
+
+ return totalLength;
+}
+
+NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableEntry);
+
+const ndn::Block&
+RoutingTableEntry::wireEncode() const
+{
+ if (m_wire.hasWire()) {
+ return m_wire;
+ }
+
+ ndn::EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ ndn::EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ m_wire = buffer.block();
+
+ return m_wire;
+}
+
+void
+RoutingTableEntry::wireDecode(const ndn::Block& wire)
+{
+ m_nexthopList.clear();
+
+ m_wire = wire;
+
+ if (m_wire.type() != ndn::tlv::nlsr::RoutingTableEntry) {
+ std::stringstream error;
+ error << "Expected RoutingTable Block, but Block is of a different type: #"
+ << m_wire.type();
+ BOOST_THROW_EXCEPTION(Error(error.str()));
+ }
+
+ m_wire.parse();
+
+ auto val = m_wire.elements_begin();
+
+ if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
+ m_destination.wireDecode(*val);
+ ++val;
+ }
+ else {
+ BOOST_THROW_EXCEPTION(Error("Missing required destination field"));
+ }
+
+ for (; val != m_wire.elements_end(); ++val) {
+ if (val->type() == ndn::tlv::nlsr::NextHop) {
+ m_nexthopList.addNextHop(NextHop(*val));
+ }
+ else {
+ std::stringstream error;
+ error << "Expected NextHop Block, but Block is of a different type: #"
+ << m_wire.type();
+ BOOST_THROW_EXCEPTION(Error(error.str()));
+ }
+ }
+}
+
std::ostream&
operator<<(std::ostream& os, const RoutingTableEntry& rte)
{
- os << "RoutingTableEntry("
- << "Destination: " << rte.getDestination()
- << "Next hop list: " << rte.getNexthopList() << ")";
+ os << " Destination: " << rte.getDestination() << "\n"
+ << rte.getNexthopList() << "\n";
return os;
}
diff --git a/src/route/routing-table-entry.hpp b/src/route/routing-table-entry.hpp
index 83731b9..ffbbfac 100644
--- a/src/route/routing-table-entry.hpp
+++ b/src/route/routing-table-entry.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
@@ -16,27 +16,38 @@
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- *
- **/
+ */
#ifndef NLSR_ROUTING_TABLE_ENTRY_HPP
#define NLSR_ROUTING_TABLE_ENTRY_HPP
#include "nexthop-list.hpp"
+#include <ndn-cxx/encoding/block.hpp>
+#include <ndn-cxx/encoding/encoding-buffer.hpp>
+#include <ndn-cxx/encoding/tlv.hpp>
#include <ndn-cxx/name.hpp>
namespace nlsr {
+/*! \brief Data abstraction for RouteTableInfo
+ *
+ * RoutingTableEntry := ROUTINGTABLEENTRY-TYPE TLV-LENGTH
+ * Name
+ * NexthopList*
+ *
+ * \sa https://redmine.named-data.net/projects/nlsr/wiki/Routing_Table_DataSet
+ */
class RoutingTableEntry
{
public:
- RoutingTableEntry()
- {
- }
+ using Error = ndn::tlv::Error;
- ~RoutingTableEntry()
+ RoutingTableEntry() = default;
+
+ RoutingTableEntry(const ndn::Block& block)
{
+ wireDecode(block);
}
RoutingTableEntry(const ndn::Name& dest)
@@ -65,15 +76,29 @@
inline bool
operator==(RoutingTableEntry& rhs)
{
- return ((*this).getDestination() == rhs.getDestination() &&
- (*this).getNexthopList() == rhs.getNexthopList());
+ return m_destination == rhs.getDestination() &&
+ m_nexthopList == rhs.getNexthopList();
}
+ template<ndn::encoding::Tag TAG>
+ size_t
+ wireEncode(ndn::EncodingImpl<TAG>& block) const;
+
+ const ndn::Block&
+ wireEncode() const;
+
+ void
+ wireDecode(const ndn::Block& wire);
+
protected:
ndn::Name m_destination;
NexthopList m_nexthopList;
+
+ mutable ndn::Block m_wire;
};
+NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableEntry);
+
std::ostream&
operator<<(std::ostream& os, const RoutingTableEntry& rte);
diff --git a/src/route/routing-table.cpp b/src/route/routing-table.cpp
index 3fcd654..a2ec12b 100644
--- a/src/route/routing-table.cpp
+++ b/src/route/routing-table.cpp
@@ -16,7 +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 <http://www.gnu.org/licenses/>.
- **/
+ */
#include "routing-table.hpp"
#include "nlsr.hpp"
@@ -26,6 +26,7 @@
#include "routing-table-entry.hpp"
#include "name-prefix-table.hpp"
#include "logger.hpp"
+#include "tlv-nlsr.hpp"
#include <list>
#include <string>
@@ -94,24 +95,22 @@
// Inform the NPT that updates have been made
NLSR_LOG_DEBUG("Calling Update NPT With new Route");
(*afterRoutingChange)(m_rTable);
- writeLog();
+ NLSR_LOG_DEBUG(*this);
m_namePrefixTable.writeLog();
m_fib.writeLog();
}
else {
- NLSR_LOG_DEBUG("Adjacency building is scheduled, so"
- " routing table can not be calculated :(");
+ NLSR_LOG_DEBUG("Adjacency building is scheduled, so routing table can not be calculated :(");
}
}
else {
- NLSR_LOG_DEBUG("No Adj LSA of router itself,"
- " so Routing table can not be calculated :(");
+ NLSR_LOG_DEBUG("No Adj LSA of router itself, so Routing table can not be calculated :(");
clearRoutingTable();
clearDryRoutingTable(); // for dry run options
// need to update NPT here
NLSR_LOG_DEBUG("Calling Update NPT With new Route");
(*afterRoutingChange)(m_rTable);
- writeLog();
+ NLSR_LOG_DEBUG(*this);
m_namePrefixTable.writeLog();
m_fib.writeLog();
// debugging purpose end
@@ -199,26 +198,6 @@
}
void
-RoutingTable::writeLog()
-{
- NLSR_LOG_DEBUG("---------------Routing Table------------------");
- for (const auto& rte : m_rTable) {
- NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
- NLSR_LOG_DEBUG("Nexthops: ");
- rte.getNexthopList().writeLog();
- }
-
- if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
- NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
- for (const auto& rte : m_dryTable) {
- NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
- NLSR_LOG_DEBUG("Nexthops: ");
- rte.getNexthopList().writeLog();
- }
- }
-}
-
-void
RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
{
NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
@@ -251,4 +230,100 @@
}
}
+template<ndn::encoding::Tag TAG>
+size_t
+RoutingTableStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const
+{
+ size_t totalLength = 0;
+
+ for (auto it = m_dryTable.rbegin(); it != m_dryTable.rend(); ++it) {
+ totalLength += it->wireEncode(block);
+ }
+
+ for (auto it = m_rTable.rbegin(); it != m_rTable.rend(); ++it) {
+ totalLength += it->wireEncode(block);
+ }
+
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTable);
+
+ return totalLength;
+}
+
+NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableStatus);
+
+const ndn::Block&
+RoutingTableStatus::wireEncode() const
+{
+ if (m_wire.hasWire()) {
+ return m_wire;
+ }
+
+ ndn::EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ ndn::EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ m_wire = buffer.block();
+
+ return m_wire;
+}
+
+void
+RoutingTableStatus::wireDecode(const ndn::Block& wire)
+{
+ m_rTable.clear();
+
+ m_wire = wire;
+
+ if (m_wire.type() != ndn::tlv::nlsr::RoutingTable) {
+ std::stringstream error;
+ error << "Expected RoutingTableStatus Block, but Block is of a different type: #"
+ << m_wire.type();
+ BOOST_THROW_EXCEPTION(Error(error.str()));
+ }
+
+ m_wire.parse();
+
+ auto val = m_wire.elements_begin();
+
+ std::set<ndn::Name> destinations;
+ for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::RoutingTableEntry; ++val) {
+ auto entry = RoutingTableEntry(*val);
+
+ if (destinations.emplace(entry.getDestination()).second) {
+ m_rTable.push_back(entry);
+ }
+ else {
+ // If destination already exists then this is the start of dry HR table
+ m_dryTable.push_back(entry);
+ }
+ }
+
+ if (val != m_wire.elements_end()) {
+ std::stringstream error;
+ error << "Expected the end of elements, but Block is of a different type: #"
+ << val->type();
+ BOOST_THROW_EXCEPTION(Error(error.str()));
+ }
+}
+
+std::ostream&
+operator<<(std::ostream& os, const RoutingTableStatus& rts)
+{
+ os << "Routing Table:\n";
+ for (const auto& rte : rts.getRoutingTableEntry()) {
+ os << rte;
+ }
+
+ if (!rts.getDryRoutingTableEntry().empty()) {
+ os << "Dry-Run Hyperbolic Routing Table:\n";
+ for (const auto& rte : rts.getDryRoutingTableEntry()) {
+ os << rte;
+ }
+ }
+ return os;
+}
+
} // namespace nlsr
diff --git a/src/route/routing-table.hpp b/src/route/routing-table.hpp
index af2ba93..8f9309b 100644
--- a/src/route/routing-table.hpp
+++ b/src/route/routing-table.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2014-2020, The University of Memphis,
* Regents of the University of California
*
@@ -16,8 +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 <http://www.gnu.org/licenses/>.
- *
- **/
+ */
#ifndef NLSR_ROUTING_TABLE_HPP
#define NLSR_ROUTING_TABLE_HPP
@@ -35,7 +34,58 @@
class NextHop;
-class RoutingTable : boost::noncopyable
+/*! \brief Data abstraction for routing table status
+ *
+ * RtStatus := RT-STATUS-TYPE TLV-LENGTH
+ * RouteTableEntry*
+ *
+ * \sa https://redmine.named-data.net/projects/nlsr/wiki/Routing_Table_Dataset
+ */
+class RoutingTableStatus
+{
+public:
+ using Error = ndn::tlv::Error;
+
+ RoutingTableStatus() = default;
+
+ RoutingTableStatus(const ndn::Block& block)
+ {
+ wireDecode(block);
+ }
+
+ const std::list<RoutingTableEntry>&
+ getRoutingTableEntry() const
+ {
+ return m_rTable;
+ }
+
+ const std::list<RoutingTableEntry>&
+ getDryRoutingTableEntry() const
+ {
+ return m_dryTable;
+ }
+
+ const ndn::Block&
+ wireEncode() const;
+
+private:
+ void
+ wireDecode(const ndn::Block& wire);
+
+ template<ndn::encoding::Tag TAG>
+ size_t
+ wireEncode(ndn::EncodingImpl<TAG>& block) const;
+
+PUBLIC_WITH_TESTS_ELSE_PROTECTED:
+ std::list<RoutingTableEntry> m_dryTable;
+ std::list<RoutingTableEntry> m_rTable;
+ mutable ndn::Block m_wire;
+};
+
+std::ostream&
+operator<<(std::ostream& os, const RoutingTableStatus& rts);
+
+class RoutingTable : public RoutingTableStatus
{
public:
explicit
@@ -84,18 +134,6 @@
return m_routingCalcInterval;
}
- const std::list<RoutingTableEntry>&
- getRoutingTableEntry() const
- {
- return m_rTable;
- }
-
- const std::list<RoutingTableEntry>&
- getDryRoutingTableEntry() const
- {
- return m_dryTable;
- }
-
uint64_t
getRtSize()
{
@@ -117,23 +155,15 @@
void
clearDryRoutingTable();
- void
- writeLog();
-
public:
std::unique_ptr<AfterRoutingChange> afterRoutingChange;
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
- std::list<RoutingTableEntry> m_rTable;
-
private:
ndn::Scheduler& m_scheduler;
Fib& m_fib;
Lsdb& m_lsdb;
NamePrefixTable& m_namePrefixTable;
- std::list<RoutingTableEntry> m_dryTable;
-
ndn::time::seconds m_routingCalcInterval;
bool m_isRoutingTableCalculating;