pub struct Broker { /* private fields */ }Expand description
The Broker Interface
This is the primary interface for devices acting as a broker.
Currently the max capacity is fixed with a maximum of 8 clients connected. Each Client may subscribe up to 8 topics. Each Client may register up to 8 shortcodes.
In the future, these limits may be configurable.
As a note, the Broker currently creates a sizable object, due to the fixed upper limits
Implementations§
Source§impl Broker
impl Broker
Sourcepub fn register_client(&mut self, id: &Uuid) -> Result<(), ServerError>
pub fn register_client(&mut self, id: &Uuid) -> Result<(), ServerError>
Register a client to the broker
This can be done dynamically, e.g. when a client connects for the first time, e.g. a TCP session is established, or the first packet is received, or can be done ahead-of-time, e.g. when communicating with a fixed set of wired devices.
Clients must be registered before messages from them can be processed.
If an already-registered client is re-registered, they will be reset to an initial connection state, dropping all subscriptions or shortcodes.
Sourcepub fn remove_client(&mut self, id: &Uuid) -> Result<(), ServerError>
pub fn remove_client(&mut self, id: &Uuid) -> Result<(), ServerError>
Remove a client from the broker
This could be necessary if the connection to a client breaks or times out Once removed, no further messages to or from this client will be processed
Sourcepub fn reset_client(&mut self, id: &Uuid) -> Result<(), ServerError>
pub fn reset_client(&mut self, id: &Uuid) -> Result<(), ServerError>
Reset a client registered with the broker, without removing it
This could be necessary if the connection to a client breaks or times out.
Sourcepub fn process_msg<'a, 'b: 'a>(
&'b mut self,
req: &'a Request<'a>,
) -> Result<Vec<Response<'a>, U8>, ServerError>
pub fn process_msg<'a, 'b: 'a>( &'b mut self, req: &'a Request<'a>, ) -> Result<Vec<Response<'a>, U8>, ServerError>
Process a single message from a client
A message from a client will be processed. If processing this message generates responses that need to be sent (e.g. a publish occurs and subscribed clients should be notified, or if the broker is responding to a request from the client), they will be returned, and the messages should be sent to the appropriate clients.
Requests and Responses are addressed by the Uuid registered for each client
NOTE: If an error occurs, you probably should send a RESET_MESSAGE to
that client to force them to reconnect. You may also want to remove_client
or reset_client, depending on the situation. This will hopefully be handled
automatically in the future.