1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import time
34 import socket
35 import threading
36 import xmlrpclib
37 import random
38 from python_qt_binding import QtCore
39
40
41 import rospy
42
43 from master_discovery_fkie.master_info import MasterInfo
44
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 = QtCore.Signal(MasterInfo)
51 '''
52 @ivar: update_signal is a signal, which is emitted, if a new
53 L{aster_discovery_fkie.MasterInfo} is retrieved.
54 '''
55
56 error_signal = QtCore.Signal(str, str)
57 '''
58 @ivar: error_signal is a signal (masteruri, error message), which is emitted,
59 if an error while retrieving a master info was occurred.
60 '''
61
62 - def __init__(self, monitoruri, masteruri, delayed_exec=0., parent=None):
63 '''
64 @param masteruri: the URI of the remote ROS master
65 @type masteruri: C{str}
66 @param monitoruri: the URI of the monitor RPC interface of the master_discovery node
67 @type monitoruri: C{str}
68 @param delayed_exec: Delay the execution of the request for given seconds.
69 @type delayed_exec: C{float}
70 '''
71 QtCore.QObject.__init__(self)
72 threading.Thread.__init__(self)
73 self._monitoruri = monitoruri
74 self._masteruri = masteruri
75 self._delayed_exec = delayed_exec
76 self.setDaemon(True)
77
79 '''
80 '''
81 try:
82 delay = self._delayed_exec+0.5+random.random()
83
84 time.sleep(delay)
85
86 socket.setdefaulttimeout(25)
87 remote_monitor = xmlrpclib.ServerProxy(self._monitoruri)
88 remote_info = remote_monitor.masterInfo()
89 master_info = MasterInfo.from_list(remote_info)
90 master_info.check_ts = time.time()
91
92 self.update_signal.emit(master_info)
93 except:
94 import traceback
95
96 formatted_lines = traceback.format_exc().splitlines()
97 rospy.logwarn("Connection to %s failed:\n\t%s", str(self._monitoruri), formatted_lines[-1])
98
99 self.error_signal.emit(self._masteruri, formatted_lines[-1])
100 finally:
101 if not socket is None:
102 socket.setdefaulttimeout(None)
103