21#include "cyber/proto/transport_conf.pb.h"
28 eprosima::fastrtps::ParticipantListener* listener)
31 send_port_(send_port),
33 fastrtps_participant_(nullptr) {}
38 if (shutdown_.exchange(
true)) {
42 std::lock_guard<std::mutex> lk(mutex_);
43 if (fastrtps_participant_ !=
nullptr) {
44 eprosima::fastrtps::Domain::removeParticipant(fastrtps_participant_);
45 fastrtps_participant_ =
nullptr;
51 if (shutdown_.load()) {
55 std::lock_guard<std::mutex> lk(mutex_);
56 if (fastrtps_participant_ !=
nullptr) {
57 return fastrtps_participant_;
60 CreateFastRtpsParticipant(name_, send_port_, listener_);
61 return fastrtps_participant_;
64void Participant::CreateFastRtpsParticipant(
65 const std::string& name,
int send_port,
66 eprosima::fastrtps::ParticipantListener* listener) {
67 uint32_t domain_id = 80;
69 const char* val = ::getenv(
"CYBER_DOMAIN_ID");
72 domain_id = std::stoi(val);
73 }
catch (
const std::exception& e) {
74 AERROR <<
"convert domain_id error " << e.what();
79 auto part_attr_conf = std::make_shared<proto::RtpsParticipantAttr>();
80 auto& global_conf = common::GlobalData::Instance()->Config();
81 if (global_conf.has_transport_conf() &&
82 global_conf.transport_conf().has_participant_attr()) {
83 part_attr_conf->CopyFrom(global_conf.transport_conf().participant_attr());
86 eprosima::fastrtps::ParticipantAttributes attr;
87 attr.rtps.defaultSendPort = send_port;
88 attr.rtps.port.domainIDGain =
89 static_cast<uint16_t
>(part_attr_conf->domain_id_gain());
90 attr.rtps.port.portBase =
static_cast<uint16_t
>(part_attr_conf->port_base());
91 attr.rtps.use_IP6_to_send =
false;
92 attr.rtps.builtin.use_SIMPLE_RTPSParticipantDiscoveryProtocol =
true;
93 attr.rtps.builtin.use_SIMPLE_EndpointDiscoveryProtocol =
true;
94 attr.rtps.builtin.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter =
96 attr.rtps.builtin.m_simpleEDP.use_PublicationWriterANDSubscriptionReader =
98 attr.rtps.builtin.domainId = domain_id;
106 attr.rtps.builtin.leaseDuration.seconds = part_attr_conf->lease_duration();
107 attr.rtps.builtin.leaseDuration_announcementperiod.seconds =
108 part_attr_conf->announcement_period();
110 attr.rtps.setName(name.c_str());
112 std::string ip_env(
"127.0.0.1");
113 const char* ip_val = ::getenv(
"CYBER_IP");
114 if (ip_val !=
nullptr) {
116 if (ip_env.empty()) {
117 AERROR <<
"invalid CYBER_IP (an empty string)";
121 ADEBUG <<
"cyber ip: " << ip_env;
123 eprosima::fastrtps::rtps::Locator_t locator;
125 RETURN_IF(!locator.set_IP4_address(ip_env));
127 locator.kind = LOCATOR_KIND_UDPv4;
129 attr.rtps.defaultUnicastLocatorList.push_back(locator);
130 attr.rtps.defaultOutLocatorList.push_back(locator);
131 attr.rtps.builtin.metatrafficUnicastLocatorList.push_back(locator);
133 locator.set_IP4_address(239, 255, 0, 1);
134 attr.rtps.builtin.metatrafficMulticastLocatorList.push_back(locator);
136 fastrtps_participant_ =
137 eprosima::fastrtps::Domain::createParticipant(attr, listener);
139 eprosima::fastrtps::Domain::registerType(fastrtps_participant_, &type_);
eprosima::fastrtps::Participant * fastrtps_participant()
Participant(const std::string &name, int send_port, eprosima::fastrtps::ParticipantListener *listener=nullptr)
#define RETURN_IF_NULL(ptr)
#define RETURN_IF(condition)