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 threading
35
36 from python_qt_binding import QtCore
37 import rospy
38 import node_manager_fkie as nm
39 try:
40
41 from multimaster_msgs_fkie.srv import ListDescription, ListNodes
42 except ImportError, e:
43 import sys
44 print >> sys.stderr, "Can't import services of default_cfg_fkie. Is default_cfg_fkie package compiled?"
45 raise ImportError(str(e))
46
48 '''
49 A class to retrieve the list of nodes from the default configuration service.
50 The received node list will be published by sending a QT signal. To retrieve
51 the configuration a new thread will be created.
52 '''
53 node_list_signal = QtCore.Signal(str, str, list)
54 '''
55 node_list_signal is a signal, which is emitted, if a new list with nodes is
56 retrieved. The signal has the URI of the service, the name of the service and
57 a list with node names as parameter.
58 '''
59 description_signal = QtCore.Signal(str, str, list)
60 '''
61 description_signal is a signal, which is emitted, if a new list with descriptions is
62 retrieved. The signal has the URI of the service, the name of the service and
63 a list with descriptions (L{default_cfg_fkie.Description}) parameter.
64 '''
65 err_signal = QtCore.Signal(str, str, str)
66
68 QtCore.QObject.__init__(self)
69 self.__serviceThreads = {}
70 self._lock = threading.RLock()
71
73 print " Shutdown default config update threads..."
74 for key, service in self.__serviceThreads.iteritems():
75 service.join(3)
76 print " Default config update threads are off!"
77
79 '''
80 This method starts a thread to get the informations about the default
81 configured nodes. If all informations are retrieved, a C{node_list_signal} of
82 this class will be emitted. If for given service a thread is
83 already running, the request will be ignored.
84 This method is thread safe.
85
86 @param service_uri: the URI of the service
87 @type service_uri: C{str}
88 @param service: the name of service to get the node list
89 @type service: C{str}
90 @param delay_exec: delayd the execution
91 @type delay_exec: C{float}
92 '''
93 with self._lock:
94 if not (self.__serviceThreads.has_key((service_uri, service))):
95 upthread = ServiceThread(service_uri, service, delay_exec)
96 upthread.update_signal.connect(self._on_node_list)
97 upthread.err_signal.connect(self._on_err)
98 self.__serviceThreads[(service_uri, service)] = upthread
99 upthread.start()
100
102 '''
103 This method starts a thread to get the descriptions from the default
104 configuration node. If all informations are retrieved, a C{description_signal} of
105 this class will be emitted. If for given service a thread is
106 already running, the request will be ignored.
107 This method is thread safe.
108
109 @param service_uri: the URI of the service
110 @type service_uri: C{str}
111 @param service: the name of service to get the description
112 @type service: C{str}
113 @param delay_exec: delayd the execution
114 @type delay_exec: C{float}
115 '''
116 with self._lock:
117 if not (self.__serviceThreads.has_key((service_uri, service))):
118 upthread = ServiceDescriptionThread(service_uri, service, delay_exec)
119 upthread.update_signal.connect(self._on_descr_list)
120 upthread.err_signal.connect(self._on_err)
121 self.__serviceThreads[(service_uri, service)] = upthread
122 upthread.start()
123
125 with self._lock:
126 try:
127 thread = self.__serviceThreads.pop((service_uri, service))
128 del thread
129 except KeyError:
130 pass
131 self.node_list_signal.emit(service_uri, service, nodes)
132
134 with self._lock:
135 try:
136 thread = self.__serviceThreads.pop((service_uri, service))
137 del thread
138 except KeyError:
139 pass
140 self.description_signal.emit(service_uri, service, items)
141
142 - def _on_err(self, service_uri, service, str):
143 with self._lock:
144 try:
145 thread = self.__serviceThreads.pop((service_uri, service))
146 del thread
147 except KeyError:
148 pass
149 self.err_signal.emit(service_uri, service, str)
150
151
152
154 '''
155 A thread to to retrieve the list of nodes from the default configuration
156 service and publish it by sending a QT signal.
157 '''
158 update_signal = QtCore.Signal(str, str, list)
159 err_signal = QtCore.Signal(str, str, str)
160
161 - def __init__(self, service_uri, service, delay_exec=0.0, parent=None):
162 QtCore.QObject.__init__(self)
163 threading.Thread.__init__(self)
164 self._service_uri = service_uri
165 self._service = service
166 self._delay_exec = delay_exec
167 self.setDaemon(True)
168
170 '''
171 '''
172 if self._service and self._service_uri:
173 try:
174 if self._delay_exec > 0:
175 time.sleep(self._delay_exec)
176 req, resp = nm.starter().callService(self._service_uri, self._service, ListNodes)
177 self.update_signal.emit(self._service_uri, self._service, resp.nodes)
178 except:
179 import traceback
180 lines = traceback.format_exc().splitlines()
181 rospy.logwarn("Error while retrieve the node list from %s[%s]: %s", str(self._service), str(self._service_uri), str(lines[-1]))
182 self.err_signal.emit(self._service_uri, self._service, lines[-1])
183
185 '''
186 A thread to to retrieve the list with descriptions from the default configuration
187 service and publish it by sending a QT signal.
188 '''
189 update_signal = QtCore.Signal(str, str, list)
190 err_signal = QtCore.Signal(str, str, str)
191
192 - def __init__(self, service_uri, service, delay_exec=0.0, parent=None):
193 QtCore.QObject.__init__(self)
194 threading.Thread.__init__(self)
195 self._service_uri = service_uri
196 self._service = service
197 self._delay_exec = delay_exec
198 self.setDaemon(True)
199
201 '''
202 '''
203 if self._service:
204 try:
205 if self._delay_exec > 0:
206 time.sleep(self._delay_exec)
207 req, resp = nm.starter().callService(self._service_uri, self._service, ListDescription)
208 self.update_signal.emit(self._service_uri, self._service, [resp])
209 except:
210 import traceback
211 lines = traceback.format_exc().splitlines()
212 rospy.logwarn("Error while retrieve the description from %s[%s]: %s", str(self._service), str(self._service_uri), str(lines[-1]))
213 self.err_signal.emit(self._service_uri, self._service, lines[-1])
214