pub struct Controller { /* private fields */ }Expand description
A controller for interacting with tosca devices.
The main functionalities include:
- Discovering
toscadevices on the network and registering them in memory. - Sending requests to a specific device identified by its ID, awaiting a response, and forwarding it directly to the caller.
- Controlling request sending by allowing or blocking requests based on the defined privacy policy.
Implementations§
Source§impl Controller
impl Controller
Sourcepub fn new(discovery: Discovery) -> Self
pub fn new(discovery: Discovery) -> Self
Creates a Controller from a Discovery configuration.
Sourcepub fn from_devices(discovery: Discovery, devices: Devices) -> Self
pub fn from_devices(discovery: Discovery, devices: Devices) -> Self
Creates a Controller from a Discovery configuration and
an initial set of Devices.
This method is useful when Devices are retrieved from database.
Sourcepub fn policy(self, privacy_policy: Policy) -> Self
pub fn policy(self, privacy_policy: Policy) -> Self
Defines a Policy while constructing a Controller.
Sourcepub fn change_policy(&mut self, privacy_policy: Policy)
pub fn change_policy(&mut self, privacy_policy: Policy)
Changes the Policy.
Sourcepub async fn discover(&mut self) -> Result<(), Error>
pub async fn discover(&mut self) -> Result<(), Error>
Discovers all available Devices on the network.
§Errors
§Discovery Errors
During the discovery process, common errors include:
- Inability to connect to the network
- Failure to disable a particular network interface
- Issues terminating the discovery process.
§Sending Requests Errors
When sending a request to a device to retrieve its structure description and routes, network failures or timeouts may prevent the request from being sent and affect the returned response as well.
Sourcepub async fn start_event_receivers(
&mut self,
buffer_size: usize,
) -> Result<Receiver<EventPayload>, Error>
pub async fn start_event_receivers( &mut self, buffer_size: usize, ) -> Result<Receiver<EventPayload>, Error>
Starts asynchronous event receiver tasks for all Devices that
support events.
An event receiver task connects to the broker of a device
and subscribes to its topic.
When a device transmits an event to the broker, the task retrieves the
event payload from the broker, parses the data, and sends the relevant
content to the Receiver returned by this method.
The buffer_size parameter specifies how many messages the event
receiver buffer can hold.
When the buffer is full, subsequent send attempts will wait until
a message is consumed from the channel.
When the Receiver is dropped, all tasks terminate automatically.
§Errors
- No event receiver tasks has started
- An error occurred while subscribing to the broker topic of a device.
Sourcepub const fn devices_mut(&mut self) -> &mut Devices
pub const fn devices_mut(&mut self) -> &mut Devices
Returns a mutable reference to Devices.
Sourcepub fn device(&self, id: usize) -> Result<DeviceSender<'_>, Error>
pub fn device(&self, id: usize) -> Result<DeviceSender<'_>, Error>
Builds a DeviceSender for the Device with the given identifier.
§Errors
An error is returned if no devices are found or if the given index does not exist.
Sourcepub async fn shutdown(self)
pub async fn shutdown(self)
Shuts down the Controller, stopping all asynchronous tasks and
releasing all associated resources.
§Note
For a graceful shutdown, this method must be called before dropping
the Controller.