mecha10_core/context/mod.rs
1//! Node execution context
2//!
3//! Provides a high-level API for nodes to interact with the framework,
4//! including type-safe pub/sub, configuration access, and lifecycle management.
5//!
6//! This module has been refactored into smaller sub-modules for better organization:
7//! - `infra_config`: Infrastructure configuration types
8//! - `receiver`: Message receiver wrapper
9//! - `builder`: Builder for constructing a `Context`
10//! - `lifecycle`: Construction, instance naming, shutdown, logging span
11//! - `config`: Infrastructure configuration loading (Redis URLs, control plane)
12//! - `node_config`: Node-level configuration loading with environment fallback
13//! - `pubsub`: Type-safe and raw publish/subscribe methods
14//! - `aggregation`: Pattern-based topic discovery and aggregated subscriptions
15//! - `topics`: Convenience helpers for well-known topic categories
16//! - `state_mgmt`: Access to the per-node persistent state manager
17
18mod aggregation;
19mod builder;
20mod config;
21mod infra_config;
22mod lifecycle;
23mod node_config;
24mod pubsub;
25mod receiver;
26mod state_mgmt;
27mod topics;
28
29pub use builder::ContextBuilder;
30pub use infra_config::{BridgeConfig, InfrastructureConfig, MinioConfig, MlflowConfig, PostgresConfig, RedisConfig};
31pub use receiver::Receiver;
32
33use std::sync::Arc;
34use tokio::sync::RwLock;
35
36pub struct Context {
37 /// Node identifier (includes instance if set: "node_name" or "node_name/instance")
38 node_id: String,
39
40 /// Optional instance name
41 instance: Option<String>,
42
43 /// Local message bus for on-robot communication (wrapped in Arc for sharing across tasks)
44 #[cfg(feature = "messaging")]
45 message_bus: Arc<RwLock<mecha10_messaging::MessageBus>>,
46
47 /// Control plane message bus for robot-to-cloud communication (optional)
48 #[cfg(feature = "messaging")]
49 control_plane_bus: Arc<RwLock<Option<mecha10_messaging::MessageBus>>>,
50
51 /// Redis bridge for forwarding messages between local and control plane
52 #[cfg(feature = "messaging")]
53 redis_bridge: Arc<RwLock<Option<crate::redis_bridge::RedisBridge>>>,
54
55 /// Shutdown signal
56 shutdown: Arc<RwLock<bool>>,
57
58 /// State manager for persistent node state (lazy initialized)
59 state_manager: Arc<RwLock<Option<Arc<crate::state::ConcreteStateManager>>>>,
60}
61impl Clone for Context {
62 fn clone(&self) -> Self {
63 Self {
64 node_id: self.node_id.clone(),
65 instance: self.instance.clone(),
66 #[cfg(feature = "messaging")]
67 message_bus: Arc::clone(&self.message_bus),
68 #[cfg(feature = "messaging")]
69 control_plane_bus: Arc::clone(&self.control_plane_bus),
70 #[cfg(feature = "messaging")]
71 redis_bridge: Arc::clone(&self.redis_bridge),
72 shutdown: Arc::clone(&self.shutdown),
73 state_manager: Arc::clone(&self.state_manager),
74 }
75 }
76}