From 25aa90eb23cce627accb43f3d41e075914383bc4 Mon Sep 17 00:00:00 2001 From: Kosuke Hata Date: Sun, 10 Sep 2023 09:17:18 -0700 Subject: [PATCH 1/5] wip --- roslibrust/examples/service_server.rs | 37 +++++++++++++++++++-------- roslibrust/src/rosbridge/client.rs | 18 ++++++++----- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/roslibrust/examples/service_server.rs b/roslibrust/examples/service_server.rs index 44eb90c..2b29c90 100644 --- a/roslibrust/examples/service_server.rs +++ b/roslibrust/examples/service_server.rs @@ -1,19 +1,22 @@ use roslibrust::ClientHandle; // One way to import message definitions: -roslibrust_codegen_macro::find_and_generate_ros_messages!("assets/ros1_common_interfaces"); +roslibrust_codegen_macro::find_and_generate_ros_messages!( + "assets/ros1_common_interfaces", + "/opt/ros/humble/share/std_srvs" +); // 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, +// ) -> Result> { +// log::info!("Got request to set bool: {request:?}"); +// 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 /// @@ -48,10 +51,22 @@ 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(), + }) + }; + // 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) + .advertise_service::("/my_set_bool", my_service) .await?; // Now try manually calling the service with the command line! diff --git a/roslibrust/src/rosbridge/client.rs b/roslibrust/src/rosbridge/client.rs index 55832d3..8cbd1e6 100644 --- a/roslibrust/src/rosbridge/client.rs +++ b/roslibrust/src/rosbridge/client.rs @@ -420,14 +420,20 @@ impl ClientHandle { /// Service will be active until the handle is dropped! /// /// See examples/service_server.rs for usage. - pub async fn advertise_service( + pub async fn advertise_service( &self, topic: &str, - server: fn( - T::Request, - ) - -> Result>, - ) -> RosLibRustResult { + server: F, + ) -> RosLibRustResult + where + T: RosServiceType, + F: Fn( + T::Request, + ) -> Result> + + Send + + Sync + + 'static, + { self.check_for_disconnect()?; { let client = self.inner.read().await; From 2c0c84d709d57203571385d578109dad757b7ea2 Mon Sep 17 00:00:00 2001 From: Kosuke July Hata Date: Sat, 21 Oct 2023 13:42:31 -0700 Subject: [PATCH 2/5] cleaning up --- roslibrust/examples/service_server.rs | 50 +++++++++++++-------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/roslibrust/examples/service_server.rs b/roslibrust/examples/service_server.rs index 2b29c90..93a190a 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! From 08c6d989a5644b096e7b28b720404414d904313b Mon Sep 17 00:00:00 2001 From: Kosuke July Hata Date: Mon, 23 Oct 2023 19:25:04 -0700 Subject: [PATCH 3/5] cargo fmt --- roslibrust/examples/service_server.rs | 13 ++++++------- roslibrust/src/rosbridge/client.rs | 3 ++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/roslibrust/examples/service_server.rs b/roslibrust/examples/service_server.rs index 93a190a..aa4b5ba 100644 --- a/roslibrust/examples/service_server.rs +++ b/roslibrust/examples/service_server.rs @@ -58,14 +58,13 @@ async fn main() -> Result<(), Box> { // 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", - move | request: std_srvs::SetBoolRequest | -> Result< - std_srvs::SetBoolResponse, + "/my_set_bool", + move |request: std_srvs::SetBoolRequest| -> Result< + std_srvs::SetBoolResponse, Box, - > { - my_service(request, my_string) - }, - ).await?; + > { my_service(request, my_string) }, + ) + .await?; // Now try manually calling the service with the command line! diff --git a/roslibrust/src/rosbridge/client.rs b/roslibrust/src/rosbridge/client.rs index 5a7eeb7..4e3d83c 100644 --- a/roslibrust/src/rosbridge/client.rs +++ b/roslibrust/src/rosbridge/client.rs @@ -429,7 +429,8 @@ impl ClientHandle { T: RosServiceType, F: Fn( T::Request, - ) -> Result> + ) + -> Result> + Send + Sync + 'static, From 909057271b005b211aa27303b50b11cbea3e05c2 Mon Sep 17 00:00:00 2001 From: Kosuke July Hata Date: Mon, 23 Oct 2023 19:28:06 -0700 Subject: [PATCH 4/5] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8653e2e..dcf82cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - The build.rs example in example_package now correctly informs cargo of filesystem dependencies +- The `advertise_serveice` method in `rosbridge/client.rs` now accepts closures ### Fixed From 113df2caa38e653241bfe32ccf4abdbd0c6aea45 Mon Sep 17 00:00:00 2001 From: carter Date: Mon, 23 Oct 2023 21:49:22 -0600 Subject: [PATCH 5/5] fix ros1 integration test --- roslibrust/src/rosbridge/integration_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roslibrust/src/rosbridge/integration_tests.rs b/roslibrust/src/rosbridge/integration_tests.rs index d4b5c54..cf40c5d 100644 --- a/roslibrust/src/rosbridge/integration_tests.rs +++ b/roslibrust/src/rosbridge/integration_tests.rs @@ -245,7 +245,7 @@ mod integration_tests { let topic = "/self_service_call"; let handle = client - .advertise_service::(topic, cb) + .advertise_service::(topic, cb) .await .expect("Failed to advertise service");