summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src
diff options
context:
space:
mode:
authorAlexander Kutsan <akutsan@luxoft.com>2018-02-06 16:18:29 +0200
committerIra Lytvynenko (GitHub) <ILytvynenko@luxoft.com>2018-06-26 12:01:42 +0300
commit210c3fa42d65f59c295816b23d8c33fdab005acb (patch)
tree31790010166e81a1129f99213f97610ff0ae50ad /src/components/application_manager/src
parentadcca686d6374d5fdd2e24414516a61847ea8b9f (diff)
downloadsdl_core-210c3fa42d65f59c295816b23d8c33fdab005acb.tar.gz
Plugin manager and plugin interface creation
add Optional type Fix compile warnings and use optional for returning plugin Add descriptions to optional Fix review issues Fix header guards Fix build with unit tests after commands factory refactoring - Change enum name from command origin to command source - Create Mock for command factory class - Fix mock_app_manager after refactoring - Fix failed unit test
Diffstat (limited to 'src/components/application_manager/src')
-rw-r--r--src/components/application_manager/src/plugin_manager/rpc_plugin_manager_impl.cc93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/components/application_manager/src/plugin_manager/rpc_plugin_manager_impl.cc b/src/components/application_manager/src/plugin_manager/rpc_plugin_manager_impl.cc
new file mode 100644
index 0000000000..250bac8eb6
--- /dev/null
+++ b/src/components/application_manager/src/plugin_manager/rpc_plugin_manager_impl.cc
@@ -0,0 +1,93 @@
+#include "application_manager/plugin_manager/rpc_plugin_manager_impl.h"
+
+#include <dlfcn.h>
+
+#include "utils/file_system.h"
+
+namespace application_manager {
+namespace plugin_manager {
+
+CREATE_LOGGERPTR_GLOBAL(logger_, "PluginManager")
+
+bool IsLibraryFile(const std::string& file_path) {
+ size_t pos = file_path.find_last_of(".");
+ if (std::string::npos == pos) {
+ return false;
+ }
+ if (file_path.substr(pos + 1).compare("so") != 0) {
+ return false;
+ }
+ return true;
+}
+
+RPCPluginPtr LoadPlugin(const std::string& full_plugin_path) {
+ if (!IsLibraryFile(full_plugin_path)) {
+ LOG4CXX_DEBUG(logger_, "Skip loading " << full_plugin_path);
+ return RPCPluginPtr();
+ }
+
+ void* plugin_dll = dlopen(full_plugin_path.c_str(), RTLD_LAZY);
+ if (nullptr == plugin_dll) {
+ LOG4CXX_ERROR(logger_,
+ "Failed to open dll " << full_plugin_path << " : "
+ << dlerror());
+ return RPCPluginPtr();
+ }
+
+ typedef RPCPlugin* (*Create)();
+ Create create_plugin = reinterpret_cast<Create>(dlsym(plugin_dll, "Create"));
+ char* error_string = dlerror();
+ if (nullptr != error_string) {
+ LOG4CXX_ERROR(logger_,
+ "Failed to export dll's " << full_plugin_path
+ << " symbols : " << error_string);
+ dlclose(plugin_dll);
+ return RPCPluginPtr();
+ }
+ RPCPlugin* plugin = create_plugin();
+ return RPCPluginPtr(plugin);
+}
+
+uint32_t RPCPluginManagerImpl::LoadPlugins(
+ const std::string& plugins_path) {
+ LOG4CXX_INFO(logger_, "Loading plugins from " << plugins_path);
+ std::vector<std::string> plugin_files = file_system::ListFiles(plugins_path);
+ for (auto& plugin_file : plugin_files) {
+ std::string full_name = plugins_path + '/' + plugin_file;
+ auto plugin = LoadPlugin(full_name);
+ if (!plugin) {
+ continue;
+ }
+ LOG4CXX_DEBUG(logger_,
+ "Loaded " << plugin->PluginName() << " plugin from "
+ << full_name);
+ if (plugin->Init()) {
+ loaded_plugins_.push_back(std::move(plugin));
+ } else {
+ LOG4CXX_ERROR(logger_,
+ "Initialisation of " << plugin->PluginName()
+ << " plugin from " << full_name
+ << " failed");
+ }
+ }
+ return loaded_plugins_.size();
+}
+
+std::vector<RPCPluginPtr>& RPCPluginManagerImpl::GetPlugins() {
+ return loaded_plugins_;
+}
+
+utils::Optional<RPCPlugin> RPCPluginManagerImpl::FindPluginToProcess(
+ const int32_t function_id,
+ const commands::Command::CommandOrigin message_source) {
+ typedef utils::Optional<RPCPlugin> PluginOptional;
+ for (auto& plugin : loaded_plugins_) {
+ if (plugin->IsAbleToProcess(function_id, message_source)) {
+ return PluginOptional(*plugin);
+ }
+ }
+ return PluginOptional(PluginOptional::EMPTY,
+ std::string("Plugin for processing RPC not found"));
+}
+} // namespace plugin_manager
+} // namespace application_manager