X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/c963a109219ed101fa42f501b16f90d7b7b4f3f2..be1b46cc784e43cd28c5521bdcde79bd7463fb40:/projects/stargazer/plugin_runner.cpp?ds=inline diff --git a/projects/stargazer/plugin_runner.cpp b/projects/stargazer/plugin_runner.cpp index e21bb75d..8938950f 100644 --- a/projects/stargazer/plugin_runner.cpp +++ b/projects/stargazer/plugin_runner.cpp @@ -16,218 +16,140 @@ /* * Author : Boris Mikhailenko + * Author : Maxim Mamontov */ -/* - $Revision: 1.17 $ - $Date: 2010/09/13 05:52:46 $ - $Author: faust $ - */ +#include "plugin_runner.h" + +#include "stg/common.h" #include #include -#include -#include "plugin_runner.h" -#include "common.h" -#include "conffiles.h" +using STG::PluginRunner; +using STG::Plugin; //----------------------------------------------------------------------------- -PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName, - const MODULE_SETTINGS & ms, - ADMINS * a, - TARIFFS * t, - USERS * u, - TRAFFCOUNTER * tc, - STORE * st, - const SETTINGS * s) - : pluginFileName(pFileName), - pluginSettingFileName(), - plugin(NULL), - isPluginLoaded(false), - errorStr(), +PluginRunner::PluginRunner(const std::string& fileName, + const std::string& name, + const ModuleSettings& ms, + Admins& admins, + Tariffs& tariffs, + Users& users, + Services& services, + Corporations& corporations, + TraffCounter& traffcounter, + Store& store, + const Settings& settings) + : pluginFileName(fileName), + pluginName(name), libHandle(NULL), - isRunning(false), - admins(a), - tariffs(t), - users(u), - store(st), - traffCnt(tc), - stgSettings(s), - modSettings(ms) -{ -} -//----------------------------------------------------------------------------- -PLUGIN_RUNNER::PLUGIN_RUNNER(const PLUGIN_RUNNER & rvalue) - : pluginFileName(rvalue.pluginFileName), - pluginSettingFileName(rvalue.pluginSettingFileName), - plugin(rvalue.plugin), - isPluginLoaded(rvalue.isPluginLoaded), - errorStr(rvalue.errorStr), - libHandle(rvalue.libHandle), - isRunning(rvalue.isRunning), - admins(rvalue.admins), - tariffs(rvalue.tariffs), - users(rvalue.users), - store(rvalue.store), - traffCnt(rvalue.traffCnt), - stgSettings(rvalue.stgSettings), - modSettings(rvalue.modSettings) -{ -} -//----------------------------------------------------------------------------- -PLUGIN_RUNNER & PLUGIN_RUNNER::operator=(const PLUGIN_RUNNER & rvalue) + m_plugin(load(ms, admins, tariffs, users, services, corporations, + traffcounter, store, settings)) { -pluginFileName = rvalue.pluginFileName; -pluginSettingFileName = rvalue.pluginSettingFileName; -plugin = rvalue.plugin; -isPluginLoaded = rvalue.isPluginLoaded; -errorStr = rvalue.errorStr; -libHandle = rvalue.libHandle; -isRunning = rvalue.isRunning; -admins = rvalue.admins; -tariffs = rvalue.tariffs; -users = rvalue.users; -store = rvalue.store; -traffCnt = rvalue.traffCnt; -stgSettings = rvalue.stgSettings; -modSettings = rvalue.modSettings; - -return *this; } //----------------------------------------------------------------------------- -PLUGIN_RUNNER::~PLUGIN_RUNNER() +PluginRunner::~PluginRunner() { -if (isPluginLoaded) - { - Unload(); - } - -isPluginLoaded = 0; -} -//----------------------------------------------------------------------------- -PLUGIN * PLUGIN_RUNNER::GetPlugin() -{ -return plugin; -} -//----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Start() -{ -if (!isPluginLoaded) - if (Load()) - return -1; - -plugin->SetTariffs(tariffs); -plugin->SetAdmins(admins); -plugin->SetUsers(users); -plugin->SetTraffcounter(traffCnt); -plugin->SetStore(store); -plugin->SetStgSettings(stgSettings); - -if (plugin->Start()) + delete &m_plugin; + if (dlclose(libHandle)) { - errorStr = plugin->GetStrError(); - return -1; + errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror(); + printfd(__FILE__, "PluginRunner::Unload() - %s", errorStr.c_str()); } -return 0; } //----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Stop() +int PluginRunner::Start() { -plugin->Stop(); - -//if (Unload()) -// return -1; -return 0; + int res = m_plugin.Start(); + errorStr = m_plugin.GetStrError(); + return res; } //----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Reload() +int PluginRunner::Stop() { -int res = plugin->Reload(); -errorStr = plugin->GetStrError(); -return res; + int res = m_plugin.Stop(); + errorStr = m_plugin.GetStrError(); + return res; } //----------------------------------------------------------------------------- -bool PLUGIN_RUNNER::IsRunning() +int PluginRunner::Reload(const ModuleSettings& ms) { -if (!isPluginLoaded) - return false; -return plugin->IsRunning(); + int res = m_plugin.Reload(ms); + errorStr = m_plugin.GetStrError(); + return res; } //----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Load() +Plugin & PluginRunner::load(const ModuleSettings& ms, + Admins& admins, + Tariffs& tariffs, + Users& users, + Services& services, + Corporations& corporations, + TraffCounter& traffcounter, + Store& store, + const Settings& settings) { -if (!pluginFileName.size()) + if (pluginFileName.empty()) { - errorStr = "Plugin loading failed. No plugin"; - printfd(__FILE__, "%s\n", errorStr.c_str()); - return -1; + const std::string msg = "Empty plugin file name."; + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); } -libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW); - -if (!libHandle) + if (access(pluginFileName.c_str(), R_OK)) { - errorStr = std::string("Plugin loading failed. ") + dlerror(); - printfd(__FILE__, "%s\n", errorStr.c_str()); - return -1; + const std::string msg = "Plugin file '" + pluginFileName + "' is missing or inaccessible."; + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); } -PLUGIN * (*GetPlugin)(); -GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin"); -if (!GetPlugin) + libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW); + + if (!libHandle) { - errorStr = std::string("GetPlugin() not found. ") + dlerror(); - return -1; + std::string msg = "Error loading plugin '" + pluginFileName + "'"; + const char* error = dlerror(); + if (error) + msg = msg + ": '" + error + "'"; + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); } -plugin = GetPlugin(); -isPluginLoaded++; -if (!plugin) + using Getter = Plugin* (*)(); + auto GetPlugin = reinterpret_cast(dlsym(libHandle, "GetPlugin")); + if (!GetPlugin) { - errorStr = "Plugin was not created!"; - printfd(__FILE__, "%s\n", errorStr.c_str()); - return -1; + const std::string msg = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. "; + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); } -plugin->SetSettings(modSettings); -printfd(__FILE__, "Plugin %s parsesettings\n", plugin->GetVersion().c_str()); -if (plugin->ParseSettings()) + Plugin* plugin = GetPlugin(); + + if (!plugin) { - errorStr = "Plugin \'" + plugin->GetVersion() + "\' error: " + plugin->GetStrError(); - return -1; + const std::string msg = "Failed to create an instance of plugin '" + pluginFileName + "'."; + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); } -return 0; -} -//----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Unload() -{ -if (isPluginLoaded) + plugin->SetSettings(ms); + plugin->SetTariffs(&tariffs); + plugin->SetAdmins(&admins); + plugin->SetUsers(&users); + plugin->SetServices(&services); + plugin->SetCorporations(&corporations); + plugin->SetTraffcounter(&traffcounter); + plugin->SetStore(&store); + plugin->SetStgSettings(&settings); + + if (plugin->ParseSettings()) { - if (dlclose(libHandle)) - { - errorStr = dlerror(); - printfd(__FILE__, "Error unloading plugin '%s': '%s'", pluginFileName.c_str(), dlerror()); - return -1; - } - isPluginLoaded--; + const std::string msg = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError(); + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); } -return 0; -} -//----------------------------------------------------------------------------- -const std::string & PLUGIN_RUNNER::GetStrError() const -{ -return errorStr; -} -//----------------------------------------------------------------------------- -uint16_t PLUGIN_RUNNER::GetStartPosition() const -{ -return plugin->GetStartPosition(); -} -//----------------------------------------------------------------------------- -uint16_t PLUGIN_RUNNER::GetStopPosition() const -{ -return plugin->GetStopPosition(); + + return *plugin; } -//-----------------------------------------------------------------------------