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

Add Publisher::get_subscription_count #457

Merged
Merged
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
37 changes: 37 additions & 0 deletions rclrs/src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ where
}
}

/// Returns the number of subscriptions of the publisher.
pub fn get_subscription_count(&self) -> Result<usize, RclrsError> {
let mut subscription_count = 0;
// SAFETY: No preconditions for the function called.
unsafe {
rcl_publisher_get_subscription_count(
&*self.handle.rcl_publisher.lock().unwrap(),
&mut subscription_count,
)
.ok()?
};
Ok(subscription_count)
}

/// Publishes a message.
///
/// The [`MessageCow`] trait is implemented by any
Expand Down Expand Up @@ -327,6 +341,29 @@ mod tests {
expected_publishers_info
);

// Test get_subscription_count()
assert_eq!(node_1_empty_publisher.get_subscription_count(), Ok(0));
assert_eq!(node_1_basic_types_publisher.get_subscription_count(), Ok(0));
assert_eq!(node_2_default_publisher.get_subscription_count(), Ok(0));
let _node_1_empty_subscriber = graph.node1.create_subscription(
"graph_test_topic_1",
QOS_PROFILE_SYSTEM_DEFAULT,
|_msg: msg::Empty| {},
);
let _node_1_basic_types_subscriber = graph.node1.create_subscription(
"graph_test_topic_2",
QOS_PROFILE_SYSTEM_DEFAULT,
|_msg: msg::BasicTypes| {},
);
let _node_2_default_subscriber = graph.node2.create_subscription(
"graph_test_topic_3",
QOS_PROFILE_SYSTEM_DEFAULT,
|_msg: msg::Defaults| {},
);
assert_eq!(node_1_empty_publisher.get_subscription_count(), Ok(1));
assert_eq!(node_1_basic_types_publisher.get_subscription_count(), Ok(1));
assert_eq!(node_2_default_publisher.get_subscription_count(), Ok(1));

Ok(())
}
}