Skip to content

Commit

Permalink
Add rcl_publisher_get_subscription_count related methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mchhoy committed Dec 13, 2023
1 parent 7b6c800 commit 9efa17d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions r2r/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
32 changes: 32 additions & 0 deletions r2r/src/publishers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 9efa17d

Please sign in to comment.