use crate::{
errors::FixerError,
fix_string::FIXString,
message::Message,
session::{AdminEnum, QueueForSend, session_id::SessionID},
tag::{TAG_BEGIN_STRING, TAG_SENDER_COMP_ID, TAG_TARGET_COMP_ID},
};
use dashmap::DashMap;
use simple_error::{SimpleError, SimpleResult};
use std::sync::Arc;
use std::sync::LazyLock;
use std::sync::Mutex as StdMutex;
use tokio::sync::mpsc::UnboundedSender;
#[derive(Clone)]
pub struct SessionRegistration {
pub admin_tx: UnboundedSender<AdminEnum>,
pub target_default_appl_ver_id: Arc<StdMutex<String>>,
pub in_chan_capacity: usize,
}
pub static SESSIONS: LazyLock<DashMap<Arc<SessionID>, SessionRegistration>> =
LazyLock::new(DashMap::new);
pub static ERR_DUPLICATE_SESSION_ID: LazyLock<SimpleError> =
LazyLock::new(|| simple_error!("Duplicate SessionID"));
pub static ERR_UNKNOWN_SESSION: LazyLock<SimpleError> =
LazyLock::new(|| simple_error!("Unknown session"));
pub trait Messageable: Send + Sync {
fn to_message(&self) -> &Message;
}
pub async fn send(message: &dyn Messageable) -> Result<(), FixerError> {
let msg = message.to_message();
let mut begin_string = FIXString::new();
msg.header.get_field(TAG_BEGIN_STRING, &mut begin_string)?;
let mut target_comp_id = FIXString::new();
msg.header
.get_field(TAG_TARGET_COMP_ID, &mut target_comp_id)?;
let mut sender_comp_id = FIXString::new();
msg.header
.get_field(TAG_SENDER_COMP_ID, &mut sender_comp_id)?;
let session_id = SessionID {
begin_string,
target_comp_id,
sender_comp_id,
..Default::default()
};
send_to_target(message, &Arc::new(session_id)).await
}
pub async fn send_owned(message: Message) -> Result<(), FixerError> {
let mut begin_string = FIXString::new();
message
.header
.get_field(TAG_BEGIN_STRING, &mut begin_string)?;
let mut target_comp_id = FIXString::new();
message
.header
.get_field(TAG_TARGET_COMP_ID, &mut target_comp_id)?;
let mut sender_comp_id = FIXString::new();
message
.header
.get_field(TAG_SENDER_COMP_ID, &mut sender_comp_id)?;
let session_id = SessionID {
begin_string,
target_comp_id,
sender_comp_id,
..Default::default()
};
send_to_target_owned(message, &Arc::new(session_id)).await
}
pub async fn send_to_target(
message: &dyn Messageable,
session_id: &Arc<SessionID>,
) -> Result<(), FixerError> {
let msg = message.to_message();
send_to_target_owned(msg.clone(), session_id).await
}
pub async fn send_to_target_owned(
message: Message,
session_id: &Arc<SessionID>,
) -> Result<(), FixerError> {
let registration = (*SESSIONS)
.get(session_id)
.ok_or_else(|| ERR_UNKNOWN_SESSION.clone())?;
let (result_tx, result_rx) = tokio::sync::oneshot::channel();
registration
.admin_tx
.send(AdminEnum::QueueForSend(QueueForSend {
msg: message,
result: result_tx,
}))
.map_err(|_| FixerError::from(ERR_UNKNOWN_SESSION.clone()))?;
result_rx
.await
.map_err(|_| FixerError::from(ERR_UNKNOWN_SESSION.clone()))?
}
pub fn unregister_session(session_id: &Arc<SessionID>) -> SimpleResult<()> {
if (*SESSIONS).contains_key(session_id) {
(*SESSIONS).remove(session_id);
return Ok(());
}
Err(ERR_UNKNOWN_SESSION.clone())
}
pub fn register_session(
session_id: Arc<SessionID>,
registration: SessionRegistration,
) -> SimpleResult<()> {
if (*SESSIONS).contains_key(&session_id) {
return Err(ERR_DUPLICATE_SESSION_ID.clone());
}
(*SESSIONS).insert(session_id, registration);
Ok(())
}
pub fn lookup_admin_tx(session_id: &Arc<SessionID>) -> Option<UnboundedSender<AdminEnum>> {
let reg = (*SESSIONS).get(session_id)?;
Some(reg.admin_tx.clone())
}
pub fn lookup_session_registration(
session_id: &Arc<SessionID>,
) -> Option<SessionRegistration> {
let reg = (*SESSIONS).get(session_id)?;
Some(reg.clone())
}
pub fn lookup_session_registration_flexible(
session_id: &Arc<SessionID>,
) -> Option<(Arc<SessionID>, SessionRegistration)> {
if let Some(reg) = (*SESSIONS).get(session_id) {
return Some((session_id.clone(), reg.clone()));
}
if session_id.qualifier.is_empty() {
for entry in &(*SESSIONS) {
let candidate = entry.key();
if candidate.begin_string == session_id.begin_string
&& candidate.sender_comp_id == session_id.sender_comp_id
&& candidate.target_comp_id == session_id.target_comp_id
&& candidate.sender_sub_id == session_id.sender_sub_id
&& candidate.sender_location_id == session_id.sender_location_id
&& candidate.target_sub_id == session_id.target_sub_id
&& candidate.target_location_id == session_id.target_location_id
{
return Some((candidate.clone(), entry.value().clone()));
}
}
}
None
}