ros_sigint_handler.h
Go to the documentation of this file.
1 /***
2  * Software License Agreement: BSD 3-Clause License
3  *
4  * Copyright (c) 2016-2018, qbrobotics®
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
8  * following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
11  * following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
14  * following disclaimer in the documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
17  * products derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef QB_DEVICE_SIGINT_HANDLER_H
29 #define QB_DEVICE_SIGINT_HANDLER_H
30 
31 // specific for custom SIGINT handler
32 #include <signal.h>
33 #include <ros/xmlrpc_manager.h>
34 
35 namespace ros_sigint_handler {
36 // signal-safe flag for whether shutdown is requested
37 static volatile sig_atomic_t g_request_shutdown = 0;
38 
39 // replacement SIGINT handler
40 static void mySigIntHandler(int _) {
41  (void)_;
42  g_request_shutdown = 1; // set flag
43 }
44 
45 // replacement "shutdown" XMLRPC callback
47  int num_params = 0;
48  if (params.getType() == XmlRpc::XmlRpcValue::TypeArray) {
49  num_params = params.size();
50  }
51  if (num_params > 1) {
52  std::string reason = params[1];
53  ROS_WARN("Shutdown request received. Reason: [%s]", reason.c_str());
54  g_request_shutdown = 1; // set flag
55  }
56 
57  result = ros::xmlrpc::responseInt(1, "", 0);
58 }
59 
61  return g_request_shutdown;
62 }
63 
65  // override SIGINT handler
66  signal(SIGINT, mySigIntHandler);
67  // override XMLRPC shutdown
68  ros::XMLRPCManager::instance()->unbind("shutdown");
70 }
71 } // namespace ros_sigint_handler
72 
73 #endif // QB_DEVICE_SIGINT_HANDLER_H
XmlRpc::XmlRpcValue responseInt(int code, const std::string &msg, int response)
int size() const
static volatile sig_atomic_t g_request_shutdown
Type const & getType() const
#define ROS_WARN(...)
static void shutdownCallback(XmlRpc::XmlRpcValue &params, XmlRpc::XmlRpcValue &result)
static const XMLRPCManagerPtr & instance()
static void mySigIntHandler(int _)


qb_device_utils
Author(s): qbrobotics®
autogenerated on Wed Oct 9 2019 03:45:39