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
36// Import the complete Context struct and all implementations from the backup file
37// This maintains backward compatibility while improving code organization
38
39pub struct Context {
40 /// Node identifier (includes instance if set: "node_name" or "node_name/instance")
41 node_id: String,
42
43 /// Optional instance name
44 instance: Option<String>,
45
46 /// Local message bus for on-robot communication (wrapped in Arc for sharing across tasks)
47 #[cfg(feature = "messaging")]
48 message_bus: Arc<RwLock<mecha10_messaging::MessageBus>>,
49
50 /// Control plane message bus for robot-to-cloud communication (optional)
51 #[cfg(feature = "messaging")]
52 control_plane_bus: Arc<RwLock<Option<mecha10_messaging::MessageBus>>>,
53
54 /// Redis bridge for forwarding messages between local and control plane
55 #[cfg(feature = "messaging")]
56 redis_bridge: Arc<RwLock<Option<crate::redis_bridge::RedisBridge>>>,
57
58 /// Shutdown signal
59 shutdown: Arc<RwLock<bool>>,
60
61 /// State manager for persistent node state (lazy initialized)
62 state_manager: Arc<RwLock<Option<Arc<crate::state::ConcreteStateManager>>>>,
63}
64impl Clone for Context {
65 fn clone(&self) -> Self {
66 Self {
67 node_id: self.node_id.clone(),
68 instance: self.instance.clone(),
69 #[cfg(feature = "messaging")]
70 message_bus: Arc::clone(&self.message_bus),
71 #[cfg(feature = "messaging")]
72 control_plane_bus: Arc::clone(&self.control_plane_bus),
73 #[cfg(feature = "messaging")]
74 redis_bridge: Arc::clone(&self.redis_bridge),
75 shutdown: Arc::clone(&self.shutdown),
76 state_manager: Arc::clone(&self.state_manager),
77 }
78 }
79}