message_subscriber.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. use async_std::sync::Mutex;
  2. use async_trait::async_trait;
  3. use log::{debug, error, warn};
  4. use rand::Rng;
  5. use std::{any::Any, collections::HashMap, io, io::Cursor, sync::Arc};
  6. use crate::{
  7. net::message::Message,
  8. util::serial::{Decodable, Encodable},
  9. Error, Result,
  10. };
  11. /// 64bit identifier for message subscription.
  12. pub type MessageSubscriptionId = u64;
  13. type MessageResult<M> = Result<Arc<M>>;
  14. /// Handles message subscriptions through a subscription ID and a receiver
  15. /// channel.
  16. pub struct MessageSubscription<M: Message> {
  17. id: MessageSubscriptionId,
  18. recv_queue: async_channel::Receiver<MessageResult<M>>,
  19. parent: Arc<MessageDispatcher<M>>,
  20. }
  21. impl<M: Message> MessageSubscription<M> {
  22. /// Start receiving messages.
  23. pub async fn receive(&self) -> MessageResult<M> {
  24. match self.recv_queue.recv().await {
  25. Ok(message) => message,
  26. Err(err) => {
  27. panic!("MessageSubscription::receive() recv_queue failed! {}", err);
  28. }
  29. }
  30. }
  31. /// Unsubscribe from a message subscription. Must be called manually.
  32. pub async fn unsubscribe(&self) {
  33. self.parent.clone().unsubscribe(self.id).await
  34. }
  35. }
  36. #[async_trait]
  37. /// Generic interface for message dispatcher.
  38. trait MessageDispatcherInterface: Send + Sync {
  39. async fn trigger(&self, payload: Vec<u8>);
  40. async fn trigger_error(&self, err: Error);
  41. fn as_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync>;
  42. }
  43. /// Maintains a list of active subscribers and handles sending messages across
  44. /// subscriptions.
  45. struct MessageDispatcher<M: Message> {
  46. subs: Mutex<HashMap<MessageSubscriptionId, async_channel::Sender<MessageResult<M>>>>,
  47. }
  48. impl<M: Message> MessageDispatcher<M> {
  49. /// Create a new message dispatcher.
  50. fn new() -> Self {
  51. MessageDispatcher { subs: Mutex::new(HashMap::new()) }
  52. }
  53. /// Create a random ID.
  54. fn random_id() -> MessageSubscriptionId {
  55. let mut rng = rand::thread_rng();
  56. rng.gen()
  57. }
  58. /// Subscribe to a channel. Assigns a new ID and adds it to the list of
  59. /// subscribers.
  60. pub async fn subscribe(self: Arc<Self>) -> MessageSubscription<M> {
  61. let (sender, recvr) = async_channel::unbounded();
  62. let sub_id = Self::random_id();
  63. self.subs.lock().await.insert(sub_id, sender);
  64. MessageSubscription { id: sub_id, recv_queue: recvr, parent: self }
  65. }
  66. /// Unsubcribe from a channel. Removes the associated ID from the subscriber
  67. /// list.
  68. async fn unsubscribe(&self, sub_id: MessageSubscriptionId) {
  69. self.subs.lock().await.remove(&sub_id);
  70. }
  71. /// Send a message to all subscriber channels. Automatically clear inactive
  72. /// channels.
  73. async fn trigger_all(&self, message: MessageResult<M>) {
  74. debug!(
  75. target: "net",
  76. "MessageDispatcher<M={}>::trigger_all({}) [START, subs={}]",
  77. M::name(),
  78. if message.is_ok() { "msg" } else { "err" },
  79. self.subs.lock().await.len()
  80. );
  81. let mut garbage_ids = Vec::new();
  82. for (sub_id, sub) in &*self.subs.lock().await {
  83. match sub.send(message.clone()).await {
  84. Ok(()) => {}
  85. Err(_err) => {
  86. // Automatically clean out closed channels
  87. garbage_ids.push(*sub_id);
  88. // panic!("Error returned sending message in notify() call!
  89. // {}", err);
  90. }
  91. }
  92. }
  93. self.collect_garbage(garbage_ids).await;
  94. debug!(
  95. target: "net",
  96. "MessageDispatcher<M={}>::trigger_all({}) [END, subs={}]",
  97. M::name(),
  98. if message.is_ok() { "msg" } else { "err" },
  99. self.subs.lock().await.len()
  100. );
  101. }
  102. /// Remove inactive channels.
  103. async fn collect_garbage(&self, ids: Vec<MessageSubscriptionId>) {
  104. let mut subs = self.subs.lock().await;
  105. for id in &ids {
  106. subs.remove(id);
  107. }
  108. }
  109. }
  110. #[async_trait]
  111. // Local implementation of the Message Dispatcher Interface.
  112. impl<M: Message> MessageDispatcherInterface for MessageDispatcher<M> {
  113. /// Deserialize data into a message type.
  114. async fn trigger(&self, payload: Vec<u8>) {
  115. // deserialize data into type
  116. // send down the pipes
  117. let cursor = Cursor::new(payload);
  118. match M::decode(cursor) {
  119. Ok(message) => {
  120. let message = Ok(Arc::new(message));
  121. self.trigger_all(message).await
  122. }
  123. Err(err) => {
  124. error!("Unable to decode data. Dropping...: {}", err);
  125. }
  126. }
  127. }
  128. /// Sends a message to all subscriber channels. Clears any inactive
  129. /// channels.
  130. async fn trigger_error(&self, err: Error) {
  131. self.trigger_all(Err(err)).await;
  132. }
  133. /// Converts to Any trait. Enables the dynamic modification of static types.
  134. fn as_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
  135. self
  136. }
  137. }
  138. /// Publish/subscribe class that can dispatch any kind of message to a
  139. /// list of dispatchers.
  140. pub struct MessageSubsystem {
  141. dispatchers: Mutex<HashMap<&'static str, Arc<dyn MessageDispatcherInterface>>>,
  142. }
  143. impl MessageSubsystem {
  144. /// Create a new message subsystem.
  145. pub fn new() -> Self {
  146. MessageSubsystem { dispatchers: Mutex::new(HashMap::new()) }
  147. }
  148. /// Add a new message dispatcher.
  149. pub async fn add_dispatch<M: Message>(&self) {
  150. self.dispatchers.lock().await.insert(M::name(), Arc::new(MessageDispatcher::<M>::new()));
  151. }
  152. /// Add a dispatcher to the list of subscribers.
  153. pub async fn subscribe<M: Message>(&self) -> Result<MessageSubscription<M>> {
  154. let dispatcher = self.dispatchers.lock().await.get(M::name()).cloned();
  155. let sub = match dispatcher {
  156. Some(dispatcher) => {
  157. let dispatcher: Arc<MessageDispatcher<M>> = dispatcher
  158. .as_any()
  159. .downcast::<MessageDispatcher<M>>()
  160. .expect("Multiple messages registered with different names");
  161. dispatcher.subscribe().await
  162. }
  163. None => {
  164. // normall return failure here
  165. // for now panic
  166. return Err(Error::OperationFailed)
  167. }
  168. };
  169. Ok(sub)
  170. }
  171. /// Sends a message out to subscribers. Returns an error if the message
  172. /// doesn't send.
  173. pub async fn notify(&self, command: &str, payload: Vec<u8>) {
  174. let dispatcher = self.dispatchers.lock().await.get(command).cloned();
  175. match dispatcher {
  176. Some(dispatcher) => {
  177. dispatcher.trigger(payload).await;
  178. }
  179. None => {
  180. warn!(
  181. "MessageSubsystem::notify(\"{}\", payload) did not find a dispatcher",
  182. command
  183. );
  184. }
  185. }
  186. }
  187. /// Send a message to all subscriber channels. Clear any inactive channels.
  188. pub async fn trigger_error(&self, err: Error) {
  189. // TODO: this could be parallelized
  190. for dispatcher in self.dispatchers.lock().await.values() {
  191. dispatcher.trigger_error(err.clone()).await;
  192. }
  193. }
  194. }
  195. impl Default for MessageSubsystem {
  196. fn default() -> Self {
  197. Self::new()
  198. }
  199. }
  200. /// Test functions for message subsystem.
  201. // This is a test function for the message subsystem code above
  202. // Normall we would use the #[test] macro but cannot since it is async code
  203. // Instead we call it using smol::block_on() in the unit test code after this
  204. // func
  205. async fn _do_message_subscriber_test() {
  206. struct MyVersionMessage {
  207. x: u32,
  208. }
  209. impl Message for MyVersionMessage {
  210. fn name() -> &'static str {
  211. "verver"
  212. }
  213. }
  214. impl Encodable for MyVersionMessage {
  215. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  216. let mut len = 0;
  217. len += self.x.encode(&mut s)?;
  218. Ok(len)
  219. }
  220. }
  221. impl Decodable for MyVersionMessage {
  222. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  223. Ok(Self { x: Decodable::decode(&mut d)? })
  224. }
  225. }
  226. println!("hello");
  227. let subsystem = MessageSubsystem::new();
  228. subsystem.add_dispatch::<MyVersionMessage>().await;
  229. // subscribe
  230. // 1. get dispatcher
  231. // 2. cast to specific type
  232. // 3. do sub, return sub
  233. let sub = subsystem.subscribe::<MyVersionMessage>().await.unwrap();
  234. let msg = MyVersionMessage { x: 110 };
  235. let mut payload = Vec::new();
  236. msg.encode(&mut payload).unwrap();
  237. // receive message and publish
  238. // 1. based on string, lookup relevant dispatcher interface
  239. // 2. publish data there
  240. subsystem.notify("verver", payload).await;
  241. // receive
  242. // 1. do a get easy
  243. let msg2 = sub.receive().await.unwrap();
  244. assert_eq!(msg2.x, 110);
  245. println!("{}", msg2.x);
  246. subsystem.trigger_error(Error::ChannelStopped).await;
  247. let msg2 = sub.receive().await;
  248. assert!(msg2.is_err());
  249. sub.unsubscribe().await;
  250. }
  251. #[cfg(test)]
  252. mod tests {
  253. use super::*;
  254. #[test]
  255. fn test_message_subscriber() {
  256. smol::block_on(_do_message_subscriber_test());
  257. }
  258. }