Package node_manager_fkie :: Module update_thread
[frames] | no frames]

Source Code for Module node_manager_fkie.update_thread

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Fraunhofer nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  from python_qt_binding.QtCore import QObject, Signal 
 34  import random 
 35  import socket 
 36  import threading 
 37  import time 
 38  import xmlrpclib 
 39   
 40  import rospy 
 41   
 42  from master_discovery_fkie.master_info import MasterInfo 
 43   
 44   
45 -class UpdateThread(QObject, threading.Thread):
46 ''' 47 A thread to retrieve the state about ROS master from remote discovery node and 48 publish it be sending a QT signal. 49 ''' 50 update_signal = Signal(MasterInfo) 51 ''' 52 @ivar: update_signal is a signal, which is emitted, if a new 53 U{master_discovery_fkie.MasterInfo<http://docs.ros.org/api/master_discovery_fkie/html/modules.html#module-master_discovery_fkie.master_info>} is retrieved. 54 ''' 55 56 master_errors_signal = Signal(str, list) 57 ''' 58 @ivar: master_errors_signal is a signal (masteruri, list of errors), 59 which is emitted, if we get a list with errors from remote master_discovery. 60 ''' 61 62 error_signal = Signal(str, str) 63 ''' 64 @ivar: error_signal is a signal (masteruri, error message), which is emitted, 65 if an error while retrieving a master info was occurred. 66 ''' 67 68 timediff_signal = Signal(str, float) 69 ''' 70 @ivar: timediff_signal is a signal (masteruri, time difference), which is emitted 71 after the difference of time to the remote host is determined. 72 ''' 73
74 - def __init__(self, monitoruri, masteruri, delayed_exec=0., parent=None):
75 ''' 76 @param masteruri: the URI of the remote ROS master 77 @type masteruri: C{str} 78 @param monitoruri: the URI of the monitor RPC interface of the master_discovery node 79 @type monitoruri: C{str} 80 @param delayed_exec: Delay the execution of the request for given seconds. 81 @type delayed_exec: C{float} 82 ''' 83 QObject.__init__(self) 84 threading.Thread.__init__(self) 85 self._monitoruri = monitoruri 86 self._masteruri = masteruri 87 self._delayed_exec = delayed_exec 88 self.setDaemon(True)
89
90 - def run(self):
91 ''' 92 ''' 93 try: 94 delay = self._delayed_exec + 0.5 + random.random() 95 # 'print "wait request update", self._monitoruri, delay 96 time.sleep(delay) 97 # 'print "request update", self._monitoruri 98 socket.setdefaulttimeout(25) 99 remote_monitor = xmlrpclib.ServerProxy(self._monitoruri) 100 # get first master errors 101 try: 102 muri, errors = remote_monitor.masterErrors() 103 self.master_errors_signal.emit(muri, errors) 104 except xmlrpclib.Fault as _err: 105 rospy.logwarn("Older master_discovery on %s detected. It does not support master error reports!" % self._masteruri) 106 # get the time difference 107 try: 108 myts = time.time() 109 muri, remote_ts = remote_monitor.getCurrentTime() 110 self.timediff_signal.emit(muri, remote_ts - myts - (time.time() - myts) / 2.0) 111 except xmlrpclib.Fault as _errts: 112 rospy.logwarn("Older master_discovery on %s detected. It does not support master error reports!" % self._masteruri) 113 # now get master info from master discovery 114 remote_info = remote_monitor.masterInfo() 115 master_info = MasterInfo.from_list(remote_info) 116 master_info.check_ts = time.time() 117 # 'print "request success", self._monitoruri 118 self.update_signal.emit(master_info) 119 except: 120 import traceback 121 # print traceback.print_exc() 122 formatted_lines = traceback.format_exc(1).splitlines() 123 rospy.logwarn("Cannot update ROS state, connection to %s failed:\n\t%s", str(self._monitoruri), formatted_lines[-1]) 124 # 'print "request failed", self._monitoruri 125 self.error_signal.emit(self._masteruri, formatted_lines[-1]) 126 finally: 127 if socket is not None: 128 socket.setdefaulttimeout(None)
129