Apollo 10.0
自动驾驶开放平台
transport.h
浏览该文件的文档.
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
17#ifndef CYBER_TRANSPORT_TRANSPORT_H_
18#define CYBER_TRANSPORT_TRANSPORT_H_
19
20#include <atomic>
21#include <memory>
22#include <string>
23
24#include "cyber/proto/transport_conf.pb.h"
25
26#include "cyber/common/macros.h"
43
44namespace apollo {
45namespace cyber {
46namespace transport {
47
49
50class Transport {
51 public:
52 virtual ~Transport();
53
54 void Shutdown();
55
56 template <typename M>
57 auto CreateTransmitter(const RoleAttributes& attr,
58 const OptionalMode& mode = OptionalMode::HYBRID) ->
59 typename std::shared_ptr<Transmitter<M>>;
60
61 template <typename M>
62 auto CreateReceiver(const RoleAttributes& attr,
63 const typename Receiver<M>::MessageListener& msg_listener,
64 const OptionalMode& mode = OptionalMode::HYBRID) ->
65 typename std::shared_ptr<Receiver<M>>;
66
67 ParticipantPtr participant() const { return participant_; }
68
69 private:
70 void CreateParticipant();
71
72 std::atomic<bool> is_shutdown_ = {false};
73 ParticipantPtr participant_ = nullptr;
74 NotifierPtr notifier_ = nullptr;
75 IntraDispatcherPtr intra_dispatcher_ = nullptr;
76 ShmDispatcherPtr shm_dispatcher_ = nullptr;
77 RtpsDispatcherPtr rtps_dispatcher_ = nullptr;
78
79 DECLARE_SINGLETON(Transport)
80};
81
82template <typename M>
84 const OptionalMode& mode) ->
85 typename std::shared_ptr<Transmitter<M>> {
86 if (is_shutdown_.load()) {
87 AINFO << "transport has been shut down.";
88 return nullptr;
89 }
90
91 std::shared_ptr<Transmitter<M>> transmitter = nullptr;
92 RoleAttributes modified_attr = attr;
93 if (!modified_attr.has_qos_profile()) {
94 modified_attr.mutable_qos_profile()->CopyFrom(
96 }
97
98 switch (mode) {
99 case OptionalMode::INTRA:
100 transmitter = std::make_shared<IntraTransmitter<M>>(modified_attr);
101 break;
102
103 case OptionalMode::SHM:
104 transmitter = std::make_shared<ShmTransmitter<M>>(modified_attr);
105 break;
106
107 case OptionalMode::RTPS:
108 transmitter =
109 std::make_shared<RtpsTransmitter<M>>(modified_attr, participant());
110 break;
111
112 default:
113 transmitter =
114 std::make_shared<HybridTransmitter<M>>(modified_attr, participant());
115 break;
116 }
117
118 RETURN_VAL_IF_NULL(transmitter, nullptr);
119 if (mode != OptionalMode::HYBRID) {
120 transmitter->Enable();
121 }
122 return transmitter;
123}
124
125template <typename M>
127 const RoleAttributes& attr,
128 const typename Receiver<M>::MessageListener& msg_listener,
129 const OptionalMode& mode) -> typename std::shared_ptr<Receiver<M>> {
130 if (is_shutdown_.load()) {
131 AINFO << "transport has been shut down.";
132 return nullptr;
133 }
134
135 std::shared_ptr<Receiver<M>> receiver = nullptr;
136 RoleAttributes modified_attr = attr;
137 if (!modified_attr.has_qos_profile()) {
138 modified_attr.mutable_qos_profile()->CopyFrom(
140 }
141
142 switch (mode) {
143 case OptionalMode::INTRA:
144 receiver =
145 std::make_shared<IntraReceiver<M>>(modified_attr, msg_listener);
146 break;
147
148 case OptionalMode::SHM:
149 receiver = std::make_shared<ShmReceiver<M>>(modified_attr, msg_listener);
150 break;
151
152 case OptionalMode::RTPS:
153 receiver = std::make_shared<RtpsReceiver<M>>(modified_attr, msg_listener);
154 break;
155
156 default:
157 receiver = std::make_shared<HybridReceiver<M>>(
158 modified_attr, msg_listener, participant());
159 break;
160 }
161
162 RETURN_VAL_IF_NULL(receiver, nullptr);
163 if (mode != OptionalMode::HYBRID) {
164 receiver->Enable();
165 }
166 return receiver;
167}
168
169} // namespace transport
170} // namespace cyber
171} // namespace apollo
172
173#endif // CYBER_TRANSPORT_TRANSPORT_H_
std::function< void(const MessagePtr &, const MessageInfo &, const RoleAttributes &)> MessageListener
Definition receiver.h:36
ParticipantPtr participant() const
Definition transport.h:67
auto CreateReceiver(const RoleAttributes &attr, const typename Receiver< M >::MessageListener &msg_listener, const OptionalMode &mode=OptionalMode::HYBRID) -> typename std::shared_ptr< Receiver< M > >
Definition transport.h:126
auto CreateTransmitter(const RoleAttributes &attr, const OptionalMode &mode=OptionalMode::HYBRID) -> typename std::shared_ptr< Transmitter< M > >
Definition transport.h:83
#define DECLARE_SINGLETON(classname)
Definition macros.h:52
#define RETURN_VAL_IF_NULL(ptr, val)
Definition log.h:98
#define AINFO
Definition log.h:42
std::shared_ptr< Participant > ParticipantPtr
RtpsDispatcher * RtpsDispatcherPtr
ShmDispatcher * ShmDispatcherPtr
NotifierBase * NotifierPtr
IntraDispatcher * IntraDispatcherPtr
class register implement
Definition arena_queue.h:37