FastDDS源码解析 二

HelloWorldPublisher

1、HelloWorldPublisher成员

首先来看一下该类包含的成员:

    HelloWorld hello_;
    //DomainParticipant指针,用于创建publisher_,send/receive的endpoint
    eprosima::fastdds::dds::DomainParticipant* participant_;
    //用于创建 writer_    
    eprosima::fastdds::dds::Publisher* publisher_;
    //同一topic的pub、sub会匹配
    eprosima::fastdds::dds::Topic* topic_;
    //用于发布信息   
    eprosima::fastdds::dds::DataWriter* writer_;


包含了DDS架构中的所有类。

2. init分析

bool HelloWorldPublisher::init(
        bool use_env)
{
    hello_.index(0);
    hello_.message("HelloWorld");
    //设置为默认服务质量策略
    DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT;
    pqos.name("Participant_pub");
    auto factory = DomainParticipantFactory::get_instance();

    //创建participant
    participant_ = factory->create_participant(0, pqos);

    //REGISTER THE TYPE
    type_.register_type(participant_);
    //CREATE THE PUBLISHER
    PublisherQos pubqos = PUBLISHER_QOS_DEFAULT;
    //创建publisher
    publisher_ = participant_->create_publisher(pubqos,nullptr);

    //CREATE THE TOPIC
    TopicQos tqos = TOPIC_QOS_DEFAULT;
    //创建topic
    topic_ = participant_->create_topic("HelloWorldTopic","HelloWorld",tqos);

    // CREATE THE WRITER
    DataWriterQos wqos = DATAWRITER_QOS_DEFAULT;
    //创建writer
    writer_ = publisher_->create_datawriter(topic_,wqos,&listener_);

    return true;
}

下面逐个分析各个类创建过程:

2.1、create_participant过程

DomainParticipant* DomainParticipantFactory::create_participant(
        DomainId_t did,
        const DomainParticipantQos& qos,
        DomainParticipantListener* listen,
        const StatusMask& mask)
{
    //加载XML配置文件
    load_profiles();
    //获取服务策略,这里为默认PARTICIPANT_QOS_DEFAULT
    const DomainParticipantQos& pqos = (&qos == &PARTICIPANT_QOS_DEFAULT) ? default_participant_qos_ : qos;
    //new一个DomainParticipant对象
    DomainParticipant* dom_part = new DomainParticipant(mask);
    //创建DomainParticipantImpl类对象
    eprosima::fastdds::statistics::dds::DomainParticipantImpl* dom_part_impl =
            new eprosima::fastdds::statistics::dds::DomainParticipantImpl(dom_part, did, pqos, listen);
    //这里dom_part_impl->guid()为 ENTITYID_RTPSParticipant 0.0.1.c1
    if (fastrtps::rtps::GUID_t::unknown() != dom_part_impl->guid())
    {
        {
            std::lock_guard<std::mutex> guard(mtx_participants_);
            using VectorIt = std::map<DomainId_t,std::vector<DomainParticipantImpl*>>::iterator;
            //participants_中第一个vector
            VectorIt vector_it = participants_.find(did);
            ......
            //填入容器vector中
            vector_it->second.push_back(dom_part_impl);
        }

        if (factory_qos_.entity_factory().autoenable_created_entities)
        {
            //执行DomainParticipant的enable()
            if (ReturnCode_t::RETCODE_OK != dom_part->enable())
            {
                delete_participant(dom_part);
                return nullptr;
            }
        }
    }
    else
    {
        delete dom_part_impl;
        return nullptr;
    }

    return dom_part;
}

domain_id_设置为did = 0。

domain_id_(did)

guid_是在基类efd::DomainParticipantImpl构造函数中设置:

bool RTPSDomainImpl::create_participant_guid(
        int32_t& participant_id,
        GUID_t& guid)
{
    bool ret_value = get_instance()->reserve_participant_id(participant_id);

    if (ret_value)
    {
        //设置guidPrefix
        guid_prefix_create(participant_id, guid.guidPrefix);
        //设置entityId
        guid.entityId = c_EntityId_RTPSParticipant;
    }

    return ret_value;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值