ncomm_core/
update_client_server.rs

1//!
2//! NComm Update Server and Client.
3//!
4//! An Update Client and Server is pretty much the same as an action client in
5//! ROS in which a server is given some long running task that they routinely
6//! update the client on.
7//!
8
9#[cfg(feature = "alloc")]
10use alloc::vec::Vec;
11#[cfg(feature = "std")]
12use std::vec::Vec;
13
14/// A common abstraction for all NComm update clients
15pub trait UpdateClient {
16    /// The type of data used as a request by the client
17    type Request;
18    /// The type of data used as an update from the server
19    type Update;
20    /// The type of data used as a response from the server
21    type Response;
22    /// The type of error from sending or receiving data from the update
23    /// server
24    type Error;
25
26    /// Send a request to the server this client is associated with
27    #[allow(clippy::type_complexity)]
28    fn send_request(&mut self, request: Self::Request) -> Result<(), Self::Error>;
29
30    /// Poll for a singular update from the server
31    #[allow(clippy::type_complexity)]
32    fn poll_for_update(&mut self) -> Result<Option<(Self::Request, Self::Update)>, Self::Error>;
33
34    #[cfg(any(feature = "alloc", feature = "std"))]
35    /// Poll for updates from the server
36    #[allow(clippy::type_complexity)]
37    fn poll_for_updates(&mut self) -> Vec<Result<(Self::Request, Self::Update), Self::Error>>;
38
39    /// Poll for a singular response from the server
40    #[allow(clippy::type_complexity)]
41    fn poll_for_response(&mut self)
42        -> Result<Option<(Self::Request, Self::Response)>, Self::Error>;
43
44    #[cfg(any(feature = "alloc", feature = "std"))]
45    /// Poll for responses from the server
46    #[allow(clippy::type_complexity)]
47    fn poll_for_responses(&mut self) -> Vec<Result<(Self::Request, Self::Response), Self::Error>>;
48}
49
50/// A common abstraction for all NComm update servers
51pub trait UpdateServer {
52    /// The type of data received as a request from the client
53    type Request: Clone;
54    /// The type of data sent as an update to the client
55    type Update;
56    /// The type of data sent as a response to the client
57    type Response;
58    /// The unique identifier type for the various clients
59    type Key;
60    /// The type of error from sending or receiving data from and
61    /// to the update client
62    type Error;
63
64    /// Check for a singular incoming request from the client
65    #[allow(clippy::type_complexity)]
66    fn poll_for_request(&mut self) -> Result<Option<(Self::Key, Self::Request)>, Self::Error>;
67
68    #[cfg(any(feature = "alloc", feature = "std"))]
69    /// Check for incoming requests from the client
70    #[allow(clippy::type_complexity)]
71    fn poll_for_requests(&mut self) -> Vec<Result<(Self::Key, Self::Request), Self::Error>>;
72
73    /// Send an update to a specific client
74    fn send_update(
75        &mut self,
76        client_key: Self::Key,
77        request: &Self::Request,
78        update: Self::Update,
79    ) -> Result<(), Self::Error>;
80
81    #[cfg(any(feature = "alloc", feature = "std"))]
82    /// Send a collection of updates to specified client
83    #[allow(clippy::type_complexity)]
84    fn send_updates(
85        &mut self,
86        mut updates: Vec<(Self::Key, &Self::Request, Self::Update)>,
87    ) -> Vec<Result<(), Self::Error>> {
88        updates
89            .drain(..)
90            .map(|update| self.send_update(update.0, update.1, update.2))
91            .collect()
92    }
93
94    /// Send a response to a specific client
95    fn send_response(
96        &mut self,
97        client_key: Self::Key,
98        request: Self::Request,
99        response: Self::Response,
100    ) -> Result<(), Self::Error>;
101
102    #[cfg(any(feature = "alloc", feature = "std"))]
103    /// Send a collection of responses to specific clients
104    #[allow(clippy::type_complexity)]
105    fn send_responses(
106        &mut self,
107        mut responses: Vec<(Self::Key, Self::Request, Self::Response)>,
108    ) -> Vec<Result<(), Self::Error>> {
109        responses
110            .drain(..)
111            .map(|response| self.send_response(response.0, response.1, response.2))
112            .collect()
113    }
114}