nautilus_common/
runner.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::{
17    cell::{OnceCell, RefCell},
18    collections::VecDeque,
19    rc::Rc,
20};
21
22use nautilus_model::data::Data;
23
24use crate::{
25    clock::Clock,
26    messages::data::{DataResponse, SubscribeCommand},
27    timer::TimeEvent,
28};
29
30pub trait DataQueue {
31    fn push(&mut self, event: DataEvent);
32}
33
34pub type GlobalDataQueue = Rc<RefCell<dyn DataQueue>>;
35
36// TODO: Refine this to reduce disparity between enum sizes
37#[allow(clippy::large_enum_variant)]
38pub enum DataEvent {
39    Response(DataResponse),
40    Data(Data),
41}
42
43pub struct SyncDataQueue(VecDeque<DataEvent>);
44
45impl DataQueue for SyncDataQueue {
46    fn push(&mut self, event: DataEvent) {
47        self.0.push_back(event);
48    }
49}
50
51#[must_use]
52pub fn get_data_queue() -> Rc<RefCell<dyn DataQueue>> {
53    DATA_QUEUE
54        .try_with(|dq| {
55            dq.get()
56                .expect("Data queue should be initialized by runner")
57                .clone()
58        })
59        .expect("Should be able to access thread local storage")
60}
61
62pub fn set_data_queue(dq: Rc<RefCell<dyn DataQueue>>) {
63    DATA_QUEUE
64        .try_with(|deque| {
65            assert!(deque.set(dq).is_ok(), "Global data queue already set");
66        })
67        .expect("Should be able to access thread local storage");
68}
69
70pub type GlobalClock = Rc<RefCell<dyn Clock>>;
71
72#[must_use]
73pub fn get_clock() -> Rc<RefCell<dyn Clock>> {
74    CLOCK
75        .try_with(|clock| {
76            clock
77                .get()
78                .expect("Clock should be initialized by runner")
79                .clone()
80        })
81        .expect("Should be able to access thread local storage")
82}
83
84pub fn set_clock(c: Rc<RefCell<dyn Clock>>) {
85    CLOCK
86        .try_with(|clock| {
87            assert!(clock.set(c).is_ok(), "Global clock already set");
88        })
89        .expect("Should be able to access thread local clock");
90}
91
92pub type MessageBusCommands = Rc<RefCell<VecDeque<SubscribeCommand>>>; // TODO: Use DataCommand?
93
94/// Get globally shared message bus command queue
95#[must_use]
96pub fn get_msgbus_cmd() -> MessageBusCommands {
97    MSGBUS_CMD
98        .try_with(std::clone::Clone::clone)
99        .expect("Should be able to access thread local storage")
100}
101
102thread_local! {
103    static CLOCK: OnceCell<GlobalClock> = OnceCell::new();
104    static DATA_QUEUE: OnceCell<GlobalDataQueue> = OnceCell::new();
105    static MSGBUS_CMD: MessageBusCommands = Rc::new(RefCell::new(VecDeque::new()));
106}
107
108pub trait SendResponse {
109    fn send(&self, resp: DataResponse);
110}
111
112pub type DataResponseQueue = Rc<RefCell<SyncDataQueue>>;
113
114// Represents different event types for the runner.
115#[allow(clippy::large_enum_variant)]
116pub enum RunnerEvent {
117    Data(DataEvent),
118    Timer(TimeEvent),
119}