X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/641204dfbdb9fc870cdd2e7f9e3169a44693e7bf..86fd475d6f97b818d7cb45e91625a1cb7ac9c8e6:/projects/stargazer/plugin_runner.cpp diff --git a/projects/stargazer/plugin_runner.cpp b/projects/stargazer/plugin_runner.cpp index 180713c0..3083352c 100644 --- a/projects/stargazer/plugin_runner.cpp +++ b/projects/stargazer/plugin_runner.cpp @@ -26,21 +26,28 @@ #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" +#include "services_impl.h" +#include "corps_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, + SERVICES_IMPL * svc, + CORPORATIONS_IMPL * crp, TRAFFCOUNTER * tc, - BASE_STORE * st, - const SETTINGS * s) + STORE * st, + const SETTINGS_IMPL * s) : pluginFileName(pFileName), pluginSettingFileName(), plugin(NULL), @@ -51,6 +58,8 @@ PLUGIN_RUNNER::PLUGIN_RUNNER(const string & pFileName, admins(a), tariffs(t), users(u), + services(svc), + corps(crp), store(st), traffCnt(tc), stgSettings(s), @@ -69,6 +78,8 @@ PLUGIN_RUNNER::PLUGIN_RUNNER(const PLUGIN_RUNNER & rvalue) admins(rvalue.admins), tariffs(rvalue.tariffs), users(rvalue.users), + services(rvalue.services), + corps(rvalue.corps), store(rvalue.store), traffCnt(rvalue.traffCnt), stgSettings(rvalue.stgSettings), @@ -88,6 +99,8 @@ isRunning = rvalue.isRunning; admins = rvalue.admins; tariffs = rvalue.tariffs; users = rvalue.users; +services = rvalue.services; +corps = rvalue.corps; store = rvalue.store; traffCnt = rvalue.traffCnt; stgSettings = rvalue.stgSettings; @@ -103,11 +116,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,9 +137,18 @@ 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); +plugin->SetServices(services); +plugin->SetCorporations(corps); plugin->SetTraffcounter(traffCnt); plugin->SetStore(store); plugin->SetStgSettings(stgSettings); @@ -129,20 +158,45 @@ if (plugin->Start()) errorStr = plugin->GetStrError(); return -1; } + return 0; } //----------------------------------------------------------------------------- int PLUGIN_RUNNER::Stop() { -plugin->Stop(); +if (!isPluginLoaded) + { + errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!"; + printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str()); + return -1; + } -//if (Unload()) -// return -1; -return 0; +if (!plugin) + { + errorStr = "Plugin '" + pluginFileName + "' was not created!"; + printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str()); + return -1; + } + +return plugin->Stop(); } //----------------------------------------------------------------------------- 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 +205,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 +241,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 +283,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(); -} -//----------------------------------------------------------------------------- -