robotrt-middleware-core 0.1.0-beta.1

RobotRT modular robotics runtime and middleware components.
Documentation
use std::any::Any;
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};

mod service;
mod topic;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TopicLoadEntry {
    pub topic: String,
    pub pending: usize,
    pub max_depth: usize,
}

impl TopicLoadEntry {
    pub fn utilization_ratio(&self) -> f64 {
        if self.max_depth == 0 {
            return 0.0;
        }
        self.pending as f64 / self.max_depth as f64
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ServiceLoadEntry {
    pub service: String,
    pub pending_requests: usize,
    pub pending_responses: usize,
}

// ─── Topic Bus ────────────────────────────────────────────────────────────────

/// Shared, type-erased FIFO slot that connects a [`BasicPublisher`] to one or
/// more [`BasicSubscriber`]s on the same topic within a single process.
pub struct TopicSlot {
    queue: Mutex<VecDeque<Box<dyn Any + Send>>>,
    max_depth: usize,
}

/// In-process topic bus: maps fully-resolved topic names to shared slots.
pub struct TopicBus {
    slots: HashMap<String, Arc<TopicSlot>>,
    default_depth: usize,
}

impl Default for TopicBus {
    fn default() -> Self {
        Self {
            slots: HashMap::new(),
            default_depth: 16,
        }
    }
}

// ─── Service Bus ──────────────────────────────────────────────────────────────

/// Shared request/response channel between a [`BasicServiceClient`] and a
/// [`BasicServiceServer`] for the same service name within a single process.
pub struct ServiceChannel {
    requests: Mutex<VecDeque<(u64, Box<dyn Any + Send>)>>,
    responses: Mutex<HashMap<u64, Box<dyn Any + Send>>>,
}

impl Default for ServiceChannel {
    fn default() -> Self {
        Self {
            requests: Mutex::new(VecDeque::new()),
            responses: Mutex::new(HashMap::new()),
        }
    }
}

/// In-process service bus: maps service names to shared channels.
#[derive(Default)]
pub struct ServiceBus {
    channels: HashMap<String, Arc<ServiceChannel>>,
}