use crate::commons::identifier::KeyIdentifier;
use crate::commons::self_signature_manager::SelfSignatureManager;
use crate::signature::Signed;
use log::debug;
use rmp_serde;
use tokio::sync::mpsc::{self, error::SendError};
use super::{MessageContent, TaskCommandContent};
use super::{command::Command, error::Error};
const LOG_TARGET: &str = "MESSAGE_SENDER";
#[derive(Clone)]
pub struct MessageSender {
sender: mpsc::Sender<Command>,
controller_id: KeyIdentifier,
signature_manager: SelfSignatureManager,
}
impl MessageSender {
pub fn new(
sender: mpsc::Sender<Command>,
controller_id: KeyIdentifier,
signature_manager: SelfSignatureManager,
) -> Self {
Self {
sender,
controller_id,
signature_manager,
}
}
pub async fn send_message<T: TaskCommandContent>(
&self,
target: KeyIdentifier,
message: T,
) -> Result<(), Error> {
let complete_message = Signed::<MessageContent<T>>::new(
self.controller_id.clone(),
target.clone(),
message,
&self.signature_manager,
)?;
let bytes = rmp_serde::to_vec(&complete_message).unwrap();
self.sender
.send(Command::SendMessage {
receptor: target.public_key,
message: bytes,
})
.await
.map_err(|_| Error::ChannelClosed)?;
Ok(())
}
#[allow(dead_code)]
pub async fn start_providing(&mut self, keys: Vec<String>) -> Result<(), SendError<Command>> {
self.sender.send(Command::StartProviding { keys }).await
}
#[allow(dead_code)]
pub async fn bootstrap(&mut self) -> Result<(), SendError<Command>> {
debug!("{}: Starting Bootstrap", LOG_TARGET);
self.sender.send(Command::Bootstrap).await
}
}