use tcp::{TcpClient, TcpConnectParams, TcpServer, TcpTransport};
use wormhole::{WormholeCaller, WormholeListener, WormholeTransport};
use crate::protocol::ControllerId;
pub enum TransportListener {
Tcp(TcpServer),
Wormhole(WormholeListener),
}
impl TransportListener {
pub fn describe_listening(&self) -> String {
match self {
Self::Tcp(tcp) => tcp.describe_listening(),
Self::Wormhole(w) => w.describe_listening(),
}
}
pub async fn accept_connection(&mut self) -> Result<Transport, String> {
match self {
Self::Tcp(tcp) => tcp.accept_connection().await.map(Transport::Tcp),
Self::Wormhole(w) => w.accept_connection().await.map(Transport::Wormhole),
}
}
}
pub enum TransportCaller {
Tcp(TcpClient),
Wormhole(WormholeCaller),
}
impl TransportCaller {
pub async fn new_connection(&self) -> Result<Transport, String> {
match self {
Self::Tcp(tcp) => tcp.new_connection().await.map(Transport::Tcp),
Self::Wormhole(w) => w.new_connection().await.map(Transport::Wormhole),
}
}
}
#[allow(clippy::large_enum_variant)]
pub enum Transport {
Tcp(TcpTransport),
Wormhole(WormholeTransport),
}
impl Transport {
pub async fn send_raw(&mut self, raw: &[u8]) -> Result<(), String> {
match self {
Self::Tcp(tcp) => tcp.send_raw(raw).await,
Self::Wormhole(w) => w.send_raw(raw).await,
}
}
pub async fn receive_raw(&mut self) -> Result<Option<Vec<u8>>, String> {
match self {
Self::Tcp(tcp) => tcp.receive_raw().await,
Self::Wormhole(w) => w.receive_raw().await,
}
}
}
#[derive(Clone, Debug)]
pub enum ConnectMethod {
Tcp(TcpConnectParams),
Wormhole,
}
impl ConnectMethod {
pub async fn new_listener(
&self,
controller_id: &ControllerId,
) -> Result<TransportListener, String> {
let listener = match self {
Self::Tcp(params) => TransportListener::Tcp(TcpServer::listen(params).await?),
Self::Wormhole => TransportListener::Wormhole(WormholeListener::new(controller_id)),
};
Ok(listener)
}
pub fn new_caller(&self, controller_id: &ControllerId) -> TransportCaller {
match self {
Self::Tcp(params) => TransportCaller::Tcp(TcpClient::new(¶ms.callback_addr)),
Self::Wormhole => TransportCaller::Wormhole(WormholeCaller::new(controller_id)),
}
}
}
pub mod tcp;
pub mod wormhole;