Skip to content

Commit 834ffb1

Browse files
committed
Add signal handling, improve robustness for sequence number file writes.
Also removes now unused Fib::Clean method. Change-Id: I7db2d66fa329920467ea7d5e7c16ff5a2ee9f44b
1 parent 20e60a2 commit 834ffb1

File tree

6 files changed

+30
-34
lines changed

6 files changed

+30
-34
lines changed

src/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2014-2023, The University of Memphis,
3+
* Copyright (c) 2014-2025, The University of Memphis,
44
* Regents of the University of California,
55
* Arizona Board of Regents.
66
*
@@ -88,7 +88,6 @@ main(int argc, char** argv)
8888
face.processEvents();
8989
}
9090
catch (const std::exception& e) {
91-
nlsr.getFib().clean();
9291
std::cerr << "FATAL: " << boost::diagnostic_information(e) << std::endl;
9392
return 1;
9493
}

src/nlsr.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
7878
m_lsdb)
7979
, m_statsCollector(m_lsdb, m_helloProtocol)
8080
, m_faceMonitor(m_face)
81+
, m_terminateSignals(face.getIoContext(), SIGINT, SIGTERM)
8182
{
8283
NLSR_LOG_DEBUG("Initializing Nlsr");
8384

@@ -107,6 +108,10 @@ Nlsr::Nlsr(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam)
107108
neighbor.setLinkCost(0);
108109
}
109110
}
111+
112+
m_terminateSignals.async_wait([this] (auto&&... args) {
113+
terminate(std::forward<decltype(args)>(args)...);
114+
});
110115
}
111116

112117
void
@@ -365,4 +370,13 @@ Nlsr::enableIncomingFaceIdIndication()
365370
});
366371
}
367372

373+
void
374+
Nlsr::terminate(const boost::system::error_code& error, int signalNo)
375+
{
376+
if (error)
377+
return;
378+
NLSR_LOG_INFO("Caught signal " << signalNo << " (" << ::strsignal(signalNo) << "), exiting...");
379+
m_face.getIoContext().stop();
380+
}
381+
368382
} // namespace nlsr

src/nlsr.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <ndn-cxx/security/key-chain.hpp>
4949
#include <ndn-cxx/util/scheduler.hpp>
5050

51+
#include <boost/asio/signal_set.hpp>
5152
namespace nlsr {
5253

5354
class Nlsr
@@ -157,6 +158,9 @@ class Nlsr
157158
void
158159
enableIncomingFaceIdIndication();
159160

161+
void
162+
terminate(const boost::system::error_code& error, int signalNo);
163+
160164
public:
161165
static inline const ndn::Name LOCALHOST_PREFIX{"/localhost/nlsr"};
162166

@@ -196,6 +200,7 @@ class Nlsr
196200

197201
private:
198202
ndn::nfd::FaceMonitor m_faceMonitor;
203+
boost::asio::signal_set m_terminateSignals;
199204
};
200205

201206
} // namespace nlsr

src/route/fib.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,6 @@ Fib::update(const ndn::Name& name, const NexthopList& allHops)
153153
}
154154
}
155155

156-
void
157-
Fib::clean()
158-
{
159-
NLSR_LOG_DEBUG("Clean called");
160-
for (const auto& it : m_table) {
161-
for (const auto& hop : it.second.nexthopSet) {
162-
unregisterPrefix(it.second.name, hop.getConnectingFaceUri());
163-
}
164-
}
165-
}
166-
167156
unsigned int
168157
Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
169158
{

src/route/fib.hpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
/*
3-
* Copyright (c) 2014-2023, The University of Memphis,
3+
* Copyright (c) 2014-2025, The University of Memphis,
44
* Regents of the University of California,
55
* Arizona Board of Regents.
66
*
@@ -90,17 +90,6 @@ class Fib
9090
void
9191
update(const ndn::Name& name, const NexthopList& allHops);
9292

93-
/*! \brief Remove all entries from the FIB.
94-
*
95-
* This method is called before terminating NLSR to minimize the
96-
* time NFD spends routing on now-invalid information. This is not
97-
* strictly necessary, because eventually those prefix registrations
98-
* will expire, but cleaning up after ourselves improves
99-
* performance.
100-
*/
101-
void
102-
clean();
103-
10493
void
10594
setEntryRefreshTime(int32_t fert)
10695
{

src/sequencing-manager.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2-
/**
3-
* Copyright (c) 2014-2020, The University of Memphis,
2+
/*
3+
* Copyright (c) 2014-2025, The University of Memphis,
44
* Regents of the University of California,
55
* Arizona Board of Regents.
66
*
@@ -17,7 +17,7 @@
1717
*
1818
* You should have received a copy of the GNU General Public License along with
1919
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20-
**/
20+
*/
2121

2222
#include "sequencing-manager.hpp"
2323
#include "logger.hpp"
@@ -43,13 +43,13 @@ void
4343
SequencingManager::writeSeqNoToFile() const
4444
{
4545
writeLog();
46-
std::ofstream outputFile(m_seqFileNameWithPath.c_str());
47-
std::ostringstream os;
48-
os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
49-
<< "AdjLsaSeq " << std::to_string(m_adjLsaSeq) << "\n"
50-
<< "CorLsaSeq " << std::to_string(m_corLsaSeq);
51-
outputFile << os.str();
46+
std::string tempPath = m_seqFileNameWithPath + ".tmp";
47+
std::ofstream outputFile(tempPath.c_str());
48+
outputFile << "NameLsaSeq " << m_nameLsaSeq << "\n"
49+
<< "AdjLsaSeq " << m_adjLsaSeq << "\n"
50+
<< "CorLsaSeq " << m_corLsaSeq;
5251
outputFile.close();
52+
std::filesystem::rename(tempPath, m_seqFileNameWithPath);
5353
}
5454

5555
void

0 commit comments

Comments
 (0)