Skip to content

Commit

Permalink
Add signal handling, improve robustness for sequence number file writes.
Browse files Browse the repository at this point in the history
Also removes now unused Fib::Clean method.

Change-Id: I7db2d66fa329920467ea7d5e7c16ff5a2ee9f44b
  • Loading branch information
awlane committed Jan 31, 2025
1 parent 20e60a2 commit 834ffb1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/main.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-2025, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -88,7 +88,6 @@ main(int argc, char** argv)
face.processEvents();
}
catch (const std::exception& e) {
nlsr.getFib().clean();
std::cerr << "FATAL: " << boost::diagnostic_information(e) << std::endl;
return 1;
}
Expand Down
14 changes: 14 additions & 0 deletions src/nlsr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
m_lsdb)
, m_statsCollector(m_lsdb, m_helloProtocol)
, m_faceMonitor(m_face)
, m_terminateSignals(face.getIoContext(), SIGINT, SIGTERM)
{
NLSR_LOG_DEBUG("Initializing Nlsr");

Expand Down Expand Up @@ -107,6 +108,10 @@ Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
neighbor.setLinkCost(0);
}
}

m_terminateSignals.async_wait([this] (auto&&... args) {
terminate(std::forward<decltype(args)>(args)...);
});
}

void
Expand Down Expand Up @@ -365,4 +370,13 @@ Nlsr::enableIncomingFaceIdIndication()
});
}

void
Nlsr::terminate(const boost::system::error_code& error, int signalNo)
{
if (error)
return;
NLSR_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), exiting...");
m_face.getIoContext().stop();
}

} // namespace nlsr
5 changes: 5 additions & 0 deletions src/nlsr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <ndn-cxx/security/key-chain.hpp>
#include <ndn-cxx/util/scheduler.hpp>

#include <boost/asio/signal_set.hpp>
namespace nlsr {

class Nlsr
Expand Down Expand Up @@ -157,6 +158,9 @@ class Nlsr
void
enableIncomingFaceIdIndication();

void
terminate(const boost::system::error_code& error, int signalNo);

public:
static inline const ndn::Name LOCALHOST_PREFIX{"/localhost/nlsr"};

Expand Down Expand Up @@ -196,6 +200,7 @@ class Nlsr

private:
ndn::nfd::FaceMonitor m_faceMonitor;
boost::asio::signal_set m_terminateSignals;
};

} // namespace nlsr
Expand Down
11 changes: 0 additions & 11 deletions src/route/fib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,6 @@ Fib::update(const ndn::Name& name, const NexthopList& allHops)
}
}

void
Fib::clean()
{
NLSR_LOG_DEBUG("Clean called");
for (const auto& it : m_table) {
for (const auto& hop : it.second.nexthopSet) {
unregisterPrefix(it.second.name, hop.getConnectingFaceUri());
}
}
}

unsigned int
Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
{
Expand Down
13 changes: 1 addition & 12 deletions src/route/fib.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-2025, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand Down Expand Up @@ -90,17 +90,6 @@ class Fib
void
update(const ndn::Name& name, const NexthopList& allHops);

/*! \brief Remove all entries from the FIB.
*
* This method is called before terminating NLSR to minimize the
* time NFD spends routing on now-invalid information. This is not
* strictly necessary, because eventually those prefix registrations
* will expire, but cleaning up after ourselves improves
* performance.
*/
void
clean();

void
setEntryRefreshTime(int32_t fert)
{
Expand Down
18 changes: 9 additions & 9 deletions src/sequencing-manager.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-2020, The University of Memphis,
/*
* Copyright (c) 2014-2025, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
Expand All @@ -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 "sequencing-manager.hpp"
#include "logger.hpp"
Expand All @@ -43,13 +43,13 @@ void
SequencingManager::writeSeqNoToFile() const
{
writeLog();
std::ofstream outputFile(m_seqFileNameWithPath.c_str());
std::ostringstream os;
os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
<< "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n"
<< "CorLsaSeq " << std::to_string(m_corLsaSeq);
outputFile << os.str();
std::string tempPath = m_seqFileNameWithPath + ".tmp";
std::ofstream outputFile(tempPath.c_str());
outputFile << "NameLsaSeq " << m_nameLsaSeq << "\n"
<< "AdjLsaSeq " << m_adjLsaSeq << "\n"
<< "CorLsaSeq " << m_corLsaSeq;
outputFile.close();
std::filesystem::rename(tempPath, m_seqFileNameWithPath);
}

void
Expand Down

0 comments on commit 834ffb1

Please sign in to comment.