robotrt-middleware-core 0.1.0-beta.2

RobotRT modular robotics runtime and middleware components.
Documentation
use std::sync::Arc;

use action_core::client_server::ActionChannel;

use crate::bus::{ServiceChannel, TopicSlot};
use crate::serialization::SerializationManager;

use super::{MiddlewareLoadSnapshot, MiddlewareStack};

impl MiddlewareStack {
    /// Get-or-create the in-process [`TopicSlot`] for `topic`.
    /// `depth` is used only when the slot is first created.
    pub fn topic_slot(&mut self, topic: &str, depth: usize) -> Arc<TopicSlot> {
        self.topic_bus.get_or_create(topic, depth)
    }

    /// Get-or-create the in-process [`ServiceChannel`] for `service`.
    pub fn service_channel(&mut self, service: &str) -> Arc<ServiceChannel> {
        self.service_bus.get_or_create(service)
    }

    /// Get-or-create the in-process [`ActionChannel`] for `action`.
    ///
    /// Both client and server must call this with the same `G/F/R` types;
    /// if the types don't match the channel stored under `action` the method
    /// panics (same process, so a type mismatch is always a programming error).
    pub fn action_channel<G, F, R>(&mut self, action: &str) -> Arc<ActionChannel<G, F, R>>
    where
        G: Send + Sync + 'static,
        F: Send + Sync + 'static,
        R: Send + Sync + 'static,
    {
        let entry = self
            .action_channels
            .entry(action.to_string())
            .or_insert_with(|| ActionChannel::<G, F, R>::new());
        entry
            .clone()
            .downcast::<ActionChannel<G, F, R>>()
            .expect("ActionChannel type mismatch for action")
    }

    pub fn load_snapshot(&self) -> MiddlewareLoadSnapshot {
        MiddlewareLoadSnapshot {
            topics: self.topic_bus.load_entries(),
            services: self.service_bus.load_entries(),
            session_lifecycle: self.session_manager.lifecycle_counts(),
            discovery: self.discovery.snapshot(),
        }
    }

    pub fn serialization(&self) -> &dyn SerializationManager {
        &self.serialization
    }
}