Add terminal service implementation and integration tests
- Introduced terminal.h header file defining the terminal service API. - Implemented integration tests for terminal service registration and health checks. - Added unit tests for FFI layer, health checks, and terminal service functionality. - Created a mock center for testing terminal registration and deregistration. - Ensured proper error handling and response validation in tests.
This commit is contained in:
@@ -0,0 +1,548 @@
|
||||
# FlySim 控制系统架构设计
|
||||
|
||||
本文档基于 `proto/control` 中的协议定义、`terminal/src` 中的当前实现,以及调度服务和终端聚合路由服务的目标需求,描述系统静态边界、运行时交互及主要类型关系。图中 Terminal 和 Registry 已有代码实现;Dispatcher 和终端聚合路由子中心目前为目标架构。Registry 的配置、持久化与健康检查契约见 [registry/README.md](registry/README.md)。
|
||||
|
||||
## 1. C4 架构图
|
||||
|
||||
### 1.1 系统上下文
|
||||
|
||||
```mermaid
|
||||
|
||||
C4Context
|
||||
title FlySim 控制系统上下文
|
||||
|
||||
Person(operator, "控制操作员", "通过控制系统管理仿真终端和场景")
|
||||
System_Ext(controlConsole, "控制系统客户端", "提交仿真任务、直接控制逻辑终端并监视状态")
|
||||
System_Ext(simulator, "仿真程序", "实现具体仿真逻辑和终端回调")
|
||||
|
||||
System_Boundary(flySim, "FlySim 控制域") {
|
||||
System(registry, "总中心(Registry)", "登记逻辑终端并发布全局终端目录及服务状态")
|
||||
System(dispatcher, "调度服务(Dispatcher)", "编排命令和事件,分配仿真任务,并根据状态动态调度")
|
||||
System(subcenter, "终端聚合路由服务(子中心)", "将下辖实体终端聚合为一个逻辑终端,并统一代理 Terminal API")
|
||||
System(terminal, "实体控制终端", "封装单个仿真程序,对外提供 Terminal API")
|
||||
System(dataService, "数据服务", "提供场景数据查询")
|
||||
}
|
||||
|
||||
Rel(operator, controlConsole, "操作")
|
||||
Rel(controlConsole, dispatcher, "提交、控制和跟踪仿真任务", "调度 API(待定义)")
|
||||
Rel(controlConsole, registry, "发现逻辑终端", "Center gRPC")
|
||||
Rel(controlConsole, subcenter, "直接控制逻辑终端和订阅聚合状态", "Terminal gRPC")
|
||||
Rel(controlConsole, dataService, "查询场景", "gRPC")
|
||||
Rel(dispatcher, registry, "发现可调度的逻辑终端", "Center gRPC stream")
|
||||
Rel(dispatcher, subcenter, "执行调度命令、管理事件、订阅聚合状态", "Terminal gRPC")
|
||||
Rel(dispatcher, dataService, "获取场景数据", "Data gRPC")
|
||||
Rel(subcenter, registry, "作为单一逻辑终端注册和注销", "Center gRPC")
|
||||
Rel(terminal, subcenter, "注册、注销实体终端", "Center gRPC")
|
||||
Rel(subcenter, terminal, "按调度结果代理命令、事件和状态订阅", "Terminal gRPC")
|
||||
Rel(terminal, simulator, "调用命令和事件回调", "C ABI")
|
||||
Rel(simulator, terminal, "配置、注册回调、推送状态、完成事件", "C ABI + Protobuf")
|
||||
```
|
||||
|
||||
### 1.2 容器视图
|
||||
|
||||
```mermaid
|
||||
C4Container
|
||||
title FlySim 两级中心容器视图
|
||||
|
||||
Person(operator, "控制操作员", "任务调度、终端控制与状态监视")
|
||||
Container_Ext(controlConsole, "控制系统客户端", "桌面端或服务", "提交任务并消费调度、Center、Terminal 和 Data API")
|
||||
|
||||
System_Boundary(flySim, "FlySim 控制域") {
|
||||
Container(registry, "总中心(Registry)", "Rust + gRPC / Protobuf", "实现 Center API,维护子中心等逻辑终端的全局目录")
|
||||
Container(dispatcher, "调度服务(Dispatcher)", "Rust,接口待定义", "执行命令与事件编排、任务分配和状态驱动调度")
|
||||
Container(dataService, "Data 服务", "gRPC / Protobuf", "根据 SceneRequest 返回场景")
|
||||
|
||||
Container_Boundary(subcenterNode, "终端集群") {
|
||||
Container(subcenter, "终端聚合路由服务(子中心)", "Center + Terminal gRPC", "管理实体终端,对外呈现一个逻辑终端并聚合状态")
|
||||
Container(routeState, "路由与聚合状态", "内存目录、连接池、句柄映射", "保存实体终端连接、调度目标和逻辑事件映射")
|
||||
}
|
||||
|
||||
Container_Boundary(terminalNode, "实体终端节点(多个)") {
|
||||
Container(simHost, "仿真宿主", "C/C++ 或其他原生程序", "实现仿真命令和事件回调,产生状态")
|
||||
Container(terminalLib, "Terminal 动态库", "Rust cdylib + ConnectRPC + Axum", "管理单个终端的生命周期、状态和事件,并托管 Terminal RPC 服务")
|
||||
}
|
||||
}
|
||||
|
||||
Rel(operator, controlConsole, "操作")
|
||||
Rel(controlConsole, dispatcher, "提交和跟踪仿真任务", "调度 API(待定义)")
|
||||
Rel(controlConsole, registry, "SubscribeTerminal", "Center gRPC stream")
|
||||
Rel(controlConsole, subcenter, "直接控制和订阅逻辑终端", "Terminal gRPC")
|
||||
Rel(controlConsole, dataService, "GetScene", "gRPC")
|
||||
Rel(dispatcher, registry, "订阅逻辑终端目录", "Center gRPC stream")
|
||||
Rel(dispatcher, dataService, "GetScene", "Data gRPC")
|
||||
Rel(dispatcher, subcenter, "调度逻辑终端并订阅聚合状态", "Terminal gRPC")
|
||||
Rel(subcenter, registry, "以单一 TerminalInfo 注册、注销", "Center gRPC")
|
||||
Rel(terminalLib, subcenter, "RegisterTerminal、DeregisterTerminal", "Center gRPC")
|
||||
Rel(subcenter, terminalLib, "代理 Terminal API", "gRPC")
|
||||
Rel(subcenter, routeState, "读取和更新")
|
||||
Rel(simHost, terminalLib, "生命周期、配置、注册、状态和事件完成", "C ABI + Protobuf bytes")
|
||||
Rel(terminalLib, simHost, "命令和事件回调", "C function pointer")
|
||||
```
|
||||
|
||||
### 1.3 Terminal 组件视图
|
||||
|
||||
```mermaid
|
||||
C4Component
|
||||
title Terminal 动态库组件视图
|
||||
|
||||
Container_Ext(controlClient, "控制系统客户端", "Terminal gRPC 客户端")
|
||||
Container_Ext(centerService, "上级 Center 服务", "子中心或总中心")
|
||||
Container_Ext(simHost, "仿真宿主", "原生应用")
|
||||
|
||||
Container_Boundary(terminalLib, "Terminal 动态库") {
|
||||
Component(ffi, "FFI 门面", "ffi.rs", "校验指针、编解码 Protobuf、管理句柄与错误码")
|
||||
Component(runtime, "运行时管理器", "runtime.rs / RunningServer", "创建 Tokio 运行时、绑定端口、启停服务并访问中心")
|
||||
Component(rpc, "Terminal RPC 适配器", "rpc.rs / Terminal trait", "将五个 RPC 映射到 Service,并把 watch 转换为流")
|
||||
Component(core, "终端领域服务", "terminal.rs / Service", "维护生命周期与仿真状态,编排命令和事件")
|
||||
Component(callback, "宿主回调适配器", "callback.rs", "编码请求、调用 C 回调、通过 sink 接收并解码响应")
|
||||
Component(eventTask, "事件任务", "Tokio task", "处理延迟、显式完成、重复和取消")
|
||||
Component(statusBus, "状态总线", "tokio::sync::watch", "发布高频状态、变更触发的元状态及关闭信号")
|
||||
}
|
||||
|
||||
Rel(simHost, ffi, "调用导出的 terminal_service_* API", "C ABI")
|
||||
Rel(ffi, runtime, "启动和停止")
|
||||
Rel(ffi, core, "配置、注册、推送状态、完成事件")
|
||||
Rel(runtime, centerService, "注册和注销", "CenterClient / gRPC")
|
||||
Rel(runtime, rpc, "挂载到 Axum 服务")
|
||||
Rel(controlClient, rpc, "调用 Terminal API", "gRPC")
|
||||
Rel(rpc, core, "委托业务操作")
|
||||
Rel(core, callback, "串行调用宿主回调")
|
||||
Rel(callback, simHost, "调用函数指针并接收 sink 响应", "C ABI")
|
||||
Rel(core, eventTask, "创建和管理")
|
||||
Rel(core, statusBus, "发布快照")
|
||||
Rel(statusBus, rpc, "提供订阅数据")
|
||||
```
|
||||
|
||||
### 1.4 调度与聚合路由组件视图(目标设计)
|
||||
|
||||
```mermaid
|
||||
C4Component
|
||||
title Dispatcher、Registry 与终端聚合路由组件视图
|
||||
|
||||
Container_Ext(controlClient, "控制系统客户端", "提交任务并查看执行状态")
|
||||
Container_Ext(dataService, "Data 服务", "提供场景数据")
|
||||
Container_Ext(terminals, "实体终端集群", "各自提供 Center 注册客户端和 Terminal 服务")
|
||||
|
||||
Container_Boundary(registryBoundary, "总中心(Registry)") {
|
||||
Component(globalDirectory, "全局终端目录", "Center API", "登记子中心等逻辑终端并发布 TerminalInfos")
|
||||
}
|
||||
|
||||
Container_Boundary(dispatcherBoundary, "调度服务(Dispatcher)") {
|
||||
Component(dispatchApi, "调度 API", "协议待定义", "接收任务、编排及控制请求")
|
||||
Component(planner, "调度规划器", "任务与能力匹配", "依据任务、终端能力和状态选择逻辑终端及执行步骤")
|
||||
Component(orchestrator, "执行编排器", "异步任务执行", "执行命令序列、发布或取消事件并跟踪结果")
|
||||
Component(stateObserver, "状态观察器", "流订阅", "消费终端目录、聚合状态和元状态,触发重调度")
|
||||
}
|
||||
|
||||
Container_Boundary(subcenterBoundary, "终端聚合路由服务(子中心)") {
|
||||
Component(childCenter, "下级 Center 接口", "Center gRPC server", "接受实体终端注册、注销并发布下级目录")
|
||||
Component(terminalFacade, "逻辑 Terminal 门面", "Terminal gRPC server", "以单一逻辑终端对外提供完整 Terminal API")
|
||||
Component(router, "终端路由器", "能力与状态路由", "根据调度意图选择一个或多个实体终端并代理调用")
|
||||
Component(statusAggregator, "状态聚合器", "流处理", "把多个实体状态和元状态归并为逻辑终端快照")
|
||||
Component(connectionPool, "终端目录与连接池", "内存状态", "维护实体 terminal_id、地址、健康状态和客户端连接")
|
||||
Component(handleMap, "逻辑事件句柄表", "内存状态", "映射逻辑事件实例与下游事件句柄")
|
||||
Component(upstreamRegistration, "上级注册客户端", "Center gRPC client", "将子中心作为一个 TerminalInfo 注册到总中心")
|
||||
}
|
||||
|
||||
Rel(controlClient, dispatchApi, "提交和跟踪任务", "调度 API(待定义)")
|
||||
Rel(dispatchApi, planner, "创建调度计划")
|
||||
Rel(planner, dataService, "读取场景", "GetScene")
|
||||
Rel(planner, stateObserver, "读取可用能力和状态")
|
||||
Rel(stateObserver, globalDirectory, "订阅逻辑终端目录", "SubscribeTerminal")
|
||||
Rel(stateObserver, terminalFacade, "订阅聚合状态和元状态", "Terminal streams")
|
||||
Rel(planner, orchestrator, "提交执行计划")
|
||||
Rel(orchestrator, terminalFacade, "执行命令和事件", "Terminal RPC")
|
||||
|
||||
Rel(terminals, childCenter, "注册和注销", "Center RPC")
|
||||
Rel(childCenter, connectionPool, "更新实体终端目录")
|
||||
Rel(terminalFacade, router, "委托控制请求")
|
||||
Rel(router, connectionPool, "选择连接")
|
||||
Rel(router, terminals, "转发命令和事件", "Terminal RPC")
|
||||
Rel(router, handleMap, "维护事件句柄映射")
|
||||
Rel(statusAggregator, connectionPool, "遍历活动终端")
|
||||
Rel(statusAggregator, terminals, "订阅实体状态", "Terminal streams")
|
||||
Rel(statusAggregator, terminalFacade, "发布单一逻辑状态")
|
||||
Rel(upstreamRegistration, globalDirectory, "注册和注销单一逻辑终端", "Center RPC")
|
||||
Rel(connectionPool, upstreamRegistration, "触发能力元信息更新")
|
||||
```
|
||||
|
||||
## 2. 运行时序图
|
||||
|
||||
### 2.1 配置、启动注册与停止注销
|
||||
|
||||
```mermaid
|
||||
|
||||
sequenceDiagram
|
||||
autonumber
|
||||
participant Host as 仿真宿主
|
||||
participant FFI as FFI 门面
|
||||
participant Handle as ServiceHandle
|
||||
participant Core as Service
|
||||
participant Runtime as RunningServer
|
||||
participant RPC as Axum + Terminal RPC
|
||||
participant Center as 上级中心(子中心或总中心)
|
||||
|
||||
Host->>FFI: terminal_service_create()
|
||||
FFI->>Handle: 创建句柄和 Service
|
||||
FFI-->>Host: opaque handle
|
||||
Host->>FFI: terminal_service_configure(TerminalInfo bytes)
|
||||
FFI->>FFI: 解码并校验输入
|
||||
FFI->>Core: configure(TerminalInfo)
|
||||
Core->>Core: Created/Stopped -> Configured
|
||||
Host->>FFI: register_command / register_event
|
||||
FFI->>Core: 保存回调与 user_data
|
||||
|
||||
Host->>FFI: terminal_service_start(center, port)
|
||||
FFI->>Runtime: start(Service, center)
|
||||
Runtime->>Core: registration_info(), reset_shutdown(), set_starting()
|
||||
Runtime->>Runtime: 创建 Tokio Runtime 并绑定监听端口
|
||||
Runtime->>Core: set_bound_port(actual_port)
|
||||
Runtime->>RPC: 挂载 Service 并启动 Axum
|
||||
loop 最多 3 次,递增退避
|
||||
Runtime->>Center: RegisterTerminal(actual TerminalInfo)
|
||||
Center-->>Runtime: TerminalRegisterResponse
|
||||
end
|
||||
Runtime->>Core: set_ready(center_assigned_id)
|
||||
Core->>Core: 发布 TerminalMetaStatus
|
||||
Runtime-->>FFI: RunningServer + bound_port
|
||||
FFI-->>Host: TERMINAL_OK + bound_port
|
||||
|
||||
Host->>FFI: terminal_service_stop()
|
||||
FFI->>Runtime: stop(Service)
|
||||
Runtime->>Core: set_stopping()
|
||||
Runtime->>Center: DeregisterTerminal(terminal_id)
|
||||
Center-->>Runtime: InvokeResponse
|
||||
Runtime->>Core: signal_shutdown()
|
||||
Runtime->>RPC: 等待优雅关闭,超时则中止
|
||||
Runtime->>Core: set_stopped()
|
||||
Runtime-->>Host: 注销结果
|
||||
```
|
||||
|
||||
### 2.2 命令执行与状态订阅
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
autonumber
|
||||
participant Client as 控制系统客户端
|
||||
participant RPC as Terminal RPC
|
||||
participant Core as Service
|
||||
participant Gate as callback_gate
|
||||
participant Adapter as CommandCallback
|
||||
participant Host as 仿真宿主
|
||||
participant Bus as status watch
|
||||
|
||||
Client->>RPC: SubscribeStatus(Empty)
|
||||
RPC->>Core: status_receiver() + shutdown_receiver()
|
||||
Core-->>RPC: watch receivers
|
||||
RPC-->>Client: 初始 TerminalStatus 快照
|
||||
|
||||
Client->>RPC: Execute(Command)
|
||||
RPC->>Core: execute_command(Command)
|
||||
Core->>Core: 校验 Ready、命令注册和仿真状态迁移合法性
|
||||
Core->>Gate: spawn_blocking 后获取全局回调锁
|
||||
Gate->>Adapter: invoke(Command)
|
||||
Adapter->>Adapter: Protobuf 编码
|
||||
Adapter->>Host: TerminalCommandCallback(..., sink)
|
||||
Host-->>Adapter: sink(InvokeResponse bytes)
|
||||
Adapter->>Adapter: 校验单次响应并解码
|
||||
Adapter-->>Core: InvokeResponse
|
||||
opt success
|
||||
Core->>Core: 应用 init/start/stop/suspend/resume/reset 状态迁移
|
||||
end
|
||||
Core-->>RPC: InvokeResponse
|
||||
RPC-->>Client: InvokeResponse
|
||||
|
||||
Host->>Core: terminal_service_push_status(TerminalStatus bytes)
|
||||
Core->>Core: 覆盖 sequence 和 timestamp
|
||||
Core->>Bus: send_replace(status)
|
||||
Bus-->>RPC: receiver.changed()
|
||||
RPC-->>Client: TerminalStatus stream item
|
||||
```
|
||||
|
||||
### 2.3 事件发布、完成、重复与取消
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
autonumber
|
||||
participant Client as 控制系统客户端
|
||||
participant RPC as Terminal RPC
|
||||
participant Core as Service
|
||||
participant Meta as meta_status watch
|
||||
participant Task as 事件 Tokio task
|
||||
participant Gate as callback_gate
|
||||
participant Host as 仿真宿主
|
||||
|
||||
Client->>RPC: PublishEvent(TerminalEvent)
|
||||
RPC->>Core: publish_event(event)
|
||||
Core->>Core: 校验状态和事件定义,分配 instance_id
|
||||
Core->>Meta: 发布 active_events 快照
|
||||
Core->>Task: spawn_event_task(event, handle, callback)
|
||||
Core-->>Client: InvokeResponse(TerminalEventHandle)
|
||||
Meta-->>Client: TerminalMetaStatus stream item
|
||||
|
||||
Task->>Task: 等待 trigger_delay_ms
|
||||
loop 首次执行及每个成功的重复周期
|
||||
Task->>Core: 标记 awaiting_completion
|
||||
Task->>Gate: spawn_blocking 后获取全局回调锁
|
||||
Gate->>Host: TerminalEventCallback(event, instance_id, sink)
|
||||
Host-->>Task: sink(InvokeResponse bytes)
|
||||
alt 回调失败或拒绝
|
||||
Task->>Core: 移除 active event
|
||||
else 回调接受
|
||||
Task->>Task: 等待显式完成、取消或关闭
|
||||
Host->>Core: terminal_service_complete_event(handle, response)
|
||||
Core-->>Task: EventSignal::Complete(response)
|
||||
alt 完成失败或 repeat_period_ms 为 0
|
||||
Task->>Core: 移除 active event
|
||||
else 完成成功且需要重复
|
||||
Task->>Task: 等待 repeat_period_ms
|
||||
end
|
||||
end
|
||||
end
|
||||
Core->>Meta: active_events 变化时发布新快照
|
||||
|
||||
opt 控制系统主动取消
|
||||
Client->>RPC: CancelEvent(event_id, instance_id)
|
||||
RPC->>Core: cancel_event(request)
|
||||
Core-->>Task: EventSignal::Cancelled
|
||||
Core->>Meta: 发布移除后的快照
|
||||
Core-->>Client: InvokeResponse(TerminalEventHandle)
|
||||
end
|
||||
```
|
||||
|
||||
### 2.4 两级注册、状态驱动调度与聚合路由
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
autonumber
|
||||
participant Terminal as 实体终端
|
||||
participant SubCenter as 聚合路由子中心
|
||||
participant Registry as 总中心 Registry
|
||||
participant Dispatcher as 调度服务
|
||||
participant Data as Data 服务
|
||||
participant Client as 控制系统客户端
|
||||
|
||||
Terminal->>SubCenter: RegisterTerminal(entity TerminalInfo)
|
||||
SubCenter->>SubCenter: 更新实体目录与连接池
|
||||
SubCenter-->>Terminal: 实体 terminal_id
|
||||
SubCenter->>Terminal: SubscribeStatus + SubscribeMetaStatus
|
||||
Terminal-->>SubCenter: 实体状态流
|
||||
SubCenter->>SubCenter: 归并为单一逻辑状态和能力
|
||||
|
||||
SubCenter->>Registry: RegisterTerminal(logical TerminalInfo)
|
||||
Registry-->>SubCenter: logical terminal_id
|
||||
Dispatcher->>Registry: SubscribeTerminal(Empty)
|
||||
Registry-->>Dispatcher: 包含子中心的 TerminalInfos 流
|
||||
Dispatcher->>SubCenter: SubscribeStatus + SubscribeMetaStatus
|
||||
SubCenter-->>Dispatcher: 单一逻辑终端状态流
|
||||
|
||||
Client->>Dispatcher: 提交仿真任务(调度 API 待定义)
|
||||
Dispatcher->>Data: GetScene(SceneRequest)
|
||||
Data-->>Dispatcher: SceneResponse
|
||||
Dispatcher->>Dispatcher: 根据任务、能力和聚合状态生成计划
|
||||
Dispatcher->>SubCenter: Execute / PublishEvent
|
||||
SubCenter->>SubCenter: 根据计划和实时状态选择一个或多个实体终端
|
||||
par 转发到目标终端
|
||||
SubCenter->>Terminal: Execute / PublishEvent
|
||||
Terminal-->>SubCenter: InvokeResponse / downstream handle
|
||||
and 持续观察状态
|
||||
Terminal-->>SubCenter: TerminalStatus / TerminalMetaStatus
|
||||
SubCenter-->>Dispatcher: 聚合状态变化
|
||||
end
|
||||
SubCenter->>SubCenter: 聚合响应并维护逻辑事件句柄映射
|
||||
SubCenter-->>Dispatcher: InvokeResponse / logical handle
|
||||
Dispatcher-->>Client: 任务进度或结果
|
||||
|
||||
opt 状态变化要求重调度
|
||||
Dispatcher->>Dispatcher: 重新选择目标并调整命令或事件
|
||||
Dispatcher->>SubCenter: Execute / PublishEvent / CancelEvent
|
||||
end
|
||||
```
|
||||
|
||||
## 3. Terminal 类图
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
direction LR
|
||||
|
||||
class TerminalRpc {
|
||||
<<interface>>
|
||||
+execute(Command) InvokeResponse
|
||||
+publish_event(TerminalEvent) InvokeResponse
|
||||
+cancel_event(TerminalCancelEvent) InvokeResponse
|
||||
+subscribe_status(Empty) Stream
|
||||
+subscribe_meta_status(Empty) Stream
|
||||
}
|
||||
|
||||
class ServiceHandle {
|
||||
-service Service
|
||||
-running MutexOptionRunningServer
|
||||
-last_error MutexString
|
||||
+new() ServiceHandle
|
||||
-set_error(TerminalError)
|
||||
-clear_error()
|
||||
}
|
||||
|
||||
class RunningServer {
|
||||
-runtime Runtime
|
||||
-server_task JoinHandle
|
||||
-center_host String
|
||||
-center_port u16
|
||||
-bound_port u16
|
||||
+start(Service, String, u16) RunningServer
|
||||
+stop(Service)
|
||||
+bound_port() u16
|
||||
}
|
||||
|
||||
class Service {
|
||||
-inner ArcInner
|
||||
+new() BoxService
|
||||
+closed() bool
|
||||
+configure(TerminalInfo)
|
||||
+register_command(String, CommandCallback)
|
||||
+register_event(TerminalEventInfo, EventCallback)
|
||||
+push_status(TerminalStatus)
|
||||
+execute_command(Command) InvokeResponse
|
||||
+publish_event(TerminalEvent) InvokeResponse
|
||||
+cancel_event(TerminalCancelEvent) InvokeResponse
|
||||
+complete_event(TerminalEventHandle, InvokeResponse)
|
||||
}
|
||||
|
||||
class Inner {
|
||||
-context MutexContext
|
||||
-callback_gate ArcMutex
|
||||
-status WatchSenderTerminalStatus
|
||||
-meta_status WatchSenderTerminalMetaStatus
|
||||
-shutdown MutexWatchSenderBool
|
||||
}
|
||||
|
||||
class Context {
|
||||
-service_state ServiceState
|
||||
-simulation_state SimulationState
|
||||
-terminal_info TerminalInfo
|
||||
-terminal_id String
|
||||
-commands HashMapCommandCallback
|
||||
-event_definitions HashMapEventDefinition
|
||||
-active_events HashMapActiveEvent
|
||||
-next_instance_id u64
|
||||
-status_sequence u64
|
||||
}
|
||||
|
||||
class ServiceState {
|
||||
<<enumeration>>
|
||||
Created
|
||||
Configured
|
||||
Starting
|
||||
Ready
|
||||
Stopping
|
||||
Stopped
|
||||
}
|
||||
|
||||
class SimulationState {
|
||||
<<enumeration>>
|
||||
Uninitialized
|
||||
Initialized
|
||||
Running
|
||||
Suspended
|
||||
Stopped
|
||||
}
|
||||
|
||||
class EventDefinition {
|
||||
-info TerminalEventInfo
|
||||
-callback EventCallback
|
||||
}
|
||||
|
||||
class ActiveEvent {
|
||||
-event_id String
|
||||
-awaiting_completion bool
|
||||
-signal WatchSenderEventSignal
|
||||
}
|
||||
|
||||
class EventSignal {
|
||||
<<enumeration>>
|
||||
Pending
|
||||
Complete
|
||||
Cancelled
|
||||
}
|
||||
|
||||
class CommandCallback {
|
||||
-callback TerminalCommandCallback
|
||||
-user_data usize
|
||||
+new(callback, user_data) CommandCallback
|
||||
+invoke(Command) InvokeResponse
|
||||
}
|
||||
|
||||
class EventCallback {
|
||||
-callback TerminalEventCallback
|
||||
-user_data usize
|
||||
+new(callback, user_data) EventCallback
|
||||
+invoke(TerminalEvent, u64) InvokeResponse
|
||||
}
|
||||
|
||||
class SinkState {
|
||||
-response OptionBytes
|
||||
-duplicate bool
|
||||
}
|
||||
|
||||
class TerminalError {
|
||||
+code str
|
||||
+message String
|
||||
+into_response() InvokeResponse
|
||||
+display_message() String
|
||||
}
|
||||
|
||||
class CenterClient {
|
||||
+register_terminal(TerminalInfo) TerminalRegisterResponse
|
||||
+deregister_terminal(StringValue) InvokeResponse
|
||||
}
|
||||
|
||||
ServiceHandle "1" *-- "1" Service
|
||||
ServiceHandle "1" *-- "0..1" RunningServer
|
||||
Service "1" *-- "1" Inner
|
||||
Service ..|> TerminalRpc
|
||||
Inner "1" *-- "1" Context
|
||||
Context --> ServiceState
|
||||
Context --> SimulationState
|
||||
Context "1" o-- "0..*" CommandCallback
|
||||
Context "1" o-- "0..*" EventDefinition
|
||||
Context "1" o-- "0..*" ActiveEvent
|
||||
EventDefinition "1" *-- "1" EventCallback
|
||||
ActiveEvent --> EventSignal
|
||||
CommandCallback ..> SinkState : response sink
|
||||
EventCallback ..> SinkState : response sink
|
||||
CommandCallback ..> TerminalError
|
||||
EventCallback ..> TerminalError
|
||||
RunningServer --> Service : lifecycle
|
||||
RunningServer ..> CenterClient : registration
|
||||
```
|
||||
|
||||
## 4. 协议与实现映射
|
||||
|
||||
| 关注点 | 协议或实现位置 | 说明 |
|
||||
| ------------ | ------------------------------------------------------------------------ | ------------------------------------------- |
|
||||
| 中心 API | [`proto/control/center.proto`](proto/control/center.proto) | 总中心与子中心复用的注册、注销和目录流 |
|
||||
| 终端 API | [`proto/control/terminal.proto`](proto/control/terminal.proto) | 命令、事件、高频状态和元状态流 |
|
||||
| 数据 API | [`proto/control/data.proto`](proto/control/data.proto) | 当前仅定义 `GetScene` |
|
||||
| 公共消息 | [`proto/control/common/common.proto`](proto/control/common/common.proto) | 参数、参数模式、命令元信息和统一调用响应 |
|
||||
| 调度服务 | [`dispatcher/src/main.rs`](dispatcher/src/main.rs) | 当前为占位程序,调度 API 和实现待补充 |
|
||||
| 总中心 | [`registry/src/main.rs`](registry/src/main.rs) | 已实现 Center API、持久化目录和主动健康检查 |
|
||||
| 聚合路由 | 尚无对应模块 | 同时实现下级 Center 和上级 Terminal |
|
||||
| FFI 生命周期 | [`terminal/src/ffi.rs`](terminal/src/ffi.rs) | C ABI、句柄、输入校验和错误码 |
|
||||
| 服务运行时 | [`terminal/src/runtime.rs`](terminal/src/runtime.rs) | Tokio/Axum 服务以及中心注册和注销 |
|
||||
| RPC 适配 | [`terminal/src/rpc.rs`](terminal/src/rpc.rs) | `Terminal` trait 实现和 watch 流转换 |
|
||||
| 领域状态 | [`terminal/src/terminal.rs`](terminal/src/terminal.rs) | 生命周期、仿真状态机、事件任务和状态发布 |
|
||||
| 回调桥接 | [`terminal/src/callback.rs`](terminal/src/callback.rs) | C 回调、响应 sink 和 Protobuf 编解码 |
|
||||
|
||||
## 5. 约束与说明
|
||||
|
||||
- Registry 是总中心的实现,维护全局逻辑终端目录;终端聚合路由服务是子中心,对下实现 Center API,对上实现 Terminal API。
|
||||
- 实体终端向子中心注册;子中心把下辖终端聚合为一个逻辑终端,再向总中心注册一个 `TerminalInfo`。
|
||||
- Dispatcher 负责命令编排、事件编排、仿真任务分配和状态驱动调度,并通过总中心发现逻辑终端。
|
||||
- 子中心内部可选择一个或多个实体终端,但对上只发布单一逻辑终端状态;事件需维护逻辑句柄到下游句柄的映射。
|
||||
- Dispatcher 和聚合路由目前没有业务实现;调度 API、调度意图如何传入子中心、响应聚合规则和逻辑状态模式仍需在编码前协议化。
|
||||
- Registry 持久化活动终端目录,按规范化 MAC 生成稳定 UUID v5;启动恢复后状态先为 UNKNOWN,再主动调用 Terminal 的标准 Health.Check。离线项保留为 NOT_SERVING,直到恢复或显式注销。
|
||||
- Terminal 使用 ConnectRPC 的 gRPC 协议并通过 HTTP/2 明文连接中心;服务端由 Axum 承载。
|
||||
- 命令与事件共用一个 `callback_gate`,因此同一终端实例上的宿主回调被串行执行。
|
||||
- `SubscribeStatus` 和 `SubscribeMetaStatus` 会先返回当前快照,之后再发送变更;关闭信号会结束流。
|
||||
- 高频状态的 `sequence` 和 `timestamp` 由 Rust 服务覆盖;元状态由事件定义、活动事件和中心分配的终端 ID 派生。
|
||||
- 事件回调成功只表示宿主接受了本次触发。重复事件必须再通过 `terminal_service_complete_event` 显式完成,才会进入下一周期。
|
||||
Reference in New Issue
Block a user