Skip to main content

middleware_core/stack/
channels.rs

1use std::sync::Arc;
2
3use action_core::client_server::ActionChannel;
4
5use crate::bus::{ServiceChannel, TopicSlot};
6use crate::serialization::SerializationManager;
7
8use super::{MiddlewareLoadSnapshot, MiddlewareStack};
9
10impl MiddlewareStack {
11    /// Get-or-create the in-process [`TopicSlot`] for `topic`.
12    /// `depth` is used only when the slot is first created.
13    pub fn topic_slot(&mut self, topic: &str, depth: usize) -> Arc<TopicSlot> {
14        self.topic_bus.get_or_create(topic, depth)
15    }
16
17    /// Get-or-create the in-process [`ServiceChannel`] for `service`.
18    pub fn service_channel(&mut self, service: &str) -> Arc<ServiceChannel> {
19        self.service_bus.get_or_create(service)
20    }
21
22    /// Get-or-create the in-process [`ActionChannel`] for `action`.
23    ///
24    /// Both client and server must call this with the same `G/F/R` types;
25    /// if the types don't match the channel stored under `action` the method
26    /// panics (same process, so a type mismatch is always a programming error).
27    pub fn action_channel<G, F, R>(&mut self, action: &str) -> Arc<ActionChannel<G, F, R>>
28    where
29        G: Send + Sync + 'static,
30        F: Send + Sync + 'static,
31        R: Send + Sync + 'static,
32    {
33        let entry = self
34            .action_channels
35            .entry(action.to_string())
36            .or_insert_with(|| ActionChannel::<G, F, R>::new());
37        entry
38            .clone()
39            .downcast::<ActionChannel<G, F, R>>()
40            .expect("ActionChannel type mismatch for action")
41    }
42
43    pub fn load_snapshot(&self) -> MiddlewareLoadSnapshot {
44        MiddlewareLoadSnapshot {
45            topics: self.topic_bus.load_entries(),
46            services: self.service_bus.load_entries(),
47            session_lifecycle: self.session_manager.lifecycle_counts(),
48            discovery: self.discovery.snapshot(),
49        }
50    }
51
52    pub fn serialization(&self) -> &dyn SerializationManager {
53        &self.serialization
54    }
55}