Apollo 10.0
自动驾驶开放平台
dv_plugin_manager.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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
19namespace apollo {
20namespace dreamview {
21
24
26 AutoLoadPlugins();
27 GetPluginClassNames();
28 CreatePluginInstances();
29}
30
31void DvPluginManager::Start() { RunInstances(); }
32
33bool DvPluginManager::AutoLoadPlugins() {
34 if (!PluginManager::Instance()->LoadInstalledPlugins()) {
35 AERROR << "Load plugins failed!";
36 return false;
37 }
38 AERROR << "pluginmanager index: " << PluginManager::Instance();
39 return true;
40}
41
42void DvPluginManager::GetPluginClassNames() {
43 derived_class_names_ =
44 PluginManager::Instance()->GetDerivedClassNameByBaseClass<DvPluginBase>();
45}
46
47bool DvPluginManager::CreatePluginInstances() {
48 if (derived_class_names_.empty()) {
49 AERROR
50 << "There be seems no dreamview_plus plugins, please install plugins.";
51 return false;
52 }
53 for (const auto &class_name : derived_class_names_) {
54 CreatePluginInstance(class_name);
55 }
56 return true;
57}
58
59bool DvPluginManager::CreatePluginInstance(const std::string class_name) {
60 auto dv_plugin_ptr =
61 PluginManager::Instance()->CreateInstance<DvPluginBase>(class_name);
62 if (dv_plugin_ptr != nullptr) {
63 dv_plugin_ptr->Init();
64 auto websocket_handlers = dv_plugin_ptr->GetWebSocketHandlers();
65 auto handlers = dv_plugin_ptr->GetHandlers();
66 auto updater_websocket_handlers = dv_plugin_ptr->GetUpdaterHandlers();
67 if (!websocket_handlers.empty()) {
68 RegisterWebsocketHandlers(websocket_handlers);
69 }
70 if (!handlers.empty()) {
71 RegisterHandlers(handlers);
72 }
73 if (!updater_websocket_handlers.empty()) {
74 auto plugin_data_handler_conf = dv_plugin_ptr->GetDataHandlerConf();
75 RegisterUpdaterHandlers(updater_websocket_handlers,
76 plugin_data_handler_conf);
77 }
78 plugin_instance_map_[class_name] = dv_plugin_ptr;
79 }
80 return true;
81}
82
83void DvPluginManager::RunInstances() {
84 for (const auto &instance : plugin_instance_map_) {
85 AINFO << "Instance: " << instance.first << " start running";
86 instance.second->Run();
87 }
88}
89
90void DvPluginManager::RegisterWebsocketHandlers(
91 std::map<std::string, WebSocketHandler *> &websocket_handler_map) {
92 for (auto &handler : websocket_handler_map) {
93 server_->addWebSocketHandler(handler.first, *handler.second);
94 }
95}
96
97void DvPluginManager::RegisterHandlers(
98 std::map<std::string, CivetHandler *> &hander_map) {
99 for (auto &handler : hander_map) {
100 server_->addHandler(handler.first, *handler.second);
101 }
102}
103
104void DvPluginManager::RegisterUpdaterHandlers(
105 std::map<std::string, UpdaterBase *> &updater_handler_map,
106 const DataHandlerConf &data_handler_conf) {
107 // 1. register updater
108 for (auto &handler : updater_handler_map) {
109 AINFO << "RegisterUpdaterHandlers: " << handler.first;
110 updater_manager_->RegisterUpdater(handler.first, handler.second);
111 }
112 // 2. register data handler info
113 for (auto &handler : data_handler_conf.data_handler_info()) {
114 if (data_handler_conf_.data_handler_info().find(handler.first) !=
116 AERROR << "There are duplicate updater handlers between dv plugins";
117 continue;
118 }
119 auto data_handler_info = data_handler_conf_.mutable_data_handler_info();
120 (*data_handler_info)[handler.first] = handler.second;
121 }
122 AINFO << "RegisterUpdaterHandlers: data_handler_conf_: "
123 << data_handler_conf_.DebugString();
124}
125
127 for (const auto &instance : plugin_instance_map_) {
128 AINFO << "Instance: " << instance.first << " stopped";
129 instance.second->Stop();
130 }
131}
132
133} // namespace dreamview
134} // namespace apollo
void Start()
Methods to run the plug-in management class.
void Init()
Method to initialize the plug-in management class.
apollo::dreamview::DataHandlerConf data_handler_conf_
void Stop()
Methods to stop the resource of plug-in management class.
void RegisterUpdater(std::string path_name, UpdaterBase *updater)
Register data updater by websocket path name.
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
map< string, DataHandlerInfo > data_handler_info