use affinidi_tdk_common::TDKSharedState;
use config::ATMConfig;
use delete_handler::DeletionHandlerCommands;
use errors::ATMError;
use profiles::Profiles;
use std::sync::Arc;
use tokio::sync::{
Mutex, RwLock, broadcast,
mpsc::{self, Receiver, Sender},
};
use tracing::debug;
use transports::websockets::WebSocketResponses;
pub mod config;
pub mod delete_handler;
pub mod errors;
pub mod messages;
pub mod profiles;
pub mod protocols;
pub mod public;
pub mod transports;
#[derive(Clone)]
pub struct ATM {
pub(crate) inner: Arc<SharedState>,
}
pub(crate) struct SharedState {
pub(crate) config: ATMConfig,
pub(crate) tdk_common: TDKSharedState,
pub(crate) profiles: Arc<RwLock<Profiles>>,
pub(crate) deletion_handler_send_stream: Sender<delete_handler::DeletionHandlerCommands>, pub(crate) deletion_handler_recv_stream:
Mutex<Receiver<delete_handler::DeletionHandlerCommands>>, }
impl ATM {
pub async fn new(config: ATMConfig, tdk_common: TDKSharedState) -> Result<ATM, ATMError> {
let (sdk_deletion_tx, deletion_sdk_rx) = mpsc::channel::<DeletionHandlerCommands>(32);
let (deletion_sdk_tx, sdk_deletion_rx) = mpsc::channel::<DeletionHandlerCommands>(32);
let shared_state = SharedState {
config: config.clone(),
tdk_common,
profiles: Arc::new(RwLock::new(Profiles::default())),
deletion_handler_send_stream: sdk_deletion_tx,
deletion_handler_recv_stream: Mutex::new(sdk_deletion_rx),
};
let atm = ATM {
inner: Arc::new(shared_state),
};
atm.start_deletion_handler(deletion_sdk_rx, deletion_sdk_tx)
.await?;
debug!("ATM SDK initialized");
Ok(atm)
}
pub async fn graceful_shutdown(&self) {
debug!("Shutting down ATM SDK");
let _ = self.abort_deletion_handler().await;
{
let mut guard = self.inner.deletion_handler_recv_stream.lock().await;
let _ = guard.recv().await;
debug!("Deletion Handler stopped");
}
{
let profiles = &*self.inner.profiles.read().await;
for (_, profile) in profiles.0.iter() {
let _ = profile.stop_websocket().await;
}
}
}
pub fn get_inbound_channel(&self) -> Option<broadcast::Receiver<WebSocketResponses>> {
self.inner
.config
.inbound_message_channel
.as_ref()
.map(|sender| sender.subscribe())
}
pub fn get_tdk(&self) -> &TDKSharedState {
&self.inner.tdk_common
}
}