feat: file transfer system (avatar/icon download with cacache) (#40)
* docs(architecture): add file transfer design, research, and implementation plan * feat(cache): add chanora_cache crate with cacache-backed blob cache - New chanora_cache crate: content-addressed blob store wrapping cacache - BlobCache API: async put/get/remove/clear/total_size/evict - Key validation: av_ prefix (32 hex chars), ic_ prefix (decimal digits) - Cacache provides crash safety, SSRI integrity, content dedup - Mtime-based eviction via cacache::list_sync + sort by timestamp - 7 unit tests all passing - Added to workspace members * feat(protocol): add file download support for avatars and icons - Add Request::DownloadFile variant with oneshot reply - Add ProtocolClient::download_avatar(client_uid) and download_icon(icon_id) - Track pending file downloads by FiletransferHandle - Handle StreamItem::FileDownload: read bytes from TCP stream - Handle StreamItem::FiletransferFailed: map to ProtocolError - Add ProtocolError::FileTransfer(String) variant - Add path helper tests for avatar/icon download paths - No tsclientlib types leak across the adapter boundary * feat(core): add blob cache wiring and avatar download orchestration - Add chanora_cache dependency to Cargo.toml - Add blob_cache field to ChanoraSession (Arc<Mutex<Option<BlobCache>>>) - Add init_cache() method: creates BlobCache, runs eviction - Add get_avatar() method: cache-first, download on miss, store in cache - Add clear_cache() and cache_size() methods for cache management - Add CoreError::Cache variant for BlobCacheError conversion - Add avatar_cache integration test * feat(bridge): add init_cache, download_avatar, and cache management functions - Add init_cache(dir) bridge function - Add download_avatar(avatar_hash, client_uid) bridge function - Add clear_file_cache() and file_cache_size() bridge functions - Map CoreError::Cache and ProtocolError::FileTransfer in BridgeError * feat(flutter): add cache initialization wiring and avatar download shims - Add wireCache() to app_bootstrap using getApplicationCacheDirectory() - Call wireCache() after wireStorage() in main bootstrap flow - Add Dart-side initCache and downloadAvatar wrapper shims in api.dart - Update Cargo.lock for new chanora_cache dependency * feat(core): FileTransferService with coalescing, throttling, negative cache - New file_transfer module with FileTransferService struct - Semaphore(2) throttles concurrent downloads - In-flight HashMap coalesces duplicate avatar requests - 5-min negative cache short-circuits ServerRejected misses - ChanoraSession delegates get_avatar through the service - connect/disconnect update shared protocol handle - clear_cache/cache_size delegate to service - 2 new unit tests (cached hit, negative cache) * feat(core,bridge): add get_icon with coalescing and negative cache - FileTransferService::get_icon() mirrors get_avatar pattern - ChanoraSession::get_icon() delegates through FileTransferService - Bridge download_icon() exposed for Flutter - Dart downloadIcon() shim added - Uses PREFIX_ICON (ic_<crc32u>) cache key format - 1 new unit test (cached icon hit) * fix(core,protocol): simplify store_protocol and add download size cap - store_protocol: always write to shared Arc<Mutex<Option<ProtocolClient>>>; the FileTransferService holds the same Arc so it sees updates automatically - read_download_bytes: reject downloads exceeding 10 MB to prevent malicious servers from causing OOM
This commit is contained in:
@@ -10,6 +10,7 @@ repository.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
[dependencies]
|
||||
chanora_cache = { path = "../../crates/chanora_cache" }
|
||||
chanora_protocol = { path = "../../crates/chanora_protocol" }
|
||||
chanora_state = { path = "../../crates/chanora_state" }
|
||||
chanora_audio = { path = "../../crates/chanora_audio" }
|
||||
|
||||
@@ -0,0 +1,344 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use chanora_cache::{BlobCache, BlobCacheError, PREFIX_AVATAR, PREFIX_ICON};
|
||||
use chanora_protocol::{ProtocolClient, ProtocolError};
|
||||
use tokio::sync::{Mutex, Semaphore, oneshot};
|
||||
use tracing::warn;
|
||||
|
||||
const MAX_CONCURRENT_DOWNLOADS: usize = 2;
|
||||
const NEGATIVE_CACHE_TTL: Duration = Duration::from_secs(5 * 60);
|
||||
|
||||
type InFlightWaiters = Vec<oneshot::Sender<Result<Option<Vec<u8>>, FileTransferError>>>;
|
||||
|
||||
/// Errors raised while resolving protocol-owned file assets.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum FileTransferError {
|
||||
/// No live protocol client is available for a download.
|
||||
#[error("not connected")]
|
||||
NotConnected,
|
||||
/// The protocol layer failed while downloading the asset.
|
||||
#[error("protocol error: {0}")]
|
||||
Protocol(#[from] ProtocolError),
|
||||
/// The blob cache failed while reading or writing the asset.
|
||||
#[error("cache error: {0}")]
|
||||
Cache(#[from] BlobCacheError),
|
||||
}
|
||||
|
||||
impl Clone for FileTransferError {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
Self::NotConnected => Self::NotConnected,
|
||||
Self::Protocol(error) => Self::Protocol(clone_protocol_error(error)),
|
||||
Self::Cache(error) => Self::Cache(clone_blob_cache_error(error)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FileTransferService {
|
||||
cache: BlobCache,
|
||||
protocol: Arc<Mutex<Option<ProtocolClient>>>,
|
||||
semaphore: Arc<Semaphore>,
|
||||
in_flight: Arc<Mutex<HashMap<String, InFlightWaiters>>>,
|
||||
negative_cache: Arc<Mutex<HashMap<String, Instant>>>,
|
||||
}
|
||||
|
||||
impl FileTransferService {
|
||||
pub fn new(cache: BlobCache, protocol: Arc<Mutex<Option<ProtocolClient>>>) -> Self {
|
||||
Self {
|
||||
cache,
|
||||
protocol,
|
||||
semaphore: Arc::new(Semaphore::new(MAX_CONCURRENT_DOWNLOADS)),
|
||||
in_flight: Arc::new(Mutex::new(HashMap::new())),
|
||||
negative_cache: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_protocol(&self, client: Option<ProtocolClient>) {
|
||||
*self.protocol.lock().await = client;
|
||||
}
|
||||
|
||||
pub async fn get_avatar(
|
||||
&self,
|
||||
avatar_hash: &str,
|
||||
client_uid: &str,
|
||||
) -> Result<Option<Vec<u8>>, FileTransferError> {
|
||||
if let Some(bytes) = self.cache.get(PREFIX_AVATAR, avatar_hash).await? {
|
||||
return Ok(Some(bytes));
|
||||
}
|
||||
|
||||
if self.is_negative_cache_hit(avatar_hash).await {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let rx = {
|
||||
let mut in_flight = self.in_flight.lock().await;
|
||||
if let Some(waiters) = in_flight.get_mut(avatar_hash) {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
waiters.push(tx);
|
||||
Some(rx)
|
||||
} else {
|
||||
in_flight.insert(avatar_hash.to_string(), Vec::new());
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(rx) = rx {
|
||||
return rx.await.unwrap_or_else(|_| {
|
||||
Err(FileTransferError::Protocol(ProtocolError::Lost(
|
||||
"coalesced avatar download waiter dropped".to_string(),
|
||||
)))
|
||||
});
|
||||
}
|
||||
|
||||
let _permit = self
|
||||
.semaphore
|
||||
.acquire()
|
||||
.await
|
||||
.expect("file transfer semaphore should stay open");
|
||||
|
||||
let result = self.do_download_avatar(avatar_hash, client_uid).await;
|
||||
self.finish_in_flight(avatar_hash, &result).await;
|
||||
result
|
||||
}
|
||||
|
||||
pub async fn get_icon(&self, icon_id: u64) -> Result<Option<Vec<u8>>, FileTransferError> {
|
||||
let icon_key = icon_id.to_string();
|
||||
let negative_key = format!("ic_{icon_id}");
|
||||
let in_flight_key = format!("icon_{icon_id}");
|
||||
|
||||
if let Some(bytes) = self.cache.get(PREFIX_ICON, &icon_key).await? {
|
||||
return Ok(Some(bytes));
|
||||
}
|
||||
|
||||
if self.is_negative_cache_hit(&negative_key).await {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let rx = {
|
||||
let mut in_flight = self.in_flight.lock().await;
|
||||
if let Some(waiters) = in_flight.get_mut(&in_flight_key) {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
waiters.push(tx);
|
||||
Some(rx)
|
||||
} else {
|
||||
in_flight.insert(in_flight_key.clone(), Vec::new());
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(rx) = rx {
|
||||
return rx.await.unwrap_or_else(|_| {
|
||||
Err(FileTransferError::Protocol(ProtocolError::Lost(
|
||||
"coalesced icon download waiter dropped".to_string(),
|
||||
)))
|
||||
});
|
||||
}
|
||||
|
||||
let _permit = self
|
||||
.semaphore
|
||||
.acquire()
|
||||
.await
|
||||
.expect("file transfer semaphore should stay open");
|
||||
|
||||
let result = self.do_download_icon(icon_id).await;
|
||||
self.finish_in_flight(&in_flight_key, &result).await;
|
||||
result
|
||||
}
|
||||
|
||||
pub async fn clear_cache(&self) -> Result<(), FileTransferError> {
|
||||
self.cache.clear().await?;
|
||||
self.negative_cache.lock().await.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cache_size(&self) -> Result<u64, FileTransferError> {
|
||||
Ok(self.cache.total_size().await?)
|
||||
}
|
||||
|
||||
async fn do_download_avatar(
|
||||
&self,
|
||||
avatar_hash: &str,
|
||||
client_uid: &str,
|
||||
) -> Result<Option<Vec<u8>>, FileTransferError> {
|
||||
if let Some(bytes) = self.cache.get(PREFIX_AVATAR, avatar_hash).await? {
|
||||
return Ok(Some(bytes));
|
||||
}
|
||||
|
||||
if self.is_negative_cache_hit(avatar_hash).await {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(FileTransferError::NotConnected)?;
|
||||
match client.download_avatar(client_uid).await {
|
||||
Ok(bytes) => {
|
||||
self.cache.put(PREFIX_AVATAR, avatar_hash, &bytes).await?;
|
||||
self.negative_cache.lock().await.remove(avatar_hash);
|
||||
Ok(Some(bytes))
|
||||
}
|
||||
Err(ProtocolError::ServerRejected { .. }) => {
|
||||
self.negative_cache
|
||||
.lock()
|
||||
.await
|
||||
.insert(avatar_hash.to_string(), Instant::now() + NEGATIVE_CACHE_TTL);
|
||||
Ok(None)
|
||||
}
|
||||
Err(error) => Err(FileTransferError::Protocol(error)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn do_download_icon(&self, icon_id: u64) -> Result<Option<Vec<u8>>, FileTransferError> {
|
||||
let icon_key = icon_id.to_string();
|
||||
let negative_key = format!("ic_{icon_id}");
|
||||
|
||||
if let Some(bytes) = self.cache.get(PREFIX_ICON, &icon_key).await? {
|
||||
return Ok(Some(bytes));
|
||||
}
|
||||
|
||||
if self.is_negative_cache_hit(&negative_key).await {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(FileTransferError::NotConnected)?;
|
||||
match client.download_icon(icon_id).await {
|
||||
Ok(bytes) => {
|
||||
self.cache.put(PREFIX_ICON, &icon_key, &bytes).await?;
|
||||
self.negative_cache.lock().await.remove(&negative_key);
|
||||
Ok(Some(bytes))
|
||||
}
|
||||
Err(ProtocolError::ServerRejected { .. }) => {
|
||||
self.negative_cache
|
||||
.lock()
|
||||
.await
|
||||
.insert(negative_key, Instant::now() + NEGATIVE_CACHE_TTL);
|
||||
Ok(None)
|
||||
}
|
||||
Err(error) => Err(FileTransferError::Protocol(error)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn finish_in_flight(
|
||||
&self,
|
||||
avatar_hash: &str,
|
||||
result: &Result<Option<Vec<u8>>, FileTransferError>,
|
||||
) {
|
||||
let waiters = self.in_flight.lock().await.remove(avatar_hash).unwrap_or_default();
|
||||
for waiter in waiters {
|
||||
if waiter.send(result.clone()).is_err() {
|
||||
warn!(target: "chanora_core", avatar_hash, "avatar download waiter dropped");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn is_negative_cache_hit(&self, avatar_hash: &str) -> bool {
|
||||
let now = Instant::now();
|
||||
let mut negative_cache = self.negative_cache.lock().await;
|
||||
match negative_cache.get(avatar_hash).copied() {
|
||||
Some(expires_at) if expires_at > now => true,
|
||||
Some(_) => {
|
||||
negative_cache.remove(avatar_hash);
|
||||
false
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn clone_protocol_error(error: &ProtocolError) -> ProtocolError {
|
||||
match error {
|
||||
ProtocolError::Invalid(message) => ProtocolError::Invalid(message.clone()),
|
||||
ProtocolError::DnsFailed { host, reason } => ProtocolError::DnsFailed {
|
||||
host: host.clone(),
|
||||
reason: reason.clone(),
|
||||
},
|
||||
ProtocolError::Connect(message) => ProtocolError::Connect(message.clone()),
|
||||
ProtocolError::DisconnectedEarly(message) => {
|
||||
ProtocolError::DisconnectedEarly(message.clone())
|
||||
}
|
||||
ProtocolError::Lost(message) => ProtocolError::Lost(message.clone()),
|
||||
ProtocolError::Identity(message) => ProtocolError::Identity(message.clone()),
|
||||
ProtocolError::Timeout => ProtocolError::Timeout,
|
||||
ProtocolError::ServerRejected { code, message } => ProtocolError::ServerRejected {
|
||||
code: *code,
|
||||
message: message.clone(),
|
||||
},
|
||||
ProtocolError::Backend(message) => ProtocolError::Backend(message.clone()),
|
||||
ProtocolError::FileTransfer(message) => ProtocolError::FileTransfer(message.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn clone_blob_cache_error(error: &BlobCacheError) -> BlobCacheError {
|
||||
match error {
|
||||
BlobCacheError::Io(message) => BlobCacheError::Io(message.clone()),
|
||||
BlobCacheError::InvalidKey(message) => BlobCacheError::InvalidKey(message.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn test_cache_dir(name: &str) -> std::path::PathBuf {
|
||||
let mut path = std::env::temp_dir();
|
||||
let nanos = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
path.push(format!("chanora-core-file-transfer-{name}-{nanos}"));
|
||||
path
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_cached_avatar_without_connection() {
|
||||
let cache_dir = test_cache_dir("cache-hit");
|
||||
let cache = BlobCache::new(&cache_dir, 1024).unwrap();
|
||||
cache.put(PREFIX_AVATAR, "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6", b"avatar")
|
||||
.await
|
||||
.unwrap();
|
||||
let service = FileTransferService::new(cache, Arc::new(Mutex::new(None)));
|
||||
|
||||
let avatar = service
|
||||
.get_avatar("a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6", "client")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(avatar, Some(b"avatar".to_vec()));
|
||||
let _ = std::fs::remove_dir_all(cache_dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn negative_cache_short_circuits_not_connected() {
|
||||
let cache_dir = test_cache_dir("negative-cache");
|
||||
let cache = BlobCache::new(&cache_dir, 1024).unwrap();
|
||||
let service = FileTransferService::new(cache, Arc::new(Mutex::new(None)));
|
||||
service
|
||||
.negative_cache
|
||||
.lock()
|
||||
.await
|
||||
.insert("a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6".to_string(), Instant::now() + NEGATIVE_CACHE_TTL);
|
||||
|
||||
let avatar = service
|
||||
.get_avatar("a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6", "client")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(avatar, None);
|
||||
let _ = std::fs::remove_dir_all(cache_dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn returns_cached_icon_without_connection() {
|
||||
let cache_dir = test_cache_dir("icon-cache-hit");
|
||||
let cache = BlobCache::new(&cache_dir, 1024).unwrap();
|
||||
cache.put(PREFIX_ICON, "12345", b"icon").await.unwrap();
|
||||
let service = FileTransferService::new(cache, Arc::new(Mutex::new(None)));
|
||||
|
||||
let icon = service.get_icon(12345).await.unwrap();
|
||||
|
||||
assert_eq!(icon, Some(b"icon".to_vec()));
|
||||
let _ = std::fs::remove_dir_all(cache_dir);
|
||||
}
|
||||
}
|
||||
+146
-37
@@ -53,6 +53,7 @@ use chanora_state::channel_join::{
|
||||
};
|
||||
|
||||
mod events;
|
||||
mod file_transfer;
|
||||
mod network_diagnostics;
|
||||
pub mod ptt;
|
||||
|
||||
@@ -75,6 +76,7 @@ pub use events::{
|
||||
NetworkState, PersistedPttBinding, PttDescriptorSnapshot, SessionEvent, VoiceJoinErrorCode,
|
||||
VoiceJoinSyncState,
|
||||
};
|
||||
pub use file_transfer::FileTransferError;
|
||||
use network_diagnostics::NetworkDiagnostics;
|
||||
|
||||
/// Errors that can arise during top-level orchestration.
|
||||
@@ -92,6 +94,12 @@ pub enum CoreError {
|
||||
/// Storage error.
|
||||
#[error("storage: {0}")]
|
||||
Storage(#[from] chanora_storage::StorageError),
|
||||
/// Blob-cache failure.
|
||||
#[error("cache: {0}")]
|
||||
Cache(#[from] chanora_cache::BlobCacheError),
|
||||
/// File-transfer failure.
|
||||
#[error("file transfer: {0}")]
|
||||
FileTransfer(#[from] FileTransferError),
|
||||
/// Diagnostics error.
|
||||
#[error("diagnostics: {0}")]
|
||||
Diagnostics(#[from] chanora_diagnostics::DiagnosticsError),
|
||||
@@ -130,7 +138,6 @@ struct SupervisorInner {
|
||||
}
|
||||
|
||||
struct ConnectedState {
|
||||
protocol: chanora_protocol::ProtocolClient,
|
||||
audio: Option<chanora_audio::AudioEngine>,
|
||||
/// Active PTT controller (SDD-088). Owns the platform input
|
||||
/// backend, the active binding, and the capability watch
|
||||
@@ -197,6 +204,8 @@ pub struct ChanoraSession {
|
||||
/// extension). Lives alongside the identity file. Wired by
|
||||
/// [`Self::init_storage`].
|
||||
bookmark_store: Arc<Mutex<Option<BookmarkRepository>>>,
|
||||
protocol: Arc<Mutex<Option<chanora_protocol::ProtocolClient>>>,
|
||||
file_transfer: Arc<Mutex<Option<Arc<file_transfer::FileTransferService>>>>,
|
||||
/// Invisible server-address prefetch cache. Warmed by Flutter typing
|
||||
/// but validated by Rust before Connect can reuse it.
|
||||
server_prefetch: ServerPrefetcher,
|
||||
@@ -252,6 +261,8 @@ impl ChanoraSession {
|
||||
network_tx,
|
||||
identity_store: Arc::new(Mutex::new(None)),
|
||||
bookmark_store: Arc::new(Mutex::new(None)),
|
||||
protocol: Arc::new(Mutex::new(None)),
|
||||
file_transfer: Arc::new(Mutex::new(None)),
|
||||
server_prefetch: ServerPrefetcher::new(),
|
||||
voice_selector: selector,
|
||||
release_tail,
|
||||
@@ -273,6 +284,17 @@ impl ChanoraSession {
|
||||
ConnectionEpoch(epoch)
|
||||
}
|
||||
|
||||
async fn store_protocol(&self, client: Option<chanora_protocol::ProtocolClient>) {
|
||||
// Always update the shared Arc. The FileTransferService holds
|
||||
// the same Arc, so it sees the new client automatically — no
|
||||
// separate set_protocol call needed.
|
||||
*self.protocol.lock().await = client;
|
||||
}
|
||||
|
||||
async fn take_protocol(&self) -> Option<chanora_protocol::ProtocolClient> {
|
||||
self.protocol.lock().await.take()
|
||||
}
|
||||
|
||||
/// Wire a directory-backed identity store. Called by the bridge
|
||||
/// during `bridge_init` once Flutter has resolved the platform
|
||||
/// app-private storage directory. Subsequent [`Self::connect`]
|
||||
@@ -337,6 +359,65 @@ impl ChanoraSession {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Configure the blob cache root.
|
||||
pub async fn init_cache(&self, dir: &str) -> Result<(), CoreError> {
|
||||
let cache = chanora_cache::BlobCache::new(dir, 100 * 1024 * 1024)?;
|
||||
cache.evict().await?;
|
||||
let service = Arc::new(file_transfer::FileTransferService::new(
|
||||
cache,
|
||||
self.protocol.clone(),
|
||||
));
|
||||
let mut guard = self.file_transfer.lock().await;
|
||||
*guard = Some(service);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Resolve avatar bytes.
|
||||
pub async fn get_avatar(
|
||||
&self,
|
||||
avatar_hash: &str,
|
||||
client_uid: &str,
|
||||
) -> Result<Option<Vec<u8>>, CoreError> {
|
||||
let service = { self.file_transfer.lock().await.clone() };
|
||||
if let Some(service) = service {
|
||||
return Ok(service.get_avatar(avatar_hash, client_uid).await?);
|
||||
}
|
||||
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
Ok(Some(client.download_avatar(client_uid).await?))
|
||||
}
|
||||
|
||||
/// Resolve icon bytes.
|
||||
pub async fn get_icon(&self, icon_id: u64) -> Result<Option<Vec<u8>>, CoreError> {
|
||||
let service = { self.file_transfer.lock().await.clone() };
|
||||
if let Some(service) = service {
|
||||
return Ok(service.get_icon(icon_id).await?);
|
||||
}
|
||||
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
Ok(Some(client.download_icon(icon_id).await?))
|
||||
}
|
||||
|
||||
/// Purge cached protocol-owned assets.
|
||||
pub async fn clear_cache(&self) -> Result<(), CoreError> {
|
||||
let service = { self.file_transfer.lock().await.clone() };
|
||||
if let Some(service) = service {
|
||||
service.clear_cache().await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Report the configured blob-cache size.
|
||||
pub async fn cache_size(&self) -> Result<u64, CoreError> {
|
||||
let service = { self.file_transfer.lock().await.clone() };
|
||||
match service {
|
||||
Some(service) => Ok(service.cache_size().await?),
|
||||
None => Ok(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// List persisted bookmarks. Returns an empty list if the store
|
||||
/// has not been wired or has no entries.
|
||||
pub async fn list_bookmarks(&self) -> Result<Vec<Bookmark>, CoreError> {
|
||||
@@ -514,6 +595,7 @@ impl ChanoraSession {
|
||||
|
||||
let supervisor = tokio::spawn(supervisor_loop(SupervisorContext {
|
||||
state_arc: self.inner.clone(),
|
||||
protocol: self.protocol.clone(),
|
||||
events_tx: self.events_tx.clone(),
|
||||
initial_cfg: cfg.clone(),
|
||||
initial_lost_rx: lost_rx,
|
||||
@@ -568,9 +650,9 @@ impl ChanoraSession {
|
||||
}
|
||||
|
||||
spawn_event_forwarders(&client, &self.events_tx);
|
||||
self.store_protocol(Some(client)).await;
|
||||
|
||||
*guard = Some(ConnectedState {
|
||||
protocol: client,
|
||||
audio: None,
|
||||
ptt_controller: None,
|
||||
cancel_tx: Some(cancel_tx),
|
||||
@@ -631,9 +713,13 @@ impl ChanoraSession {
|
||||
|
||||
/// Return a fresh snapshot of the current server state.
|
||||
pub async fn snapshot(&self) -> Result<ServerSnapshot, CoreError> {
|
||||
let snap = {
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
client.snapshot().await?
|
||||
};
|
||||
let mut guard = self.inner.lock().await;
|
||||
let state = guard.as_mut().ok_or(CoreError::NotConnected)?;
|
||||
let snap = state.protocol.snapshot().await?;
|
||||
let current_channel = self
|
||||
.find_own_in(&snap)
|
||||
.await
|
||||
@@ -661,9 +747,9 @@ impl ChanoraSession {
|
||||
|
||||
/// Fetch richer profile and live connection details for one online client.
|
||||
pub async fn client_profile(&self, client_id: u64) -> Result<ClientProfile, CoreError> {
|
||||
let guard = self.inner.lock().await;
|
||||
let state = guard.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
Ok(state.protocol.client_profile(client_id).await?)
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
Ok(client.client_profile(client_id).await?)
|
||||
}
|
||||
|
||||
/// True if a connection is currently active.
|
||||
@@ -680,9 +766,9 @@ impl ChanoraSession {
|
||||
if !should_dispatch_text_message(&message, &target) {
|
||||
return Ok(());
|
||||
}
|
||||
let guard = self.inner.lock().await;
|
||||
let state = guard.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
state.protocol.send_text_message(message, target).await?;
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
client.send_text_message(message, target).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -741,11 +827,16 @@ impl ChanoraSession {
|
||||
// session permanently unable to restart audio without a
|
||||
// reconnect (the user saw "voice_in already taken" on the
|
||||
// second channel switch).
|
||||
let voice_out = state.protocol.voice_out();
|
||||
let voice_in = state
|
||||
.protocol
|
||||
.take_voice_in()
|
||||
.ok_or(CoreError::Invariant("voice_in already taken"))?;
|
||||
let (voice_out, voice_in) = {
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
(
|
||||
client.voice_out(),
|
||||
client
|
||||
.take_voice_in()
|
||||
.ok_or(CoreError::Invariant("voice_in already taken"))?,
|
||||
)
|
||||
};
|
||||
let gate = AudioTransmitGate::new(cfg.ptt_initial);
|
||||
cfg.voice_activity_selector = Some(self.voice_selector.clone());
|
||||
let new_engine = match chanora_audio::AudioEngine::start_with_gate(
|
||||
@@ -976,10 +1067,11 @@ impl ChanoraSession {
|
||||
let password_to_send = requested_password
|
||||
.clone()
|
||||
.or_else(|| state.channel_passwords.get(&channel_id).cloned());
|
||||
state
|
||||
.protocol
|
||||
.move_to_channel(channel_id, password_to_send)
|
||||
.await?;
|
||||
{
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
client.move_to_channel(channel_id, password_to_send).await?;
|
||||
}
|
||||
if let Some(pw) = requested_password {
|
||||
state.channel_passwords.insert(channel_id, pw);
|
||||
}
|
||||
@@ -1004,7 +1096,11 @@ impl ChanoraSession {
|
||||
if let Some(muted) = output {
|
||||
state.local_output_muted = muted;
|
||||
}
|
||||
state.protocol.set_muted(input, output).await?;
|
||||
{
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
client.set_muted(input, output).await?;
|
||||
}
|
||||
if let Some(muted) = output {
|
||||
if let Some(audio) = state.audio.as_ref() {
|
||||
audio.set_output_muted(muted);
|
||||
@@ -1327,11 +1423,12 @@ impl ChanoraSession {
|
||||
let password_to_send = requested_password
|
||||
.clone()
|
||||
.or_else(|| state.channel_passwords.get(&channel_id).cloned());
|
||||
if let Err(e) = state
|
||||
.protocol
|
||||
.queue_move_to_channel(channel_id, password_to_send)
|
||||
.await
|
||||
{
|
||||
let move_result = {
|
||||
let protocol = self.protocol.lock().await;
|
||||
let client = protocol.as_ref().ok_or(CoreError::NotConnected)?;
|
||||
client.queue_move_to_channel(channel_id, password_to_send).await
|
||||
};
|
||||
if let Err(e) = move_result {
|
||||
// TS3 error 0x0302 = `channel_already_in`: we're already
|
||||
// in the target channel, so this is a no-op success.
|
||||
// Rolling `in_channel` back to false would break PTT
|
||||
@@ -1593,7 +1690,9 @@ impl ChanoraSession {
|
||||
audio.stop();
|
||||
let _ = self.events_tx.send(SessionEvent::AudioStopped);
|
||||
}
|
||||
state.protocol.disconnect().await;
|
||||
if let Some(protocol) = self.take_protocol().await {
|
||||
protocol.disconnect().await;
|
||||
}
|
||||
// Wait for the supervisor to wind down so we don't race
|
||||
// a redial against the explicit disconnect.
|
||||
if let Some(handle) = state.supervisor.take() {
|
||||
@@ -1655,6 +1754,7 @@ async fn await_supervisor_shutdown(mut handle: JoinHandle<()>, timeout_duration:
|
||||
|
||||
struct SupervisorContext {
|
||||
state_arc: Arc<Mutex<Option<ConnectedState>>>,
|
||||
protocol: Arc<Mutex<Option<chanora_protocol::ProtocolClient>>>,
|
||||
events_tx: broadcast::Sender<SessionEvent>,
|
||||
initial_cfg: ConnectConfig,
|
||||
initial_lost_rx: oneshot::Receiver<chanora_protocol::DisconnectReason>,
|
||||
@@ -1789,6 +1889,7 @@ fn spawn_event_forwarders(
|
||||
async fn supervisor_loop(ctx: SupervisorContext) {
|
||||
let SupervisorContext {
|
||||
state_arc,
|
||||
protocol,
|
||||
events_tx,
|
||||
initial_cfg,
|
||||
initial_lost_rx,
|
||||
@@ -2058,20 +2159,21 @@ async fn supervisor_loop(ctx: SupervisorContext) {
|
||||
|
||||
// Reattach into the session state.
|
||||
let restart_audio = {
|
||||
let guard = state_arc.lock().await;
|
||||
if guard.is_none() {
|
||||
// Session was disposed mid-reconnect.
|
||||
return;
|
||||
}
|
||||
drop(guard);
|
||||
let old = protocol.lock().await.replace(new_client);
|
||||
drop(old);
|
||||
let mut guard = state_arc.lock().await;
|
||||
let state = match guard.as_mut() {
|
||||
Some(s) => s,
|
||||
None => {
|
||||
// Session was disposed mid-reconnect.
|
||||
return;
|
||||
}
|
||||
};
|
||||
// Replace the dead protocol client with the new one.
|
||||
// The old client's background task either already
|
||||
// exited (loss notifier fired) or will exit when
|
||||
// its request channel drops (watchdog path).
|
||||
let old = std::mem::replace(&mut state.protocol, new_client);
|
||||
drop(old);
|
||||
|
||||
let _ = channel_join::reduce(
|
||||
&mut state.join_state,
|
||||
@@ -2101,9 +2203,9 @@ async fn supervisor_loop(ctx: SupervisorContext) {
|
||||
});
|
||||
|
||||
{
|
||||
let guard = state_arc.lock().await;
|
||||
if let Some(state) = guard.as_ref() {
|
||||
spawn_event_forwarders(&state.protocol, &events_tx);
|
||||
let protocol = protocol.lock().await;
|
||||
if let Some(client) = protocol.as_ref() {
|
||||
spawn_event_forwarders(client, &events_tx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2115,8 +2217,15 @@ async fn supervisor_loop(ctx: SupervisorContext) {
|
||||
};
|
||||
let mut guard = state_arc.lock().await;
|
||||
if let Some(state) = guard.as_mut() {
|
||||
let voice_out = state.protocol.voice_out();
|
||||
if let Some(voice_in) = state.protocol.take_voice_in() {
|
||||
let (voice_out, voice_in) = {
|
||||
let protocol = protocol.lock().await;
|
||||
let client = match protocol.as_ref() {
|
||||
Some(client) => client,
|
||||
None => return,
|
||||
};
|
||||
(client.voice_out(), client.take_voice_in())
|
||||
};
|
||||
if let Some(voice_in) = voice_in {
|
||||
let gate = chanora_audio::AudioTransmitGate::new(
|
||||
audio_cfg.ptt_initial,
|
||||
);
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_avatar_returns_cached_bytes_without_connection() {
|
||||
let tmp = mktemp("chanora_core_avatar_cache_test");
|
||||
let session = chanora_core::ChanoraSession::new();
|
||||
session.init_cache(tmp.to_str().unwrap()).await.unwrap();
|
||||
|
||||
let cache = chanora_cache::BlobCache::new(&tmp, 100 * 1024 * 1024).unwrap();
|
||||
let hash = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6";
|
||||
cache
|
||||
.put(chanora_cache::PREFIX_AVATAR, hash, b"avatar-bytes")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let bytes = session
|
||||
.get_avatar(hash, "client-uid")
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(bytes, b"avatar-bytes");
|
||||
|
||||
let _ = std::fs::remove_dir_all(&tmp);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_avatar_without_cache_or_connection_returns_not_connected() {
|
||||
let session = chanora_core::ChanoraSession::new();
|
||||
let err = session
|
||||
.get_avatar("a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6", "client-uid")
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(err, chanora_core::CoreError::NotConnected));
|
||||
}
|
||||
|
||||
fn mktemp(label: &str) -> PathBuf {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
let p = env::temp_dir()
|
||||
.join(label)
|
||||
.join(format!("{}-{nanos}", process::id()));
|
||||
std::fs::create_dir_all(&p).unwrap();
|
||||
p
|
||||
}
|
||||
Reference in New Issue
Block a user