Skip to content
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

QoS for Clients and Services #404

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion examples/minimal_client_service/src/minimal_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ fn main() -> Result<(), Error> {

let node = rclrs::create_node(&context, "minimal_client")?;

let client = node.create_client::<example_interfaces::srv::AddTwoInts>("add_two_ints")?;
let client = node.create_client::<example_interfaces::srv::AddTwoInts>(
"add_two_ints",
rclrs::QOS_PROFILE_SERVICES_DEFAULT,
)?;

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

Expand Down
5 changes: 4 additions & 1 deletion examples/minimal_client_service/src/minimal_client_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ async fn main() -> Result<(), Error> {

let node = rclrs::create_node(&context, "minimal_client")?;

let client = node.create_client::<example_interfaces::srv::AddTwoInts>("add_two_ints")?;
let client = node.create_client::<example_interfaces::srv::AddTwoInts>(
"add_two_ints",
rclrs::QOS_PROFILE_SERVICES_DEFAULT,
)?;

println!("Starting client");

Expand Down
7 changes: 5 additions & 2 deletions examples/minimal_client_service/src/minimal_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ fn main() -> Result<(), Error> {

let node = rclrs::create_node(&context, "minimal_service")?;

let _server = node
.create_service::<example_interfaces::srv::AddTwoInts, _>("add_two_ints", handle_service)?;
let _server = node.create_service::<example_interfaces::srv::AddTwoInts, _>(
"add_two_ints",
rclrs::QOS_PROFILE_SERVICES_DEFAULT,
handle_service,
)?;

println!("Starting server");
rclrs::spin(node).map_err(|err| err.into())
Expand Down
14 changes: 10 additions & 4 deletions rclrs/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rosidl_runtime_rs::Message;

use crate::{
error::{RclReturnCode, ToResult},
qos::QoSProfile,
rcl_bindings::*,
MessageCow, NodeHandle, RclrsError, ENTITY_LIFECYCLE_MUTEX,
};
Expand Down Expand Up @@ -83,7 +84,11 @@ where
T: rosidl_runtime_rs::Service,
{
/// Creates a new client.
pub(crate) fn new(node_handle: Arc<NodeHandle>, topic: &str) -> Result<Self, RclrsError>
pub(crate) fn new(
node_handle: Arc<NodeHandle>,
topic: &str,
qos: QoSProfile,
) -> Result<Self, RclrsError>
// This uses pub(crate) visibility to avoid instantiating this struct outside
// [`Node::create_client`], see the struct's documentation for the rationale
where
Expand All @@ -99,7 +104,8 @@ where
})?;

// SAFETY: No preconditions for this function.
let client_options = unsafe { rcl_client_get_default_options() };
let mut client_options = unsafe { rcl_client_get_default_options() };
client_options.qos = qos.into();

{
let rcl_node = node_handle.rcl_node.lock().unwrap();
Expand Down Expand Up @@ -310,7 +316,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::*;
use crate::{test_helpers::*, QOS_PROFILE_SERVICES_DEFAULT};
use test_msgs::srv;

#[test]
Expand All @@ -325,7 +331,7 @@ mod tests {
let graph = construct_test_graph(namespace)?;
let _node_2_empty_client = graph
.node2
.create_client::<srv::Empty>("graph_test_topic_4")?;
.create_client::<srv::Empty>("graph_test_topic_4", QOS_PROFILE_SERVICES_DEFAULT)?;

std::thread::sleep(std::time::Duration::from_millis(200));

Expand Down
10 changes: 8 additions & 2 deletions rclrs/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,15 @@ impl Node {
///
/// [1]: crate::Client
// TODO: make client's lifetime depend on node's lifetime
pub fn create_client<T>(&self, topic: &str) -> Result<Arc<Client<T>>, RclrsError>
pub fn create_client<T>(
&self,
topic: &str,
qos: QoSProfile,
) -> Result<Arc<Client<T>>, RclrsError>
where
T: rosidl_runtime_rs::Service,
{
let client = Arc::new(Client::<T>::new(Arc::clone(&self.handle), topic)?);
let client = Arc::new(Client::<T>::new(Arc::clone(&self.handle), topic, qos)?);
{ self.clients_mtx.lock().unwrap() }.push(Arc::downgrade(&client) as Weak<dyn ClientBase>);
Ok(client)
}
Expand Down Expand Up @@ -274,6 +278,7 @@ impl Node {
pub fn create_service<T, F>(
&self,
topic: &str,
qos: QoSProfile,
callback: F,
) -> Result<Arc<Service<T>>, RclrsError>
where
Expand All @@ -283,6 +288,7 @@ impl Node {
let service = Arc::new(Service::<T>::new(
Arc::clone(&self.handle),
topic,
qos,
callback,
)?);
{ self.services_mtx.lock().unwrap() }
Expand Down
43 changes: 32 additions & 11 deletions rclrs/src/parameter/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rosidl_runtime_rs::Sequence;
use super::ParameterMap;
use crate::{
parameter::{DeclaredValue, ParameterKind, ParameterStorage},
rmw_request_id_t, Node, RclrsError, Service,
rmw_request_id_t, Node, RclrsError, Service, QOS_PROFILE_SERVICES_DEFAULT,
};

// The variables only exist to keep a strong reference to the services and are technically unused.
Expand Down Expand Up @@ -248,6 +248,7 @@ impl ParameterService {
let map = parameter_map.clone();
let describe_parameters_service = node.create_service(
&(fqn.clone() + "/describe_parameters"),
QOS_PROFILE_SERVICES_DEFAULT,
move |_req_id: &rmw_request_id_t, req: DescribeParameters_Request| {
let map = map.lock().unwrap();
describe_parameters(req, &map)
Expand All @@ -256,6 +257,7 @@ impl ParameterService {
let map = parameter_map.clone();
let get_parameter_types_service = node.create_service(
&(fqn.clone() + "/get_parameter_types"),
QOS_PROFILE_SERVICES_DEFAULT,
move |_req_id: &rmw_request_id_t, req: GetParameterTypes_Request| {
let map = map.lock().unwrap();
get_parameter_types(req, &map)
Expand All @@ -264,6 +266,7 @@ impl ParameterService {
let map = parameter_map.clone();
let get_parameters_service = node.create_service(
&(fqn.clone() + "/get_parameters"),
QOS_PROFILE_SERVICES_DEFAULT,
move |_req_id: &rmw_request_id_t, req: GetParameters_Request| {
let map = map.lock().unwrap();
get_parameters(req, &map)
Expand All @@ -272,6 +275,7 @@ impl ParameterService {
let map = parameter_map.clone();
let list_parameters_service = node.create_service(
&(fqn.clone() + "/list_parameters"),
QOS_PROFILE_SERVICES_DEFAULT,
move |_req_id: &rmw_request_id_t, req: ListParameters_Request| {
let map = map.lock().unwrap();
list_parameters(req, &map)
Expand All @@ -280,13 +284,15 @@ impl ParameterService {
let map = parameter_map.clone();
let set_parameters_service = node.create_service(
&(fqn.clone() + "/set_parameters"),
QOS_PROFILE_SERVICES_DEFAULT,
move |_req_id: &rmw_request_id_t, req: SetParameters_Request| {
let mut map = map.lock().unwrap();
set_parameters(req, &mut map)
},
)?;
let set_parameters_atomically_service = node.create_service(
&(fqn.clone() + "/set_parameters_atomically"),
QOS_PROFILE_SERVICES_DEFAULT,
move |_req_id: &rmw_request_id_t, req: SetParametersAtomically_Request| {
let mut map = parameter_map.lock().unwrap();
set_parameters_atomically(req, &mut map)
Expand All @@ -313,7 +319,7 @@ mod tests {
srv::rmw::*,
},
Context, MandatoryParameter, Node, NodeBuilder, ParameterRange, ParameterValue, RclrsError,
ReadOnlyParameter,
ReadOnlyParameter, QOS_PROFILE_SERVICES_DEFAULT,
};
use rosidl_runtime_rs::{seq, Sequence};
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -431,7 +437,10 @@ mod tests {
async fn test_list_parameters_service() -> Result<(), RclrsError> {
let context = Context::new([]).unwrap();
let (node, client) = construct_test_nodes(&context, "list");
let list_client = client.create_client::<ListParameters>("/list/node/list_parameters")?;
let list_client = client.create_client::<ListParameters>(
"/list/node/list_parameters",
QOS_PROFILE_SERVICES_DEFAULT,
)?;

try_until_timeout(|| list_client.service_is_ready().unwrap())
.await
Expand Down Expand Up @@ -566,10 +575,18 @@ mod tests {
async fn test_get_set_parameters_service() -> Result<(), RclrsError> {
let context = Context::new([]).unwrap();
let (node, client) = construct_test_nodes(&context, "get_set");
let get_client = client.create_client::<GetParameters>("/get_set/node/get_parameters")?;
let set_client = client.create_client::<SetParameters>("/get_set/node/set_parameters")?;
let set_atomically_client = client
.create_client::<SetParametersAtomically>("/get_set/node/set_parameters_atomically")?;
let get_client = client.create_client::<GetParameters>(
"/get_set/node/get_parameters",
QOS_PROFILE_SERVICES_DEFAULT,
)?;
let set_client = client.create_client::<SetParameters>(
"/get_set/node/set_parameters",
QOS_PROFILE_SERVICES_DEFAULT,
)?;
let set_atomically_client = client.create_client::<SetParametersAtomically>(
"/get_set/node/set_parameters_atomically",
QOS_PROFILE_SERVICES_DEFAULT,
)?;

try_until_timeout(|| {
get_client.service_is_ready().unwrap()
Expand Down Expand Up @@ -799,10 +816,14 @@ mod tests {
async fn test_describe_get_types_parameters_service() -> Result<(), RclrsError> {
let context = Context::new([]).unwrap();
let (node, client) = construct_test_nodes(&context, "describe");
let describe_client =
client.create_client::<DescribeParameters>("/describe/node/describe_parameters")?;
let get_types_client =
client.create_client::<GetParameterTypes>("/describe/node/get_parameter_types")?;
let describe_client = client.create_client::<DescribeParameters>(
"/describe/node/describe_parameters",
QOS_PROFILE_SERVICES_DEFAULT,
)?;
let get_types_client = client.create_client::<GetParameterTypes>(
"/describe/node/get_parameter_types",
QOS_PROFILE_SERVICES_DEFAULT,
)?;

try_until_timeout(|| {
describe_client.service_is_ready().unwrap()
Expand Down
24 changes: 13 additions & 11 deletions rclrs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rosidl_runtime_rs::Message;

use crate::{
error::{RclReturnCode, ToResult},
qos::QoSProfile,
rcl_bindings::*,
MessageCow, NodeHandle, RclrsError, ENTITY_LIFECYCLE_MUTEX,
};
Expand Down Expand Up @@ -83,6 +84,7 @@ where
pub(crate) fn new<F>(
node_handle: Arc<NodeHandle>,
topic: &str,
qos: QoSProfile,
callback: F,
) -> Result<Self, RclrsError>
// This uses pub(crate) visibility to avoid instantiating this struct outside
Expand All @@ -101,7 +103,8 @@ where
})?;

// SAFETY: No preconditions for this function.
let service_options = unsafe { rcl_service_get_default_options() };
let mut service_options = unsafe { rcl_service_get_default_options() };
service_options.qos = qos.into();

{
let rcl_node = node_handle.rcl_node.lock().unwrap();
Expand Down Expand Up @@ -230,7 +233,7 @@ mod tests {

#[test]
fn test_services() -> Result<(), RclrsError> {
use crate::TopicNamesAndTypes;
use crate::{TopicNamesAndTypes, QOS_PROFILE_SERVICES_DEFAULT};
use test_msgs::srv;

let namespace = "/test_services_graph";
Expand All @@ -242,17 +245,16 @@ mod tests {
assert!(types.contains(&"test_msgs/srv/Empty".to_string()));
};

let _node_1_empty_service =
graph
.node1
.create_service::<srv::Empty, _>("graph_test_topic_4", |_, _| {
srv::Empty_Response {
structure_needs_at_least_one_member: 0,
}
})?;
let _node_1_empty_service = graph.node1.create_service::<srv::Empty, _>(
"graph_test_topic_4",
QOS_PROFILE_SERVICES_DEFAULT,
|_, _| srv::Empty_Response {
structure_needs_at_least_one_member: 0,
},
)?;
let _node_2_empty_client = graph
.node2
.create_client::<srv::Empty>("graph_test_topic_4")?;
.create_client::<srv::Empty>("graph_test_topic_4", QOS_PROFILE_SERVICES_DEFAULT)?;

std::thread::sleep(std::time::Duration::from_millis(100));

Expand Down
Loading