use std::sync::Arc;
use crate::Device;
pub use burn_dispatch::backends::remote::server::{
AllowAll, AuthorizationRequest, PeerAuthorizer, RemoteProtocol,
};
pub use burn_dispatch::backends::remote::telemetry;
pub use burn_dispatch::backends::remote::{Endpoint, RemoteSecret};
pub use burn_dispatch::devices::BURN_REMOTE_ALPN;
use telemetry::TelemetryProbe;
pub use burn_dispatch::remote_server::Channel;
pub fn protocol(device: Device, endpoint: &Endpoint) -> RemoteProtocolBuilder<'_> {
RemoteProtocolBuilder::new(device, endpoint)
}
pub struct RemoteProtocolBuilder<'a> {
device: Device,
endpoint: &'a Endpoint,
probe: Option<TelemetryProbe>,
authorizer: Option<Arc<dyn PeerAuthorizer>>,
}
impl<'a> RemoteProtocolBuilder<'a> {
pub fn new(device: Device, endpoint: &'a Endpoint) -> Self {
Self {
device,
endpoint,
probe: None,
authorizer: None,
}
}
pub fn with_telemetry(mut self, probe: TelemetryProbe) -> Self {
self.probe = Some(probe);
self
}
pub fn with_authorizer(mut self, authorizer: impl PeerAuthorizer) -> Self {
self.authorizer = Some(Arc::new(authorizer));
self
}
pub fn build(self) -> RemoteProtocol {
burn_dispatch::remote_server::remote_protocol(
self.device.into_dispatch(),
self.endpoint,
self.probe.unwrap_or_else(TelemetryProbe::disabled),
self.authorizer.unwrap_or_else(|| Arc::new(AllowAll)),
)
}
}
impl<'a> From<RemoteProtocolBuilder<'a>> for RemoteProtocol {
fn from(builder: RemoteProtocolBuilder<'a>) -> Self {
builder.build()
}
}
#[cfg(not(target_family = "wasm"))]
pub fn start(device: Device, channel: Channel) {
burn_dispatch::remote_server::start(device.into_dispatch(), channel)
}
#[cfg(not(target_family = "wasm"))]
pub async fn start_async(device: Device, channel: Channel) {
burn_dispatch::remote_server::start_async(device.into_dispatch(), channel).await
}