From fd1e9406489606fea52b5e3e60d2b4cc66dab109 Mon Sep 17 00:00:00 2001 From: Davide Pesavento Date: Mon, 13 Nov 2023 15:40:41 -0500 Subject: [PATCH] Switch to Face::getIoContext() and simplify some code Change-Id: Iba49171f6e59ead02283e8f975219ab97fcbeb00 --- src/adjacency-list.cpp | 78 +++++++++----------------- src/hello-protocol.cpp | 24 ++++---- src/lsdb.cpp | 34 +++++------ src/lsdb.hpp | 4 +- src/nlsr.cpp | 27 ++++----- src/route/routing-table-calculator.cpp | 2 +- tests/test-adjacency-list.cpp | 4 +- 7 files changed, 65 insertions(+), 108 deletions(-) diff --git a/src/adjacency-list.cpp b/src/adjacency-list.cpp index 302d9b9..cb192eb 100644 --- a/src/adjacency-list.cpp +++ b/src/adjacency-list.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2021, The University of Memphis, + * Copyright (c) 2014-2023, The University of Memphis, * Regents of the University of California, * Arizona Board of Regents. * @@ -43,9 +43,9 @@ Adjacent AdjacencyList::getAdjacent(const ndn::Name& adjName) { Adjacent adj(adjName); - std::list::iterator it = find(adjName); + auto it = find(adjName); if (it != m_adjList.end()) { - return (*it); + return *it; } return adj; } @@ -60,56 +60,47 @@ AdjacencyList::operator==(const AdjacencyList& adl) const std::set ourSet(m_adjList.cbegin(), m_adjList.cend()); std::set theirSet(theirList.cbegin(), theirList.cend()); - return ourSet == theirSet; } bool AdjacencyList::isNeighbor(const ndn::Name& adjName) const { - std::list::const_iterator it = find(adjName); - if (it == m_adjList.end()) - { - return false; - } - return true; + return find(adjName) != m_adjList.end(); } void AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor) { - std::list::iterator it = find(neighbor); - if (it == m_adjList.end()) { - return ; + auto it = find(neighbor); + if (it != m_adjList.end()) { + it->setInterestTimedOutNo(it->getInterestTimedOutNo() + 1); } - (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1); } void -AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor, - uint32_t count) +AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor, uint32_t count) { - std::list::iterator it = find(neighbor); + auto it = find(neighbor); if (it != m_adjList.end()) { - (*it).setInterestTimedOutNo(count); + it->setInterestTimedOutNo(count); } } int32_t AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const { - std::list::const_iterator it = find(neighbor); + auto it = find(neighbor); if (it == m_adjList.end()) { return -1; } - return (*it).getInterestTimedOutNo(); + return it->getInterestTimedOutNo(); } Adjacent::Status AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const { - std::list::const_iterator it = find(neighbor); - + auto it = find(neighbor); if (it == m_adjList.end()) { return Adjacent::STATUS_UNKNOWN; } @@ -121,7 +112,7 @@ AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const void AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status) { - std::list::iterator it = find(neighbor); + auto it = find(neighbor); if (it != m_adjList.end()) { it->setStatus(status); } @@ -144,8 +135,7 @@ AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const { uint32_t nTimedOutNeighbors = 0; - for (const Adjacent& adjacency : m_adjList) { - + for (const auto& adjacency : m_adjList) { if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) { return true; } @@ -154,12 +144,7 @@ AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const } } - if (nTimedOutNeighbors == m_adjList.size()) { - return true; - } - else { - return false; - } + return nTimedOutNeighbors == m_adjList.size(); } int32_t @@ -177,54 +162,43 @@ AdjacencyList::getNumOfActiveNeighbor() const std::list::iterator AdjacencyList::find(const ndn::Name& adjName) { - return std::find_if(m_adjList.begin(), - m_adjList.end(), + return std::find_if(m_adjList.begin(), m_adjList.end(), std::bind(&Adjacent::compare, _1, std::cref(adjName))); } std::list::const_iterator AdjacencyList::find(const ndn::Name& adjName) const { - return std::find_if(m_adjList.cbegin(), - m_adjList.cend(), + return std::find_if(m_adjList.cbegin(), m_adjList.cend(), std::bind(&Adjacent::compare, _1, std::cref(adjName))); } AdjacencyList::iterator AdjacencyList::findAdjacent(const ndn::Name& adjName) { - return std::find_if(m_adjList.begin(), - m_adjList.end(), - std::bind(&Adjacent::compare, - _1, std::cref(adjName))); + return std::find_if(m_adjList.begin(), m_adjList.end(), + std::bind(&Adjacent::compare, _1, std::cref(adjName))); } AdjacencyList::iterator AdjacencyList::findAdjacent(uint64_t faceId) { - return std::find_if(m_adjList.begin(), - m_adjList.end(), - std::bind(&Adjacent::compareFaceId, - _1, faceId)); + return std::find_if(m_adjList.begin(), m_adjList.end(), + std::bind(&Adjacent::compareFaceId, _1, faceId)); } AdjacencyList::iterator AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri) { - return std::find_if(m_adjList.begin(), - m_adjList.end(), - std::bind(&Adjacent::compareFaceUri, - _1, faceUri)); + return std::find_if(m_adjList.begin(), m_adjList.end(), + std::bind(&Adjacent::compareFaceUri, _1, faceUri)); } uint64_t AdjacencyList::getFaceId(const ndn::FaceUri& faceUri) { - std::list::iterator it = std::find_if(m_adjList.begin(), - m_adjList.end(), - std::bind(&Adjacent::compareFaceUri, - _1, faceUri)); - + auto it = std::find_if(m_adjList.begin(), m_adjList.end(), + std::bind(&Adjacent::compareFaceUri, _1, faceUri)); return it != m_adjList.end() ? it->getFaceId() : 0; } diff --git a/src/hello-protocol.cpp b/src/hello-protocol.cpp index ec03da7..49722fe 100644 --- a/src/hello-protocol.cpp +++ b/src/hello-protocol.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2022, The University of Memphis, + * Copyright (c) 2014-2023, The University of Memphis, * Regents of the University of California * * This file is part of NLSR (Named-data Link State Routing). @@ -34,7 +34,7 @@ HelloProtocol::HelloProtocol(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam, RoutingTable& routingTable, Lsdb& lsdb) : m_face(face) - , m_scheduler(m_face.getIoService()) + , m_scheduler(m_face.getIoContext()) , m_keyChain(keyChain) , m_signingInfo(confParam.getSigningInfo()) , m_confParam(confParam) @@ -72,8 +72,7 @@ HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds) interest.setCanBePrefix(true); m_face.expressInterest(interest, std::bind(&HelloProtocol::onContent, this, _1, _2), - [this, seconds] (const ndn::Interest& interest, const ndn::lp::Nack& nack) - { + [this, seconds] (const auto& interest, const auto& nack) { NDN_LOG_TRACE("Received Nack with reason: " << nack.getReason()); NDN_LOG_TRACE("Will treat as timeout in " << 2 * seconds << " seconds"); m_scheduler.schedule(ndn::time::seconds(2 * seconds), @@ -89,7 +88,6 @@ void HelloProtocol::sendHelloInterest(const ndn::Name& neighbor) { auto adjacent = m_adjacencyList.findAdjacent(neighbor); - if (adjacent == m_adjacencyList.end()) { return; } @@ -119,10 +117,10 @@ HelloProtocol::processInterest(const ndn::Name& name, // increment RCV_HELLO_INTEREST hpIncrementSignal(Statistics::PacketType::RCV_HELLO_INTEREST); - NLSR_LOG_DEBUG("Interest Received for Name: " << interestName); + NLSR_LOG_DEBUG("Interest received for Name: " << interestName); if (interestName.get(-2).toUri() != INFO_COMPONENT) { - NLSR_LOG_DEBUG("INFO_COMPONENT not found or interestName: " << interestName - << " does not match expression"); + NLSR_LOG_DEBUG("INFO_COMPONENT not found or Interest Name " << interestName + << " does not match expression"); return; } @@ -130,9 +128,9 @@ HelloProtocol::processInterest(const ndn::Name& name, neighbor.wireDecode(interestName.get(-1).blockFromValue()); NLSR_LOG_DEBUG("Neighbor: " << neighbor); if (m_adjacencyList.isNeighbor(neighbor)) { - std::shared_ptr data = std::make_shared(); + auto data = std::make_shared(); data->setName(ndn::Name(interest.getName()).appendVersion()); - data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec + data->setFreshnessPeriod(10_s); data->setContent(ndn::make_span(reinterpret_cast(INFO_COMPONENT.data()), INFO_COMPONENT.size())); @@ -233,8 +231,8 @@ HelloProtocol::onContentValidated(const ndn::Data& data) m_adjacencyList.setTimedOutInterestCount(neighbor, 0); Adjacent::Status newStatus = m_adjacencyList.getStatusOfNeighbor(neighbor); - NLSR_LOG_DEBUG("Neighbor : " << neighbor); - NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus); + NLSR_LOG_DEBUG("Neighbor: " << neighbor); + NLSR_LOG_DEBUG("Old Status: " << oldStatus << ", New Status: " << newStatus); // change in Adjacency list if ((oldStatus - newStatus) != 0) { if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { @@ -254,7 +252,7 @@ void HelloProtocol::onContentValidationFailed(const ndn::Data& data, const ndn::security::ValidationError& ve) { - NLSR_LOG_DEBUG("Validation Error: " << ve); + NLSR_LOG_DEBUG("Validation error: " << ve); } } // namespace nlsr diff --git a/src/lsdb.cpp b/src/lsdb.cpp index 876fa57..f672a41 100644 --- a/src/lsdb.cpp +++ b/src/lsdb.cpp @@ -33,7 +33,7 @@ INIT_LOGGER(Lsdb); Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam) : m_face(face) - , m_scheduler(face.getIoService()) + , m_scheduler(face.getIoContext()) , m_confParam(confParam) , m_sync(m_face, keyChain, [this] (const auto& routerName, const Lsa::Type& lsaType, @@ -203,7 +203,7 @@ Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest) } // else the interest is for other router's LSA, serve signed data from LsaSegmentStorage else if (auto lsaSegment = m_lsaStorage.find(interest); lsaSegment) { - NLSR_LOG_TRACE("Found data in lsa storage. Sending data for " << interest.getName()); + NLSR_LOG_TRACE("Found data in LSA storage. Sending data for " << interest.getName()); m_face.put(*lsaSegment); } } @@ -256,19 +256,16 @@ Lsdb::installLsa(std::shared_ptr lsa) auto chkLsa = findLsa(lsa->getOriginRouter(), lsa->getType()); if (chkLsa == nullptr) { - NLSR_LOG_DEBUG("Adding " << lsa->getType() << " LSA"); - NLSR_LOG_DEBUG(lsa->toString()); + NLSR_LOG_DEBUG("Adding LSA:\n" << lsa->toString()); m_lsdb.emplace(lsa); - onLsdbModified(lsa, LsdbUpdate::INSTALLED, {}, {}); lsa->setExpiringEventId(scheduleLsaExpiration(lsa, timeToExpire)); } // Else this is a known name LSA, so we are updating it. else if (chkLsa->getSeqNo() < lsa->getSeqNo()) { - NLSR_LOG_DEBUG("Updating " << lsa->getType() << " LSA:"); - NLSR_LOG_DEBUG(chkLsa->toString()); + NLSR_LOG_DEBUG("Updating LSA:\n" << chkLsa->toString()); chkLsa->setSeqNo(lsa->getSeqNo()); chkLsa->setExpirationTimePoint(lsa->getExpirationTimePoint()); @@ -278,8 +275,7 @@ Lsdb::installLsa(std::shared_ptr lsa) } chkLsa->setExpiringEventId(scheduleLsaExpiration(chkLsa, timeToExpire)); - NLSR_LOG_DEBUG("Updated " << lsa->getType() << " LSA:"); - NLSR_LOG_DEBUG(chkLsa->toString()); + NLSR_LOG_DEBUG("Updated LSA:\n" << chkLsa->toString()); } } @@ -288,8 +284,7 @@ Lsdb::removeLsa(const LsaContainer::index::type::iterator& lsaIt) { if (lsaIt != m_lsdb.end()) { auto lsaPtr = *lsaIt; - NLSR_LOG_DEBUG("Removing " << lsaPtr->getType() << " LSA:"); - NLSR_LOG_DEBUG(lsaPtr->toString()); + NLSR_LOG_DEBUG("Removing LSA:\n" << lsaPtr->toString()); m_lsdb.erase(lsaIt); onLsdbModified(lsaPtr, LsdbUpdate::REMOVED, {}, {}); } @@ -385,13 +380,11 @@ Lsdb::expireOrRefreshLsa(std::shared_ptr lsa) if (lsaPtr->getSeqNo() == lsa->getSeqNo()) { if (lsaPtr->getOriginRouter() == m_thisRouterPrefix) { NLSR_LOG_DEBUG("Own " << lsaPtr->getType() << " LSA, so refreshing it"); - NLSR_LOG_DEBUG("Current LSA:"); - NLSR_LOG_DEBUG(lsaPtr->toString()); + NLSR_LOG_DEBUG("Current LSA:\n" << lsaPtr->toString()); lsaPtr->setSeqNo(lsaPtr->getSeqNo() + 1); m_sequencingManager.setLsaSeq(lsaPtr->getSeqNo(), lsaPtr->getType()); lsaPtr->setExpirationTimePoint(getLsaExpirationTimePoint()); - NLSR_LOG_DEBUG("Updated LSA:"); - NLSR_LOG_DEBUG(lsaPtr->toString()); + NLSR_LOG_DEBUG("Updated LSA:\n" << lsaPtr->toString()); // schedule refreshing event again lsaPtr->setExpiringEventId(scheduleLsaExpiration(lsaPtr, m_lsaRefreshTime)); m_sequencingManager.writeSeqNoToFile(); @@ -490,13 +483,12 @@ Lsdb::onFetchLsaError(uint32_t errorCode, const std::string& msg, const ndn::Nam // immediately since at the least the LSA Interest lifetime has elapsed. // Otherwise, it is necessary to delay the Interest re-expression to prevent // the potential for constant Interest flooding. - ndn::time::seconds delay = m_confParam.getLsaInterestLifetime(); - + auto delay = m_confParam.getLsaInterestLifetime(); if (errorCode == ndn::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) { - delay = ndn::time::seconds(0); + delay = 0_s; } - m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this, - interestName, retransmitNo + 1, /*Multicast FaceID*/0, deadline)); + m_scheduler.schedule(delay, std::bind(&Lsdb::expressInterest, this, interestName, + retransmitNo + 1, /*Multicast FaceID*/0, deadline)); } } } @@ -559,7 +551,7 @@ Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, const ndn::Name& inter } } catch (const std::exception& e) { - NLSR_LOG_TRACE("LSA data decoding error :( " << e.what()); + NLSR_LOG_TRACE("LSA data decoding error: " << e.what()); } } } diff --git a/src/lsdb.hpp b/src/lsdb.hpp index 2c9008d..1116e98 100644 --- a/src/lsdb.hpp +++ b/src/lsdb.hpp @@ -220,11 +220,11 @@ PUBLIC_WITH_TESTS_ELSE_PRIVATE: \param seqNo The sequence number to check. */ bool - isLsaNew(const ndn::Name& originRouter, const Lsa::Type& lsaType, uint64_t lsSeqNo) const + isLsaNew(const ndn::Name& originRouter, const Lsa::Type& lsaType, uint64_t seqNo) const { // Is the name in the LSDB and the supplied seq no is the highest so far auto lsaPtr = findLsa(originRouter, lsaType); - return lsaPtr ? lsaPtr->getSeqNo() < lsSeqNo : true; + return lsaPtr ? lsaPtr->getSeqNo() < seqNo : true; } void diff --git a/src/nlsr.cpp b/src/nlsr.cpp index 8c4033a..f135636 100644 --- a/src/nlsr.cpp +++ b/src/nlsr.cpp @@ -36,7 +36,7 @@ INIT_LOGGER(Nlsr); Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam) : m_face(face) - , m_scheduler(face.getIoService()) + , m_scheduler(face.getIoContext()) , m_confParam(confParam) , m_adjacencyList(confParam.getAdjacencyList()) , m_namePrefixList(confParam.getNamePrefixList()) @@ -259,7 +259,7 @@ void Nlsr::initializeFaces(const FetchDatasetCallback& onFetchSuccess, const FetchDatasetTimeoutCallback& onFetchFailure) { - NLSR_LOG_TRACE("Initializing Faces..."); + NLSR_LOG_TRACE("Initializing faces"); m_faceDatasetController.fetch(onFetchSuccess, onFetchFailure); } @@ -317,14 +317,12 @@ Nlsr::onFaceDatasetFetchTimeout(uint32_t code, const std::string& msg, uint32_t nRetriesSoFar) { - NLSR_LOG_DEBUG("onFaceDatasetFetchTimeout"); // If we have exceeded the maximum attempt count, do not try again. if (nRetriesSoFar++ < m_confParam.getFaceDatasetFetchTries()) { NLSR_LOG_DEBUG("Failed to fetch dataset: " << msg << ". Attempting retry #" << nRetriesSoFar); - m_faceDatasetController.fetch(std::bind(&Nlsr::processFaceDataset, - this, _1), - std::bind(&Nlsr::onFaceDatasetFetchTimeout, - this, _1, _2, nRetriesSoFar)); + m_faceDatasetController.fetch(std::bind(&Nlsr::processFaceDataset, this, _1), + std::bind(&Nlsr::onFaceDatasetFetchTimeout, + this, _1, _2, nRetriesSoFar)); } else { NLSR_LOG_ERROR("Failed to fetch dataset: " << msg << ". Exceeded limit of " << @@ -339,17 +337,12 @@ Nlsr::onFaceDatasetFetchTimeout(uint32_t code, void Nlsr::scheduleDatasetFetch() { - NLSR_LOG_DEBUG("Scheduling Dataset Fetch in " << m_confParam.getFaceDatasetFetchInterval()); + NLSR_LOG_DEBUG("Scheduling dataset fetch in " << m_confParam.getFaceDatasetFetchInterval()); - m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(), - [this] { - this->initializeFaces( - [this] (const std::vector& faces) { - this->processFaceDataset(faces); - }, - [this] (uint32_t code, const std::string& msg) { - this->onFaceDatasetFetchTimeout(code, msg, 0); - }); + m_scheduler.schedule(m_confParam.getFaceDatasetFetchInterval(), [this] { + initializeFaces( + [this] (const auto& faces) { processFaceDataset(faces); }, + [this] (uint32_t code, const std::string& msg) { onFaceDatasetFetchTimeout(code, msg, 0); }); }); } diff --git a/src/route/routing-table-calculator.cpp b/src/route/routing-table-calculator.cpp index 54c8a47..474604a 100644 --- a/src/route/routing-table-calculator.cpp +++ b/src/route/routing-table-calculator.cpp @@ -435,7 +435,7 @@ HyperbolicRoutingCalculator::calculatePath(Map& map, RoutingTable& rt, // Iterate over directly connected neighbors std::list neighbors = adjacencies.getAdjList(); - for (std::list::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) { + for (auto adj = neighbors.begin(); adj != neighbors.end(); ++adj) { // Don't calculate nexthops using an inactive router if (adj->getStatus() == Adjacent::STATUS_INACTIVE) { diff --git a/tests/test-adjacency-list.cpp b/tests/test-adjacency-list.cpp index 1a2ca87..1f4158b 100644 --- a/tests/test-adjacency-list.cpp +++ b/tests/test-adjacency-list.cpp @@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE(Basic) const std::string ADJ_NAME_1 = "testname"; const std::string ADJ_NAME_2 = "testname2"; -//adjacent needed to test adjacency list. + // adjacent needed to test adjacency list Adjacent adjacent1(ADJ_NAME_1); Adjacent adjacent2(ADJ_NAME_2); @@ -71,7 +71,7 @@ BOOST_AUTO_TEST_CASE(FindAdjacentByFaceUri) AdjacencyList adjList; adjList.insert(adj1); - std::list::iterator adjIter = adjList.findAdjacent(faceUri); + auto adjIter = adjList.findAdjacent(faceUri); BOOST_CHECK(adjIter != adjList.end()); }