void PluginManager::loadPlugin(string plugin_name) { removePlugin(plugin_name); string path = "./plugins/" + plugin_name; void* handle = dlopen(path.c_str(), RTLD_LAZY); if(!handle) throw PluginException("failed to load plugin from " + path + ": " + dlerror()); Plugin* (*make_plugin)(); *(void **) (&make_plugin) = dlsym(handle, "make_plugin"); plugins[plugin_name] = make_plugin(); handles[plugin_name] = handle; }
int PluginDepotImp::initing() { if (m_manage_lugin_config.init() == -1) { DEF_LOG_ERROR("failed to init m_manage_lugin_config\n"); return -1; } static bool is_first_time = true; MAKE_PLUGIN_INSTANCE make_plugin = NULL; DllInfo * dll_info = NULL; int init_plugin_result = 0; Plugin * plugin_ins = NULL; PluginCfg plugin_cfg; DllInfoVec_t::iterator it_dll = m_plugin_depot_cfg.plugin_dll_vec.begin(); PluginParamConfigVec_t::iterator it_param = m_plugin_depot_cfg.plugin_param_vec->begin(); for (; it_dll != m_plugin_depot_cfg.plugin_dll_vec.end(); ++it_dll, ++it_param) { dll_info = *it_dll; make_plugin = (MAKE_PLUGIN_INSTANCE)dll_info->dll_handle->symbol(PLUGIN_EXPORT_NAME); if (NULL == make_plugin) { PDEPOT_LOG_ERROR("failed to get plugin export function, the plugin is <%s>", dll_info->dll_path.c_str()); return -1; } plugin_ins = make_plugin(); if (NULL == plugin_ins) { PDEPOT_LOG_ERROR("failed to get plugin instance, the plugin is <%s>", dll_info->dll_path.c_str()); return -1; } m_plugin_class_vec.push_back(plugin_ins); if (!isAvailablePlugin(plugin_ins)) { // todo //delete plugin_ins; plugin_ins = NULL; continue; } PDEPOT_LOG_INFO("start to init plugin : <%s>", dll_info->dll_path.c_str()); //plugin_cfg.plugin_depot = this; plugin_cfg.scene = m_plugin_depot_cfg.scene; plugin_cfg.pool = m_plugin_depot_cfg.pool; //plugin_cfg.handle_output = m_plugin_depot_cfg.handle_output; plugin_cfg.plugin_config = *it_param; plugin_cfg.cache_type = m_plugin_depot_cfg.cache_type; plugin_cfg.line_no = m_plugin_depot_cfg.line_no; plugin_cfg.manage_grid = m_plugin_depot_cfg.manage_grid; plugin_cfg.message = m_plugin_depot_cfg.scene; plugin_cfg.template_id = m_plugin_depot_cfg.template_id; plugin_cfg.logger = m_plugin_depot_cfg.logger; plugin_cfg.line_scene = m_plugin_depot_cfg.line_scene; plugin_cfg.scene_request = m_plugin_depot_cfg.scene_request; plugin_cfg.is_first_launch = m_plugin_depot_cfg.is_first_launch; plugin_cfg.enable_gm = m_plugin_depot_cfg.enable_gm; plugin_cfg.data_record = m_plugin_depot_cfg.data_record; plugin_cfg.server_cfg = m_plugin_depot_cfg.server_cfg; plugin_cfg.cross_server = m_plugin_depot_cfg.cross_server; plugin_cfg.manage_container = m_plugin_depot_cfg.line_scene->getManageContainer(); if (plugin_ins->init(plugin_cfg) == -1) { init_plugin_result = -1; PDEPOT_LOG_ERROR("failed to init the plugin, plugin path is <%s>", dll_info->dll_path.c_str()); break; } if (is_first_time) { PDEPOT_LOG_INFO("success to init plugin : <%s>, type is <%d>", dll_info->dll_path.c_str(), plugin_ins->getType()); } std::pair<PluginMap_t::iterator, bool> insert_result = m_plugin_map.insert(std::make_pair(plugin_ins->getType(), plugin_ins)); if (!insert_result.second) { init_plugin_result = -1; PDEPOT_LOG_ERROR("getting reduplicate plugin dll, the second plugin path is <%s>, plugin type is <%d>", dll_info->dll_path.c_str(), plugin_ins->getType()); break; } } if (-1 != init_plugin_result) { if (!buildMsgMapping()) { init_plugin_result = -1; } } is_first_time = false; return init_plugin_result; }
TEST(PluginTest, HasName) { auto plugin = make_plugin(); ASSERT_STREQ(plugin->name().c_str(), "Hello World Plugin"); plugin->destroy(); }
TEST(PluginTest, CanCreatePlugin) { auto plugin = make_plugin(); ASSERT_TRUE(plugin != NULL); plugin->destroy(); }