1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
use super::super::rosxmlrpc::{self, Response as Result}; use serde::{Deserialize, Serialize}; use xml_rpc; pub struct Master { client: rosxmlrpc::Client, client_id: String, caller_api: String, } macro_rules! request { ($s:expr; $name:ident; $($item:expr),*)=> ({ $s.client.request(stringify!($name),&(&$s.client_id, $( $item, )* )) }) } macro_rules! request_tree { ($s:expr; $name:ident; $($item:expr),*)=> ({ $s.client.request_tree(stringify!($name),&(&$s.client_id, $( $item, )* )) }) } impl Master { pub fn new( master_uri: &str, client_id: &str, caller_api: &str, ) -> rosxmlrpc::error::Result<Master> { Ok(Master { client: rosxmlrpc::Client::new(master_uri)?, client_id: client_id.to_owned(), caller_api: caller_api.to_owned(), }) } pub fn register_service(&self, service: &str, service_api: &str) -> Result<i32> { request!(self; registerService; service, service_api, &self.caller_api) } pub fn unregister_service(&self, service: &str, service_api: &str) -> Result<i32> { request!(self; unregisterService; service, service_api) } pub fn register_subscriber(&self, topic: &str, topic_type: &str) -> Result<Vec<String>> { request!(self; registerSubscriber; topic, topic_type, &self.caller_api) } pub fn unregister_subscriber(&self, topic: &str) -> Result<i32> { request!(self; unregisterSubscriber; topic, &self.caller_api) } pub fn register_publisher(&self, topic: &str, topic_type: &str) -> Result<Vec<String>> { request!(self; registerPublisher; topic, topic_type, &self.caller_api) } pub fn unregister_publisher(&self, topic: &str) -> Result<i32> { request!(self; unregisterPublisher; topic, &self.caller_api) } #[allow(dead_code)] pub fn lookup_node(&self, node_name: &str) -> Result<String> { request!(self; lookupNode; node_name) } #[allow(dead_code)] pub fn get_published_topics(&self, subgraph: &str) -> Result<Vec<(String, String)>> { request!(self; getPublishedTopics; subgraph) } pub fn get_topic_types(&self) -> Result<Vec<TopicTuple>> { request!(self; getTopicTypes;) } pub fn get_system_state(&self) -> Result<SystemStateTuple> { request!(self; getSystemState;) } #[allow(dead_code)] pub fn get_uri(&self) -> Result<String> { request!(self; getUri;) } pub fn lookup_service(&self, service: &str) -> Result<String> { request!(self; lookupService; service) } pub fn delete_param(&self, key: &str) -> Result<i32> { request!(self; deleteParam; key) } pub fn set_param<T: Serialize>(&self, key: &str, value: &T) -> Result<i32> { request!(self; setParam; key, value) } pub fn set_param_any(&self, key: &str, value: xml_rpc::Value) -> Result<()> { self.client .request_tree_with_tree( "setParam", vec![ xml_rpc::Value::String(self.client_id.clone()), xml_rpc::Value::String(key.into()), value, ], ) .and(Ok(())) } pub fn get_param<'a, T: Deserialize<'a>>(&self, key: &str) -> Result<T> { request!(self; getParam; key) } pub fn get_param_any(&self, key: &str) -> Result<xml_rpc::Value> { request_tree!(self; getParam; key) } pub fn search_param(&self, key: &str) -> Result<String> { request!(self; searchParam; key) } #[allow(dead_code)] pub fn subscribe_param<'a, T: Deserialize<'a>>(&self, key: &str) -> Result<T> { request!(self; subscribeParam; &self.caller_api, key) } #[allow(dead_code)] pub fn subscribe_param_any(&self, key: &str) -> Result<xml_rpc::Value> { request_tree!(self; subscribeParam; &self.caller_api, key) } #[allow(dead_code)] pub fn unsubscribe_param(&self, key: &str) -> Result<i32> { request!(self; unsubscribeParam; &self.caller_api, key) } pub fn has_param(&self, key: &str) -> Result<bool> { request!(self; hasParam; key) } pub fn get_param_names(&self) -> Result<Vec<String>> { request!(self; getParamNames;) } } #[derive(Debug)] pub struct TopicData { pub name: String, pub connections: Vec<String>, } #[derive(Debug)] pub struct SystemState { pub publishers: Vec<TopicData>, pub subscribers: Vec<TopicData>, pub services: Vec<TopicData>, } #[derive(Debug, serde_derive::Deserialize)] pub struct TopicDataTuple(String, Vec<String>); #[derive(Debug, serde_derive::Deserialize)] pub struct SystemStateTuple( Vec<TopicDataTuple>, Vec<TopicDataTuple>, Vec<TopicDataTuple>, ); impl Into<SystemState> for SystemStateTuple { fn into(self) -> SystemState { SystemState { publishers: self.0.into_iter().map(Into::into).collect(), subscribers: self.1.into_iter().map(Into::into).collect(), services: self.2.into_iter().map(Into::into).collect(), } } } impl Into<TopicData> for TopicDataTuple { fn into(self) -> TopicData { TopicData { name: self.0, connections: self.1, } } } #[derive(Debug, serde_derive::Deserialize)] pub struct TopicTuple(String, String); impl Into<Topic> for TopicTuple { fn into(self) -> Topic { Topic { name: self.0, datatype: self.1, } } } pub struct Topic { pub name: String, pub datatype: String, }