X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/641204dfbdb9fc870cdd2e7f9e3169a44693e7bf..7370ec4d9c429d3c71c24d4e2c5c8296b751ae12:/projects/stargazer/plugin_runner.cpp?ds=inline diff --git a/projects/stargazer/plugin_runner.cpp b/projects/stargazer/plugin_runner.cpp index 180713c0..b93d6d0a 100644 --- a/projects/stargazer/plugin_runner.cpp +++ b/projects/stargazer/plugin_runner.cpp @@ -26,21 +26,24 @@ #include #include -#include +#include "stg/common.h" +#include "stg/traffcounter.h" #include "plugin_runner.h" -#include "common.h" -#include "conffiles.h" +#include "settings_impl.h" +#include "admins_impl.h" +#include "tariffs_impl.h" +#include "users_impl.h" //----------------------------------------------------------------------------- -PLUGIN_RUNNER::PLUGIN_RUNNER(const string & pFileName, +PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName, const MODULE_SETTINGS & ms, - ADMINS * a, - TARIFFS * t, - USERS * u, + ADMINS_IMPL * a, + TARIFFS_IMPL * t, + USERS_IMPL * u, TRAFFCOUNTER * tc, - BASE_STORE * st, - const SETTINGS * s) + STORE * st, + const SETTINGS_IMPL * s) : pluginFileName(pFileName), pluginSettingFileName(), plugin(NULL), @@ -103,11 +106,18 @@ if (isPluginLoaded) Unload(); } -isPluginLoaded = 0; +isPluginLoaded = false; } //----------------------------------------------------------------------------- -BASE_PLUGIN * PLUGIN_RUNNER::GetPlugin() +PLUGIN * PLUGIN_RUNNER::GetPlugin() { +if (!isPluginLoaded) + { + errorStr = "Plugin '" + pluginFileName + "' is not loaded yet!"; + printfd(__FILE__, "PLUGIN_LOADER::GetPlugin() - %s\n", errorStr.c_str()); + return NULL; + } + return plugin; } //----------------------------------------------------------------------------- @@ -117,6 +127,13 @@ if (!isPluginLoaded) if (Load()) return -1; +if (!plugin) + { + errorStr = "Plugin '" + pluginFileName + "' was not created!"; + printfd(__FILE__, "PLUGIN_LOADER::Start() - %s\n", errorStr.c_str()); + return -1; + } + plugin->SetTariffs(tariffs); plugin->SetAdmins(admins); plugin->SetUsers(users); @@ -129,20 +146,47 @@ if (plugin->Start()) errorStr = plugin->GetStrError(); return -1; } + return 0; } //----------------------------------------------------------------------------- int PLUGIN_RUNNER::Stop() { +if (!isPluginLoaded) + { + errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!"; + printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str()); + return -1; + } + +if (!plugin) + { + errorStr = "Plugin '" + pluginFileName + "' was not created!"; + printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str()); + return -1; + } + plugin->Stop(); -//if (Unload()) -// return -1; return 0; } //----------------------------------------------------------------------------- int PLUGIN_RUNNER::Reload() { +if (!isPluginLoaded) + { + errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!"; + printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str()); + return -1; + } + +if (!plugin) + { + errorStr = "Plugin '" + pluginFileName + "' was not created!"; + printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str()); + return -1; + } + int res = plugin->Reload(); errorStr = plugin->GetStrError(); return res; @@ -151,16 +195,35 @@ return res; bool PLUGIN_RUNNER::IsRunning() { if (!isPluginLoaded) + { + errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!"; + printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str()); + return false; + } + +if (!plugin) + { + errorStr = "Plugin '" + pluginFileName + "' was not created!"; + printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str()); return false; + } + return plugin->IsRunning(); } //----------------------------------------------------------------------------- int PLUGIN_RUNNER::Load() { -if (!pluginFileName.size()) +if (isPluginLoaded) { - errorStr = "Plugin loading failed. No plugin"; - printfd(__FILE__, "%s\n", errorStr.c_str()); + errorStr = "Plugin '" + pluginFileName + "' was already loaded!"; + printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str()); + return -1; + } + +if (pluginFileName.empty()) + { + errorStr = "Empty plugin file name!"; + printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str()); return -1; } @@ -168,33 +231,36 @@ libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW); if (!libHandle) { - errorStr = string("Plugin loading failed. ") + dlerror(); - printfd(__FILE__, "%s\n", errorStr.c_str()); + errorStr = "Error loading plugin '" + + pluginFileName + "': '" + dlerror() + "'"; + printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str()); return -1; } -BASE_PLUGIN * (*GetPlugin)(); -GetPlugin = (BASE_PLUGIN * (*)())dlsym(libHandle, "GetPlugin"); +isPluginLoaded = true; + +PLUGIN * (*GetPlugin)(); +GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin"); if (!GetPlugin) { - errorStr = string("GetPlugin() not found. ") + dlerror(); + errorStr = std::string("GetPlugin() not found. ") + dlerror(); + printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str()); return -1; } plugin = GetPlugin(); -isPluginLoaded++; if (!plugin) { errorStr = "Plugin was not created!"; - printfd(__FILE__, "%s\n", errorStr.c_str()); + printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str()); return -1; } plugin->SetSettings(modSettings); -printfd(__FILE__, "Plugin %s parsesettings\n", plugin->GetVersion().c_str()); if (plugin->ParseSettings()) { - errorStr = "Plugin \'" + plugin->GetVersion() + "\' error: " + plugin->GetStrError(); + errorStr = plugin->GetStrError(); + printfd(__FILE__, "PLUGIN_LOADER::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str()); return -1; } @@ -207,28 +273,15 @@ if (isPluginLoaded) { if (dlclose(libHandle)) { - errorStr = dlerror(); - printfd(__FILE__, "Error unloading plugin '%s': '%s'", pluginFileName.c_str(), dlerror()); + errorStr = "Failed to unload plugin '"; + errorStr += pluginFileName + "': "; + errorStr += dlerror(); + printfd(__FILE__, "PLUGIN_LOADER::Unload() - %s", errorStr.c_str()); return -1; } - isPluginLoaded--; + plugin = NULL; + isPluginLoaded = false; } return 0; } //----------------------------------------------------------------------------- -const 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(); -} -//----------------------------------------------------------------------------- -