Skip to content

Commit fbcb17a

Browse files
Carter12scartercarter
authored
Add service_is_ready() (#339)
* Add check if service is ready * Update changelog * Fix unneeded return --------- Co-authored-by: carter <[email protected]> Co-authored-by: carter <[email protected]>
1 parent ed10311 commit fbcb17a

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

examples/minimal_client_service/src/minimal_client.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ fn main() -> Result<(), Error> {
1313

1414
println!("Starting client");
1515

16-
std::thread::sleep(std::time::Duration::from_millis(500));
16+
while !client.service_is_ready()? {
17+
std::thread::sleep(std::time::Duration::from_millis(10));
18+
}
1719

1820
client.async_send_request_with_callback(
1921
&request,

examples/minimal_client_service/src/minimal_client_async.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ async fn main() -> Result<(), Error> {
1212

1313
println!("Starting client");
1414

15-
std::thread::sleep(std::time::Duration::from_millis(500));
15+
while !client.service_is_ready()? {
16+
std::thread::sleep(std::time::Duration::from_millis(10));
17+
}
1618

1719
let request = example_interfaces::srv::AddTwoInts_Request { a: 41, b: 1 };
1820

rclrs/CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog for package rclrs
33
^^^^^^^^^^^^^^^^^^^^^^^^^^^
44

5+
Unreleased
6+
----------------
7+
* Service clients now support service_is_ready to check if a service server is present ahead of calling (`#399 <https://github.com/ros2-rust/ros2_rust/pull/339>`_)
8+
59
0.3 (2022-07-22)
610
----------------
711
* Loaned messages (zero-copy) (`#212 <https://github.com/ros2-rust/ros2_rust/pull/212>`_)

rclrs/src/client.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,24 @@ where
238238
.ok()?;
239239
Ok((T::Response::from_rmw_message(response_out), request_id_out))
240240
}
241+
242+
/// Check if a service server is available.
243+
///
244+
/// Will return true if there is a service server available, false if unavailable.
245+
///
246+
pub fn service_is_ready(&self) -> Result<bool, RclrsError> {
247+
let mut is_ready = false;
248+
let client = &mut *self.handle.rcl_client_mtx.lock().unwrap();
249+
let node = &mut *self.handle.rcl_node_mtx.lock().unwrap();
250+
251+
unsafe {
252+
// SAFETY both node and client are guaranteed to be valid here
253+
// client is guaranteed to have been generated with node
254+
rcl_service_server_is_available(node as *const _, client as *const _, &mut is_ready)
255+
}
256+
.ok()?;
257+
Ok(is_ready)
258+
}
241259
}
242260

243261
impl<T> ClientBase for Client<T>

0 commit comments

Comments
 (0)