ncomm_core/
client_server.rs

1//!
2//! NComm Client Server Traits.
3//!
4//! Servers should be a single unique entity that can have multiple clients
5//! that request something from them (in the form of a request).
6//!
7
8#[cfg(feature = "alloc")]
9use alloc::vec::Vec;
10#[cfg(feature = "std")]
11use std::vec::Vec;
12
13/// A common abstraction for all NComm clients to allow for the creation
14/// of a common method of sending requests and receiving responses.
15pub trait Client {
16    /// The type of data used as a request by the client
17    type Request;
18    /// The type of data used as a response from the server
19    type Response;
20    /// The type of error from sending or receiving data from
21    /// the server
22    type Error;
23
24    /// Send a request to the server this client is associated with
25    fn send_request(&mut self, request: Self::Request) -> Result<(), Self::Error>;
26
27    /// Check for a singular response from the server containing the request and response
28    /// from the server
29    #[allow(clippy::type_complexity)]
30    fn poll_for_response(&mut self)
31        -> Result<Option<(Self::Request, Self::Response)>, Self::Error>;
32
33    #[cfg(any(feature = "alloc", feature = "std"))]
34    /// Check for a response from the server containing both the sent
35    /// request and the response from the server
36    #[allow(clippy::type_complexity)]
37    fn poll_for_responses(&mut self) -> Vec<Result<(Self::Request, Self::Response), Self::Error>>;
38}
39
40/// A common abstraction for all NComm servers that outlines the necessary
41/// base requirements for all NComm servers
42pub trait Server {
43    /// The type of data received as a request from the client
44    type Request;
45    /// The type of data sent as a response to the client
46    type Response;
47    /// The unique identifier type for the various clients
48    type Key;
49    /// The type of error from sending or receiving data from
50    /// the client
51    type Error;
52
53    /// Check for an incoming request from the client
54    #[allow(clippy::type_complexity)]
55    fn poll_for_request(&mut self) -> Result<Option<(Self::Key, Self::Request)>, Self::Error>;
56
57    #[cfg(any(feature = "alloc", feature = "std"))]
58    /// Check for incoming requests from the client
59    #[allow(clippy::type_complexity)]
60    fn poll_for_requests(&mut self) -> Vec<Result<(Self::Key, Self::Request), Self::Error>>;
61
62    /// Send a response to a specific client
63    fn send_response(
64        &mut self,
65        client_key: Self::Key,
66        request: Self::Request,
67        response: Self::Response,
68    ) -> Result<(), Self::Error>;
69
70    #[cfg(any(feature = "alloc", feature = "std"))]
71    /// Send a collection of responses to specified clients
72    fn send_responses(
73        &mut self,
74        mut responses: Vec<(Self::Key, Self::Request, Self::Response)>,
75    ) -> Vec<Result<(), Self::Error>> {
76        responses
77            .drain(..)
78            .map(|response| self.send_response(response.0, response.1, response.2))
79            .collect()
80    }
81}