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  from node_manager_fkie.common import utf8 
 44   
 45   
46 -class UpdateThread(QObject, threading.Thread):
47 ''' 48 A thread to retrieve the state about ROS master from remote discovery node and 49 publish it be sending a QT signal. 50 ''' 51 update_signal = Signal(MasterInfo) 52 ''' 53 @ivar: update_signal is a signal, which is emitted, if a new 54 U{master_discovery_fkie.MasterInfo<http://docs.ros.org/api/master_discovery_fkie/html/modules.html#module-master_discovery_fkie.master_info>} is retrieved. 55 ''' 56 57 master_errors_signal = Signal(str, list) 58 ''' 59 @ivar: master_errors_signal is a signal (masteruri, list of errors), 60 which is emitted, if we get a list with errors from remote master_discovery. 61 ''' 62 63 error_signal = Signal(str, str) 64 ''' 65 @ivar: error_signal is a signal (masteruri, error message), which is emitted, 66 if an error while retrieving a master info was occurred. 67 ''' 68 69 timediff_signal = Signal(str, float) 70 ''' 71 @ivar: timediff_signal is a signal (masteruri, time difference), which is emitted 72 after the difference of time to the remote host is determined. 73 ''' 74
75 - def __init__(self, monitoruri, masteruri, delayed_exec=0., parent=None):
76 ''' 77 @param masteruri: the URI of the remote ROS master 78 @type masteruri: C{str} 79 @param monitoruri: the URI of the monitor RPC interface of the master_discovery node 80 @type monitoruri: C{str} 81 @param delayed_exec: Delay the execution of the request for given seconds. 82 @type delayed_exec: C{float} 83 ''' 84 QObject.__init__(self) 85 threading.Thread.__init__(self) 86 self._monitoruri = monitoruri 87 self._masteruri = masteruri 88 self._delayed_exec = delayed_exec 89 self.setDaemon(True)
90
91 - def run(self):
92 ''' 93 ''' 94 try: 95 delay = self._delayed_exec + 0.5 + random.random() 96 # 'print "wait request update", self._monitoruri, delay 97 time.sleep(delay) 98 # 'print "request update", self._monitoruri 99 socket.setdefaulttimeout(25) 100 remote_monitor = xmlrpclib.ServerProxy(self._monitoruri) 101 # get first master errors 102 try: 103 muri, errors = remote_monitor.masterErrors() 104 self.master_errors_signal.emit(muri, errors) 105 except xmlrpclib.Fault as _err: 106 rospy.logwarn("Older master_discovery on %s detected. It does not support master error reports!" % self._masteruri) 107 # get the time difference 108 try: 109 myts = time.time() 110 muri, remote_ts = remote_monitor.getCurrentTime() 111 self.timediff_signal.emit(muri, remote_ts - myts - (time.time() - myts) / 2.0) 112 except xmlrpclib.Fault as _errts: 113 rospy.logwarn("Older master_discovery on %s detected. It does not support master error reports!" % self._masteruri) 114 # now get master info from master discovery 115 remote_info = remote_monitor.masterInfo() 116 master_info = MasterInfo.from_list(remote_info) 117 master_info.check_ts = time.time() 118 # 'print "request success", self._monitoruri 119 self.update_signal.emit(master_info) 120 except: 121 import traceback 122 # print traceback.print_exc() 123 formatted_lines = traceback.format_exc(1).splitlines() 124 rospy.logwarn("Cannot update ROS state, connection to %s failed:\n\t%s", utf8(self._monitoruri), formatted_lines[-1]) 125 # 'print "request failed", self._monitoruri 126 self.error_signal.emit(self._masteruri, formatted_lines[-1]) 127 finally: 128 if socket is not None: 129 socket.setdefaulttimeout(None)
130