ZBLOG

15.14.2. Getting started For Request-Reply communication perform the following steps: Define two structures in an IDL file. One structure will be used as Request Topic’s data type and the other one as Reply Topic’s data type. enum OperationType...

这段代码是关于使用Fast DDS(Data Distribution Service)实现请求-回复通信模式的步骤说明。下面是对每个部分的详细解释:

15.14.2. 开始

  1. 定义IDL结构体
    • 定义两个结构体,分别用于请求和回复。
    • OperationType 是一个枚举类型,用于表示操作类型(加法、减法、乘法和除法)。
    • RequestType 包含要进行运算的两个长整型数值(x 和 y)以及运算类型。
    • ReplyType 返回运算结果。

客户端应用程序

  1. 创建DataWriter和DataReader
    • 在客户端中,需要为请求主题创建一个 DataWriter,并为回复主题创建一个 DataReader。
    ”`cpp participant->register_type(request_type); participant->register_type(reply_type);

Topic* request_topic = participant->create_topic(“CalculatorRequest”, request_type.get_type_name(), TOPIC_QOS_DEFAULT);

Topic* reply_topic = participant->create_topic(“CalculatorReply”, reply_type.get_type_name(), TOPIC_QOS_DEFAULT);

DataWriter* request_writer = publisher->create_datawriter(request_topic, DATAWRITER_QOS_DEFAULT);

DataReader* reply_reader = subscriber->create_datareader(reply_topic, DATAREADER_QOS_DEFAULT, &listener);


### 服务器应用程序
1. **创建相应的DataWriter和DataReader**:
   - 在服务器端,注册相同的数据类型,并为请求和回复各自创建一个 DataWriter 和 DataReader。

### 15.14.3. 发送请求并存储标识符
- 当客户端发送请求时,它需要获取并存储系统分配给已发布样本的内部标识符。这可以通过重载 `write()` 函数来完成,其中第二个参数是 `WriteParams` 对象。如下所示:
```cpp
eprosima::fastdds::rtps::SampleIdentity my_request_sample_identity;
RequestType request;

// 填充请求数据

// 发布请求
eprosima::fastdds::rtps::WriteParams write_params;
request_writer->write(static_cast<void*>(&request), write_params);

// 存储样本身份标识符
my_request_sample_identity = write_params.sample_identity();

15.14.4. 接收请求并发送关联的回复

  • 当服务器接收到请求时,它可以在 on_data_available() 方法中获取该请求的标识符,如下所示:
void on_data_available(eprosima::fastdds::dds::DataReader* reader) override {
    RequestType request;
    eprosima::fastdds::dds::SampleInfo sample_info;

    reader->take_next_sample(&request, &sample_info);

    if (eprosima::fastdds::dds::InstanceStateKind::ALIVE_INSTANCE_STATE == sample_info.instance_state) {
        // 存储请求身份标识符。
        eprosima::fastdds::rtps::SampleIdentity client_request_identity = sample_info.sample_identity;
    }
}
  • 在处理完该请求后,服务器将根据存储的标识符发送对应的回复:
ReplyType reply;

// 填充回复数据

// 发送与客户机请求关联的回复。
eprosima::fastdds::rtps::WriteParams write_params;
write_params.related_sample_identity() = client_request_identity;
reply_writer->write(reinterpret_cast<void*>(&reply), write_params);

15.14.5. 为客户端识别回复

  • 客户端在接收到回复时,也会使用类似的方法来确认这个响应是否是自己期望中的响应。这通过比较存储的 SampleIdentity 和 incoming 的 related_sample_identity 来实现:
void on_data_available(eprosima::fastdds::dds::DataReader* reader) override {
    ReplyType reply;
    eprosima::fastdds::dds::SampleInfo sample_info;

    reader->take_next_sample(&reply, &sample_info);

    if (eprosima::fastdds::dds::InstanceStateKind::ALIVE_INSTANCE_STATE == sample_info.instance_state) {
        if (sample_info.related_sample_identity == my_request_sample_identity) {
            // 做进一步处理
        }
    }
}

总结

通过以上步骤,客户端能够成功地向服务器发送计算请求,服务器则能处理这些请求并返回对应结果。整个流程利用了DDS的数据传输机制,实现了高效且可靠的信息交换。在设计过程中,可以保证多个客户端与服务端之间的数据解耦,从而提高了系统扩展性和灵活性。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=7711

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?