use std::sync::Arc;
use crate::api::handle::{CallId, SessionHandle};
use crate::api::headers::SipRequestOptions;
use crate::api::unified::UnifiedCoordinator;
pub trait Surface: Send + Sync + 'static {
type SessionRef: Send + Sync;
fn into_session_ref(&self, id: CallId) -> Self::SessionRef;
fn resolve_from(&self, from: Option<String>) -> String;
fn resolve_target(&self, target: &str) -> String {
target.to_string()
}
}
pub struct SurfaceBuilder<B, S>
where
B: SipRequestOptions,
S: Surface,
{
pub(crate) inner: B,
pub(crate) surface: Arc<S>,
}
impl<B, S> SurfaceBuilder<B, S>
where
B: SipRequestOptions,
S: Surface,
{
pub fn new(inner: B, surface: Arc<S>) -> Self {
Self { inner, surface }
}
pub fn map<F, B2>(self, f: F) -> SurfaceBuilder<B2, S>
where
F: FnOnce(B) -> B2,
B2: SipRequestOptions,
{
SurfaceBuilder {
inner: f(self.inner),
surface: self.surface,
}
}
pub fn into_parts(self) -> (B, Arc<S>) {
(self.inner, self.surface)
}
pub fn inner(&self) -> &B {
&self.inner
}
pub fn surface(&self) -> &S {
&self.surface
}
}
impl<B, S> SipRequestOptions for SurfaceBuilder<B, S>
where
B: SipRequestOptions,
S: Surface,
{
fn method(&self) -> rvoip_sip_core::types::Method {
self.inner.method()
}
fn header_state_mut(&mut self) -> &mut crate::api::headers::BuilderHeaderState {
self.inner.header_state_mut()
}
fn header_state(&self) -> &crate::api::headers::BuilderHeaderState {
self.inner.header_state()
}
}
impl Surface for UnifiedCoordinator {
type SessionRef = CallId;
fn into_session_ref(&self, id: CallId) -> CallId {
id
}
fn resolve_from(&self, from: Option<String>) -> String {
from.unwrap_or_else(|| self.config_local_uri())
}
}
pub fn wrap_handle(coord: &Arc<UnifiedCoordinator>, id: CallId) -> SessionHandle {
SessionHandle::new(id, coord.clone())
}