diff --git a/roslibrust/examples/service_server.rs b/roslibrust/examples/service_server.rs index 2b29c905..93a190ab 100644 --- a/roslibrust/examples/service_server.rs +++ b/roslibrust/examples/service_server.rs @@ -1,22 +1,22 @@ use roslibrust::ClientHandle; // One way to import message definitions: -roslibrust_codegen_macro::find_and_generate_ros_messages!( - "assets/ros1_common_interfaces", - "/opt/ros/humble/share/std_srvs" -); +roslibrust_codegen_macro::find_and_generate_ros_messages!("assets/ros1_common_interfaces"); // A basic service server exampple, that logs the request is recieves and returns // a canned response. -// fn my_service( -// request: std_srvs::SetBoolRequest, -// ) -> Result> { -// log::info!("Got request to set bool: {request:?}"); -// Ok(std_srvs::SetBoolResponse { -// success: true, -// message: "You set my bool!".to_string(), -// }) -// } +fn my_service( + request: std_srvs::SetBoolRequest, + my_string: &str, +) -> Result> { + log::info!("Got request to set bool: {request:?}"); + log::info!("Using my string: {}", my_string); // Use the string here + + Ok(std_srvs::SetBoolResponse { + success: true, + message: "You set my bool!".to_string(), + }) +} /// This examples shows hosting a service server and calling it to confirm it is working /// @@ -51,23 +51,21 @@ async fn main() -> Result<(), Box> { // Create a new client let client = ClientHandle::new("ws://localhost:9090").await?; - let my_string = "Some string".to_string(); // The string you want to pass in - - let my_service = move |request: std_srvs::SetBoolRequest| -> Result> { - log::info!("Got request to set bool: {:?}", request); - log::info!("Using my string: {}", my_string); // Use the string here - - Ok(std_srvs::SetBoolResponse { - success: true, - message: "You set my bool!".to_string(), - }) - }; + // The string you want to pass in to the closure + let my_string = "Some string"; // Actually advertise our service // The handle returned here establishes the lifetime of our service and dropping it will unadvertise the service let _handle = client - .advertise_service::("/my_set_bool", my_service) - .await?; + .advertise_service::( + "/my_set_bool", + move | request: std_srvs::SetBoolRequest | -> Result< + std_srvs::SetBoolResponse, + Box, + > { + my_service(request, my_string) + }, + ).await?; // Now try manually calling the service with the command line!