Apollo 11.0
自动驾驶开放平台
participant.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
20#include "cyber/common/log.h"
21#include "cyber/proto/transport_conf.pb.h"
22
23namespace apollo {
24namespace cyber {
25namespace transport {
26
27Participant::Participant(const std::string& name, int send_port,
28 eprosima::fastrtps::ParticipantListener* listener)
29 : shutdown_(false),
30 name_(name),
31 send_port_(send_port),
32 listener_(listener),
33 fastrtps_participant_(nullptr) {}
34
36
38 if (shutdown_.exchange(true)) {
39 return;
40 }
41
42 std::lock_guard<std::mutex> lk(mutex_);
43 if (fastrtps_participant_ != nullptr) {
44 eprosima::fastrtps::Domain::removeParticipant(fastrtps_participant_);
45 fastrtps_participant_ = nullptr;
46 listener_ = nullptr;
47 }
48}
49
50eprosima::fastrtps::Participant* Participant::fastrtps_participant() {
51 if (shutdown_.load()) {
52 return nullptr;
53 }
54
55 std::lock_guard<std::mutex> lk(mutex_);
56 if (fastrtps_participant_ != nullptr) {
57 return fastrtps_participant_;
58 }
59
60 CreateFastRtpsParticipant(name_, send_port_, listener_);
61 return fastrtps_participant_;
62}
63
64void Participant::CreateFastRtpsParticipant(
65 const std::string& name, int send_port,
66 eprosima::fastrtps::ParticipantListener* listener) {
67 uint32_t domain_id = 80;
68
69 const char* val = ::getenv("CYBER_DOMAIN_ID");
70 if (val != nullptr) {
71 try {
72 domain_id = std::stoi(val);
73 } catch (const std::exception& e) {
74 AERROR << "convert domain_id error " << e.what();
75 return;
76 }
77 }
78
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());
84 }
85
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 =
95 true;
96 attr.rtps.builtin.m_simpleEDP.use_PublicationWriterANDSubscriptionReader =
97 true;
98 attr.rtps.builtin.domainId = domain_id;
99
106 attr.rtps.builtin.leaseDuration.seconds = part_attr_conf->lease_duration();
107 attr.rtps.builtin.leaseDuration_announcementperiod.seconds =
108 part_attr_conf->announcement_period();
109
110 attr.rtps.setName(name.c_str());
111
112 std::string ip_env("127.0.0.1");
113 const char* ip_val = ::getenv("CYBER_IP");
114 if (ip_val != nullptr) {
115 ip_env = ip_val;
116 if (ip_env.empty()) {
117 AERROR << "invalid CYBER_IP (an empty string)";
118 return;
119 }
120 }
121 ADEBUG << "cyber ip: " << ip_env;
122
123 eprosima::fastrtps::rtps::Locator_t locator;
124 locator.port = 0;
125 RETURN_IF(!locator.set_IP4_address(ip_env));
126
127 locator.kind = LOCATOR_KIND_UDPv4;
128
129 attr.rtps.defaultUnicastLocatorList.push_back(locator);
130 attr.rtps.defaultOutLocatorList.push_back(locator);
131 attr.rtps.builtin.metatrafficUnicastLocatorList.push_back(locator);
132
133 locator.set_IP4_address(239, 255, 0, 1);
134 attr.rtps.builtin.metatrafficMulticastLocatorList.push_back(locator);
135
136 fastrtps_participant_ =
137 eprosima::fastrtps::Domain::createParticipant(attr, listener);
138 RETURN_IF_NULL(fastrtps_participant_);
139 eprosima::fastrtps::Domain::registerType(fastrtps_participant_, &type_);
140}
141
142} // namespace transport
143} // namespace cyber
144} // namespace apollo
eprosima::fastrtps::Participant * fastrtps_participant()
Participant(const std::string &name, int send_port, eprosima::fastrtps::ParticipantListener *listener=nullptr)
#define RETURN_IF_NULL(ptr)
Definition log.h:90
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define RETURN_IF(condition)
Definition log.h:106
class register implement
Definition arena_queue.h:37