Browse Source

[1959] End the program gracefully if interrupted.

Marcin Siodelski 12 years ago
parent
commit
b753e8ebd1
2 changed files with 26 additions and 1 deletions
  1. 16 1
      tests/tools/perfdhcp/test_control.cc
  2. 10 0
      tests/tools/perfdhcp/test_control.h

+ 16 - 1
tests/tools/perfdhcp/test_control.cc

@@ -16,6 +16,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <unistd.h>
+#include <signal.h>
 
 #include <boost/date_time/posix_time/posix_time.hpp>
 
@@ -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());
@@ -385,6 +391,11 @@ TestControl::getSentPacketsNum(const ExchangeType xchg_type) const {
 }
 
 void
+TestControl::handleInterrupt(int) {
+    interrupted_ = true;
+}
+
+void
 TestControl::initPacketTemplates() {
     CommandOptions& options = CommandOptions::instance();
     std::vector<std::string> template_files = options.getTemplateFiles();
@@ -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) {

+ 10 - 0
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_;
 };