X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/8c6fa3fbaccc22127280bf77a48fab5a3ee0716e..46b0747592074017ff0ea4b33d4a7194235886e5:/stargazer/store_loader.cpp diff --git a/stargazer/store_loader.cpp b/stargazer/store_loader.cpp new file mode 100644 index 00000000..1529a2cc --- /dev/null +++ b/stargazer/store_loader.cpp @@ -0,0 +1,132 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +/* + $Revision: 1.6 $ + $Date: 2010/03/04 12:24:19 $ + $Author: faust $ + */ + +/* + * An implementation of RAII store plugin loader + */ + +#include + +#include "stg/common.h" +#include "stg/store.h" +#include "store_loader.h" +#include "settings_impl.h" + +STORE_LOADER::STORE_LOADER(const SETTINGS_IMPL & settings) + : isLoaded(false), + handle(NULL), + plugin(NULL), + errorStr(), + storeSettings(settings.GetStoreModuleSettings()), + pluginFileName(settings.GetModulesPath() + "/mod_" + storeSettings.moduleName + ".so") +{ +} + +STORE_LOADER::~STORE_LOADER() +{ +Unload(); +} + +bool STORE_LOADER::Load() +{ +if (isLoaded) + { + errorStr = "Store plugin '" + pluginFileName + "' was already loaded!"; + printfd(__FILE__, "STORE_LOADER::Load() - %s\n", errorStr.c_str()); + return false; + } + +if (pluginFileName.empty()) + { + errorStr = "Empty store plugin filename"; + printfd(__FILE__, "STORE_LOADER::Load() - %s\n", errorStr.c_str()); + return true; + } + +handle = dlopen(pluginFileName.c_str(), RTLD_NOW); + +if (!handle) + { + errorStr = "Error loading plugin '" + + pluginFileName + "': '" + dlerror() + "'"; + printfd(__FILE__, "STORE_LOADER::Load() - %s\n", errorStr.c_str()); + return true; + } + +isLoaded = true; + +STORE * (*GetStore)(); +GetStore = reinterpret_cast(dlsym(handle, "GetStore")); +if (!GetStore) + { + errorStr = std::string("GetStore() not found! ") + dlerror(); + printfd(__FILE__, "STORE_LOADER::Load() - %s\n", errorStr.c_str()); + return true; + } + +plugin = GetStore(); + +if (!plugin) + { + errorStr = "Plugin was not created!"; + printfd(__FILE__, "STORE_LOADER::Load() - %s\n"); + return true; + } + +plugin->SetSettings(storeSettings); +if (plugin->ParseSettings()) + { + errorStr = plugin->GetStrError(); + printfd(__FILE__, "STORE_LOADER::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str()); + return true; + } + +return false; +} + +bool STORE_LOADER::Unload() +{ +printfd(__FILE__, "STORE_LOADER::Unload()\n"); +if (!isLoaded) + { + return true; + } + +delete plugin; + +if (dlclose(handle)) + { + errorStr = "Failed to unload plugin '"; + errorStr += pluginFileName + "': "; + errorStr += dlerror(); + printfd(__FILE__, "STORE_LOADER::Unload() - %s\n", errorStr.c_str()); + return true; + } + +isLoaded = false; + +return false; +}