Skip to content

Wait for service #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,7 @@ Cargo.lock
# Ignore generated .cargo/config.toml files
**/.cargo/config.toml

# Ignore .vscode
.vscode

# End of https://www.toptal.com/developers/gitignore/api/cmake,rust,python,cpp,c
2 changes: 1 addition & 1 deletion examples/minimal_client_service/src/minimal_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() -> Result<(), Error> {

println!("Starting client");

std::thread::sleep(std::time::Duration::from_millis(500));
client.wait_for_service(500)?;

client.async_send_request_with_callback(
&request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn main() -> Result<(), Error> {

println!("Starting client");

std::thread::sleep(std::time::Duration::from_millis(500));
client.wait_for_service(500)?;

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

Expand Down
25 changes: 25 additions & 0 deletions rclrs/src/node/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::boxed::Box;
use std::collections::HashMap;
use std::ffi::CString;
use std::sync::Arc;
use std::{thread, time::Duration};

use crate::error::{RclReturnCode, ToResult};
use crate::MessageCow;
Expand Down Expand Up @@ -240,6 +241,30 @@ where
.ok()?;
Ok((T::Response::from_rmw_message(response_out), request_id_out))
}

pub fn service_is_ready(&self) -> Result<bool, RclrsError> {
let mut is_ready = false;
let client = &mut *self.handle.rcl_client_mtx.lock();
let node = &mut *self.handle.rcl_node_mtx.lock();

unsafe {
rcl_service_server_is_available(node as *const _, client as *const _, &mut is_ready)
}
.ok()?;
return Ok(is_ready);
}

pub fn wait_for_service(&self, timeout_in_ms: u64) -> Result<bool, RclrsError> {
let mut total_time = timeout_in_ms;
let interval = 250;
let mut is_ready = false;
while !is_ready && total_time > 0 {
is_ready = self.service_is_ready()?;
thread::sleep(Duration::from_millis(interval));
total_time -= interval;
}
Ok(is_ready)
}
}

impl<T> ClientBase for Client<T>
Expand Down
3 changes: 2 additions & 1 deletion rclrs/src/rcl_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <rcl/rcl.h>
#include <rcl_yaml_param_parser/parser.h>
#include <rcutils/error_handling.h>
#include <rcutils/error_handling.h>
#include <rcl/graph.h>