DDS笔记: hello world解析

fast dds 2.2.x中hello world代码总结:

1) 核心逻辑流程

获取DomainParticipantFactory -> 创建participant -> 注册sub-pub type -> 创建topic(顺序可延后) -> 创建publisher / subscriber -> 创建writer / reader -> write / read / WaitSet.wait

2)除获取factory外,创建其他对象之前都要准备好对应qos、listener、mask等参数。

3)subscriber有2种模式:

(1)listener callback

创建subscriber时传入listener参数,通过listener回调事件和数据(on_subscription_matched, on_data_available)。

(2)wait set

创建subscriber后,通过WaitSet来关联多个Condition,监听相应各个Condition的变化。

关联reader的status condition,监听topic的订阅和数据更新

关联stop condition,监听程序停止动作。

  1. publisher app主要流程
    1)创建factory
    2)创建participant
    3)创建publisher
    4)创建topic
    5)创建writer
    6)发布数据
c++ 复制代码
	// 1
	auto factory = DomainParticipantFactory::get_instance();
	// 2 listener null, mask none
	auto participant = factory->create_participant_with_default_profile(null_listener, mask);
	assert(participant);
	// 注册关联pub-sub-type
	pub_sub_type.register_type(participant);

	// 3 listener null, mask none
	auto publisher = pariticipant->create_publisher(pub_qos, null_listener, mask);
	assert(publisher);

	// 4
	auto topic = participant->create_topic(topic_name, topic_type, topic_qos);
	assert(topic);
	
	// 5 listener, mask all
	auto writer = publisher->create_datawriter(topic, writer_qos, listener, mask);
	assert(writer);

	// 6
	Helloworld hello{...};
	writer->write(&hello);
  1. ListenerSubscriberApp主要流程
    1)创建participant factory
    2)创建participant
    3)创建subscriber
    4)创建topic
    5)创建data reader
    6)data callback
cpp 复制代码
	// 1
	auto factory = DomainParticipantFactory::get_instance();

	// 2 listener null, mask none
	auto participant = factory->create_participant_with_default_profile(null_listener, mask);
	assert(participant);
	pub_sub_type.register_type(participant);

	// 3 listener null, mask none
	auto subscriber = participant->create_subscriber(sub_qos, null_listener, mask);
	assert(subscriber);

	// 4
	auto topic = participant->create_topic(topic_name, topic_type, topic_qos);
	assert(topic);

	// 5 listener, mask all
	auto reader = subscriber->create_datareader(topic, reader_qos, listener, mask);
	assert(reader);

	// 6 data callback
	class DataReaderListener {
		void on_data_available(DataReader *reader); // <<<---
		void on_subscription_matched(DataReader *reader, status); // <<<---
		void on_requested_deadline_missed(DataReader *reader, status);
		void on_liveness_changed(DataReader *reader, status);
		void on_sample_rejected(DataReader *reader, status);
		void on_sample_lost(DataReader *reader, status);
		void on_requested_incompatible_qos(DataReader *reader, status);
	};
	
	void on_subscription_matched(DataReader *reader, SubscriptionMatchedStatus& status) {
		// check matched count
	}

	void on_data_available(Datareader *reader) {
		// read and process data
		Helloworld hello;
		SampleInfo info;
		reader->take_next_sample(&hello, &info);
	}
  1. WaitsetSubscriberApp main process
    1)创建factory
    2)创建participant
    3)创建subscriber
    4)创建topic
    5)创建data reader
    6)WaitSet attach Condition
    7)WaitSet wait Condition
cpp 复制代码
	// 1
	auto factory = DomainParticipantFactory::get_instance();
	
	// 2 listener null, mask none
	auto participant = factory->create_participant_with_default_profile(null_listener, mask);
	assert(participant);	
	pub_sub_type.register_type(participant);

	// 3 listener null, mask none
	auto subscriber = participant->create_subscriber(sub_qos, null_listener, mask)
	assert(subscriber);
	
	// 4
	auto topic = participant->create_topic(topic_name, topic_type, topic_qos);
	assert(topic);

	// 5 null listener, mask all
	auto reader = subscriber->create_datareader(topic, sub_qos, null_listener, mask);
	assert(reader);

	// 6
//	.hpp: 	WaitSet wait_setp;
//			GuardCondtion terminate_condition;

	// ---->>> ***************
	// 这里是关键,把wait和reader的status condition关联起来,监听reader的变化
	// 同时处理terminate_condition,监听stop事件
	wait_set.attach_condition(reader->get_statuscondition());
	wait_set.attach_condition(terminate_condition);

	// 7
	ConditionSeq triggered_conditions;
	wait_set.wait(triggered_conditions, timeout);
	for(Condition* cond : triggered_conditions) {
		auto *status_cond = dynamic_cast<StatusCondition>(cond);
		assert(status_cond);

		Entity *entity = status_cond.get_entity();
		StatusMask changed_status = entity->get_status_changes();

		if (changed_status.is_active(StatusMask::subscriptioin_matched()) {
			// check matched count
		}

		if (changed_status.is_active(StatusMask::data_available())) {
			// read and process data
			SampleInfo info;
			Helloworld hello;
			reader->take_next_sample(&hello, &info);
		}
	}
相关推荐
我要吐泡泡了哦25 分钟前
GAMES104:15 游戏引擎的玩法系统基础-学习笔记
笔记·学习·游戏引擎
骑鱼过海的猫12326 分钟前
【tomcat】tomcat学习笔记
笔记·学习·tomcat
贾saisai2 小时前
Xilinx系FPGA学习笔记(九)DDR3学习
笔记·学习·fpga开发
北岛寒沫3 小时前
JavaScript(JS)学习笔记 1(简单介绍 注释和输入输出语句 变量 数据类型 运算符 流程控制 数组)
javascript·笔记·学习
烟雨666_java3 小时前
JDBC笔记
笔记
GEEKVIP3 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
铁匠匠匠4 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
Moliay5 小时前
【资料分析】刷题日记2
笔记·公考·行测·常识·资料分析
迷迭所归处5 小时前
C++ —— 关于vector
开发语言·c++·算法
懒洋洋的华3695 小时前
消息队列-Kafka(概念篇)
分布式·中间件·kafka