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

Be able to pass in closure to advertise_service #142

Merged
merged 6 commits into from
Oct 24, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion roslibrust/examples/service_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ roslibrust_codegen_macro::find_and_generate_ros_messages!("assets/ros1_common_in
// a canned response.
fn my_service(
request: std_srvs::SetBoolRequest,
my_string: &str,
) -> Result<std_srvs::SetBoolResponse, Box<dyn std::error::Error + Send + Sync>> {
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(),
Expand Down Expand Up @@ -48,10 +51,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Create a new client
let client = ClientHandle::new("ws://localhost:9090").await?;

// 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::<std_srvs::SetBool>("/my_set_bool", my_service)
.advertise_service::<std_srvs::SetBool, _>(
"/my_set_bool",
move |request: std_srvs::SetBoolRequest| -> Result<
std_srvs::SetBoolResponse,
Box<dyn std::error::Error + Send + Sync>,
> { my_service(request, my_string) },
)
.await?;

// Now try manually calling the service with the command line!
Expand Down
19 changes: 13 additions & 6 deletions roslibrust/src/rosbridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,21 @@ impl ClientHandle {
/// Service will be active until the handle is dropped!
///
/// See examples/service_server.rs for usage.
pub async fn advertise_service<T: RosServiceType>(
pub async fn advertise_service<T, F>(
&self,
topic: &str,
server: fn(
T::Request,
)
-> Result<T::Response, Box<dyn std::error::Error + 'static + Send + Sync>>,
) -> RosLibRustResult<ServiceHandle> {
server: F,
) -> RosLibRustResult<ServiceHandle>
where
T: RosServiceType,
F: Fn(
T::Request,
)
-> Result<T::Response, Box<dyn std::error::Error + 'static + Send + Sync>>
+ Send
+ Sync
+ 'static,
{
self.check_for_disconnect()?;
{
let client = self.inner.read().await;
Expand Down
2 changes: 1 addition & 1 deletion roslibrust/src/rosbridge/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ mod integration_tests {
let topic = "/self_service_call";

let handle = client
.advertise_service::<SetBool>(topic, cb)
.advertise_service::<SetBool, _>(topic, cb)
.await
.expect("Failed to advertise service");

Expand Down