logic_mesh/base/engine/mod.rs
1// Copyright (c) 2022-2023, Radu Racariu.
2
3//!
4//! Defines the block execution engine
5//!
6
7use anyhow::Result;
8
9use super::{
10 block::Block,
11 program::data::{BlockData, LinkData},
12};
13
14pub mod messages;
15
16/// Specifies the interface for an engine
17/// that implements the block execution logic.
18pub trait Engine {
19 /// The transmission type of the blocks
20 type Writer;
21 /// The reception type of the blocks
22 type Reader;
23
24 /// The type used to send messages to/from this engine.
25 type Channel: Send + Sync + Clone;
26
27 /// Schedule a block to be executed by this engine.
28 /// This operation can be performed while the engine is running.
29 fn schedule<B: Block<Writer = Self::Writer, Reader = Self::Reader> + 'static>(
30 &mut self,
31 block: B,
32 );
33
34 /// Load the blocks and links into the engine.
35 /// This operation should be performed before the engine is run.
36 fn load_blocks_and_links(&mut self, blocks: &[BlockData], links: &[LinkData]) -> Result<()>;
37
38 /// Runs the event loop of this engine
39 /// an execute the blocks that where scheduled
40 #[allow(async_fn_in_trait)]
41 async fn run(&mut self);
42
43 /// Get a handle to this engines messaging system so external
44 /// systems can communicate with this engine once the engine will run.
45 ///
46 /// # Arguments
47 /// - sender_id The sender unique id.
48 /// - sender_channel The sender chanel to send notifications from the engine.
49 ///
50 /// # Returns
51 /// A sender chanel that is used to send messages to the engine.
52 ///
53 fn create_message_channel(
54 &mut self,
55 sender_id: uuid::Uuid,
56 sender_channel: Self::Channel,
57 ) -> Self::Channel;
58}