Skip to content

Add WaitSet::new_for_node() #276

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

Merged
merged 1 commit into from
Oct 5, 2022
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
34 changes: 1 addition & 33 deletions rclrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,7 @@ pub use wait::*;
///
/// [1]: crate::RclReturnCode
pub fn spin_once(node: &Node, timeout: Option<Duration>) -> Result<(), RclrsError> {
let live_subscriptions = node.live_subscriptions();
let live_clients = node.live_clients();
let live_guard_conditions = node.live_guard_conditions();
let live_services = node.live_services();
let ctx = Context {
rcl_context_mtx: node.rcl_context_mtx.clone(),
};
let mut wait_set = WaitSet::new(
live_subscriptions.len(),
live_guard_conditions.len(),
0,
live_clients.len(),
live_services.len(),
0,
&ctx,
)?;

for live_subscription in &live_subscriptions {
wait_set.add_subscription(live_subscription.clone())?;
}

for live_client in &live_clients {
wait_set.add_client(live_client.clone())?;
}

for live_guard_condition in &live_guard_conditions {
wait_set.add_guard_condition(live_guard_condition.clone())?;
}

for live_service in &live_services {
wait_set.add_service(live_service.clone())?;
}

let mut wait_set = WaitSet::new_for_node(node)?;
let ready_entities = wait_set.wait(timeout)?;

for ready_subscription in ready_entities.subscriptions {
Expand Down
41 changes: 40 additions & 1 deletion rclrs/src/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::vec::Vec;

use crate::error::{to_rclrs_result, RclReturnCode, RclrsError, ToResult};
use crate::rcl_bindings::*;
use crate::{ClientBase, Context, ServiceBase, SubscriptionBase};
use crate::{ClientBase, Context, Node, ServiceBase, SubscriptionBase};

mod exclusivity_guard;
mod guard_condition;
Expand Down Expand Up @@ -117,6 +117,45 @@ impl WaitSet {
})
}

/// Creates a new wait set and adds all waitable entities in the node to it.
///
/// The wait set is sized to fit the node exactly, so there is no capacity for adding other entities.
pub fn new_for_node(node: &Node) -> Result<Self, RclrsError> {
let live_subscriptions = node.live_subscriptions();
let live_clients = node.live_clients();
let live_guard_conditions = node.live_guard_conditions();
let live_services = node.live_services();
let ctx = Context {
rcl_context_mtx: node.rcl_context_mtx.clone(),
};
let mut wait_set = WaitSet::new(
live_subscriptions.len(),
live_guard_conditions.len(),
0,
live_clients.len(),
live_services.len(),
0,
&ctx,
)?;

for live_subscription in &live_subscriptions {
wait_set.add_subscription(live_subscription.clone())?;
}

for live_client in &live_clients {
wait_set.add_client(live_client.clone())?;
}

for live_guard_condition in &live_guard_conditions {
wait_set.add_guard_condition(live_guard_condition.clone())?;
}

for live_service in &live_services {
wait_set.add_service(live_service.clone())?;
}
Ok(wait_set)
}

/// Removes all entities from the wait set.
///
/// This effectively resets the wait set to the state it was in after being created by
Expand Down