00001 #! /usr/bin/env python 00002 # -*- coding: utf-8 -*- 00003 00004 # Software License Agreement (BSD) 00005 # 00006 # file @monitor.py 00007 # authors Mike Purvis <mpurvis@clearpathrobotics.com> 00008 # copyright Copyright (c) 2012, Clearpath Robotics, Inc., All rights reserved. 00009 # 00010 # Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00011 # the following conditions are met: 00012 # * Redistributions of source code must retain the above copyright notice, this list of conditions and the 00013 # following disclaimer. 00014 # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 00015 # following disclaimer in the documentation and/or other materials provided with the distribution. 00016 # * Neither the name of Clearpath Robotics nor the names of its contributors may be used to endorse or promote 00017 # products derived from this software without specific prior written permission. 00018 # 00019 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WAR- 00020 # RANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00021 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, IN- 00022 # DIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00023 # OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00024 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00025 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 00027 import rospy 00028 import threading 00029 00030 00031 class Monitor(threading.Thread): 00032 def __init__(self, ports): 00033 super(Monitor, self).__init__() 00034 self.ports = ports 00035 self.finish = threading.Event() 00036 00037 def run(self): 00038 while not self.finish: 00039 rospy.sleep(1.0) 00040 for name, port in ports.items(): 00041 if not port.is_alive(): 00042 rospy.logerr("Port %s thread died. Signalling node shutdown." % name) 00043 rospy.signal_shutdown("Node lost thread.")