Skip to content

Commit

Permalink
communication: refactor SyncLogicHandler to not need a ConfParameter …
Browse files Browse the repository at this point in the history
…reference

refs #4208

Change-Id: I5194c0a775dc1581af3a6ebd2db3699892671fb4
  • Loading branch information
yoursunny committed Jan 9, 2024
1 parent b875293 commit f467467
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 125 deletions.
71 changes: 33 additions & 38 deletions src/communication/sync-logic-handler.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-2022, The University of Memphis,
* Copyright (c) 2014-2024, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand All @@ -24,33 +24,29 @@
#include "logger.hpp"
#include "utility/name-helper.hpp"

#include <boost/lexical_cast.hpp>

namespace nlsr {

INIT_LOGGER(SyncLogicHandler);

const std::string LSA_COMPONENT{"LSA"};

SyncLogicHandler::SyncLogicHandler(ndn::Face& face, ndn::KeyChain& keyChain,
IsLsaNew isLsaNew, const ConfParameter& conf)
IsLsaNew isLsaNew, const SyncLogicOptions& opts)
: m_isLsaNew(std::move(isLsaNew))
, m_confParam(conf)
, m_nameLsaUserPrefix(ndn::Name(m_confParam.getSyncUserPrefix()).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)))
, m_syncLogic(face, keyChain, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
m_nameLsaUserPrefix, m_confParam.getSyncInterestLifetime(),
, m_routerPrefix(opts.routerPrefix)
, m_hyperbolicState(opts.hyperbolicState)
, m_nameLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::NAME))
, m_adjLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::ADJACENCY))
, m_coorLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::COORDINATE))
, m_syncLogic(face, keyChain, opts.syncProtocol, opts.syncPrefix,
m_nameLsaUserPrefix, opts.syncInterestLifetime,
std::bind(&SyncLogicHandler::processUpdate, this, _1, _2, _3))
{
m_adjLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
.append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY));
m_coorLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
.append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));

if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
m_syncLogic.addUserNode(m_adjLsaUserPrefix);
}

if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
m_syncLogic.addUserNode(m_coorLsaUserPrefix);
}
}
Expand Down Expand Up @@ -83,37 +79,36 @@ SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
{
NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);

// A router should not try to fetch its own LSA
if (originRouter != m_confParam.getRouterPrefix()) {

Lsa::Type lsaType;
std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;

NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
" sequence number than entry in LSDB");
if (originRouter == m_routerPrefix) {
// A router should not try to fetch its own LSA
return;
}

if (m_isLsaNew(originRouter, lsaType, seqNo, incomingFaceId)) {
if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing " <<
"is enabled. Not going to fetch.");
return;
}
auto lsaType = boost::lexical_cast<Lsa::Type>(updateName.get(-1).toUri());
NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
" sequence number than entry in LSDB");

if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state " <<
"is enabled. Not going to fetch.");
return;
}
if (m_isLsaNew(originRouter, lsaType, seqNo, incomingFaceId)) {
if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
m_hyperbolicState == HYPERBOLIC_STATE_ON) {
NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing "
"is enabled. Not going to fetch.");
return;
}

onNewLsa(updateName, seqNo, originRouter, incomingFaceId);
if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
m_hyperbolicState == HYPERBOLIC_STATE_OFF) {
NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state "
"is enabled. Not going to fetch.");
return;
}

onNewLsa(updateName, seqNo, originRouter, incomingFaceId);
}
}

void
SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
SyncLogicHandler::publishRoutingUpdate(Lsa::Type type, uint64_t seqNo)
{
switch (type) {
case Lsa::Type::ADJACENCY:
Expand Down
38 changes: 28 additions & 10 deletions src/communication/sync-logic-handler.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-2022, 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 @@ -28,14 +28,30 @@
#include "sync-protocol-adapter.hpp"
#include "test-access-control.hpp"

#include <boost/lexical_cast.hpp>

namespace nlsr {

/*! \brief NLSR-to-ChronoSync interaction point
struct SyncLogicOptions
{
SyncProtocol syncProtocol;
ndn::Name syncPrefix;
ndn::Name userPrefix;
ndn::time::milliseconds syncInterestLifetime;
ndn::Name routerPrefix;
HyperbolicState hyperbolicState;
};

inline ndn::Name
makeLsaUserPrefix(const ndn::Name& userPrefix, Lsa::Type lsaType)
{
return ndn::Name(userPrefix).append(boost::lexical_cast<std::string>(lsaType));
}

/*! \brief NLSR-to-sync interaction point
*
* This class serves as the abstraction for the syncing portion of
* NLSR and its components. NLSR has no particular reliance on
* ChronoSync, except that the NLSR source would need to be modified
* for use with other sync protocols.
* NLSR and its components.
*/
class SyncLogicHandler
{
Expand All @@ -46,11 +62,12 @@ class SyncLogicHandler
using std::runtime_error::runtime_error;
};

using IsLsaNew =
std::function<bool(const ndn::Name&, const Lsa::Type& lsaType, const uint64_t&, uint64_t/*inFace*/)>;
using IsLsaNew = std::function<
bool (const ndn::Name& routerName, Lsa::Type lsaType, uint64_t seqNo, uint64_t inFace)
>;

SyncLogicHandler(ndn::Face& face, ndn::KeyChain& keyChain,
IsLsaNew isLsaNew, const ConfParameter& conf);
IsLsaNew isLsaNew, const SyncLogicOptions& opts);

/*! \brief Instruct ChronoSync to publish an update.
*
Expand All @@ -62,7 +79,7 @@ class SyncLogicHandler
* \sa publishSyncUpdate
*/
void
publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo);
publishRoutingUpdate(Lsa::Type type, uint64_t seqNo);

PUBLIC_WITH_TESTS_ELSE_PRIVATE:
/*! \brief Callback from Sync protocol
Expand Down Expand Up @@ -91,7 +108,8 @@ class SyncLogicHandler

private:
IsLsaNew m_isLsaNew;
const ConfParameter& m_confParam;
ndn::Name m_routerPrefix;
HyperbolicState m_hyperbolicState;

PUBLIC_WITH_TESTS_ELSE_PRIVATE:
ndn::Name m_nameLsaUserPrefix;
Expand Down
8 changes: 4 additions & 4 deletions src/conf-parameter.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 @@ -340,12 +340,12 @@ class ConfParameter
}

void
setHyperbolicState(int32_t ihc)
setHyperbolicState(HyperbolicState ihc)
{
m_hyperbolicState = ihc;
}

int32_t
HyperbolicState
getHyperbolicState() const
{
return m_hyperbolicState;
Expand Down Expand Up @@ -517,7 +517,7 @@ class ConfParameter

uint32_t m_infoInterestInterval;

int32_t m_hyperbolicState;
HyperbolicState m_hyperbolicState;
double m_corR;
std::vector<double> m_corTheta;

Expand Down
15 changes: 11 additions & 4 deletions src/lsdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ Lsdb::Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
, m_scheduler(face.getIoContext())
, m_confParam(confParam)
, m_sync(m_face, keyChain,
[this] (const auto& routerName, const Lsa::Type& lsaType,
uint64_t sequenceNumber, uint64_t incomingFaceId) {
return isLsaNew(routerName, lsaType, sequenceNumber);
}, m_confParam)
[this] (const auto& routerName, Lsa::Type lsaType, uint64_t seqNo, uint64_t) {
return isLsaNew(routerName, lsaType, seqNo);
},
SyncLogicOptions{
confParam.getSyncProtocol(),
confParam.getSyncPrefix(),
confParam.getSyncUserPrefix(),
confParam.getSyncInterestLifetime(),
confParam.getRouterPrefix(),
confParam.getHyperbolicState()
})
, m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
, m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
, m_thisRouterPrefix(m_confParam.getRouterPrefix())
Expand Down
4 changes: 2 additions & 2 deletions src/lsdb.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 @@ -220,7 +220,7 @@ class Lsdb
\param seqNo The sequence number to check.
*/
bool
isLsaNew(const ndn::Name& originRouter, const Lsa::Type& lsaType, uint64_t seqNo) const
isLsaNew(const ndn::Name& originRouter, 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);
Expand Down
Loading

0 comments on commit f467467

Please sign in to comment.