Apollo 11.0
自动驾驶开放平台
apollo::cyber::Node类 参考

Node is the fundamental building block of Cyber RT. 更多...

#include <node.h>

apollo::cyber::Node 的协作图:

Public 成员函数

virtual ~Node ()
 
const std::string & Name () const
 Get node's name.
 
template<typename MessageT >
auto CreateWriter (const proto::RoleAttributes &role_attr) -> std::shared_ptr< Writer< MessageT > >
 Create a Writer with specific message type.
 
template<typename MessageT >
auto CreateWriter (const std::string &channel_name) -> std::shared_ptr< Writer< MessageT > >
 Create a Writer with specific message type.
 
template<typename MessageT >
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 default
 
template<typename MessageT >
auto CreateReader (const ReaderConfig &config, const CallbackFunc< MessageT > &reader_func=nullptr) -> std::shared_ptr< cyber::Reader< MessageT > >
 Create a Reader with specific message type with reader config
 
template<typename MessageT >
auto CreateReader (const proto::RoleAttributes &role_attr, const CallbackFunc< MessageT > &reader_func=nullptr) -> std::shared_ptr< cyber::Reader< MessageT > >
 Create a Reader object with RoleAttributes
 
template<typename Request , typename Response >
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
 
template<typename Request , typename Response >
auto CreateClient (const std::string &service_name) -> std::shared_ptr< Client< Request, Response > >
 Create a Client object to request Service with service_name
 
bool DeleteReader (const std::string &channel_name)
 
bool DeleteReader (const ReaderConfig &config)
 
bool DeleteReader (const proto::RoleAttributes &role_attr)
 
void Observe ()
 Observe all readers' data
 
void ClearData ()
 clear all readers' data
 
template<typename MessageT >
auto GetReader (const std::string &channel_name) -> std::shared_ptr< Reader< MessageT > >
 Get the Reader object that subscribe channel_name
 

友元

template<typename M0 , typename M1 , typename M2 , typename M3 >
class Component
 
class TimerComponent
 
bool Init (const char *, const std::string &)
 
std::unique_ptr< NodeCreateNode (const std::string &, const std::string &)
 

详细描述

Node is the fundamental building block of Cyber RT.

every module contains and communicates through the node. A module can have different types of communication by defining read/write and/or service/client in a node.

警告
Duplicate name is not allowed in topo objects, such as node, reader/writer, service/clinet in the topo.

在文件 node.h44 行定义.

构造及析构函数说明

◆ ~Node()

Node::~Node ( )
virtual

在文件 node.cc33 行定义.

33{}

成员函数说明

◆ ClearData()

void Node::ClearData ( )

clear all readers' data

在文件 node.cc43 行定义.

43 {
44 for (auto& reader : readers_) {
45 reader.second->ClearData();
46 }
47}

◆ CreateClient()

template<typename Request , typename Response >
auto Node::CreateClient ( const std::string &  service_name) -> std::shared_ptr<Client<Request, Response>>

Create a Client object to request Service with service_name

模板参数
RequestMessage Type of the Request
ResponseMessage Type of the Response
参数
service_namespecific service name to a Service
返回
std::shared_ptr<Client<Request, Response>> result Client

在文件 node.h267 行定义.

268 {
269 return node_service_impl_->template CreateClient<Request, Response>(
270 service_name);
271}

◆ CreateReader() [1/3]

template<typename MessageT >
auto Node::CreateReader ( const proto::RoleAttributes role_attr,
const CallbackFunc< MessageT > &  reader_func = nullptr 
) -> std::shared_ptr<cyber::Reader<MessageT>>

Create a Reader object with RoleAttributes

模板参数
MessageTMessage Type
参数
role_attrinstance of RoleAttributes, includes channel name, qos, etc.
reader_funcinvoked when message receive
返回
std::shared_ptr<cyber::Reader<MessageT>> result Reader Object

在文件 node.h204 行定义.

206 {
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}
#define AWARN
Definition log.h:43

◆ CreateReader() [2/3]

template<typename MessageT >
auto Node::CreateReader ( const ReaderConfig config,
const CallbackFunc< MessageT > &  reader_func = nullptr 
) -> std::shared_ptr<cyber::Reader<MessageT>>

Create a Reader with specific message type with reader config

模板参数
MessageTMessage Type
参数
configinstance of ReaderConfig, include channel name, qos and pending queue size
reader_funcinvoked when message receive
返回
std::shared_ptr<cyber::Reader<MessageT>> result Reader Object

在文件 node.h222 行定义.

224 {
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}

◆ CreateReader() [3/3]

template<typename MessageT >
auto Node::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 default

模板参数
MessageTMessage Type
参数
channel_namethe channel of the reader subscribed.
reader_funcinvoked when message receive invoked when the message is received.
返回
std::shared_ptr<cyber::Reader<MessageT>> result Reader Object

在文件 node.h240 行定义.

242 {
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}

◆ CreateService()

template<typename Request , typename Response >
auto Node::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

模板参数
RequestMessage Type of the Request
ResponseMessage Type of the Response
参数
service_namespecific service name to a serve
service_callbackinvoked when a service is called
返回
std::shared_ptr<Service<Request, Response>> result Service

在文件 node.h258 行定义.

261 {
262 return node_service_impl_->template CreateService<Request, Response>(
263 service_name, service_callback);
264}

◆ CreateWriter() [1/2]

template<typename MessageT >
auto Node::CreateWriter ( const proto::RoleAttributes role_attr) -> std::shared_ptr<Writer<MessageT>>

Create a Writer with specific message type.

模板参数
MessageTMessage Type
参数
role_attris a protobuf message RoleAttributes, which includes the channel name and other info.
返回
std::shared_ptr<Writer<MessageT>> result Writer Object

在文件 node.h192 行定义.

193 {
194 return node_channel_impl_->template CreateWriter<MessageT>(role_attr);
195}

◆ CreateWriter() [2/2]

template<typename MessageT >
auto Node::CreateWriter ( const std::string &  channel_name) -> std::shared_ptr<Writer<MessageT>>

Create a Writer with specific message type.

模板参数
MessageTMessage Type
参数
channel_namethe channel name to be published.
返回
std::shared_ptr<Writer<MessageT>> result Writer Object

在文件 node.h198 行定义.

199 {
200 return node_channel_impl_->template CreateWriter<MessageT>(channel_name);
201}

◆ DeleteReader() [1/3]

bool Node::DeleteReader ( const proto::RoleAttributes role_attr)

在文件 node.cc56 行定义.

56 {
57 std::lock_guard<std::mutex> lg(readers_mutex_);
58 int result = readers_.erase(role_attr.channel_name());
59 if (1 == result) return true;
60 return false;
61}

◆ DeleteReader() [2/3]

bool Node::DeleteReader ( const ReaderConfig config)

在文件 node.cc63 行定义.

63 {
64 std::lock_guard<std::mutex> lg(readers_mutex_);
65 int result = readers_.erase(config.channel_name);
66 if (1 == result) return true;
67 return false;
68}

◆ DeleteReader() [3/3]

bool Node::DeleteReader ( const std::string &  channel_name)

在文件 node.cc49 行定义.

49 {
50 std::lock_guard<std::mutex> lg(readers_mutex_);
51 int result = readers_.erase(channel_name);
52 if (1 == result) return true;
53 return false;
54}

◆ GetReader()

template<typename MessageT >
auto Node::GetReader ( const std::string &  channel_name) -> std::shared_ptr<Reader<MessageT>>

Get the Reader object that subscribe channel_name

模板参数
MessageTMessage Type
参数
channel_namechannel name
返回
std::shared_ptr<Reader<MessageT>> result reader

在文件 node.h274 行定义.

275 {
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}

◆ Name()

const std::string & Node::Name ( ) const

Get node's name.

警告
duplicate node name is not allowed in the topo.

在文件 node.cc35 行定义.

35{ return node_name_; }

◆ Observe()

void Node::Observe ( )

Observe all readers' data

在文件 node.cc37 行定义.

37 {
38 for (auto& reader : readers_) {
39 reader.second->Observe();
40 }
41}

友元及相关函数文档

◆ Component

template<typename M0 , typename M1 , typename M2 , typename M3 >
friend class Component
friend

在文件 node.h47 行定义.

◆ CreateNode

std::unique_ptr< Node > CreateNode ( const std::string &  ,
const std::string &   
)
friend

在文件 cyber.cc33 行定义.

34 {
35 bool is_reality_mode = GlobalData::Instance()->IsRealityMode();
36 if (is_reality_mode && !OK()) {
37 // add some hint log
38 AERROR << "please initialize cyber firstly.";
39 return nullptr;
40 }
41 return std::unique_ptr<Node>(new Node(node_name, name_space));
42}
Definition node.h:31
#define AERROR
Definition log.h:44
bool OK()
Definition state.h:44

◆ Init

bool Init ( const char *  ,
const std::string &   
)
friend

在文件 init.cc99 行定义.

99 {
100 std::lock_guard<std::mutex> lg(g_mutex);
101 if (GetState() != STATE_UNINITIALIZED) {
102 return false;
103 }
104
105 InitLogger(binary_name);
106 auto thread = const_cast<std::thread*>(async_logger->LogThread());
107 scheduler::Instance()->SetInnerThreadAttr("async_log", thread);
108 SysMo::Instance();
109 std::signal(SIGINT, OnShutdown);
110 // Register exit handlers
111 if (!g_atexit_registered) {
112 if (std::atexit(ExitHandle) != 0) {
113 AERROR << "Register exit handle failed";
114 return false;
115 }
116 AINFO << "Register exit handle succ.";
117 g_atexit_registered = true;
118 }
120
121 auto global_data = GlobalData::Instance();
122 if (global_data->IsMockTimeMode()) {
123 auto node_name = kClockNode + std::to_string(getpid());
124 clock_node = std::unique_ptr<Node>(new Node(node_name));
125 auto cb =
126 [](const std::shared_ptr<const apollo::cyber::proto::Clock>& msg) {
127 if (msg->has_clock()) {
128 Clock::Instance()->SetNow(Time(msg->clock()));
129 }
130 };
131 clock_node->CreateReader<apollo::cyber::proto::Clock>(kClockChannel, cb);
132 }
133
134 if (dag_info != "") {
135 std::string dump_path;
136 if (dag_info.length() > 200) {
137 std::string truncated = dag_info.substr(0, 200);
138 dump_path = common::GetEnv(
139 "APOLLO_ENV_WORKROOT", "/apollo") + "/dumps/" + truncated;
140 } else {
141 dump_path = \
142 common::GetEnv("APOLLO_ENV_WORKROOT", "/apollo") + "/dumps/" + dag_info;
143 }
144 google::SetCommandLineOption("bvar_dump_file", dump_path.c_str());
145 } else {
146 statistics::Statistics::Instance()->DisableChanVar();
147 }
148 google::SetCommandLineOption("bvar_dump_exclude", "*qps");
149 google::SetCommandLineOption("bvar_dump", "true");
150
151 return true;
152}
Cyber has builtin time type Time.
Definition time.h:31
void SetInnerThreadAttr(const std::string &name, std::thread *thr)
Definition scheduler.cc:90
#define AINFO
Definition log.h:42
std::string GetEnv(const std::string &var_name, const std::string &default_value="")
Definition environment.h:29
void OnShutdown(int sig)
Definition init.cc:90
void SetState(const State &state)
Definition state.cc:30
void ExitHandle()
Definition init.cc:97
@ STATE_INITIALIZED
Definition state.h:36
@ STATE_UNINITIALIZED
Definition state.h:35
State GetState()
Definition state.cc:28

◆ TimerComponent

friend class TimerComponent
friend

在文件 node.h48 行定义.


该类的文档由以下文件生成: