actr_runtime/
context_factory.rs

1//! Context factory
2//!
3//! 负责创建 RuntimeContext 实例,注入 OutGate 和其他依赖。
4
5use crate::context::RuntimeContext;
6use crate::inbound::{DataStreamRegistry, MediaFrameRegistry};
7use crate::outbound::OutGate;
8use crate::transport::InprocTransportManager;
9use crate::wire::webrtc::SignalingClient;
10use actr_protocol::{AIdCredential, ActrId};
11use std::sync::Arc;
12
13/// Context factory
14///
15/// # 职责
16///
17/// - 创建 RuntimeContext 实例
18/// - 注入 OutGate(enum dispatch,零虚函数)
19/// - 管理默认配置
20#[derive(Clone)]
21pub struct ContextFactory {
22    /// 进程内通信 gate(本地调用)- 立即可用
23    pub(crate) inproc_gate: OutGate,
24
25    /// 跨进程通信 gate(远程调用)- 延迟初始化
26    pub(crate) outproc_gate: Option<OutGate>,
27
28    /// Shell → Workload 方向的传输管理器
29    pub(crate) shell_to_workload: Arc<InprocTransportManager>,
30
31    /// Workload → Shell 方向的传输管理器
32    pub(crate) workload_to_shell: Arc<InprocTransportManager>,
33
34    /// DataStream 回调注册表
35    pub(crate) data_stream_registry: Arc<DataStreamRegistry>,
36
37    /// MediaTrack 回调注册表
38    pub(crate) media_frame_registry: Arc<MediaFrameRegistry>,
39
40    /// Signaling client for discovery
41    pub(crate) signaling_client: Arc<dyn SignalingClient>,
42}
43
44impl ContextFactory {
45    /// 创建新的 ContextFactory
46    ///
47    /// # 参数
48    ///
49    /// - `inproc_gate`: 进程内通信 gate(Dest::Shell/Local)- 立即可用
50    /// - `shell_to_workload`: Shell → Workload 方向的传输管理器
51    /// - `workload_to_shell`: Workload → Shell 方向的传输管理器
52    /// - `data_stream_registry`: DataStream 回调注册表
53    /// - `media_frame_registry`: MediaTrack 回调注册表
54    ///
55    /// # 设计说明
56    ///
57    /// - **inproc_gate**: 在 ActrSystem::new() 时创建,立即可用(Shell/Local 通信不需要 ActorId)
58    /// - **outproc_gate**: 初始为 None,在 ActrNode::start() WebRTC 初始化完成后设置
59    /// - **双向 InprocTransportManager**: 确保 Shell 和 Workload 的 pending_requests 完全分离
60    /// - **data_stream_registry**: 管理 DataStream 回调,支持应用数据流传输
61    /// - **media_frame_registry**: 管理 MediaTrack 回调,支持 WebRTC 原生媒体流
62    pub fn new(
63        inproc_gate: OutGate,
64        shell_to_workload: Arc<InprocTransportManager>,
65        workload_to_shell: Arc<InprocTransportManager>,
66        data_stream_registry: Arc<DataStreamRegistry>,
67        media_frame_registry: Arc<MediaFrameRegistry>,
68        signaling_client: Arc<dyn SignalingClient>,
69    ) -> Self {
70        Self {
71            inproc_gate,
72            outproc_gate: None, // 延迟初始化,等待 WebRTC 就绪
73            shell_to_workload,
74            workload_to_shell,
75            data_stream_registry,
76            media_frame_registry,
77            signaling_client,
78        }
79    }
80
81    /// 设置跨进程通信 gate
82    ///
83    /// # 用途
84    ///
85    /// ActrNode::start() 完成 WebRTC 初始化后调用
86    pub fn set_outproc_gate(&mut self, gate: OutGate) {
87        tracing::debug!("🔄 Setting outproc OutGate in ContextFactory");
88        self.outproc_gate = Some(gate);
89    }
90
91    /// 获取 Shell → Workload 方向的传输管理器
92    pub fn shell_to_workload(&self) -> Arc<InprocTransportManager> {
93        self.shell_to_workload.clone()
94    }
95
96    /// 获取 Workload → Shell 方向的传输管理器
97    pub fn workload_to_shell(&self) -> Arc<InprocTransportManager> {
98        self.workload_to_shell.clone()
99    }
100
101    /// 创建 Context(用于消息处理)
102    ///
103    /// # 参数
104    ///
105    /// - `self_id`: 当前 Actor ID
106    /// - `caller_id`: 调用方 Actor ID(可选)
107    /// - `request_id`: 请求唯一 ID
108    ///
109    /// # 返回
110    ///
111    /// 返回 RuntimeContext 实例(实现了 Context trait)
112    pub fn create(
113        &self,
114        self_id: &ActrId,
115        caller_id: Option<&ActrId>,
116        request_id: &str,
117        credential: &AIdCredential,
118    ) -> RuntimeContext {
119        RuntimeContext::new(
120            self_id.clone(),
121            caller_id.cloned(),
122            request_id.to_string(),
123            self.inproc_gate.clone(), // Clone OutGate enum(Arc 内部,开销低)
124            self.outproc_gate.clone(), // Clone Option<OutGate>
125            self.data_stream_registry.clone(), // Clone Arc<DataStreamRegistry>
126            self.media_frame_registry.clone(), // Clone Arc<MediaFrameRegistry>
127            self.signaling_client.clone(),
128            credential.clone(),
129        )
130    }
131
132    /// 创建引导 Context(用于生命周期钩子)
133    ///
134    /// # 用途
135    ///
136    /// 用于 on_start/on_stop 钩子,无 caller_id
137    pub fn create_bootstrap(&self, self_id: &ActrId, credential: &AIdCredential) -> RuntimeContext {
138        RuntimeContext::new(
139            self_id.clone(),
140            None,
141            uuid::Uuid::new_v4().to_string(),
142            self.inproc_gate.clone(),
143            self.outproc_gate.clone(),
144            self.data_stream_registry.clone(),
145            self.media_frame_registry.clone(),
146            self.signaling_client.clone(),
147            credential.clone(),
148        )
149    }
150}