源程序:【免费】uDDS源程序subscriber资源-CSDN文库
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "IDL_TypeSupport.h"
class UserDataTypeListener : public DataReaderListener {
public:
virtual void on_data_available(DataReader* reader);
};
void UserDataTypeListener::on_data_available(DataReader* reader)
{
UserDataTypeDataReader *UserDataType_reader = NULL;
UserDataTypeSeq data_seq;
SampleInfoSeq info_seq;
ReturnCode_t retcode;
int i;
UserDataType_reader = UserDataTypeDataReader::narrow(reader);
if (UserDataType_reader == NULL) {
fprintf(stderr, "DataReader narrow error\n");
return;
}
retcode = UserDataType_reader->read(
data_seq, info_seq, 10, 0, 0, 0);
if (retcode == RETCODE_NO_DATA) {
return;
}
else if (retcode != RETCODE_OK) {
fprintf(stderr, "take error %d\n", retcode);
return;
}
for (i = 0; i < data_seq.length(); ++i) {
printf("receive: x=%hd,y=%hd,color=%s\n",
data_seq[i].x,data_seq[i].y, data_seq[i].color);
delete data_seq[i].color;
data_seq[i].color = NULL;
}
}
static int subscriber_shutdown(
DomainParticipant *participant)
{
ReturnCode_t retcode;
int status = 0;
if (participant != NULL) {
retcode = participant->delete_contained_entities();
if (retcode != RETCODE_OK) {
fprintf(stderr, "delete_contained_entities error %d\n", retcode);
status = -1;
}
retcode = DomainParticipantFactory::get_instance()->delete_participant(participant);
if (retcode != RETCODE_OK) {
fprintf(stderr, "delete_participant error %d\n", retcode);
status = -1;
}
}
return status;
}
extern "C" int subscriber_main(int domainId, int sample_count)
{
DomainParticipant *participant = NULL;
Subscriber *subscriber = NULL;
Topic *topic = NULL;
UserDataTypeListener *reader_listener = NULL;
DataReader *reader = NULL;
ReturnCode_t retcode;
const char *type_name = NULL;
int count = 0;
int status = 0;
participant = DomainParticipantFactory::get_instance()->create_participant(
domainId, PARTICIPANT_QOS_DEFAULT/* participant榛樿QoS */,
NULL /* listener */, STATUS_MASK_NONE);
if (participant == NULL) {
fprintf(stderr, "create_participant error\n");
subscriber_shutdown(participant);
return -1;
}
subscriber = participant->create_subscriber(
SUBSCRIBER_QOS_DEFAULT/* 榛樿QoS */,
NULL /* listener */, STATUS_MASK_NONE);
if (subscriber == NULL) {
fprintf(stderr, "create_subscriber error\n");
subscriber_shutdown(participant);
return -1;
}
type_name = UserDataTypeTypeSupport::get_type_name();
retcode = UserDataTypeTypeSupport::register_type(
participant, type_name);
if (retcode != RETCODE_OK) {
fprintf(stderr, "register_type error %d\n", retcode);
subscriber_shutdown(participant);
return -1;
}
topic = participant->create_topic(
"Example UserDataType"/* 涓婚鍚嶏紝搴斾笌鍙戝竷鑰呬富棰樺悕涓€鑷?*/,
type_name, TOPIC_QOS_DEFAULT/* 榛樿QoS */,
NULL /* listener */, STATUS_MASK_NONE);
if (topic == NULL) {
fprintf(stderr, "create_topic error\n");
subscriber_shutdown(participant);
return -1;
}
reader_listener = new UserDataTypeListener();
reader = subscriber->create_datareader(
topic, DATAREADER_QOS_DEFAULT/* 榛樿QoS */,
reader_listener/* listener */, STATUS_MASK_ALL);
if (reader == NULL) {
fprintf(stderr, "create_datareader error\n");
subscriber_shutdown(participant);
delete reader_listener;
return -1;
}
for (count = 0; (sample_count == 0) || (count < sample_count); ++count) {
}
status = subscriber_shutdown(participant);
delete reader_listener;
return status;
}
int main(int argc, char *argv[])
{
int domain_id = 0;
int sample_count = 0;
if (argc >= 2) {
domain_id = atoi(argv[1]);
}
if (argc >= 3) {
sample_count = atoi(argv[2]);
}
return subscriber_main(domain_id, sample_count);
}
这是一个C++程序,它涉及到RabbitMQ(通常用于消息传递)和ROS(Robot Operating System,一种机器人操作系统)。`subscriber_main`函数创建了一个订阅者(subscriber),主要用于从指定主题(topic)接收用户自定义数据(UserDataType)的消息:
1. 创建DomainParticipant(领域参与者),这是ROS中的一个核心组件,代表了整个通信上下文。
2. 创建Subscriber(订阅者),用于订阅特定主题的数据流。
3. 注册UserDataType类型,并创建Topic。
4. 实例化UserDataTypeListener,这是数据读取事件的回调函数。
5. 使用DataReader创建一个读者,它会按照指定的质量级(DATAREADER_QOS_DEFAULT)接收消息。
6. 当有样本(sample_count)需要获取时,进入循环等待并处理接收到的消息。
`main`函数作为程序入口,解析命令行参数domain_id和sample_count,然后调用`subscriber_main`函数进行实际操作。