use crate::rx_dispatcher::{DispatcherCommand, DispatcherMessage};
use crate::{Command, DeviceFilter, Error, OnError, SeqTy, StreamEvent};
use std::collections::HashMap;
use std::time::Duration;
use tokio::sync::{mpsc, oneshot};
use tracing::trace;
use wire_weaver::prelude::UNib32;
use wire_weaver::shrink_wrap::SerializeShrinkWrap;
use ww_client_server::{PathKind, PathKindOwned, RequestKindOwned, StreamSidebandCommand};
use ww_version::{CompactVersion, FullVersionOwned};
pub struct CommandSender {
transport_cmd_tx: mpsc::UnboundedSender<Command>,
dispatcher_cmd_tx: mpsc::UnboundedSender<DispatcherCommand>,
seq_rx: mpsc::Receiver<SeqTy>,
trait_path: Option<Vec<UNib32>>,
gid_map: HashMap<FullVersionOwned, CompactVersion>,
scratch: [u8; 4096],
}
impl CommandSender {
pub fn new(
transport_cmd_tx: mpsc::UnboundedSender<Command>,
dispatcher_msg_rx: mpsc::UnboundedReceiver<DispatcherMessage>,
) -> Self {
let (seq_tx, seq_rx) = mpsc::channel(16); let (dispatcher_cmd_tx, dispatcher_cmd_rx) = mpsc::unbounded_channel(); tokio::spawn(async move {
crate::rx_dispatcher::rx_dispatcher(dispatcher_cmd_rx, dispatcher_msg_rx).await;
});
_ = dispatcher_cmd_tx.send(DispatcherCommand::RegisterSeqSource { seq_tx });
Self {
transport_cmd_tx,
dispatcher_cmd_tx,
seq_rx,
trait_path: None,
gid_map: HashMap::new(),
scratch: [0; 4096],
}
}
pub async fn connect(
&mut self,
filter: DeviceFilter,
user_protocol_version: FullVersionOwned,
on_error: OnError,
) -> Result<(), Error> {
let connected_rx = self.connect_inner(filter, user_protocol_version, on_error)?;
let connection_result = connected_rx.await.map_err(|_| Error::EventLoopNotRunning)?;
connection_result?;
Ok(())
}
pub fn connect_blocking(
&mut self,
filter: DeviceFilter,
user_protocol_version: FullVersionOwned,
on_error: OnError,
) -> Result<(), Error> {
let connected_rx = self.connect_inner(filter, user_protocol_version, on_error)?;
let connection_result = connected_rx
.blocking_recv()
.map_err(|_| Error::EventLoopNotRunning)?;
connection_result?;
Ok(())
}
fn connect_inner(
&mut self,
filter: DeviceFilter,
user_protocol_version: FullVersionOwned,
on_error: OnError,
) -> Result<oneshot::Receiver<Result<(), Error>>, Error> {
let (connected_tx, connected_rx) = oneshot::channel();
self.transport_cmd_tx
.send(Command::Connect {
filter,
user_protocol_version,
on_error,
connected_tx: Some(connected_tx),
})
.map_err(|_| Error::EventLoopNotRunning)?;
Ok(connected_rx)
}
pub fn send(&self, command: Command) -> Result<(), Error> {
self.transport_cmd_tx
.send(command)
.map_err(|_| Error::EventLoopNotRunning)?;
Ok(())
}
fn send_request(
&mut self,
seq: SeqTy,
path_kind: PathKindOwned,
kind: RequestKindOwned,
) -> Result<(), Error> {
let req = ww_client_server::RequestOwned {
seq,
path_kind,
kind,
};
let req = req.to_ww_bytes(&mut self.scratch)?; let cmd = Command::SendMessage {
bytes: req.to_vec(),
};
self.send(cmd)?;
Ok(())
}
pub async fn send_call_receive_reply(
&mut self,
path: PathKind<'_>,
args: Vec<u8>,
timeout: Duration,
) -> Result<Vec<u8>, Error> {
let seq = self.next_seq().await?;
let path_kind = self.to_ww_client_server_path(path)?;
let (done_tx, done_rx) = oneshot::channel();
self.dispatcher_cmd_tx
.send(DispatcherCommand::OnCallReturn {
seq,
done_tx,
timeout: Some(timeout),
})
.map_err(|_| Error::RxDispatcherNotRunning)?;
self.send_request(seq, path_kind, RequestKindOwned::Call { args })?;
let data = self.receive_reply(timeout, done_rx, "call").await?;
Ok(data)
}
pub fn send_call_receive_reply_blocking(
&mut self,
path: PathKind<'_>,
args: Vec<u8>,
timeout: Duration,
) -> Result<Vec<u8>, Error> {
let seq = self.next_seq_blocking()?;
let path_kind = self.to_ww_client_server_path(path)?;
let (done_tx, done_rx) = oneshot::channel();
self.dispatcher_cmd_tx
.send(DispatcherCommand::OnCallReturn {
seq,
done_tx,
timeout: Some(timeout),
})
.map_err(|_| Error::RxDispatcherNotRunning)?;
self.send_request(seq, path_kind, RequestKindOwned::Call { args })?;
let data = done_rx
.blocking_recv()
.map_err(|_| Error::EventLoopNotRunning)??;
Ok(data)
}
pub fn send_call_forget(&mut self, path: PathKind<'_>, args: Vec<u8>) -> Result<(), Error> {
let path_kind = self.to_ww_client_server_path(path)?;
self.send_request(0, path_kind, RequestKindOwned::Call { args })?;
Ok(())
}
pub async fn send_write_receive_reply(
&mut self,
path: PathKind<'_>,
value_bytes: Vec<u8>,
timeout: Duration,
) -> Result<(), Error> {
let seq = self.next_seq().await?;
let path_kind = self.to_ww_client_server_path(path)?;
let (done_tx, done_rx) = oneshot::channel();
self.dispatcher_cmd_tx
.send(DispatcherCommand::OnWriteComplete {
seq,
done_tx,
timeout: Some(timeout),
})
.map_err(|_| Error::RxDispatcherNotRunning)?;
self.send_request(
seq,
path_kind,
RequestKindOwned::Write { data: value_bytes },
)?;
let _data = self.receive_reply(timeout, done_rx, "write").await?;
Ok(())
}
pub fn send_write_forget(
&mut self,
path: PathKind<'_>,
value_bytes: Vec<u8>,
) -> Result<(), Error> {
let path_kind = self.to_ww_client_server_path(path)?;
self.send_request(0, path_kind, RequestKindOwned::Write { data: value_bytes })?;
Ok(())
}
pub fn send_stream_open(
&mut self,
path: PathKind<'_>,
stream_event_tx: mpsc::UnboundedSender<StreamEvent>,
) -> Result<(), Error> {
let path_kind = self.to_ww_client_server_path(path)?;
self.dispatcher_cmd_tx
.send(DispatcherCommand::OnStreamEvent {
path_kind: path_kind.clone(),
stream_event_tx,
})
.map_err(|_| Error::RxDispatcherNotRunning)?;
self.send_request(
0,
path_kind,
RequestKindOwned::StreamSideband {
sideband_cmd: StreamSidebandCommand::Open,
},
)?;
Ok(())
}
pub async fn send_read_receive_reply(
&mut self,
path: PathKind<'_>,
timeout: Duration,
) -> Result<Vec<u8>, Error> {
let seq = self.next_seq().await?;
let path_kind = self.to_ww_client_server_path(path)?;
let (done_tx, done_rx) = oneshot::channel();
self.dispatcher_cmd_tx
.send(DispatcherCommand::OnReadValue {
seq,
done_tx,
timeout: Some(timeout),
})
.map_err(|_| Error::RxDispatcherNotRunning)?;
self.send_request(seq, path_kind, RequestKindOwned::Read)?;
let data = self.receive_reply(timeout, done_rx, "read").await?;
Ok(data)
}
async fn receive_reply(
&self,
timeout: Duration,
done_rx: oneshot::Receiver<Result<Vec<u8>, Error>>,
desc: &'static str,
) -> Result<Vec<u8>, Error> {
let rx_or_timeout = tokio::time::timeout(timeout, done_rx).await;
trace!("got {desc} response: {:02x?}", rx_or_timeout);
let rx_or_recv_err = rx_or_timeout.map_err(|_| Error::Timeout)?;
let response = rx_or_recv_err.map_err(|_| Error::EventLoopNotRunning)?;
let data = response?;
Ok(data)
}
pub async fn next_seq(&mut self) -> Result<SeqTy, Error> {
let seq = self
.seq_rx
.recv()
.await
.ok_or(Error::RxDispatcherNotRunning)?;
Ok(seq)
}
pub fn next_seq_blocking(&mut self) -> Result<SeqTy, Error> {
let seq = self
.seq_rx
.blocking_recv()
.ok_or(Error::RxDispatcherNotRunning)?;
Ok(seq)
}
fn to_ww_client_server_path(&self, path: PathKind<'_>) -> Result<PathKindOwned, Error> {
if matches!(path, PathKind::Absolute { .. }) && self.trait_path.is_some() {
return Err(Error::User(
"CommandSender configured as trait attachment, but used with absolute path".into(),
));
}
let path_kind = match path {
PathKind::Absolute { path } => PathKindOwned::Absolute {
path: path.iter().collect::<Result<Vec<_>, _>>()?,
},
PathKind::GlobalCompact {
gid,
path_from_trait,
} => {
if let Some(base) = &self.trait_path {
let mut path = base.clone();
for n in path_from_trait.iter() {
path.push(n?);
}
PathKindOwned::Absolute { path }
} else {
PathKindOwned::GlobalCompact {
gid,
path_from_trait: path_from_trait.iter().collect::<Result<Vec<_>, _>>()?,
}
}
}
PathKind::GlobalFull {
gid,
path_from_trait,
} => {
if let Some(base) = &self.trait_path {
let mut path = base.clone();
for n in path_from_trait.iter() {
path.push(n?);
}
PathKindOwned::Absolute { path }
} else if let Some(compact) = self.gid_map.get(&gid.make_owned()) {
PathKindOwned::GlobalCompact {
gid: *compact,
path_from_trait: path_from_trait.iter().collect::<Result<Vec<_>, _>>()?,
}
} else {
PathKindOwned::GlobalFull {
gid: gid.make_owned(),
path_from_trait: path_from_trait.iter().collect::<Result<Vec<_>, _>>()?,
}
}
}
};
Ok(path_kind)
}
}