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