#![cfg(not(target_arch = "wasm32"))]
use crate::client::EndpointHandle;
use crate::connection_manager::{ConnectionManager, ConnectionRecord};
use crate::runtime_manager::RuntimeManager;
use anyhow::Result;
use iroh::Endpoint;
use std::sync::Arc;
#[derive(Clone)]
pub struct IrohNodeManagerAdapter {
runtime_manager: Arc<RuntimeManager>,
}
impl IrohNodeManagerAdapter {
pub fn new(runtime_manager: Arc<RuntimeManager>) -> Self {
Self { runtime_manager }
}
pub async fn create(&self, secret_key: Option<Vec<u8>>) -> Result<String> {
self.runtime_manager.init_runtime(secret_key).await
}
pub async fn adopt_endpoint(&self, endpoint: Endpoint) -> Result<String> {
self.runtime_manager.adopt_endpoint(endpoint).await
}
pub async fn get_endpoint(&self) -> Result<Endpoint> {
self.runtime_manager.get_endpoint().await
}
pub async fn get_node_id(&self) -> Option<String> {
self.runtime_manager.current_node_id().await
}
pub async fn export_endpoint_handle(&self) -> Result<EndpointHandle> {
self.runtime_manager.export_endpoint_handle().await
}
}
#[derive(Clone, Default)]
pub struct ConnectionRepositoryAdapter {
manager: Arc<ConnectionManager>,
}
impl ConnectionRepositoryAdapter {
pub fn new(manager: Arc<ConnectionManager>) -> Self {
Self { manager }
}
pub fn manager(&self) -> Arc<ConnectionManager> {
self.manager.clone()
}
pub async fn upsert_pending(
&self,
connection_id: String,
node_id: Option<String>,
device_id: Option<String>,
endpoint_id: Option<String>,
) -> ConnectionRecord {
self.manager
.upsert_pending(connection_id, node_id, device_id, endpoint_id)
.await
}
pub async fn mark_connected(
&self,
connection_id: &str,
endpoint_id: Option<String>,
) -> Option<ConnectionRecord> {
self.manager.set_connected(connection_id, endpoint_id).await
}
pub async fn mark_disconnected(&self, connection_id: &str, reason: Option<String>) {
self.manager.set_closed(connection_id, reason).await;
}
pub async fn get_by_connection_id(&self, connection_id: &str) -> Option<ConnectionRecord> {
self.manager.get_by_connection_id(connection_id).await
}
pub async fn get_by_node_id(&self, node_id: &str) -> Vec<ConnectionRecord> {
self.manager.get_by_node_id(node_id).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn connection_repository_adapter_matches_manager_index_queries() {
let manager = Arc::new(ConnectionManager::new());
let adapter = ConnectionRepositoryAdapter::new(manager.clone());
adapter
.upsert_pending(
"conn-42".to_string(),
Some("node-42".to_string()),
Some("device-42".to_string()),
None,
)
.await;
adapter
.mark_connected("conn-42", Some("endpoint-42".to_string()))
.await;
let from_adapter = adapter.get_by_node_id("node-42").await;
let from_manager = manager.get_by_node_id("node-42").await;
assert_eq!(from_adapter.len(), from_manager.len());
assert_eq!(from_adapter[0].connection_id, "conn-42");
adapter
.mark_disconnected("conn-42", Some("done".to_string()))
.await;
assert_eq!(manager.list_active().await.len(), 0);
}
}