Skip to content

Commit

Permalink
allow service creation without handling the request header
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbone committed Mar 19, 2024
1 parent 99dab88 commit 4522c98
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
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
19 changes: 19 additions & 0 deletions rclrs/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,25 @@ impl Node {
/// [1]: crate::Service
// TODO: make service's lifetime depend on node's lifetime
pub fn create_service<T>(
&self,
topic: &str,
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,
Expand Down
8 changes: 5 additions & 3 deletions rclrs/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ type ServiceCallback<Request, Response> =

/// 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 Down

0 comments on commit 4522c98

Please sign in to comment.