ros2_helpers/
communicate.rs1use 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
15pub trait Communicate: Send + Sync {
17 fn new_publisher<T: TypeSupport>(&self, attributes: &Attributes) -> Result<Publisher<T>>;
19
20 fn new_subscriber<T: TypeSupport>(&self, attributes: &Attributes) -> Result<Subscriber<T>>;
22 fn new_server<T: ServiceMsg>(&self, attributes: &Attributes) -> Result<Server<T>>;
24 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}