From 97949b2f4a1e894493c21f813f067d099f39176c Mon Sep 17 00:00:00 2001 From: fly_1997 Date: Sun, 28 Apr 2024 10:09:30 +0800 Subject: [PATCH 2/4] add error code and replace raw poniters with smart pointers --- oeAware.service | 2 +- src/common/message_protocol.h | 15 +- src/plugin_mgr/CMakeLists.txt | 4 +- src/plugin_mgr/config.cpp | 10 +- src/plugin_mgr/dep_handler.cpp | 43 ++- src/plugin_mgr/dep_handler.h | 25 +- src/plugin_mgr/error_code.cpp | 29 ++ src/plugin_mgr/error_code.h | 33 ++ src/plugin_mgr/instance_run_handler.cpp | 65 ++-- src/plugin_mgr/instance_run_handler.h | 30 +- src/plugin_mgr/main.cpp | 21 +- src/plugin_mgr/memory_store.h | 24 +- src/plugin_mgr/message_manager.cpp | 1 - src/plugin_mgr/message_manager.h | 9 +- src/plugin_mgr/plugin.h | 16 +- src/plugin_mgr/plugin_manager.cpp | 389 ++++++++++++++---------- src/plugin_mgr/plugin_manager.h | 55 ++-- 17 files changed, 443 insertions(+), 328 deletions(-) create mode 100644 src/plugin_mgr/error_code.cpp create mode 100644 src/plugin_mgr/error_code.h diff --git a/oeAware.service b/oeAware.service index 9260492..3ab4b69 100644 --- a/oeAware.service +++ b/oeAware.service @@ -4,7 +4,7 @@ After=network.target [Service] Type=simple -ExecStart=/usr/bin/oeAware /etc/oeAware/config.yaml +ExecStart=/usr/bin/oeaware /etc/oeAware/config.yaml ExecStop=kill -9 $MAINPID Restart=on-failure RestartSec=1 diff --git a/src/common/message_protocol.h b/src/common/message_protocol.h index 1226e0b..3771298 100644 --- a/src/common/message_protocol.h +++ b/src/common/message_protocol.h @@ -24,7 +24,6 @@ const int MAX_RECV_BUFF_SIZE = 16384; const int MAX_EVENT_SIZE = 1024; const int PROTOCOL_LENGTH_SIZE = sizeof(size_t); const int HEADER_LENGTH_SIZE = sizeof(size_t); - const int HEADER_STATE_OK = 0; const int HEADER_STATE_FAILED = 1; @@ -39,7 +38,8 @@ enum class Opt { QUERY_ALL_TOP, LIST, DOWNLOAD, - RESPONSE, + RESPONSE_OK, + RESPONSE_ERROR, SHUTDOWN, }; @@ -70,6 +70,9 @@ class Msg { void set_opt(Opt opt) { this->_opt = opt; } + Opt get_opt() const { + return this->_opt; + } private: Opt _opt; std::vector _payload; @@ -80,17 +83,17 @@ private: friend class boost::serialization::access; template void serialize(Archive &ar, const unsigned int version) { - ar & state_code; + ar & code; } public: void set_state_code(int code) { - this->state_code = code; + this->code = code; } int get_state_code() { - return this->state_code; + return this->code; } private: - int state_code; + int code; }; class MessageProtocol { diff --git a/src/plugin_mgr/CMakeLists.txt b/src/plugin_mgr/CMakeLists.txt index 09fa80a..a32c800 100644 --- a/src/plugin_mgr/CMakeLists.txt +++ b/src/plugin_mgr/CMakeLists.txt @@ -12,7 +12,7 @@ include_directories(/usr/include) include_directories(../common) link_directories(/usr/lib64) -add_executable (oeAware +add_executable (oeaware ${SOURCE} ) -target_link_libraries(oeAware common) +target_link_libraries(oeaware common) diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp index bd87937..b997b8f 100644 --- a/src/plugin_mgr/config.cpp +++ b/src/plugin_mgr/config.cpp @@ -52,14 +52,14 @@ bool Config::load(const std::string path) { YAML::Node enable_list = node["enable_list"]; if (enable_list.IsSequence()) { for (int i = 0; i < enable_list.size(); ++i) { - YAML::Node instances = enable_list[i]["instances"]; + YAML::Node plugin = enable_list[i]["name"]; std::string name = enable_list[i]["name"].as(); EnableItem enable_item(name); - if (instances.IsNull()) { + if (plugin.IsScalar()) { enable_item.set_enabled(true); - } else if (instances.IsSequence()) { - for (int j = 0; j < instances.size(); ++j) { - std::string i_name = instances[j]["name"].as(); + } else if (plugin.IsSequence()) { + for (int j = 0; j < plugin.size(); ++j) { + std::string i_name = plugin[j].as(); enable_item.add_instance(i_name); } } else { diff --git a/src/plugin_mgr/dep_handler.cpp b/src/plugin_mgr/dep_handler.cpp index 9a6ddc5..eff333c 100644 --- a/src/plugin_mgr/dep_handler.cpp +++ b/src/plugin_mgr/dep_handler.cpp @@ -13,13 +13,13 @@ #include #include -void DepHandler::add_arc_node(Node* node, const std::vector &dep_nodes) { - ArcNode *arc_head = node->head; +void DepHandler::add_arc_node(std::shared_ptr node, const std::vector &dep_nodes) { + std::shared_ptr arc_head = node->head; node->cnt = dep_nodes.size(); int real_cnt = 0; bool state = true; for (auto name : dep_nodes) { - ArcNode *tmp = new ArcNode(); + std::shared_ptr tmp = std::make_shared(); tmp->arc_name = name; tmp->node_name = node->name; tmp->next = arc_head->next; @@ -39,7 +39,7 @@ void DepHandler::add_arc_node(Node* node, const std::vector &dep_no void DepHandler::add_node(std::string name, std::vector dep_nodes) { - Node *cur_node = add_new_node(name); + std::shared_ptr cur_node = add_new_node(name); this->nodes[name] = cur_node; add_arc_node(cur_node, dep_nodes); change_arc_nodes(name, true); @@ -51,27 +51,24 @@ void DepHandler::del_node(std::string name) { } -Node* DepHandler::get_node(std::string name) { +std::shared_ptr DepHandler::get_node(std::string name) { return this->nodes[name]; } -Node* DepHandler::add_new_node(std::string name) { - Node *cur_node = new Node(name); - cur_node->head = new ArcNode(); - +std::shared_ptr DepHandler::add_new_node(std::string name) { + std::shared_ptr cur_node = std::make_shared(name); + cur_node->head = std::make_shared(); tail->next = cur_node; tail = cur_node; return cur_node; } - - -void DepHandler::del_node_and_arc_nodes(Node *node) { - Node *next = node->next; - ArcNode *arc = node->head; +void DepHandler::del_node_and_arc_nodes(std::shared_ptr node) { + std::shared_ptr next = node->next; + std::shared_ptr arc = node->head; while(arc) { - ArcNode *tmp = arc->next; + std::shared_ptr tmp = arc->next; if (arc != node->head){ std::string name = arc->arc_name; arc_nodes[name].erase(arc); @@ -79,19 +76,16 @@ void DepHandler::del_node_and_arc_nodes(Node *node) { arc_nodes.erase(name); } } - delete arc; - arc = tmp; - + arc = tmp; } - delete node; } void DepHandler::change_arc_nodes(std::string name, bool state) { if (!nodes[name]->state || !arc_nodes.count(name)) return; - std::unordered_map &mp = arc_nodes[name]; + std::unordered_map, bool> &mp = arc_nodes[name]; for (auto &vec : mp) { vec.second = state; if (nodes.count(vec.first->node_name)) { - Node *tmp = nodes[vec.first->node_name]; + std::shared_ptr tmp = nodes[vec.first->node_name]; if (state) { tmp->real_cnt++; if (tmp->real_cnt == tmp->cnt) { @@ -101,6 +95,7 @@ void DepHandler::change_arc_nodes(std::string name, bool state) { tmp->real_cnt--; tmp->state = false; } + change_arc_nodes(vec.first->node_name, state); } } } @@ -113,7 +108,7 @@ void DepHandler::query_all_top(std::vector> &query) { } void DepHandler::query_node_top(std::string name, std::vector> &query) { - ArcNode *p = nodes[name]->head; + std::shared_ptr p = nodes[name]->head; if (p->next == nullptr) { query.emplace_back(std::vector{name}); return; @@ -126,7 +121,7 @@ void DepHandler::query_node_top(std::string name, std::vector> &query) { if (!nodes.count(name)) return; - Node *p = nodes[name]; + std::shared_ptr p = nodes[name]; query.emplace_back(std::vector{name}); for (auto cur = p->head->next; cur != nullptr; cur = cur->next) { query.emplace_back(std::vector{name, cur->arc_name}); @@ -136,7 +131,7 @@ void DepHandler::query_node(std::string name, std::vector DepHandler::get_pre_dependencies(std::string name) { std::vector res; - std::queue q; + std::queue> q; q.push(nodes[name]); while (!q.empty()) { auto &node = q.front(); diff --git a/src/plugin_mgr/dep_handler.h b/src/plugin_mgr/dep_handler.h index 8ff91e2..cc8570a 100644 --- a/src/plugin_mgr/dep_handler.h +++ b/src/plugin_mgr/dep_handler.h @@ -16,9 +16,10 @@ #include #include #include +#include struct ArcNode { - ArcNode *next; + std::shared_ptr next; std::string arc_name; std::string node_name; ArcNode() : next(nullptr) {} @@ -26,8 +27,8 @@ struct ArcNode { // a instance node struct Node { - Node *next; - ArcNode *head; + std::shared_ptr next; + std::shared_ptr head; std::string name; int cnt; int real_cnt; @@ -39,10 +40,10 @@ struct Node { class DepHandler { public: DepHandler() { - this->head = new Node(); + this->head = std::make_shared(); this->tail = head; } - Node* get_node(std::string name); + std::shared_ptr get_node(std::string name); bool get_node_state(std::string name) { return this->nodes[name]->state; } @@ -64,15 +65,15 @@ public: } private: void query_node_top(std::string name, std::vector> &query); - void add_arc_node(Node* node, const std::vector &dep_nodes); + void add_arc_node(std::shared_ptr node, const std::vector &dep_nodes); void change_arc_nodes(std::string name, bool state); - void del_node_and_arc_nodes(Node *node); - Node* add_new_node(std::string name); + void del_node_and_arc_nodes(std::shared_ptr node); + std::shared_ptr add_new_node(std::string name); - std::unordered_map> arc_nodes; - std::unordered_map nodes; - Node * head; - Node *tail; + std::unordered_map, bool>> arc_nodes; + std::unordered_map> nodes; + std::shared_ptr head; + std::shared_ptr tail; }; #endif // !PLUGIN_MGR_DEP_HANDLER_H diff --git a/src/plugin_mgr/error_code.cpp b/src/plugin_mgr/error_code.cpp new file mode 100644 index 0000000..252e4c2 --- /dev/null +++ b/src/plugin_mgr/error_code.cpp @@ -0,0 +1,29 @@ +#include "error_code.h" + +const std::unordered_map ErrorText::error_codes = { + {ErrorCode::ENABLE_INSTANCE_NOT_LOAD, "instance is not loaded"}, + {ErrorCode::ENABLE_INSTANCE_UNAVAILABLE, "instance is unavailable"}, + {ErrorCode::ENABLE_INSTANCE_ALREADY_ENABLED, "instance is already enabled"}, + {ErrorCode::DISABLE_INSTANCE_NOT_LOAD, "instance is not loaded"}, + {ErrorCode::DISABLE_INSTANCE_UNAVAILABLE, "instance is unavailable"}, + {ErrorCode::DISABLE_INSTANCE_ALREADY_DISABLED, "instance is already disabled"}, + {ErrorCode::REMOVE_PLUGIN_NOT_EXIST, "plugin does not exist"}, + {ErrorCode::REMOVE_INSTANCE_IS_RUNNING, "instance is running"}, + {ErrorCode::REMOVE_INSTANCE_HAVE_DEP, "instance with pre-dependency"}, + {ErrorCode::LOAD_PLUGIN_FILE_NOT_EXIST, "plugin file does not exist"}, + {ErrorCode::LOAD_PLUGIN_FILE_PERMISSION_DEFINED, "plugin file permission defined"}, + {ErrorCode::LOAD_PLUGIN_EXIST, "plugin already loaded"}, + {ErrorCode::LOAD_PLUGIN_DLOPEN_FAILED, "plugin dlopen failed"}, + {ErrorCode::LOAD_PLUGIN_DLSYM_FAILED, "plugin dlsym failed"}, + {ErrorCode::QUERY_PLUGIN_NOT_EXIST, "plugin does not exist"}, + {ErrorCode::QUERY_DEP_NOT_EXIST, "instance does not exist"}, + {ErrorCode::DOWNLOAD_NOT_FOUND, "unable to find a match"}, +}; +std::string ErrorText::get_error_text(ErrorCode code) { + auto it = ErrorText::error_codes.find(code); + if (it != ErrorText::error_codes.end()) { + return it->second; + } else { + return "unknown error."; + } +} \ No newline at end of file diff --git a/src/plugin_mgr/error_code.h b/src/plugin_mgr/error_code.h new file mode 100644 index 0000000..f58d5ed --- /dev/null +++ b/src/plugin_mgr/error_code.h @@ -0,0 +1,33 @@ +#ifndef PLUGIN_MGR_ERROR_CODE_H +#define PLUGIN_MGR_ERROR_CODE_H +#include +#include + +enum class ErrorCode { + ENABLE_INSTANCE_NOT_LOAD, + ENABLE_INSTANCE_UNAVAILABLE, + ENABLE_INSTANCE_ALREADY_ENABLED, + DISABLE_INSTANCE_NOT_LOAD, + DISABLE_INSTANCE_UNAVAILABLE, + DISABLE_INSTANCE_ALREADY_DISABLED, + REMOVE_PLUGIN_NOT_EXIST, + REMOVE_INSTANCE_IS_RUNNING, + REMOVE_INSTANCE_HAVE_DEP, + LOAD_PLUGIN_FILE_NOT_EXIST, + LOAD_PLUGIN_FILE_PERMISSION_DEFINED, + LOAD_PLUGIN_EXIST, + LOAD_PLUGIN_DLOPEN_FAILED, + LOAD_PLUGIN_DLSYM_FAILED, + QUERY_PLUGIN_NOT_EXIST, + QUERY_DEP_NOT_EXIST, + DOWNLOAD_NOT_FOUND, + OK, +}; + +class ErrorText { +public: + static std::string get_error_text(ErrorCode code); + static const std::unordered_map error_codes; +}; + +#endif // !PLUGIN_MGR_ERROR_CODE_H \ No newline at end of file diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp index 162079e..862e806 100644 --- a/src/plugin_mgr/instance_run_handler.cpp +++ b/src/plugin_mgr/instance_run_handler.cpp @@ -13,74 +13,74 @@ #include #include -static void* get_ring_buf(Instance *instance) { +static void* get_ring_buf(std::shared_ptr instance) { if (instance == nullptr) { return nullptr; } switch (instance->get_type()) { case PluginType::COLLECTOR: - return ((CollectorInstance*)instance)->get_interface()->get_ring_buf(); + return (std::dynamic_pointer_cast(instance))->get_interface()->get_ring_buf(); case PluginType::SCENARIO: - return ((ScenarioInstance*)instance)->get_interface()->get_ring_buf(); + return (std::dynamic_pointer_cast(instance))->get_interface()->get_ring_buf(); case PluginType::TUNE: break; } return nullptr; } -static void reflash_ring_buf(Instance *instance) { - ((CollectorInstance*)instance)->get_interface()->reflash_ring_buf(); +static void reflash_ring_buf(std::shared_ptr instance) { + (std::dynamic_pointer_cast(instance))->get_interface()->reflash_ring_buf(); } -void InstanceRunHandler::run_aware(Instance *instance, std::vector &deps) { +void InstanceRunHandler::run_aware(std::shared_ptr instance, std::vector &deps) { void *a[MAX_DEPENDENCIES_SIZE]; for (int i = 0; i < deps.size(); ++i) { - Instance *ins = memory_store->get_instance(deps[i]); + std::shared_ptr ins = memory_store.get_instance(deps[i]); a[i] = get_ring_buf(ins); } - ((ScenarioInstance*)instance)->get_interface()->aware(a, (int)deps.size()); + (std::dynamic_pointer_cast(instance))->get_interface()->aware(a, (int)deps.size()); } -void InstanceRunHandler::run_tune(Instance *instance, std::vector &deps) { +void InstanceRunHandler::run_tune(std::shared_ptr instance, std::vector &deps) { void *a[MAX_DEPENDENCIES_SIZE]; for (int i = 0; i < deps.size(); ++i) { - Instance *ins = memory_store->get_instance(deps[i]); + std::shared_ptr ins = memory_store.get_instance(deps[i]); a[i] = get_ring_buf(ins); } - ((TuneInstance*)instance)->get_interface()->tune(a, (int)deps.size()); + (std::dynamic_pointer_cast(instance))->get_interface()->tune(a, (int)deps.size()); } -void InstanceRunHandler::insert_instance(Instance *instance) { +void InstanceRunHandler::insert_instance(std::shared_ptr instance) { switch (instance->get_type()) { case PluginType::COLLECTOR: collector[instance->get_name()] = instance; - ((CollectorInstance*)instance)->get_interface()->enable(); + (std::dynamic_pointer_cast(instance))->get_interface()->enable(); break; case PluginType::SCENARIO: scenario[instance->get_name()] = instance; - ((ScenarioInstance*)instance)->get_interface()->enable(); + (std::dynamic_pointer_cast(instance))->get_interface()->enable(); break; case PluginType::TUNE: tune[instance->get_name()] = instance; - ((TuneInstance*)instance)->get_interface()->enable(); + (std::dynamic_pointer_cast(instance))->get_interface()->enable(); break; } INFO("[PluginManager] " << instance->get_name() << " instance insert into running queue."); } -void InstanceRunHandler::delete_instance(Instance *instance) { +void InstanceRunHandler::delete_instance(std::shared_ptr instance) { switch (instance->get_type()) { case PluginType::COLLECTOR: collector.erase(instance->get_name()); - ((CollectorInstance*)instance)->get_interface()->disable(); + (std::dynamic_pointer_cast(instance))->get_interface()->disable(); break; case PluginType::SCENARIO: scenario.erase(instance->get_name()); - ((ScenarioInstance*)instance)->get_interface()->disable(); + (std::dynamic_pointer_cast(instance))->get_interface()->disable(); break; case PluginType::TUNE: tune.erase(instance->get_name()); - ((TuneInstance*)instance)->get_interface()->disable(); + (std::dynamic_pointer_cast(instance))->get_interface()->disable(); break; } INFO("[PluginManager] " << instance->get_name() << " instance delete from running queue."); @@ -89,21 +89,22 @@ void InstanceRunHandler::delete_instance(Instance *instance) { void InstanceRunHandler::handle_instance() { InstanceRunMessage msg; while(this->recv_queue_try_pop(msg)){ - Instance *instance = msg.get_instance(); + std::shared_ptr instance = msg.get_instance(); switch (msg.get_type()){ case RunType::ENABLED: - insert_instance(instance); + insert_instance(std::move(instance)); break; case RunType::DISABLED: - delete_instance(instance); + delete_instance(std::move(instance)); break; } } } template -static std::vector get_deps(Instance *instance) { - std::string deps = ((T*)instance)->get_interface()->get_dep(); +static std::vector get_deps(std::shared_ptr instance) { + std::shared_ptr t_instance = std::dynamic_pointer_cast(instance); + std::string deps = (t_instance)->get_interface()->get_dep(); std::string dep = ""; std::vector vec; for (int i = 0; i < deps.length(); ++i) { @@ -129,11 +130,11 @@ void InstanceRunHandler::adjust_collector_queue(const std::vector & if (ok) continue; if (flag) { if (is_instance_exist(m_dep) && !collector.count(m_dep)) { - this->insert_instance(memory_store->get_instance(m_dep)); + this->insert_instance(memory_store.get_instance(m_dep)); } } else { if (is_instance_exist(m_dep) && collector.count(m_dep)) { - this->delete_instance(memory_store->get_instance(m_dep)); + this->delete_instance(memory_store.get_instance(m_dep)); } } } @@ -146,8 +147,8 @@ void InstanceRunHandler::check_scenario_dependency(const std::vectorget_interface()->get_cycle(); + std::shared_ptr instance = p.second; + int t = (std::dynamic_pointer_cast(instance))->get_interface()->get_cycle(); if (time % t != 0) return; reflash_ring_buf(instance); } @@ -155,8 +156,8 @@ void InstanceRunHandler::schedule_collector(uint64_t time) { void InstanceRunHandler::schedule_scenario(uint64_t time) { for (auto &p : scenario) { - Instance *instance = p.second; - int t = ((ScenarioInstance*)instance)->get_interface()->get_cycle(); + std::shared_ptr instance = p.second; + int t = (std::dynamic_pointer_cast(instance))->get_interface()->get_cycle(); if (time % t != 0) return; std::vector origin_deps = get_deps(instance); run_aware(instance, origin_deps); @@ -167,8 +168,8 @@ void InstanceRunHandler::schedule_scenario(uint64_t time) { void InstanceRunHandler::schedule_tune(uint64_t time) { for (auto &p : tune) { - Instance *instance = p.second; - int t = ((TuneInstance*)instance)->get_interface()->get_cycle(); + std::shared_ptr instance = p.second; + int t = (std::dynamic_pointer_cast(instance))->get_interface()->get_cycle(); if (time % t != 0) return; std::vector deps = get_deps(instance); run_tune(instance, deps); diff --git a/src/plugin_mgr/instance_run_handler.h b/src/plugin_mgr/instance_run_handler.h index c9a8dfc..83f9f4a 100644 --- a/src/plugin_mgr/instance_run_handler.h +++ b/src/plugin_mgr/instance_run_handler.h @@ -20,6 +20,7 @@ #include #include #include +#include enum class RunType { ENABLED, @@ -30,30 +31,27 @@ enum class RunType { class InstanceRunMessage { public: InstanceRunMessage() {} - InstanceRunMessage(RunType type, Instance *instance) : type(type), instance(instance) {} + InstanceRunMessage(RunType type, std::shared_ptr instance) : type(type), instance(instance) {} RunType get_type() { return type; } - Instance* get_instance() { + std::shared_ptr get_instance() { return instance; } private: RunType type; - Instance *instance; + std::shared_ptr instance; }; // A handler to schedule plugin instance class InstanceRunHandler { public: - InstanceRunHandler() : cycle(DEFAULT_CYCLE_SIZE) {} + InstanceRunHandler(MemoryStore &memory_store) : memory_store(memory_store), cycle(DEFAULT_CYCLE_SIZE) {} void run(); void schedule_collector(uint64_t time); void schedule_scenario(uint64_t time); void schedule_tune(uint64_t time); void handle_instance(); - void set_memory_store(MemoryStore *memory_store) { - this->memory_store = memory_store; - } void set_cycle(int cycle) { this->cycle = cycle; } @@ -61,7 +59,7 @@ public: return cycle; } bool is_instance_exist(const std::string &name) { - return memory_store->is_instance_exist(name); + return memory_store.is_instance_exist(name); } void recv_queue_push(InstanceRunMessage &msg) { this->recv_queue.push(msg); @@ -73,18 +71,18 @@ public: return this->recv_queue.try_pop(msg); } private: - void run_aware(Instance *instance, std::vector &deps); - void run_tune(Instance *instance, std::vector &deps); - void delete_instance(Instance *instance); - void insert_instance(Instance *instance); + void run_aware(std::shared_ptr instance, std::vector &deps); + void run_tune(std::shared_ptr instance, std::vector &deps); + void delete_instance(std::shared_ptr instance); + void insert_instance(std::shared_ptr instance); void adjust_collector_queue(const std::vector &deps, const std::vector &m_deps, bool flag); void check_scenario_dependency(const std::vector &deps, const std::vector &m_deps); - std::unordered_map collector; - std::unordered_map scenario; - std::unordered_map tune; + std::unordered_map> collector; + std::unordered_map> scenario; + std::unordered_map> tune; SafeQueue recv_queue; - MemoryStore *memory_store; + MemoryStore &memory_store; int cycle; static const int DEFAULT_CYCLE_SIZE = 10; static const int MAX_DEPENDENCIES_SIZE = 20; diff --git a/src/plugin_mgr/main.cpp b/src/plugin_mgr/main.cpp index 8ec0577..92f48bb 100644 --- a/src/plugin_mgr/main.cpp +++ b/src/plugin_mgr/main.cpp @@ -13,10 +13,25 @@ Logger logger; +void print_help() { + printf("Usage: ./oeaware [path]\n" + " ./oeaware --help\n" + "Examples:\n" + " ./oeaware /etc/oeAware/config.yaml\n"); +} + int main(int argc, char **argv) { Config config; if (argc < 2) { - ERROR("System need config arg!"); + ERROR("System need a argument!"); + exit(EXIT_FAILURE); + } + if (std::string(argv[1]) == "--help") { + print_help(); + exit(EXIT_SUCCESS); + } + if (!file_exist(argv[1])) { + ERROR("Config file " << argv[1] << " does not exist!"); exit(EXIT_FAILURE); } std::string config_path(argv[1]); @@ -36,8 +51,8 @@ int main(int argc, char **argv) { message_manager.init(&config); message_manager.run(); INFO("[PluginManager] Start plugin manager!"); - PluginManager plugin_manager(&handler_msg, &res_msg); - plugin_manager.init(&config); + PluginManager plugin_manager(config, handler_msg, res_msg); + plugin_manager.init(); plugin_manager.pre_load(); plugin_manager.run(); return 0; diff --git a/src/plugin_mgr/memory_store.h b/src/plugin_mgr/memory_store.h index 190fcdd..ac3ff98 100644 --- a/src/plugin_mgr/memory_store.h +++ b/src/plugin_mgr/memory_store.h @@ -14,29 +14,27 @@ #include "plugin.h" #include "logger.h" #include +#include //OeAware memory storage, which is used to store plugins and instances in the memory. class MemoryStore { public: - void add_plugin(const std::string &name, Plugin *plugin) { + void add_plugin(const std::string &name, std::shared_ptr plugin) { this->plugins.insert(std::make_pair(name, plugin)); } - void add_instance(const std::string &name, Instance *instance) { + void add_instance(const std::string &name, std::shared_ptr instance) { this->instances.insert(std::make_pair(name, instance)); } - Plugin* get_plugin(const std::string &name) const { + std::shared_ptr get_plugin(const std::string &name) const { return this->plugins.at(name); } - Instance* get_instance(const std::string &name) const { + std::shared_ptr get_instance(const std::string &name) const { return this->instances.at(name); } void delete_plugin(const std::string &name) { - Plugin *plugin = plugins.at(name); this->plugins.erase(name); - delete plugin; } void delete_instance(const std::string &name) { - Instance *instance = instances.at(name); this->instances.erase(name); } bool is_plugin_exist(const std::string &name) const { @@ -45,23 +43,23 @@ public: bool is_instance_exist(const std::string &name) const { return this->instances.count(name); } - std::vector get_all_plugins() { - std::vector res; + std::vector> get_all_plugins() { + std::vector> res; for (auto &p : plugins) { res.emplace_back(p.second); } return res; } - std::vector get_all_instances() { - std::vector res; + std::vector> get_all_instances() { + std::vector> res; for (auto &p : instances) { res.emplace_back(p.second); } return res; } private: - std::unordered_map plugins; - std::unordered_map instances; + std::unordered_map> plugins; + std::unordered_map> instances; }; #endif // !PLUGIN_MGR_MEMORY_STORE_H diff --git a/src/plugin_mgr/message_manager.cpp b/src/plugin_mgr/message_manager.cpp index 296c682..e2fd3b6 100644 --- a/src/plugin_mgr/message_manager.cpp +++ b/src/plugin_mgr/message_manager.cpp @@ -68,7 +68,6 @@ static void recv_msg(Msg &msg, MessageHeader &header, SafeQueue *res_ms for (int i = 0; i < res.get_payload_len(); ++i) { msg.add_payload(res.get_payload(i)); } - header.set_state_code(res.get_state_code()); } void TcpSocket::serve_accept(SafeQueue *handler_msg, SafeQueue *res_msg){ diff --git a/src/plugin_mgr/message_manager.h b/src/plugin_mgr/message_manager.h index d1faad3..4cd7311 100644 --- a/src/plugin_mgr/message_manager.h +++ b/src/plugin_mgr/message_manager.h @@ -28,7 +28,7 @@ enum class MessageType { class Message { public: - Message() : type(MessageType::EXTERNAL), state_code(HEADER_STATE_OK) {} + Message() : type(MessageType::EXTERNAL) {} Message(Opt opt) : opt(opt) {} Message(Opt opt, MessageType type) : opt(opt), type(type) {} Message(Opt opt, std::vector payload) : opt(opt), payload(payload) {} @@ -53,16 +53,9 @@ public: int get_payload_len() const { return this->payload.size(); } - void set_state_code(int code) { - this->state_code = code; - } - int get_state_code() const { - return this->state_code; - } private: Opt opt; MessageType type; - int state_code; std::vector payload; }; diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h index 11b4748..69837af 100644 --- a/src/plugin_mgr/plugin.h +++ b/src/plugin_mgr/plugin.h @@ -14,6 +14,7 @@ #include "interface.h" #include #include +#include #include enum class PluginType { @@ -122,11 +123,8 @@ private: class Plugin { public: Plugin(std::string name, PluginType type) : name(name), type(type), handler(nullptr) { } - ~Plugin() { - for (int i = 0; i < instances.size(); ++i) { - delete instances[i]; - } - dlclose(this->handler); + ~Plugin() { + dlclose(handler); } int load(const std::string dl_path); std::string get_name() const { @@ -135,21 +133,21 @@ public: PluginType get_type() const { return this->type; } - void add_instance(Instance *ins) { + void add_instance(std::shared_ptr ins) { instances.emplace_back(ins); } - Instance* get_instance(int i) const { + std::shared_ptr get_instance(int i) const { return instances[i]; } size_t get_instance_len() const { return instances.size(); } - void * get_handler() const { + void* get_handler() const { return handler; } private: void *handler; - std::vector instances; + std::vector> instances; PluginType type; std::string name; }; diff --git a/src/plugin_mgr/plugin_manager.cpp b/src/plugin_mgr/plugin_manager.cpp index 47f9451..e7e32bf 100644 --- a/src/plugin_mgr/plugin_manager.cpp +++ b/src/plugin_mgr/plugin_manager.cpp @@ -21,61 +21,64 @@ const std::string PluginManager::SCENARIO_TEXT = "scenario"; const std::string PluginManager::TUNE_TEXT = "tune"; const static int ST_MODE_MASK = 0777; -void PluginManager::init(Config *config) { +void PluginManager::init() { plugin_types[COLLECTOR_TEXT] = PluginType::COLLECTOR; plugin_types[SCENARIO_TEXT] = PluginType::SCENARIO; plugin_types[TUNE_TEXT] = PluginType::TUNE; - this->config = config; } -bool PluginManager::remove(const std::string &name) { - if (!memory_store.is_plugin_exist(name)) return false; - Plugin *plugin = memory_store.get_plugin(name); +ErrorCode PluginManager::remove(const std::string &name) { + if (!memory_store.is_plugin_exist(name)) { + return ErrorCode::REMOVE_PLUGIN_NOT_EXIST; + } + std::shared_ptr plugin = memory_store.get_plugin(name); std::vector instance_names; for (int i = 0; i < plugin->get_instance_len(); ++i) { - Instance *instance = plugin->get_instance(i); + std::shared_ptr instance = plugin->get_instance(i); std::string iname = instance->get_name(); - if (dep_handler->have_dep(iname)) { - return false; + if (instance->get_enabled()) { + return ErrorCode::REMOVE_INSTANCE_IS_RUNNING; + } + if (dep_handler.have_dep(iname)) { + return ErrorCode::REMOVE_INSTANCE_HAVE_DEP; } instance_names.emplace_back(iname); } for(auto &iname : instance_names) { memory_store.delete_instance(iname); - dep_handler->del_node(iname); + dep_handler.del_node(iname); } memory_store.delete_plugin(name); update_instance_state(); - return true; + return ErrorCode::OK; } -bool PluginManager::query_all_plugins(Message &res) { - std::vector all_plugins = memory_store.get_all_plugins(); +ErrorCode PluginManager::query_all_plugins(std::string &res) { + std::vector> all_plugins = memory_store.get_all_plugins(); for (auto &p : all_plugins) { - res.add_payload(p->get_name()); + res += p->get_name() + "\n"; for (int i = 0; i < p->get_instance_len(); ++i) { std::string info = p->get_instance(i)->get_info(); - res.add_payload(" " + info); + res += "\t" + info + "\n"; } } - return 1; + return ErrorCode::OK; } -bool PluginManager::query_plugin(std::string name, Message &res) { +ErrorCode PluginManager::query_plugin(const std::string &name, std::string &res) { if (!memory_store.is_plugin_exist(name)) { - res.add_payload("no such plugin!"); - return true; + return ErrorCode::QUERY_PLUGIN_NOT_EXIST; } - Plugin *plugin = memory_store.get_plugin(name); - res.add_payload(name); + std::shared_ptr plugin = memory_store.get_plugin(name); + res += name + "\n"; for (int i = 0; i < plugin->get_instance_len(); ++i) { std::string info = plugin->get_instance(i)->get_info(); - res.add_payload(" " + info); + res += "\t" + info + "\n"; } - return true; + return ErrorCode::OK; } template -int PluginManager::load_dl_instance(Plugin *plugin, T **interface_list) { +int PluginManager::load_dl_instance(std::shared_ptr plugin, T **interface_list) { int (*get_instance)(T**) = (int(*)(T**))dlsym(plugin->get_handler(), "get_instance"); if (get_instance == nullptr) { ERROR("[PluginManager] dlsym error!\n"); @@ -105,11 +108,11 @@ std::vector get_dep(T *interface) { } template -void PluginManager::save_instance(Plugin *plugin, T *interface_list, int len) { +void PluginManager::save_instance(std::shared_ptr plugin, T *interface_list, int len) { if (interface_list == nullptr) return; for (int i = 0; i < len; ++i) { T *interface = interface_list + i; - Instance *instance = new U(); + std::shared_ptr instance = std::make_shared(); std::string name = interface->get_name(); instance->set_name(name); instance->set_plugin_name(plugin->get_name()); @@ -117,19 +120,19 @@ void PluginManager::save_instance(Plugin *plugin, T *interface_list, int len) { instance->set_enabled(false); if (plugin->get_type() == PluginType::COLLECTOR) { DEBUG("[PluginManager] add node"); - dep_handler->add_node(name); + dep_handler.add_node(name); } else { - dep_handler->add_node(name, get_dep(interface)); + dep_handler.add_node(name, get_dep(interface)); } - instance->set_state(dep_handler->get_node_state(name)); - ((U*)instance)->set_interface(interface); + instance->set_state(dep_handler.get_node_state(name)); + (std::dynamic_pointer_cast(instance))->set_interface(interface); DEBUG("[PluginManager] Instance: " << name.c_str()); memory_store.add_instance(name, instance); plugin->add_instance(instance); } } -bool PluginManager::load_instance(Plugin *plugin) { +bool PluginManager::load_instance(std::shared_ptr plugin) { int len = 0; DEBUG("plugin: " << plugin->get_name()); switch (plugin->get_type()) { @@ -160,9 +163,9 @@ bool PluginManager::load_instance(Plugin *plugin) { } void PluginManager::update_instance_state() { - std::vector all_instances = memory_store.get_all_instances(); + std::vector> all_instances = memory_store.get_all_instances(); for (auto &instance : all_instances) { - if (dep_handler->get_node_state(instance->get_name())) { + if (dep_handler.get_node_state(instance->get_name())) { instance->set_state(true); } else { instance->set_state(false); @@ -170,24 +173,28 @@ void PluginManager::update_instance_state() { } } -bool PluginManager::load_plugin(const std::string name, PluginType type) { +ErrorCode PluginManager::load_plugin(const std::string name, PluginType type) { + std::string plugin_path = get_path(type) + "/" + name; + if (!file_exist(plugin_path)) { + return ErrorCode::LOAD_PLUGIN_FILE_NOT_EXIST; + } + if (!check_permission(plugin_path, S_IRUSR | S_IRGRP)) { + return ErrorCode::LOAD_PLUGIN_FILE_PERMISSION_DEFINED; + } if (memory_store.is_plugin_exist(name)) { - WARN("[PluginManager] " << name << " already loaded!"); - return false; + return ErrorCode::LOAD_PLUGIN_EXIST; } const std::string dl_path = get_path(type) + '/' + name; - Plugin *plugin = new Plugin(name, type); + std::shared_ptr plugin = std::make_shared(name, type); int error = plugin->load(dl_path); if (error) { - WARN("[PluginManager] " << name << " load error!"); - return false; + return ErrorCode::LOAD_PLUGIN_DLOPEN_FAILED; } if (!this->load_instance(plugin)) { - delete plugin; - return false; + return ErrorCode::LOAD_PLUGIN_DLSYM_FAILED; } memory_store.add_plugin(name, plugin); - return true; + return ErrorCode::OK; } std::string generate_dot(MemoryStore &memory_store, const std::vector> &query) { @@ -195,7 +202,7 @@ std::string generate_dot(MemoryStore &memory_store, const std::vector> sub_graph; for (auto &vec : query) { - Instance *instance = memory_store.get_instance(vec[0]); + std::shared_ptr instance = memory_store.get_instance(vec[0]); sub_graph[instance->get_plugin_name()].emplace_back(vec[0]); if (vec.size() == 1) { continue; @@ -219,47 +226,37 @@ std::string generate_dot(MemoryStore &memory_store, const std::vector> query; - dep_handler->query_node(name, query); - if (query.empty()) { - res.add_payload("Instance not available!"); - return false; - } - std::string dot_text = generate_dot(memory_store, query); - res.add_payload(dot_text); - return true; + dep_handler.query_node(name, query); + res = generate_dot(memory_store, query); + return ErrorCode::OK; } -bool PluginManager::query_all_tops(Message &res) { +ErrorCode PluginManager::query_all_tops(std::string &res) { std::vector> query; - dep_handler->query_all_top(query); + dep_handler.query_all_top(query); DEBUG("[PluginManager] query size:" << query.size()); - if (query.empty()) { - res.add_payload("No instance available!"); - return false; - } - std::string dot_text = generate_dot(memory_store, query); - res.add_payload(dot_text); - return true; + res = generate_dot(memory_store, query); + return ErrorCode::OK; } -bool PluginManager::instance_enabled(std::string name) { +ErrorCode PluginManager::instance_enabled(std::string name) { if (!memory_store.is_instance_exist(name)) { - WARN("[PluginManager] " << name << " instance can't load!"); - return false; + return ErrorCode::ENABLE_INSTANCE_NOT_LOAD; } - Instance *instance = memory_store.get_instance(name); + std::shared_ptr instance = memory_store.get_instance(name); if (!instance->get_state()) { - WARN("[PluginManager] " << name << " instance is unavailable, lacking dependencies!"); - return false; + return ErrorCode::ENABLE_INSTANCE_UNAVAILABLE; } if (instance->get_enabled()) { - WARN("[PluginManager] " << name << " instance was enabled!"); - return false; + return ErrorCode::ENABLE_INSTANCE_ALREADY_ENABLED; } - std::vector pre_dependencies = dep_handler->get_pre_dependencies(name); + std::vector pre_dependencies = dep_handler.get_pre_dependencies(name); for (int i = pre_dependencies.size() - 1; i >= 0; --i) { instance = memory_store.get_instance(pre_dependencies[i]); if (instance->get_enabled()) { @@ -267,29 +264,25 @@ bool PluginManager::instance_enabled(std::string name) { } instance->set_enabled(true); instance_run_handler->recv_queue_push(InstanceRunMessage(RunType::ENABLED, instance)); - INFO("[PluginManager] " << name << " instance enabled!"); + DEBUG("[PluginManager] " << instance->get_name() << " instance enabled."); } - return true; + return ErrorCode::OK; } -bool PluginManager::instance_disabled(std::string name) { +ErrorCode PluginManager::instance_disabled(std::string name) { if (!memory_store.is_instance_exist(name)) { - WARN("[PluginManager] " << name << " instance can't load!"); - return false; + return ErrorCode::DISABLE_INSTANCE_NOT_LOAD; } - Instance *instance = memory_store.get_instance(name); + std::shared_ptr instance = memory_store.get_instance(name); if (!instance->get_state()) { - WARN("[PluginManager] " << name << " instance is unavailable, lacking dependencies!"); - return false; + return ErrorCode::DISABLE_INSTANCE_UNAVAILABLE; } if (!instance->get_enabled()) { - WARN("[PluginManager] " << name << " instance was disabled!"); - return false; + return ErrorCode::DISABLE_INSTANCE_ALREADY_DISABLED; } instance->set_enabled(false); instance_run_handler->recv_queue_push(InstanceRunMessage(RunType::DISABLED, instance)); - INFO("[PluginManager] " << name << " instance disabled!"); - return true; + return ErrorCode::OK; } static bool end_with(const std::string &s, const std::string &ending) { @@ -320,30 +313,46 @@ static std::string get_plugin_in_dir(const std::string &path) { return res; } -void PluginManager::add_list(Message &res) { - std::string list_text; - list_text += "Download Packages:\n"; - for (int i = 0; i < config->get_plugin_list_size(); ++i) { - PluginInfo info = config->get_plugin_list(i); - list_text += info.get_name() + "\n"; +ErrorCode PluginManager::add_list(std::string &res) { + res += "Download Packages:\n"; + for (int i = 0; i < config.get_plugin_list_size(); ++i) { + PluginInfo info = config.get_plugin_list(i); + res += info.get_name() + "\n"; } - list_text += "Installed Plugins:\n"; - list_text += get_plugin_in_dir(DEFAULT_COLLECTOR_PATH); - list_text += get_plugin_in_dir(DEFAULT_SCENARIO_PATH); - list_text += get_plugin_in_dir(DEFAULT_TUNE_PATH); - res.add_payload(list_text); + res += "Installed Plugins:\n"; + res += get_plugin_in_dir(DEFAULT_COLLECTOR_PATH); + res += get_plugin_in_dir(DEFAULT_SCENARIO_PATH); + res += get_plugin_in_dir(DEFAULT_TUNE_PATH); + return ErrorCode::OK; +} + +ErrorCode PluginManager::download(const std::string &name, std::string &res) { + std::string url; + std::string type; + for (int i = 0; i < config.get_plugin_list_size(); ++i) { + PluginInfo info = config.get_plugin_list(i); + if (info.get_name() == name) { + url = info.get_url(); + break; + } + } + if (url.empty()) { + return ErrorCode::DOWNLOAD_NOT_FOUND; + } + res += url; + return ErrorCode::OK; } void PluginManager::pre_enable() { - for (int i = 0; i < config->get_enable_list_size(); ++i) { - EnableItem item = config->get_enable_list(i); + for (int i = 0; i < config.get_enable_list_size(); ++i) { + EnableItem item = config.get_enable_list(i); if (item.get_enabled()) { std::string name = item.get_name(); if (!memory_store.is_plugin_exist(name)) { WARN("[PluginManager] plugin " << name << " cannot be enabled, because it does not exist."); continue; } - Plugin *plugin = memory_store.get_plugin(name); + std::shared_ptr plugin = memory_store.get_plugin(name); for (int j = 0; j < plugin->get_instance_len(); ++j) { instance_enabled(plugin->get_instance(i)->get_name()); } @@ -373,19 +382,15 @@ void PluginManager::pre_load() { pre_enable(); } -static bool check_load_msg(Message &msg, std::unordered_map &plugin_types) { - return msg.get_payload_len() == 2 && plugin_types.count(msg.get_payload(1)); -} - void* PluginManager::get_data_buffer(std::string name) { - Instance *instance = memory_store.get_instance(name); + std::shared_ptr instance = memory_store.get_instance(name); switch (instance->get_type()) { case PluginType::COLLECTOR: { - CollectorInterface *collector_interface = ((CollectorInstance*)instance)->get_interface(); + CollectorInterface *collector_interface = (std::dynamic_pointer_cast(instance))->get_interface(); return collector_interface->get_ring_buf(); } case PluginType::SCENARIO: { - ScenarioInterface *scenario_interface = ((ScenarioInstance*)instance)->get_interface(); + ScenarioInterface *scenario_interface = (std::dynamic_pointer_cast(instance))->get_interface(); return scenario_interface->get_ring_buf(); } default: @@ -394,12 +399,13 @@ void* PluginManager::get_data_buffer(std::string name) { return nullptr; } -void PluginManager::instance_dep_check(std::string name, Message &res) { - Plugin *plugin = memory_store.get_plugin(name); +std::string PluginManager::instance_dep_check(const std::string &name) { + std::shared_ptr plugin = memory_store.get_plugin(name); + std::string res; for (int i = 0; i < plugin->get_instance_len(); ++i) { std::string instance_name = plugin->get_instance(i)->get_name(); std::vector> query; - dep_handler->query_node(instance_name, query); + dep_handler.query_node(instance_name, query); std::vector lack; for (auto &item : query) { if (item.size() < 2) continue; @@ -408,13 +414,13 @@ void PluginManager::instance_dep_check(std::string name, Message &res) { } } if (!lack.empty()) { - std::string info = instance_name + " needed the following dependencies:"; - for (auto &dep : lack) { - info += "\n " + dep; + for (int i = 0; i < lack.size(); ++i) { + res += "\t" + lack[i]; + if (i != lack.size() - 1) res += '\n'; } - res.add_payload(info); } } + return res; } // Check the file permission. The file owner is root. @@ -429,76 +435,110 @@ bool check_permission(std::string path, int mode) { return true; } -static bool file_exist(const std::string &file_name) { +bool file_exist(const std::string &file_name) { std::ifstream file(file_name); return file.good(); } int PluginManager::run() { - instance_run_handler->set_memory_store(&memory_store); instance_run_handler->run(); while (true) { Message msg; Message res; - res.set_opt(Opt::RESPONSE); - this->handler_msg->wait_and_pop(msg); + this->handler_msg.wait_and_pop(msg); if (msg.get_opt() == Opt::SHUTDOWN) break; switch (msg.get_opt()) { case Opt::LOAD: { - if (!check_load_msg(msg, plugin_types)) { - WARN("[PluginManager] args error!"); - res.add_payload("args error!"); - break; - } std::string plugin_name = msg.get_payload(0); PluginType type = plugin_types[msg.get_payload(1)]; - std::string plugin_path = get_path(type) + "/" + plugin_name; if (!end_with(plugin_name, ".so")) break; - if (!file_exist(plugin_path)) { - WARN("[PluginManager] plugin " << plugin_name << " does not exist!"); - res.add_payload("plugin does not exist!"); - break; - } - if (!check_permission(plugin_path, S_IRUSR | S_IRGRP)) { - WARN("[PluginManager] plugin " << plugin_name << " does not have the execute permission!"); - res.add_payload("does not have the execute permission!"); - break; - } - if(this->load_plugin(plugin_name, type)) { - INFO("[PluginManager] plugin " << plugin_name << " loaded."); - res.add_payload("plugin load succeed!"); - instance_dep_check(plugin_name, res); - DEBUG("[PluginManager] instance dependency checked!"); + ErrorCode ret_code = load_plugin(plugin_name, type); + if(ret_code == ErrorCode::OK) { + INFO("[PluginManager] " << plugin_name << "plugin loaded."); + res.set_opt(Opt::RESPONSE_OK); + std::string lack_dep = instance_dep_check(plugin_name); + if (!lack_dep.empty()) { + INFO("[PluginManager] " << plugin_name << " requires the following dependencies:\n" << lack_dep); + res.add_payload(lack_dep); + } } else { - INFO("[PluginManager] plugin " << plugin_name << " load error!"); - res.add_payload("plugin load failed!"); + WARN("[PluginManager] " << plugin_name << " " << ErrorText::get_error_text(ret_code) << "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); } break; } case Opt::REMOVE: { std::string name = msg.get_payload(0); - if (remove(name)) { - res.add_payload(name + " removed!"); - INFO("[PluginManager] " << name << " removed!"); + ErrorCode ret_code = remove(name); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] " << name << " plugin removed."); + res.set_opt(Opt::RESPONSE_OK); } else { - res.add_payload(name + " remove failed!"); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); + INFO("[PluginManager] " << name << " " << ErrorText::get_error_text(ret_code) + "."); } break; } case Opt::QUERY_ALL: { - query_all_plugins(res); + std::string res_text; + ErrorCode ret_code = query_all_plugins(res_text); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] query all plugins information."); + res.set_opt(Opt::RESPONSE_OK); + res.add_payload(res_text); + } else { + WARN("[PluginManager] query all plugins failed, because " << ErrorText::get_error_text(ret_code) + "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); + } break; } case Opt::QUERY: { - query_plugin(msg.get_payload(0), res); + std::string res_text; + std::string name = msg.get_payload(0); + ErrorCode ret_code = query_plugin(name, res_text); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] " << name << " plugin query successfully."); + res.set_opt(Opt::RESPONSE_OK); + res.add_payload(res_text); + } else { + WARN("[PluginManager] " << name << " " << ErrorText::get_error_text(ret_code) + "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); + } break; } case Opt::QUERY_TOP: { - query_top(msg.get_payload(0), res); + std::string res_text; + std::string name = msg.get_payload(0); + ErrorCode ret_code = query_top(name , res_text); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] query " << name << " instance dependencies."); + res.set_opt(Opt::RESPONSE_OK); + res.add_payload(res_text); + } else { + WARN("[PluginManager] query "<< name << " instance dependencies failed, because " + << ErrorText::get_error_text(ret_code) << "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); + } break; } case Opt::QUERY_ALL_TOP: { - query_all_tops(res); + std::string res_text; + ErrorCode ret_code = query_all_tops(res_text); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] query all instances dependencies."); + res.set_opt(Opt::RESPONSE_OK); + res.add_payload(res_text); + } else { + WARN("[PluginManager] query all instances dependencies failed. because " + << ErrorText::get_error_text(ret_code) << "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); + } break; } case Opt::ENABLED: { @@ -508,10 +548,15 @@ int PluginManager::run() { break; } std::string instance_name = msg.get_payload(0); - if (instance_enabled(instance_name)) { - res.add_payload("instance enabled!"); + ErrorCode ret_code = instance_enabled(instance_name); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] " << instance_name << " enabled successful."); + res.set_opt(Opt::RESPONSE_OK); } else { - res.add_payload("instance enabled failed!"); + WARN("[PluginManager] " << instance_name << " enabled failed. because " + << ErrorText::get_error_text(ret_code) + "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); } break; } @@ -522,42 +567,50 @@ int PluginManager::run() { break; } std::string instance_name = msg.get_payload(0); - if (instance_disabled(instance_name)) { - res.add_payload("instance disabled!"); + ErrorCode ret_code = instance_disabled(instance_name); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] " << instance_name << " disabled successful."); + res.set_opt(Opt::RESPONSE_OK); } else { - res.add_payload("instance disabled failed!"); + WARN("[PluginManager] " << instance_name << " " << ErrorText::get_error_text(ret_code) << "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); } break; } case Opt::LIST: { - add_list(res); + std::string res_text; + ErrorCode ret_code = add_list(res_text); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] query plugin_list."); + res.set_opt(Opt::RESPONSE_OK); + res.add_payload(res_text); + } else { + WARN("[PluginManager] query plugin_list failed, because " << ErrorText::get_error_text(ret_code) << "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); + } break; } case Opt::DOWNLOAD: { + std::string res_text; std::string name = msg.get_payload(0); - std::string url = ""; - std::string type = ""; - for (int i = 0; i < config->get_plugin_list_size(); ++i) { - PluginInfo info = config->get_plugin_list(i); - if (info.get_name() == name) { - url = info.get_url(); - break; - } - } - if (url.empty()) { - WARN("[PluginManager] unable to find a match: " << name); - res.set_state_code(HEADER_STATE_FAILED); - res.add_payload("unable to find a match: " + name); - break; + ErrorCode ret_code = download(name, res_text); + if (ret_code == ErrorCode::OK) { + INFO("[PluginManager] download " << name << " from " << res_text << "."); + res.set_opt(Opt::RESPONSE_OK); + res.add_payload(res_text); + } else { + WARN("[PluginManager] download " << name << " failed, because " << ErrorText::get_error_text(ret_code) + "."); + res.set_opt(Opt::RESPONSE_ERROR); + res.add_payload(ErrorText::get_error_text(ret_code)); } - res.add_payload(url); - INFO("[PluginManager] download " << name << " from " << url << "."); } default: break; } if (msg.get_type() == MessageType::EXTERNAL) - res_msg->push(res); + res_msg.push(res); } return 0; } diff --git a/src/plugin_mgr/plugin_manager.h b/src/plugin_mgr/plugin_manager.h index f648931..8d240cc 100644 --- a/src/plugin_mgr/plugin_manager.h +++ b/src/plugin_mgr/plugin_manager.h @@ -17,6 +17,7 @@ #include "memory_store.h" #include "dep_handler.h" #include "message_manager.h" +#include "error_code.h" #include #include #include @@ -24,45 +25,43 @@ class PluginManager { public: - PluginManager(SafeQueue *handler_msg, SafeQueue *res_msg) { - this->handler_msg = handler_msg; - this->res_msg = res_msg; - dep_handler = new DepHandler(); - instance_run_handler = new InstanceRunHandler(); - } - ~PluginManager() { } + PluginManager(Config &config, SafeQueue &handler_msg, SafeQueue &res_msg) : + config(config), handler_msg(handler_msg), res_msg(res_msg) { + instance_run_handler.reset(new InstanceRunHandler(memory_store)); + } int run(); void pre_load(); void pre_enable(); - void init(Config *config); + void init(); void* get_data_buffer(std::string name); private: void pre_load_plugin(PluginType type); - bool query_all_plugins(Message &res); - bool query_plugin(std::string name, Message &res); - bool query_top(std::string name, Message &res); - bool query_all_tops(Message &res); - bool instance_enabled(std::string name); - bool instance_disabled(std::string name); - void instance_dep_check(std::string name, Message &res); + ErrorCode load_plugin(const std::string path, PluginType type); + ErrorCode remove(const std::string &name); + ErrorCode query_all_plugins(std::string &res); + ErrorCode query_plugin(const std::string &name, std::string &res); + ErrorCode query_top(const std::string &name, std::string &res); + ErrorCode query_all_tops(std::string &res); + ErrorCode instance_enabled(std::string name); + ErrorCode instance_disabled(std::string name); + ErrorCode add_list(std::string &res); + ErrorCode download(const std::string &name, std::string &res); + std::string instance_dep_check(const std::string &name); template - int load_dl_instance(Plugin *plugin, T **interface_list); + int load_dl_instance(std::shared_ptr plugin, T **interface_list); template - void save_instance(Plugin *plugin, T *interface_list, int len); - bool load_instance(Plugin *plugin); - bool load_plugin(const std::string path, PluginType type); + void save_instance(std::shared_ptr plugin, T *interface_list, int len); + bool load_instance(std::shared_ptr plugin); void batch_load(); - bool remove(const std::string &name); void batch_remove(); - void add_list(Message &msg); void update_instance_state(); private: - InstanceRunHandler *instance_run_handler; - Config *config; - SafeQueue *handler_msg; - SafeQueue *res_msg; + std::unique_ptr instance_run_handler; + Config &config; + SafeQueue &handler_msg; + SafeQueue &res_msg; MemoryStore memory_store; - DepHandler *dep_handler; + DepHandler dep_handler; std::unordered_map plugin_types; static const std::string COLLECTOR_TEXT; static const std::string SCENARIO_TEXT; @@ -70,5 +69,5 @@ private: }; bool check_permission(std::string path, int mode); - -#endif +bool file_exist(const std::string &file_name); +#endif -- 2.33.0