Apollo 10.0
自动驾驶开放平台
module_controller.cc
浏览该文件的文档.
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
18
19#include <utility>
20
22#include "cyber/common/file.h"
25
26namespace apollo {
27namespace cyber {
28namespace mainboard {
29
31 for (auto& component : component_list_) {
32 component->Shutdown();
33 }
34 component_list_.clear(); // keep alive
35 class_loader_manager_.UnloadAllLibrary();
36}
37
39 const std::string work_root = common::WorkRoot();
40 const std::string current_path = common::GetCurrentPath();
41 const std::string dag_root_path = common::GetAbsolutePath(work_root, "dag");
42 std::vector<std::string> paths;
43 for (auto& plugin_description : args_.GetPluginDescriptionList()) {
45 plugin_description);
46 }
47 if (!args_.GetDisablePluginsAutoLoad()) {
50 }
51 for (auto& dag_conf : args_.GetDAGConfList()) {
52 std::string module_path = dag_conf;
53 if (!common::GetFilePathWithEnv(dag_conf, "APOLLO_DAG_PATH",
54 &module_path)) {
55 AERROR << "no dag conf [" << dag_conf << "] found!";
56 return false;
57 }
58 AINFO << "mainboard: use dag conf " << module_path;
59 total_component_nums += GetComponentNum(module_path);
60 paths.emplace_back(std::move(module_path));
61 }
62 if (has_timer_component) {
63 total_component_nums += scheduler::Instance()->TaskPoolSize();
64 }
65 common::GlobalData::Instance()->SetComponentNums(total_component_nums);
66 for (auto module_path : paths) {
67 AINFO << "Start initialize dag: " << module_path;
68 if (!LoadModule(module_path)) {
69 AERROR << "Failed to load module: " << module_path;
70 return false;
71 }
72 }
73 return true;
74}
75
76bool ModuleController::LoadModule(const DagConfig& dag_config) {
77 for (auto module_config : dag_config.module_config()) {
78 std::string load_path;
79 if (!common::GetFilePathWithEnv(module_config.module_library(),
80 "APOLLO_LIB_PATH", &load_path)) {
81 AERROR << "no module library [" << module_config.module_library()
82 << "] found!";
83 return false;
84 }
85 AINFO << "mainboard: use module library " << load_path;
86
87 class_loader_manager_.LoadLibrary(load_path);
88
89 for (auto& component : module_config.components()) {
90 const std::string& class_name = component.class_name();
91 std::shared_ptr<ComponentBase> base =
92 class_loader_manager_.CreateClassObj<ComponentBase>(class_name);
93 if (base == nullptr || !base->Initialize(component.config())) {
94 return false;
95 }
96 component_list_.emplace_back(std::move(base));
97 }
98
99 for (auto& component : module_config.timer_components()) {
100 const std::string& class_name = component.class_name();
101 std::shared_ptr<ComponentBase> base =
102 class_loader_manager_.CreateClassObj<ComponentBase>(class_name);
103 if (base == nullptr || !base->Initialize(component.config())) {
104 return false;
105 }
106 component_list_.emplace_back(std::move(base));
107 }
108 }
109 return true;
110}
111
112bool ModuleController::LoadModule(const std::string& path) {
113 DagConfig dag_config;
114 if (!common::GetProtoFromFile(path, &dag_config)) {
115 AERROR << "Get proto failed, file: " << path;
116 return false;
117 }
118 return LoadModule(dag_config);
119}
120
121int ModuleController::GetComponentNum(const std::string& path) {
122 DagConfig dag_config;
123 int component_nums = 0;
124 if (common::GetProtoFromFile(path, &dag_config)) {
125 for (auto module_config : dag_config.module_config()) {
126 component_nums += module_config.components_size();
127 if (module_config.timer_components_size() > 0) {
128 has_timer_component = true;
129 }
130 }
131 }
132 return component_nums;
133}
134
135} // namespace mainboard
136} // namespace cyber
137} // namespace apollo
std::shared_ptr< Base > CreateClassObj(const std::string &class_name)
bool LoadLibrary(const std::string &library_path)
const std::list< std::string > & GetDAGConfList() const
const std::list< std::string > & GetPluginDescriptionList() const
static PluginManager * Instance()
get singleton instance of PluginManager
bool LoadInstalledPlugins()
load plugins from installed path
bool LoadPlugin(const std::string &plugin_description_file_path)
load plugin clases from file
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::string GetCurrentPath()
Definition file.cc:488
bool GetFilePathWithEnv(const std::string &path, const std::string &env_var, std::string *file_path)
get file path, judgement priority:
Definition file.cc:436
std::string GetAbsolutePath(const std::string &prefix, const std::string &relative_path)
Get absolute path by concatenating prefix and relative_path.
Definition file.cc:179
const std::string WorkRoot()
Definition environment.h:40
class register implement
Definition arena_queue.h:37