Apollo 10.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/client in the topo.

在文件 node.h44 行定义.

构造及析构函数说明

◆ ~Node()

Node::~Node ( )
virtual

在文件 node.cc28 行定义.

28{}

成员函数说明

◆ ClearData()

void Node::ClearData ( )

clear all readers' data

在文件 node.cc38 行定义.

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

◆ 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.cc51 行定义.

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

◆ DeleteReader() [2/3]

bool Node::DeleteReader ( const ReaderConfig config)

在文件 node.cc58 行定义.

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

◆ DeleteReader() [3/3]

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

在文件 node.cc44 行定义.

44 {
45 std::lock_guard<std::mutex> lg(readers_mutex_);
46 int result = readers_.erase(channel_name);
47 if (1 == result) return true;
48 return false;
49}

◆ 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.cc30 行定义.

30{ return node_name_; }

◆ Observe()

void Node::Observe ( )

Observe all readers' data

在文件 node.cc32 行定义.

32 {
33 for (auto& reader : readers_) {
34 reader.second->Observe();
35 }
36}

友元及相关函数文档

◆ 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.cc98 行定义.

98 {
99 const char* apollo_runtime_path = std::getenv("APOLLO_RUNTIME_PATH");
100 if (apollo_runtime_path != nullptr) {
101 if (std::filesystem::is_directory(
102 std::filesystem::status(apollo_runtime_path))) {
103 std::filesystem::current_path(apollo_runtime_path);
104 }
105 }
106
107 std::lock_guard<std::mutex> lg(g_mutex);
108 if (GetState() != STATE_UNINITIALIZED) {
109 return false;
110 }
111
112 InitLogger(binary_name);
113 auto thread = const_cast<std::thread*>(async_logger->LogThread());
114 scheduler::Instance()->SetInnerThreadAttr("async_log", thread);
115 SysMo::Instance();
116 std::signal(SIGINT, OnShutdown);
117 // Register exit handlers
118 if (!g_atexit_registered) {
119 if (std::atexit(ExitHandle) != 0) {
120 AERROR << "Register exit handle failed";
121 return false;
122 }
123 AINFO << "Register exit handle succ.";
124 g_atexit_registered = true;
125 }
127
128 auto global_data = GlobalData::Instance();
129 if (global_data->IsMockTimeMode()) {
130 auto node_name = kClockNode + std::to_string(getpid());
131 clock_node = std::unique_ptr<Node>(new Node(node_name));
132 auto cb =
133 [](const std::shared_ptr<const apollo::cyber::proto::Clock>& msg) {
134 if (msg->has_clock()) {
135 Clock::Instance()->SetNow(Time(msg->clock()));
136 }
137 };
138 clock_node->CreateReader<apollo::cyber::proto::Clock>(kClockChannel, cb);
139 }
140
141 if (dag_info != "") {
142 std::string dump_path;
143 if (dag_info.length() > 200) {
144 std::string truncated = dag_info.substr(0, 200);
145 dump_path = common::GetEnv("APOLLO_ENV_WORKROOT", "/apollo") + "/dumps/" +
146 truncated;
147 } else {
148 dump_path = common::GetEnv("APOLLO_ENV_WORKROOT", "/apollo") + "/dumps/" +
149 dag_info;
150 }
151 google::SetCommandLineOption("bvar_dump_file", dump_path.c_str());
152 } else {
153 statistics::Statistics::Instance()->DisableChanVar();
154 }
155 google::SetCommandLineOption("bvar_dump_exclude", "*qps");
156 google::SetCommandLineOption("bvar_dump", "true");
157
158 return true;
159}
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:89
void SetState(const State &state)
Definition state.cc:30
void ExitHandle()
Definition init.cc:96
@ 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 行定义.


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