# car3 **Repository Path**: w7y7x7/car3 ## Basic Information - **Project Name**: car3 - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-18 - **Last Updated**: 2026-05-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DDS - 轻量级数据分发服务 基于 Python asyncio 的高性能去中心化实时数据通信系统,完全遵循 OMG DDS 概念模型。 ## 架构概览 ``` ┌─────────────────────────────────────────────────────────┐ │ DomainParticipant │ │ ┌───────────┐ ┌───────────┐ ┌───────────────────┐ │ │ │ Publisher │ │ Subscriber│ │ DiscoveryManager │ │ │ │ ┌───────┐ │ │ ┌───────┐ │ │ ┌─────────────┐ │ │ │ │ │DW:Topic│ │ │ │DR:Topic│ │ │ │ SPDP 心跳 │ │ │ │ │ │DW:Topic│ │ │ │DR:Topic│ │ │ │ Writer/Reader│ │ │ │ └───────┘ │ │ └───────┘ │ │ │ 匹配引擎 │ │ │ └───────────┘ └───────────┘ │ └─────────────┘ │ │ │ │ │ │ │ ┌──────┴──────┐ ┌───────┴───────┐ │ │ │ Transport │ │ UDP 组播 │ │ │ │ Manager │◄─────►│ 239.255.0.1 │ │ │ └─────────────┘ └───────────────┘ │ └─────────────────────────────────────────────────────────┘ ``` ## 核心特性 | 特性 | 说明 | |------|------| | **发布/订阅** | 去中心化的 P/S 通信模型,无中心节点 | | **主题机制** | 基于主题的数据分发,支持多主题 | | **QoS 策略** | 可靠性、持久性、历史深度等策略可配置 | | **动态发现** | SPDP 协议实现节点自动发现,零配置 | | **支持所有场景** | 发布端先启动 or 订阅端先启动均可自动连接 | ## QoS 策略 | 策略 | 类型 | 说明 | |------|------|------| | **可靠性** | `RELIABLE` / `BEST_EFFORT` | 可靠保证送达 / 尽力而为低延迟 | | **持久性** | `TRANSIENT_LOCAL` / `VOLATILE` | 本地缓存历史 / 不缓存 | | **历史** | `KEEP_LAST` / `KEEP_ALL` | 保留最近 N 条 / 保留全部 | ## 快速开始 ### 1. 启动订阅端 ```bash # 终端 1:启动订阅端(回调模式) cd examples python subscriber.py --topic SensorData --mode callback # 或使用轮询模式 python subscriber.py --topic SensorData --mode polling # 低延迟模式(BEST_EFFORT + VOLATILE) python subscriber.py --topic SensorData --qos best_effort --mode callback ``` ### 2. 启动发布端 ```bash # 终端 2:启动发布端 cd examples python publisher.py --topic SensorData --interval 100 # 可靠模式 + 历史深度 64 python publisher.py --topic SensorData --qos reliable --depth 64 --interval 50 # 性能压测(10000条) python publisher.py --benchmark --benchmark-count 10000 ``` ### 3. 运行效果 ``` [发布端] 初始化完成 主题 : SensorData QoS 模式 : reliable 等待订阅者接入... [发布端] ✓ reader 已匹配到主题 'SensorData' [发布端] 开始发送数据,间隔 100ms ------------------------------------------------------------ [发布端] 已发送 10 条 | 速率 98.5 msg/s | 耗时 0.1s ``` ``` [订阅端] 初始化完成 主题 : SensorData 等待发布者数据... [订阅端] ✓ writer 已匹配到主题 'SensorData' [订阅端] ▶ 收到 #1 sensor_id: sensor-005 temperature: 25.73 humidity: 55.21 pressure: 1010.45 [订阅端] 已收 10 条 | 速率 97.2 msg/s | 耗时 0.1s ``` ## 命令行参数 ### 发布端 | 参数 | 默认值 | 说明 | |------|--------|------| | `--topic` | SensorData | 主题名称 | | `--qos` | reliable | 可选:reliable / best_effort / keep_all | | `--interval` | 100 | 发送间隔(毫秒) | | `--count` | 无限 | 发送条数限制 | | `--benchmark` | — | 压测模式 | | `--benchmark-count` | 10000 | 压测条数 | ### 订阅端 | 参数 | 默认值 | 说明 | |------|--------|------| | `--topic` | SensorData | 主题名称 | | `--qos` | reliable | 可选:reliable / best_effort / keep_all | | `--mode` | callback | 可选:callback / polling / await | ## 编程 API 使用 ```python import asyncio from dds import DomainParticipant, Publisher, Subscriber, Topic, WriterQos, ReaderQos async def main(): # 1. 创建参与者 participant = DomainParticipant(domain_id=0) # 2. 创建主题 topic = participant.create_topic("MyTopic") # 3. 创建发布者和 DataWriter pub = participant.create_publisher() writer_qos = WriterQos(reliability="RELIABLE", history_depth=32) writer = pub.create_data_writer(topic, writer_qos) # 4. 创建订阅者和 DataReader sub = participant.create_subscriber() reader_qos = ReaderQos(reliability="RELIABLE", history_depth=32) reader = sub.create_data_reader(topic, reader_qos, callback=lambda s: print(s.data)) # 5. 启动 await participant.start() # 6. 发布数据 await writer.write({"hello": "world"}) # 7. 接收数据(回调自动处理) # 8. 关闭 await participant.stop() asyncio.run(main()) ``` ## 性能指标 | 场景 | 吞吐量 | 延迟 | |------|--------|------| | BEST_EFFORT (单机) | ~50,000 msg/s | < 1ms | | RELIABLE (单机) | ~30,000 msg/s | < 2ms | | BEST_EFFORT (局域网) | ~15,000 msg/s | < 5ms | | RELIABLE (局域网) | ~8,000 msg/s | < 10ms | > 注:实际性能受网络环境、消息大小、系统负载等因素影响