Skip to content

Commit 86703a9

Browse files
Don't loop forever on registering a nonexistent service; also each service call is in its own thread.
1 parent 7279efa commit 86703a9

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

ros_tcp_endpoint/client.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,22 @@ def send_ros_service_request(self, srv_id, destination, data):
158158
# TODO: send a response to Unity anyway?
159159
return
160160
else:
161-
ros_communicator = self.tcp_server.source_destination_dict[
161+
ros_communicator = self.tcp_server.source_destination_dict[destination]
162+
service_thread = threading.Thread(target=self.service_call_thread, args=(srv_id, destination, data, ros_communicator))
163+
service_thread.daemon = True
164+
service_thread.start()
165+
166+
def service_call_thread(self, srv_id, destination, data, ros_communicator):
167+
response = ros_communicator.send(data)
168+
169+
if not response:
170+
error_msg = "No response data from service '{}'!".format(
162171
destination
163-
]
164-
response = ros_communicator.send(data)
165-
if not response:
166-
error_msg = "No response data from service '{}'!".format(
167-
destination
168-
)
169-
self.tcp_server.send_unity_error(error_msg)
170-
self.logerr(error_msg)
171-
# TODO: send a response to Unity anyway?
172-
return
172+
)
173+
self.tcp_server.send_unity_error(error_msg)
174+
self.logerr(error_msg)
175+
# TODO: send a response to Unity anyway?
176+
return
173177

174178
self.tcp_server.unity_tcp_sender.send_ros_service_response(srv_id, destination, response)
175179

ros_tcp_endpoint/service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ def __init__(self, service, service_class):
3434
node_name = f'{strippedService}_RosService'
3535
RosSender.__init__(self, node_name)
3636

37+
self.service_topic = service
3738
self.cli = self.create_client(service_class, service)
38-
while not self.cli.wait_for_service(timeout_sec=1.0):
39-
self.get_logger().info(f'{service} not available, waiting again...')
4039
self.req = service_class.Request()
4140

4241

@@ -55,6 +54,10 @@ def send(self, data):
5554
message_type = type(self.req)
5655
message = deserialize_message(data, message_type)
5756

57+
if not self.cli.service_is_ready():
58+
self.get_logger().error('Ignoring service call to {} - service is not ready.'.format(self.service_topic))
59+
return None
60+
5861
self.future = self.cli.call_async(message)
5962

6063
while rclpy.ok():

0 commit comments

Comments
 (0)