use std::{future::Future, sync::Arc};
use opcua_types::{EndpointDescription, Error, StatusCode};
use crate::transport::{state::SecureChannelState, RequestRecv};
use super::{tcp::TransportConfiguration, TcpConnector, TransportPollResult};
pub trait Connector: Send + Sync {
type Transport: Transport + Send + Sync + 'static;
fn connect(
&self,
channel: Arc<SecureChannelState>,
outgoing_recv: RequestRecv,
config: TransportConfiguration,
) -> impl Future<Output = Result<Self::Transport, StatusCode>> + Send + Sync;
fn default_endpoint(&self) -> EndpointDescription;
}
pub trait ConnectorBuilder: Send + Sync {
type ConnectorType: Connector + Send + Sync + 'static;
fn build(self) -> Result<Self::ConnectorType, Error>;
}
impl ConnectorBuilder for String {
type ConnectorType = TcpConnector;
fn build(self) -> Result<Self::ConnectorType, Error> {
ConnectorBuilder::build(self.as_str())
}
}
impl ConnectorBuilder for &str {
type ConnectorType = TcpConnector;
fn build(self) -> Result<Self::ConnectorType, Error> {
TcpConnector::new(self)
}
}
impl ConnectorBuilder for &String {
type ConnectorType = TcpConnector;
fn build(self) -> Result<Self::ConnectorType, Error> {
ConnectorBuilder::build(self.as_str())
}
}
impl<T> ConnectorBuilder for T
where
T: Connector + Send + Sync + 'static,
{
type ConnectorType = T;
fn build(self) -> Result<Self, Error> {
Ok(self)
}
}
pub trait Transport: Send + Sync + 'static {
fn poll(&mut self) -> impl Future<Output = TransportPollResult> + Send + Sync;
fn connected_url(&self) -> &str;
}