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 crate::{
23    clock::Clock,
24    messages::data::{DataEvent, DataResponse, SubscriptionCommand},
25    timer::TimeEvent,
26};
27
28pub trait DataQueue {
29    fn push(&mut self, event: DataEvent);
30}
31
32pub type GlobalDataQueue = Rc<RefCell<dyn DataQueue>>;
33
34pub struct SyncDataQueue(VecDeque<DataEvent>);
35
36impl DataQueue for SyncDataQueue {
37    fn push(&mut self, event: DataEvent) {
38        self.0.push_back(event);
39    }
40}
41
42#[must_use]
43pub fn get_data_queue() -> Rc<RefCell<dyn DataQueue>> {
44    DATA_QUEUE
45        .try_with(|dq| {
46            dq.get()
47                .expect("Data queue should be initialized by runner")
48                .clone()
49        })
50        .expect("Should be able to access thread local storage")
51}
52
53pub fn set_data_queue(dq: Rc<RefCell<dyn DataQueue>>) {
54    DATA_QUEUE
55        .try_with(|deque| {
56            assert!(deque.set(dq).is_ok(), "Global data queue already set");
57        })
58        .expect("Should be able to access thread local storage");
59}
60
61pub type GlobalClock = Rc<RefCell<dyn Clock>>;
62
63#[must_use]
64pub fn get_clock() -> Rc<RefCell<dyn Clock>> {
65    CLOCK
66        .try_with(|clock| {
67            clock
68                .get()
69                .expect("Clock should be initialized by runner")
70                .clone()
71        })
72        .expect("Should be able to access thread local storage")
73}
74
75pub fn set_clock(c: Rc<RefCell<dyn Clock>>) {
76    CLOCK
77        .try_with(|clock| {
78            assert!(clock.set(c).is_ok(), "Global clock already set");
79        })
80        .expect("Should be able to access thread local clock");
81}
82
83pub type MessageBusCommands = Rc<RefCell<VecDeque<SubscriptionCommand>>>;
84
85/// Get globally shared message bus command queue
86#[must_use]
87pub fn get_msgbus_cmd() -> MessageBusCommands {
88    MSGBUS_CMD
89        .try_with(std::clone::Clone::clone)
90        .expect("Should be able to access thread local storage")
91}
92
93thread_local! {
94    static CLOCK: OnceCell<GlobalClock> = OnceCell::new();
95    static DATA_QUEUE: OnceCell<GlobalDataQueue> = OnceCell::new();
96    static MSGBUS_CMD: MessageBusCommands = Rc::new(RefCell::new(VecDeque::new()));
97}
98
99pub trait SendResponse {
100    fn send(&self, resp: DataResponse);
101}
102
103pub type DataResponseQueue = Rc<RefCell<SyncDataQueue>>;
104
105// Represents different event types for the runner.
106#[allow(clippy::large_enum_variant)]
107pub enum RunnerEvent {
108    Data(DataEvent),
109    Timer(TimeEvent),
110}