Skip to main content

ave_network/
service.rs

1//! # Network service
2//!
3
4use crate::{Command, Error};
5
6use tokio::sync::mpsc::Sender;
7
8/// The network service.
9#[derive(Debug, Clone)]
10pub struct NetworkService {
11    /// The command sender to communicate with the worker.
12    command_sender: Sender<Command>,
13}
14
15impl NetworkService {
16    /// Create a new `NetworkService`.
17    pub const fn new(command_sender: Sender<Command>) -> Self {
18        Self { command_sender }
19    }
20
21    /// Send command to the network worker.
22    pub async fn send_command(
23        &mut self,
24        command: Command,
25    ) -> Result<(), Error> {
26        self.command_sender
27            .send(command)
28            .await
29            .map_err(|e| Error::CommandSend(e.to_string()))
30    }
31
32    /// Send a message to the network worker.
33    pub fn sender(&self) -> Sender<Command> {
34        self.command_sender.clone()
35    }
36}