use std::sync::Arc;
use tokio::sync::Mutex;
use super::types::*;
pub struct RealTimeSession {
pub(crate) session_id: String,
#[allow(dead_code)]
pub(crate) config: SessionConfig,
pub(crate) state: Arc<Mutex<SessionState>>,
pub(crate) stats: Arc<Mutex<SessionStats>>,
}
impl RealTimeSession {
pub(crate) fn new(session_id: String, config: SessionConfig) -> Self {
Self {
session_id,
config,
state: Arc::new(Mutex::new(SessionState::Connecting)),
stats: Arc::new(Mutex::new(SessionStats {
duration_seconds: 0,
packets_sent: 0,
packets_received: 0,
bytes_sent: 0,
bytes_received: 0,
transcription_count: 0,
})),
}
}
pub fn session_id(&self) -> &str {
&self.session_id
}
pub async fn state(&self) -> SessionState {
*self.state.lock().await
}
pub async fn stats(&self) -> SessionStats {
self.stats.lock().await.clone()
}
pub async fn is_active(&self) -> bool {
matches!(
*self.state.lock().await,
SessionState::Connected
| SessionState::Listening
| SessionState::Processing
| SessionState::Speaking
)
}
pub(crate) async fn update_state(&self, new_state: SessionState) {
*self.state.lock().await = new_state;
}
#[allow(dead_code)]
pub(crate) async fn record_packet_sent(&self, bytes: u64) {
let mut stats = self.stats.lock().await;
stats.packets_sent += 1;
stats.bytes_sent += bytes;
}
#[allow(dead_code)]
pub(crate) async fn record_packet_received(&self, bytes: u64) {
let mut stats = self.stats.lock().await;
stats.packets_received += 1;
stats.bytes_received += bytes;
}
#[allow(dead_code)]
pub(crate) async fn increment_transcription_count(&self) {
let mut stats = self.stats.lock().await;
stats.transcription_count += 1;
}
}