From: Marcin Siodelski Date: Fri, 24 Aug 2012 14:09:36 +0000 (+0200) Subject: [1959] End the program gracefully if interrupted. X-Git-Tag: trac2351_base~47^2~11^2~19 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b753e8ebd1f6b3e16628e22640efab38fff2cac8;p=thirdparty%2Fkea.git [1959] End the program gracefully if interrupted. --- diff --git a/tests/tools/perfdhcp/test_control.cc b/tests/tools/perfdhcp/test_control.cc index af59105dc6..775ed6fd31 100644 --- a/tests/tools/perfdhcp/test_control.cc +++ b/tests/tools/perfdhcp/test_control.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include @@ -37,6 +38,8 @@ using namespace isc::asiolink; namespace isc { namespace perfdhcp { +bool TestControl::interrupted_ = false; + TestControl::TestControlSocket::TestControlSocket(int socket) : socket_(socket), addr_("127.0.0.1") { @@ -84,8 +87,11 @@ TestControl::TestControl() { bool TestControl::checkExitConditions() const { + if (interrupted_) { + return(true); + } CommandOptions& options = CommandOptions::instance(); - // Check if test period passed.. + // Check if test period passed. if (options.getPeriod() != 0) { if (options.getIpVersion() == 4) { time_period period(stats_mgr4_->getTestPeriod()); @@ -384,6 +390,11 @@ TestControl::getSentPacketsNum(const ExchangeType xchg_type) const { getSentPacketsNum(static_cast(xchg_type))); } +void +TestControl::handleInterrupt(int) { + interrupted_ = true; +} + void TestControl::initPacketTemplates() { CommandOptions& options = CommandOptions::instance(); @@ -692,6 +703,7 @@ TestControl::reset() { transid_gen_.reset(); transid_gen_ = TransidGeneratorPtr(new TransidGenerator()); first_packet_serverid_.clear(); + interrupted_ = false; } void @@ -722,6 +734,9 @@ TestControl::run() { srandom(options.getSeed()); } + // If user interrupts the program we will exit gracefully. + signal(SIGINT, TestControl::handleInterrupt); + // Preload server with number of packets. const bool do_preload = true; for (int i = 0; i < options.getPreload(); ++i) { diff --git a/tests/tools/perfdhcp/test_control.h b/tests/tools/perfdhcp/test_control.h index c94d7b685e..e3a03afd39 100644 --- a/tests/tools/perfdhcp/test_control.h +++ b/tests/tools/perfdhcp/test_control.h @@ -584,6 +584,14 @@ private: /// \return number of sent packets. uint64_t getSentPacketsNum(const ExchangeType xchg_type) const; + /// \brief Handle interrupt signal. + /// + /// Function sets flag indicating that program has been + /// interupted. + /// + /// \param sig signal (ignored) + static void handleInterrupt(int sig); + boost::posix_time::ptime send_due_; ///< Due time to initiate next chunk ///< of exchanges. boost::posix_time::ptime last_sent_; ///< Indicates when the last exchange @@ -602,6 +610,8 @@ private: /// Packet template buffers. TemplateBufferList template_buffers_; + static bool interrupted_; + uint64_t sent_packets_0_; uint64_t sent_packets_1_; };