Apollo 10.0
自动驾驶开放平台
node.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_NODE_NODE_H_
18#define CYBER_NODE_NODE_H_
19
20#include <map>
21#include <memory>
22#include <string>
23#include <utility>
24
27
28namespace apollo {
29namespace cyber {
30
31template <typename M0, typename M1, typename M2, typename M3>
32class Component;
33class TimerComponent;
34
44class Node {
45 public:
46 template <typename M0, typename M1, typename M2, typename M3>
47 friend class Component;
48 friend class TimerComponent;
49 friend bool Init(const char*, const std::string&);
50 friend std::unique_ptr<Node> CreateNode(const std::string&,
51 const std::string&);
52 virtual ~Node();
53
58 const std::string& Name() const;
59
68 template <typename MessageT>
69 auto CreateWriter(const proto::RoleAttributes& role_attr)
70 -> std::shared_ptr<Writer<MessageT>>;
71
79 template <typename MessageT>
80 auto CreateWriter(const std::string& channel_name)
81 -> std::shared_ptr<Writer<MessageT>>;
82
93 template <typename MessageT>
94 auto CreateReader(const std::string& channel_name,
95 const CallbackFunc<MessageT>& reader_func = nullptr)
96 -> std::shared_ptr<cyber::Reader<MessageT>>;
97
107 template <typename MessageT>
108 auto CreateReader(const ReaderConfig& config,
109 const CallbackFunc<MessageT>& reader_func = nullptr)
110 -> std::shared_ptr<cyber::Reader<MessageT>>;
111
121 template <typename MessageT>
122 auto CreateReader(const proto::RoleAttributes& role_attr,
123 const CallbackFunc<MessageT>& reader_func = nullptr)
124 -> std::shared_ptr<cyber::Reader<MessageT>>;
125
135 template <typename Request, typename Response>
136 auto CreateService(const std::string& service_name,
138 service_callback)
139 -> std::shared_ptr<Service<Request, Response>>;
140
149 template <typename Request, typename Response>
150 auto CreateClient(const std::string& service_name)
151 -> std::shared_ptr<Client<Request, Response>>;
152
153 bool DeleteReader(const std::string& channel_name);
154 bool DeleteReader(const ReaderConfig& config);
155 bool DeleteReader(const proto::RoleAttributes& role_attr);
159 void Observe();
160
164 void ClearData();
165
173 template <typename MessageT>
174 auto GetReader(const std::string& channel_name)
175 -> std::shared_ptr<Reader<MessageT>>;
176
177 private:
178 explicit Node(const std::string& node_name,
179 const std::string& name_space = "");
180
181 std::string node_name_;
182 std::string name_space_;
183
184 std::mutex readers_mutex_;
185 std::map<std::string, std::shared_ptr<ReaderBase>> readers_;
186
187 std::unique_ptr<NodeChannelImpl> node_channel_impl_ = nullptr;
188 std::unique_ptr<NodeServiceImpl> node_service_impl_ = nullptr;
189};
190
191template <typename MessageT>
193 -> std::shared_ptr<Writer<MessageT>> {
194 return node_channel_impl_->template CreateWriter<MessageT>(role_attr);
195}
196
197template <typename MessageT>
198auto Node::CreateWriter(const std::string& channel_name)
199 -> std::shared_ptr<Writer<MessageT>> {
200 return node_channel_impl_->template CreateWriter<MessageT>(channel_name);
201}
202
203template <typename MessageT>
205 const CallbackFunc<MessageT>& reader_func)
206 -> std::shared_ptr<Reader<MessageT>> {
207 std::lock_guard<std::mutex> lg(readers_mutex_);
208 if (readers_.find(role_attr.channel_name()) != readers_.end()) {
209 AWARN << "Failed to create reader: reader with the same channel already "
210 "exists.";
211 return nullptr;
212 }
213 auto reader = node_channel_impl_->template CreateReader<MessageT>(
214 role_attr, reader_func);
215 if (reader != nullptr) {
216 readers_.emplace(std::make_pair(role_attr.channel_name(), reader));
217 }
218 return reader;
219}
220
221template <typename MessageT>
223 const CallbackFunc<MessageT>& reader_func)
224 -> std::shared_ptr<cyber::Reader<MessageT>> {
225 std::lock_guard<std::mutex> lg(readers_mutex_);
226 if (readers_.find(config.channel_name) != readers_.end()) {
227 AWARN << "Failed to create reader: reader with the same channel already "
228 "exists.";
229 return nullptr;
230 }
231 auto reader =
232 node_channel_impl_->template CreateReader<MessageT>(config, reader_func);
233 if (reader != nullptr) {
234 readers_.emplace(std::make_pair(config.channel_name, reader));
235 }
236 return reader;
237}
238
239template <typename MessageT>
240auto Node::CreateReader(const std::string& channel_name,
241 const CallbackFunc<MessageT>& reader_func)
242 -> std::shared_ptr<Reader<MessageT>> {
243 std::lock_guard<std::mutex> lg(readers_mutex_);
244 if (readers_.find(channel_name) != readers_.end()) {
245 AWARN << "Failed to create reader: reader with the same channel already "
246 "exists.";
247 return nullptr;
248 }
249 auto reader = node_channel_impl_->template CreateReader<MessageT>(
250 channel_name, reader_func);
251 if (reader != nullptr) {
252 readers_.emplace(std::make_pair(channel_name, reader));
253 }
254 return reader;
255}
256
257template <typename Request, typename Response>
259 const std::string& service_name,
261 service_callback) -> std::shared_ptr<Service<Request, Response>> {
262 return node_service_impl_->template CreateService<Request, Response>(
263 service_name, service_callback);
264}
265
266template <typename Request, typename Response>
267auto Node::CreateClient(const std::string& service_name)
268 -> std::shared_ptr<Client<Request, Response>> {
269 return node_service_impl_->template CreateClient<Request, Response>(
270 service_name);
271}
272
273template <typename MessageT>
274auto Node::GetReader(const std::string& name)
275 -> std::shared_ptr<Reader<MessageT>> {
276 std::lock_guard<std::mutex> lg(readers_mutex_);
277 auto it = readers_.find(name);
278 if (it != readers_.end()) {
279 return std::dynamic_pointer_cast<Reader<MessageT>>(it->second);
280 }
281 return nullptr;
282}
283
284} // namespace cyber
285} // namespace apollo
286
287#endif // CYBER_NODE_NODE_H_
Node is the fundamental building block of Cyber RT.
Definition node.h:44
bool DeleteReader(const std::string &channel_name)
Definition node.cc:44
auto CreateReader(const std::string &channel_name, const CallbackFunc< MessageT > &reader_func=nullptr) -> std::shared_ptr< cyber::Reader< MessageT > >
Create a Reader with specific message type with channel name qos and other configs used will be defau...
Definition node.h:240
friend std::unique_ptr< Node > CreateNode(const std::string &, const std::string &)
Definition cyber.cc:33
auto GetReader(const std::string &channel_name) -> std::shared_ptr< Reader< MessageT > >
Get the Reader object that subscribe channel_name
Definition node.h:274
void ClearData()
clear all readers' data
Definition node.cc:38
auto CreateClient(const std::string &service_name) -> std::shared_ptr< Client< Request, Response > >
Create a Client object to request Service with service_name
Definition node.h:267
const std::string & Name() const
Get node's name.
Definition node.cc:30
virtual ~Node()
Definition node.cc:28
auto CreateService(const std::string &service_name, const typename Service< Request, Response >::ServiceCallback &service_callback) -> std::shared_ptr< Service< Request, Response > >
Create a Service object with specific service_name
Definition node.h:258
friend bool Init(const char *, const std::string &)
Definition init.cc:98
auto CreateWriter(const proto::RoleAttributes &role_attr) -> std::shared_ptr< Writer< MessageT > >
Create a Writer with specific message type.
Definition node.h:192
void Observe()
Observe all readers' data
Definition node.cc:32
std::function< void(const std::shared_ptr< Request > &, std::shared_ptr< Response > &)> ServiceCallback
Definition service.h:45
#define AWARN
Definition log.h:43
std::function< void(const std::shared_ptr< M0 > &)> CallbackFunc
Definition reader.h:46
class register implement
Definition arena_queue.h:37