diff --git a/r2r/Cargo.toml b/r2r/Cargo.toml index 39baf8ba6..d72551d61 100644 --- a/r2r/Cargo.toml +++ b/r2r/Cargo.toml @@ -25,6 +25,7 @@ r2r_actions = { path = "../r2r_actions", version = "0.8.2" } r2r_macros = { path = "../r2r_macros", version = "0.8.2" } uuid = { version = "1.2.2", features = ["serde", "v4"] } futures = "0.3.25" +futures-timer = "3.0.2" log = "0.4.18" phf = "0.11.1" diff --git a/r2r/src/publishers.rs b/r2r/src/publishers.rs index 55668c49a..9874307f7 100644 --- a/r2r/src/publishers.rs +++ b/r2r/src/publishers.rs @@ -242,4 +242,36 @@ where Err(Error::from_rcl_error(result)) } } + + /// Gets the number of external subscribers (i.e. it doesn't + /// count subscribers from the same process). + pub fn get_inter_process_subscription_count(&self) -> usize { + // See https://github.com/ros2/rclcpp/issues/623 + + let mut inter_process_subscription_count = 0; + + unsafe { + rcl_publisher_get_subscription_count( + self.handle.as_ptr(), + &mut inter_process_subscription_count as *mut usize, + ) + }; + + inter_process_subscription_count + } + + /// Waits for at least one external subscriber to begin subscribing to the + /// topic. It doesn't count subscribers from the same process. + pub async fn wait_for_inter_process_subscribers(&self) { + // According to this there is no event available: + // + // https://github.com/ros2/rclcpp/issues/623 + loop { + if self.get_inter_process_subscription_count() > 0 { + break; + } + + futures_timer::Delay::new(std::time::Duration::from_millis(100)).await + } + } }