Skip to main content

stdiobus_core/
backend.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026-present Raman Marozau <raman@worktif.com>
3// Copyright (c) 2026-present stdiobus contributors
4
5//! Backend trait for stdio_bus implementations
6
7use crate::{BusState, BusStats, Result};
8use async_trait::async_trait;
9use tokio::sync::mpsc;
10
11/// Message received from the bus
12#[derive(Debug, Clone)]
13pub struct BusMessage {
14    pub json: String,
15}
16
17/// Backend trait that all implementations must satisfy
18#[async_trait]
19pub trait Backend: Send + Sync {
20    /// Start the backend
21    async fn start(&self) -> Result<()>;
22
23    /// Stop the backend gracefully
24    async fn stop(&self, timeout_secs: u32) -> Result<()>;
25
26    /// Send a message to workers
27    async fn send(&self, message: &str) -> Result<()>;
28
29    /// Get current state
30    fn state(&self) -> BusState;
31
32    /// Get statistics
33    fn stats(&self) -> BusStats;
34
35    /// Get number of running workers (-1 if unknown)
36    fn worker_count(&self) -> i32;
37
38    /// Get number of connected clients (-1 if unknown)
39    fn client_count(&self) -> i32;
40
41    /// Subscribe to incoming messages.
42    ///
43    /// Returns `Some(Receiver)` on the first call. Subsequent calls return `None`
44    /// because the receiver can only have one owner. To share messages across
45    /// multiple consumers, use a broadcast channel on top of the returned receiver.
46    fn subscribe(&self) -> Option<mpsc::Receiver<BusMessage>>;
47
48    /// Get backend type name
49    fn backend_type(&self) -> &'static str;
50}