Apollo  6.0
Open source self driving car software
node.h
Go to the documentation of this file.
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 
28 namespace apollo {
29 namespace cyber {
30 
31 template <typename M0, typename M1, typename M2, typename M3>
32 class Component;
33 class TimerComponent;
34 
44 class 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*);
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 
156  void Observe();
157 
161  void ClearData();
162 
170  template <typename MessageT>
171  auto GetReader(const std::string& channel_name)
172  -> std::shared_ptr<Reader<MessageT>>;
173 
174  private:
175  explicit Node(const std::string& node_name,
176  const std::string& name_space = "");
177 
178  std::string node_name_;
179  std::string name_space_;
180 
181  std::mutex readers_mutex_;
182  std::map<std::string, std::shared_ptr<ReaderBase>> readers_;
183 
184  std::unique_ptr<NodeChannelImpl> node_channel_impl_ = nullptr;
185  std::unique_ptr<NodeServiceImpl> node_service_impl_ = nullptr;
186 };
187 
188 template <typename MessageT>
189 auto Node::CreateWriter(const proto::RoleAttributes& role_attr)
190  -> std::shared_ptr<Writer<MessageT>> {
191  return node_channel_impl_->template CreateWriter<MessageT>(role_attr);
192 }
193 
194 template <typename MessageT>
195 auto Node::CreateWriter(const std::string& channel_name)
196  -> std::shared_ptr<Writer<MessageT>> {
197  return node_channel_impl_->template CreateWriter<MessageT>(channel_name);
198 }
199 
200 template <typename MessageT>
201 auto Node::CreateReader(const proto::RoleAttributes& role_attr,
202  const CallbackFunc<MessageT>& reader_func)
203  -> std::shared_ptr<Reader<MessageT>> {
204  std::lock_guard<std::mutex> lg(readers_mutex_);
205  if (readers_.find(role_attr.channel_name()) != readers_.end()) {
206  AWARN << "Failed to create reader: reader with the same channel already "
207  "exists.";
208  return nullptr;
209  }
210  auto reader = node_channel_impl_->template CreateReader<MessageT>(
211  role_attr, reader_func);
212  if (reader != nullptr) {
213  readers_.emplace(std::make_pair(role_attr.channel_name(), reader));
214  }
215  return reader;
216 }
217 
218 template <typename MessageT>
219 auto Node::CreateReader(const ReaderConfig& config,
220  const CallbackFunc<MessageT>& reader_func)
221  -> std::shared_ptr<cyber::Reader<MessageT>> {
222  std::lock_guard<std::mutex> lg(readers_mutex_);
223  if (readers_.find(config.channel_name) != readers_.end()) {
224  AWARN << "Failed to create reader: reader with the same channel already "
225  "exists.";
226  return nullptr;
227  }
228  auto reader =
229  node_channel_impl_->template CreateReader<MessageT>(config, reader_func);
230  if (reader != nullptr) {
231  readers_.emplace(std::make_pair(config.channel_name, reader));
232  }
233  return reader;
234 }
235 
236 template <typename MessageT>
237 auto Node::CreateReader(const std::string& channel_name,
238  const CallbackFunc<MessageT>& reader_func)
239  -> std::shared_ptr<Reader<MessageT>> {
240  std::lock_guard<std::mutex> lg(readers_mutex_);
241  if (readers_.find(channel_name) != readers_.end()) {
242  AWARN << "Failed to create reader: reader with the same channel already "
243  "exists.";
244  return nullptr;
245  }
246  auto reader = node_channel_impl_->template CreateReader<MessageT>(
247  channel_name, reader_func);
248  if (reader != nullptr) {
249  readers_.emplace(std::make_pair(channel_name, reader));
250  }
251  return reader;
252 }
253 
254 template <typename Request, typename Response>
256  const std::string& service_name,
258  service_callback) -> std::shared_ptr<Service<Request, Response>> {
259  return node_service_impl_->template CreateService<Request, Response>(
260  service_name, service_callback);
261 }
262 
263 template <typename Request, typename Response>
264 auto Node::CreateClient(const std::string& service_name)
265  -> std::shared_ptr<Client<Request, Response>> {
266  return node_service_impl_->template CreateClient<Request, Response>(
267  service_name);
268 }
269 
270 template <typename MessageT>
271 auto Node::GetReader(const std::string& name)
272  -> std::shared_ptr<Reader<MessageT>> {
273  std::lock_guard<std::mutex> lg(readers_mutex_);
274  auto it = readers_.find(name);
275  if (it != readers_.end()) {
276  return std::dynamic_pointer_cast<Reader<MessageT>>(it->second);
277  }
278  return nullptr;
279 }
280 
281 } // namespace cyber
282 } // namespace apollo
283 
284 #endif // CYBER_NODE_NODE_H_
void ClearData()
clear all readers&#39; data
TimerComponent is a timer component. Your component can inherit from Component, and implement Init() ...
Definition: timer_component.h:35
Definition: node_channel_impl.h:37
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Reader subscribes a channel, it has two main functions:
Definition: reader.h:68
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:237
friend std::unique_ptr< Node > CreateNode(const std::string &, const std::string &)
std::function< void(const std::shared_ptr< Request > &, std::shared_ptr< Response > &)> ServiceCallback
Definition: service.h:45
Node is the fundamental building block of Cyber RT. every module contains and communicates through th...
Definition: node.h:44
void Observe()
Observe all readers&#39; data.
std::function< void(const std::shared_ptr< M0 > &)> CallbackFunc
Definition: reader.h:45
auto GetReader(const std::string &channel_name) -> std::shared_ptr< Reader< MessageT >>
Get the Reader object that subscribe channel_name
Definition: node.h:271
auto CreateWriter(const proto::RoleAttributes &role_attr) -> std::shared_ptr< Writer< MessageT >>
Create a Writer with specific message type.
Definition: node.h:189
friend bool Init(const char *)
#define AWARN
Definition: log.h:43
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:255
const std::string & Name() const
Get node&#39;s name.
The Component can process up to four channels of messages. The message type is specified when the com...
Definition: component.h:58
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:264