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::{cell::RefCell, rc::Rc};
#[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, PositionEvent},
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 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());
#[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()
})
}