ros2_helpers/
communicate.rs

1//! A trait for simplifying cmmunication
2//!
3use safe_drive::{
4    msg::{ServiceMsg, TypeSupport},
5    node::Node,
6    topic::{publisher::Publisher, subscriber::Subscriber},
7};
8use std::sync::Arc;
9
10use crate::{
11    clientserver::{Client, Server},
12    common::{Attributes, Result},
13};
14
15/// Communicate traits to define all needed methods
16pub trait Communicate: Send + Sync {
17    /// Create a publisher
18    fn new_publisher<T: TypeSupport>(&self, attributes: &Attributes) -> Result<Publisher<T>>;
19
20    /// create a Subscriber
21    fn new_subscriber<T: TypeSupport>(&self, attributes: &Attributes) -> Result<Subscriber<T>>;
22    /// create an RPC Server
23    fn new_server<T: ServiceMsg>(&self, attributes: &Attributes) -> Result<Server<T>>;
24    /// create an RPC Client
25    fn new_client<T: ServiceMsg>(&self, attributes: &Attributes) -> Result<Client<T>>;
26}
27
28impl Communicate for Arc<Node> {
29    fn new_publisher<T: TypeSupport>(&self, attributes: &Attributes) -> Result<Publisher<T>> {
30        Ok(self.create_publisher::<T>(attributes.name, attributes.qos.clone())?)
31    }
32    fn new_subscriber<T: TypeSupport>(&self, attributes: &Attributes) -> Result<Subscriber<T>> {
33        Ok(self.create_subscriber(attributes.name, attributes.qos.clone())?)
34    }
35    fn new_server<T: ServiceMsg>(&self, attributes: &Attributes) -> Result<Server<T>> {
36        let server = self.create_server(attributes.name, attributes.qos.clone())?;
37        Ok(Server(Some(server)))
38    }
39    fn new_client<T: ServiceMsg>(&self, attributes: &Attributes) -> Result<Client<T>> {
40        let client = self.create_client(attributes.name, attributes.qos.clone())?;
41        Ok(Client(Some(client)))
42    }
43}