mod api;
pub mod core;
pub mod database;
pub mod matching;
pub mod message;
pub mod mstr;
pub mod stubs;
pub mod switchboard;
pub mod typed_endpoints;
pub mod typed_handler;
pub mod typed_router;
use std::{any::Any, cell::RefCell, rc::Rc};
use nautilus_core::UUID4;
#[cfg(feature = "defi")]
use nautilus_model::defi::{Block, Pool, PoolFeeCollect, PoolFlash, PoolLiquidityUpdate, PoolSwap};
use nautilus_model::{
data::{
Bar, FundingRateUpdate, GreeksData, IndexPriceUpdate, MarkPriceUpdate, OrderBookDeltas,
OrderBookDepth10, QuoteTick, TradeTick,
option_chain::{OptionChainSlice, OptionGreeks},
},
events::{AccountState, OrderEventAny, PortfolioSnapshot, PositionEvent},
instruments::InstrumentAny,
orderbook::OrderBook,
};
use smallvec::SmallVec;
pub use self::{
api::*,
core::{MessageBus, Subscription},
message::BusMessage,
mstr::{Endpoint, MStr, Pattern, Topic},
switchboard::MessagingSwitchboard,
typed_endpoints::{EndpointMap, IntoEndpointMap},
typed_handler::{
CallbackHandler, Handler, IntoHandler, ShareableMessageHandler, TypedHandler,
TypedIntoHandler,
},
typed_router::{TopicRouter, TypedSubscription},
};
pub(super) const HANDLER_BUFFER_CAP: usize = 64;
thread_local! {
pub(super) static MESSAGE_BUS: RefCell<Option<Rc<RefCell<MessageBus>>>> = const { RefCell::new(None) };
pub(super) static ANY_HANDLERS: RefCell<SmallVec<[ShareableMessageHandler; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static DELTAS_HANDLERS: RefCell<SmallVec<[TypedHandler<OrderBookDeltas>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static DEPTH10_HANDLERS: RefCell<SmallVec<[TypedHandler<OrderBookDepth10>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static BOOK_HANDLERS: RefCell<SmallVec<[TypedHandler<OrderBook>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static QUOTE_HANDLERS: RefCell<SmallVec<[TypedHandler<QuoteTick>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static TRADE_HANDLERS: RefCell<SmallVec<[TypedHandler<TradeTick>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static BAR_HANDLERS: RefCell<SmallVec<[TypedHandler<Bar>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static MARK_PRICE_HANDLERS: RefCell<SmallVec<[TypedHandler<MarkPriceUpdate>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static INDEX_PRICE_HANDLERS: RefCell<SmallVec<[TypedHandler<IndexPriceUpdate>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static FUNDING_RATE_HANDLERS: RefCell<SmallVec<[TypedHandler<FundingRateUpdate>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static GREEKS_HANDLERS: RefCell<SmallVec<[TypedHandler<GreeksData>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static OPTION_GREEKS_HANDLERS: RefCell<SmallVec<[TypedHandler<OptionGreeks>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static OPTION_CHAIN_HANDLERS: RefCell<SmallVec<[TypedHandler<OptionChainSlice>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static ACCOUNT_STATE_HANDLERS: RefCell<SmallVec<[TypedHandler<AccountState>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static PORTFOLIO_SNAPSHOT_HANDLERS: RefCell<SmallVec<[TypedHandler<PortfolioSnapshot>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static ORDER_EVENT_HANDLERS: RefCell<SmallVec<[TypedHandler<OrderEventAny>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static POSITION_EVENT_HANDLERS: RefCell<SmallVec<[TypedHandler<PositionEvent>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
pub(super) static INSTRUMENT_HANDLERS: RefCell<SmallVec<[TypedHandler<InstrumentAny>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
#[cfg(feature = "defi")]
pub(super) static DEFI_BLOCK_HANDLERS: RefCell<SmallVec<[TypedHandler<Block>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
#[cfg(feature = "defi")]
pub(super) static DEFI_POOL_HANDLERS: RefCell<SmallVec<[TypedHandler<Pool>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
#[cfg(feature = "defi")]
pub(super) static DEFI_SWAP_HANDLERS: RefCell<SmallVec<[TypedHandler<PoolSwap>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
#[cfg(feature = "defi")]
pub(super) static DEFI_LIQUIDITY_HANDLERS: RefCell<SmallVec<[TypedHandler<PoolLiquidityUpdate>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
#[cfg(feature = "defi")]
pub(super) static DEFI_COLLECT_HANDLERS: RefCell<SmallVec<[TypedHandler<PoolFeeCollect>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
#[cfg(feature = "defi")]
pub(super) static DEFI_FLASH_HANDLERS: RefCell<SmallVec<[TypedHandler<PoolFlash>; HANDLER_BUFFER_CAP]>> =
RefCell::new(SmallVec::new());
}
pub fn set_message_bus(msgbus: Rc<RefCell<MessageBus>>) {
MESSAGE_BUS.with(|bus| {
*bus.borrow_mut() = Some(msgbus);
});
}
pub fn get_message_bus() -> Rc<RefCell<MessageBus>> {
MESSAGE_BUS.with(|bus| {
let mut slot = bus.borrow_mut();
let rc = slot.get_or_insert_with(|| Rc::new(RefCell::new(MessageBus::default())));
rc.clone()
})
}
pub trait BusTap: 'static {
fn on_publish(&self, topic: MStr<Topic>, message: &dyn Any);
fn on_send(&self, endpoint: MStr<Endpoint>, message: &dyn Any);
fn on_response(&self, _correlation_id: &UUID4, _message: &dyn Any) {}
}
thread_local! {
pub(super) static BUS_TAP: RefCell<Option<Rc<dyn BusTap>>> = const { RefCell::new(None) };
}
pub fn set_bus_tap(tap: Rc<dyn BusTap>) {
BUS_TAP.with(|slot| {
*slot.borrow_mut() = Some(tap);
});
}
pub fn clear_bus_tap() {
BUS_TAP.with(|slot| {
*slot.borrow_mut() = None;
});
}
#[inline]
pub(super) fn dispatch_tap_publish(topic: MStr<Topic>, message: &dyn Any) {
let tap = BUS_TAP.with(|slot| slot.borrow().clone());
if let Some(tap) = tap {
tap.on_publish(topic, message);
}
}
#[inline]
pub(super) fn dispatch_tap_send(endpoint: MStr<Endpoint>, message: &dyn Any) {
let tap = BUS_TAP.with(|slot| slot.borrow().clone());
if let Some(tap) = tap {
tap.on_send(endpoint, message);
}
}
#[inline]
pub(super) fn dispatch_tap_response(correlation_id: &UUID4, message: &dyn Any) {
let tap = BUS_TAP.with(|slot| slot.borrow().clone());
if let Some(tap) = tap {
tap.on_response(correlation_id, message);
}
}