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

Improve service server ergonomics #373

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: 2 additions & 3 deletions examples/minimal_client_service/src/minimal_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::env;
use anyhow::{Error, Result};

fn handle_service(
_request_header: &rclrs::rmw_request_id_t,
request: example_interfaces::srv::AddTwoInts_Request,
) -> example_interfaces::srv::AddTwoInts_Response {
println!("request: {} + {}", request.a, request.b);
Expand All @@ -17,8 +16,8 @@ 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", handle_service)?;

println!("Starting server");
rclrs::spin(node).map_err(|err| err.into())
Expand Down
24 changes: 21 additions & 3 deletions rclrs/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,32 @@ impl Node {
///
/// [1]: crate::Service
// TODO: make service's lifetime depend on node's lifetime
pub fn create_service<T, F>(
pub fn create_service<T>(
&self,
topic: &str,
callback: F,
mut callback: impl FnMut(T::Request) -> T::Response + 'static + Send,
) -> Result<Arc<Service<T>>, RclrsError>
where
T: rosidl_runtime_rs::Service,
{
let callback =
move |_request_header: &rmw_request_id_t, request: T::Request| callback(request);
self.create_service_with_header(topic, callback)
}

/// Creates a [`Service`][1]. Same as [`create_service`][2] but the callback
/// also has access to the ID of the service request.
///
/// [1]: crate::Service
/// [2]: Self::create_service
// TODO: make service's lifetime depend on node's lifetime
pub fn create_service_with_header<T>(
&self,
topic: &str,
callback: impl FnMut(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,
) -> Result<Arc<Service<T>>, RclrsError>
where
T: rosidl_runtime_rs::Service,
F: Fn(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,
{
let service = Arc::new(Service::<T>::new(
Arc::clone(&self.rcl_node_mtx),
Expand Down
12 changes: 7 additions & 5 deletions rclrs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ pub trait ServiceBase: Send + Sync {
}

type ServiceCallback<Request, Response> =
Box<dyn Fn(&rmw_request_id_t, Request) -> Response + 'static + Send>;
Box<dyn FnMut(&rmw_request_id_t, Request) -> Response + 'static + Send>;

/// Main class responsible for responding to requests sent by ROS clients.
///
/// The only available way to instantiate services is via [`Node::create_service()`][1], this is to
/// ensure that [`Node`][2]s can track all the services that have been created.
/// The only available way to instantiate services is via [`Node::create_service()`][1] and
/// [`Node::create_service_with_header()`][2], this is to
/// ensure that [`Node`][3]s can track all the services that have been created.
///
/// [1]: crate::Node::create_service
/// [2]: crate::Node
/// [2]: crate::Node::create_service_with_header
/// [3]: crate::Node
pub struct Service<T>
where
T: rosidl_runtime_rs::Service,
Expand All @@ -79,7 +81,7 @@ where
// [`Node::create_service`], see the struct's documentation for the rationale
where
T: rosidl_runtime_rs::Service,
F: Fn(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,
F: FnMut(&rmw_request_id_t, T::Request) -> T::Response + 'static + Send,
{
// SAFETY: Getting a zero-initialized value is always safe.
let mut rcl_service = unsafe { rcl_get_zero_initialized_service() };
Expand Down
2 changes: 1 addition & 1 deletion rclrs_tests/src/graph_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn test_services() -> Result<(), RclrsError> {
let _node_1_empty_service =
graph
.node1
.create_service::<srv::Empty, _>("graph_test_topic_4", |_, _| srv::Empty_Response {
.create_service::<srv::Empty>("graph_test_topic_4", |_| srv::Empty_Response {
structure_needs_at_least_one_member: 0,
})?;
let _node_2_empty_client = graph
Expand Down
Loading