ZBLOG

The following constitutes a full example on how to configure server and client both programmatically and using XML. You may also have a look at the eProsima Fast DDS Github repository, which contains an example similar to the one discussed in this...

这段文本提供了一个完整的示例,展示如何在eProsima Fast DDS框架中以编程方式和使用XML配置服务器和客户端。以下是对代码的详细解释:

1. 服务器端设置

C++ 代码示例:

// 获取默认参与者QoS
DomainParticipantQos server_qos = PARTICIPANT_QOS_DEFAULT;

// 将参与者设置为SERVER
server_qos.wire_protocol().builtin.discovery_config.discoveryProtocol =
        DiscoveryProtocol::SERVER;

// 设置SERVER的监听定位器用于PDP
Locator_t locator;
IPLocator::setIPv4(locator, 127, 0, 0, 1);
locator.port = 11811;
server_qos.wire_protocol().builtin.metatrafficUnicastLocatorList.push_back(locator);

/* 添加一个远程服务器,该服务器将与此服务器连接 */
// 设置远程SERVER的监听定位器用于PDP
Locator_t remote_locator;
IPLocator::setIPv4(remote_locator, 127, 0, 0, 1);
remote_locator.port = 11812;

// 将远程SERVER添加到SERVER的列表中
server_qos.wire_protocol().builtin.discovery_config.m_DiscoveryServers.push_back(remote_locator);

// 创建SERVER实例
DomainParticipant* server =
        DomainParticipantFactory::get_instance()->create_participant(0, server_qos);
if (nullptr == server)
{
    // 错误处理
    return;
}

分析:

  • 获取默认QoS:首先,获取默认的参与者质量服务(QoS)配置。
  • 设置为服务器模式:将discoveryProtocol设置为SERVER,使该参与者作为服务器工作。
  • 配置监听定位器:通过指定本地IP地址(127.0.0.1)和端口号(11811),设置该服务器的元流量单播定位器列表。
  • 添加远程服务器:定义另一个定位器,以便指向另一个可能存在的远程服务器(在这里也是localhost,但使用不同端口11812)。然后,将其加入到当前服务器的发现配置中,使其能够连接到这个远程服务。
  • 创建DomainParticipant:调用工厂方法创建域参与者,并检查是否成功。

2. 客户端设置

C++ 代码示例:

// 获取默认参与者QoS
DomainParticipantQos client_qos = PARTICIPANT_QOS_DEFAULT;

// 将参与者设置为CLIENT
client_qos.wire_protocol().builtin.discovery_config.discoveryProtocol =
        DiscoveryProtocol::CLIENT;

// 设置SERVER的监听定位器用于PDP
Locator_t locator;
IPLocator::setIPv4(locator, 127, 0, 0, 1);
locator.port = 11811;

// 将远程SERVER添加到CLIENT的列表中
client_qos.wire_protocol().builtin.discovery_config.m_DiscoveryServers.push_back(locator);

// 设置ping周期为250毫秒
client_qos.wire_protocol().builtin.discovery_config.discoveryServer_client_syncperiod =
        Duration_t(0, 250000000);

// 创建CLIENT实例
DomainParticipant* client =
        DomainParticipantFactory::get_instance()->create_participant(0, client_qos);
if (nullptr == client)
{
    // 错误处理
    return;
}

分析:

  • 获取默认QoS:与服务器相同,先获得默认QoS配置。
  • 设置为客户端模式:将discoveryProtocol设置为CLIENT,使该参与者作为客户端工作。
  • 配置目标服务定位器:同样,通过指定本地IP地址和对应端口(11811),将其作为要连接的服务器加入到发现配置中。
  • Ping周期设定:设定客户端与发现服务同步周期为250毫秒,这意味着它会每250毫秒进行一次状态更新以保持连接活跃。
  • 创建DomainParticipant:最后,调用工厂方法来创建客户端实例,并检查结果。

总结

这个示例展示了如何利用Fast DDS API来分别构建一个简单的服务端和客户端,通过合适地配置QoS属性,使二者可以互相通信。程序化的方法使得这些配置信息更加灵活,也可以很容易地集成到其他系统或应用中。在实际应用时,你还可以通过类似的方法实现更复杂的数据交换、节点管理等功能。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?