use crate::call::adapters::{console_to_call_command, rwi_to_call_command};
use crate::call::domain::CallCommand;
use crate::call::runtime::{CommandResult, CommandSource, ExecutionContext, MediaCapabilityCheck};
use crate::console::handlers::call_control::CallCommandPayload;
use crate::proxy::active_call_registry::ActiveProxyCallRegistry;
use crate::rwi::session::RwiCommandPayload;
use std::sync::Arc;
pub fn dispatch_rwi_command(
registry: &Arc<ActiveProxyCallRegistry>,
session_id: Option<&str>,
payload: RwiCommandPayload,
) -> anyhow::Result<CommandResult> {
let command = match rwi_to_call_command(payload, session_id) {
Ok(cmd) => cmd,
Err(e) => {
return Ok(CommandResult::failure(format!(
"command not supported by unified path: {}",
e
)));
}
};
let session_id = session_id.unwrap_or_default();
let ctx = ExecutionContext::new(session_id).with_source(CommandSource::Rwi);
match ctx.check_media_capability(&command) {
MediaCapabilityCheck::Denied { reason } => {
return Ok(CommandResult::not_supported(reason));
}
MediaCapabilityCheck::Degraded { reason: _ } => {
}
MediaCapabilityCheck::Allowed => {}
}
dispatch_command(registry, session_id, command)
}
pub fn dispatch_console_command(
registry: &Arc<ActiveProxyCallRegistry>,
session_id: &str,
payload: CallCommandPayload,
) -> anyhow::Result<CommandResult> {
let command = console_to_call_command(payload, session_id)?;
let ctx = ExecutionContext::new(session_id).with_source(CommandSource::Console);
match ctx.check_media_capability(&command) {
MediaCapabilityCheck::Denied { reason } => {
return Ok(CommandResult::not_supported(reason));
}
MediaCapabilityCheck::Degraded { reason: _ } => {
}
MediaCapabilityCheck::Allowed => {}
}
dispatch_command(registry, session_id, command)
}
pub fn dispatch_call_command(
registry: &Arc<ActiveProxyCallRegistry>,
session_id: &str,
command: CallCommand,
source: CommandSource,
) -> anyhow::Result<CommandResult> {
let ctx = ExecutionContext::new(session_id).with_source(source);
match ctx.check_media_capability(&command) {
MediaCapabilityCheck::Denied { reason } => {
return Ok(CommandResult::not_supported(reason));
}
MediaCapabilityCheck::Degraded { reason: _ } => {
}
MediaCapabilityCheck::Allowed => {}
}
dispatch_command(registry, session_id, command)
}
fn dispatch_command(
registry: &Arc<ActiveProxyCallRegistry>,
session_id: &str,
command: CallCommand,
) -> anyhow::Result<CommandResult> {
if let CallCommand::Bridge { .. } = &command {
let Some(handle) = registry.get_handle(session_id) else {
return Ok(CommandResult::failure(format!(
"session {} not found",
session_id
)));
};
match handle.send_command(command) {
Ok(_) => return Ok(CommandResult::success()),
Err(e) => return Ok(CommandResult::failure(format!("failed to dispatch: {}", e))),
}
}
let Some(handle) = registry.get_handle(session_id) else {
return Ok(CommandResult::failure(format!(
"session {} not found",
session_id
)));
};
match handle.send_command(command) {
Ok(_) => Ok(CommandResult::success()),
Err(e) => Ok(CommandResult::failure(format!("failed to dispatch: {}", e))),
}
}
#[cfg(test)]
mod tests {
}