acme_engine/
engine.rs

1/*
2    appellation: engine <module>
3    authors: @FL03
4*/
5use crate::pipes::PipeRouter;
6use crate::scheduler::Scheduler;
7use crate::sources::SourceManager;
8use std::sync::Arc;
9#[cfg(not(feature = "tokio"))]
10use std::sync::RwLock;
11#[cfg(feature = "tokio")]
12use tokio::sync::RwLock;
13
14/// The [`Engine`] implementation focuses on aggregating information from various sources,
15/// synchronizing their content, and providing a unified, extensible interface for data.
16#[derive(Clone, Debug, Default)]
17#[repr(C)]
18pub struct Engine {
19    pub(crate) controller: SourceManager,
20    pub(crate) scheduler: Scheduler,
21    pub(crate) pipe_router: Arc<RwLock<PipeRouter>>,
22}
23
24impl Engine {
25    pub fn new() -> crate::Result<Self> {
26        let pipe_router = PipeRouter::new();
27
28        let controller = SourceManager::from_router(pipe_router.clone());
29        let scheduler = Scheduler::new();
30        let pipe_router = Arc::new(RwLock::new(pipe_router));
31        Ok(Self {
32            controller,
33            scheduler,
34            pipe_router,
35        })
36    }
37    /// returns an immutable reference to the current [`SourceManager`].
38    pub const fn controller(&self) -> &SourceManager {
39        &self.controller
40    }
41    /// returns a mutable reference to the current [`SourceManager`].
42    pub const fn controller_mut(&mut self) -> &mut SourceManager {
43        &mut self.controller
44    }
45    /// returns a reference to the [`PipeRouter`]
46    pub const fn pipe_router(&self) -> &Arc<RwLock<PipeRouter>> {
47        &self.pipe_router
48    }
49    /// returns a mutable reference to the [`PipeRouter`]
50    pub fn pipe_router_mut(&mut self) -> Arc<RwLock<PipeRouter>> {
51        Arc::clone(&self.pipe_router)
52    }
53    /// returns a reference to the [`Scheduler`]
54    pub const fn scheduler(&self) -> &Scheduler {
55        &self.scheduler
56    }
57    /// returns a mutable reference to the [`Scheduler`]
58    pub const fn scheduler_mut(&mut self) -> &mut Scheduler {
59        &mut self.scheduler
60    }
61    /// starts the engine
62    #[cfg_attr(feature = "tracing", tracing::instrument(skip_all, level = "trace"))]
63    pub async fn run(&self) -> crate::Result<()> {
64        self.scheduler().start(self.controller().clone()).await;
65        Ok(())
66    }
67}