From: Maxim Mamontov Date: Fri, 24 Oct 2014 02:56:36 +0000 (+0300) Subject: Moved plugin-related things into a separate file. X-Git-Tag: 2.409~234 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/55a204e46261850f1a26ebafc45fb3ba6f0604c6?ds=sidebyside;hp=436fa7f75542e619096c6fad7f05029083743102 Moved plugin-related things into a separate file. Conflicts: projects/stargazer/user_impl.cpp --- diff --git a/projects/stargazer/Makefile b/projects/stargazer/Makefile index 07c3f79e..e210dc2f 100644 --- a/projects/stargazer/Makefile +++ b/projects/stargazer/Makefile @@ -13,6 +13,7 @@ SRCS = ./admin_impl.cpp \ ./main.cpp \ ./pidfile.cpp \ ./plugin_runner.cpp \ + ./plugin_mgr.cpp \ ./settings_impl.cpp \ ./services_impl.cpp \ ./stg_timer.cpp \ diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 200fd2a6..6a96519b 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -18,50 +18,44 @@ * Author : Boris Mikhailenko */ - /* - $Revision: 1.124 $ - $Date: 2010/10/04 20:19:12 $ - $Author: faust $ - */ - -#include -#include -#include -#include -#include -#include // S_IRUSR -#include // create - -#include -#include -#include -#include // srandom, exit -#include -#include -#include -#include - -#include "stg/user.h" -#include "stg/common.h" -#include "stg/plugin.h" -#include "stg/logger.h" -#include "stg/scriptexecuter.h" -#include "stg/version.h" -#include "stg_timer.h" -#include "settings_impl.h" +#include "store_loader.h" +#include "plugin_mgr.h" +#include "plugin_runner.h" #include "users_impl.h" #include "admins_impl.h" #include "tariffs_impl.h" #include "services_impl.h" #include "corps_impl.h" #include "traffcounter_impl.h" -#include "plugin_runner.h" -#include "store_loader.h" +#include "settings_impl.h" #include "pidfile.h" #include "eventloop.h" +#include "stg_timer.h" + +#include "stg/user.h" +#include "stg/common.h" +#include "stg/plugin.h" +#include "stg/logger.h" +#include "stg/scriptexecuter.h" +#include "stg/version.h" + +#include +#include +#include +#include +#include +#include +#include // srandom, exit + +#include +#include +#include +#include +#include +#include // S_IRUSR +#include // create #ifdef DEBUG - #define MAIN_DEBUG (1) #define NO_DAEMON (1) #endif @@ -71,23 +65,11 @@ namespace { std::set executersPid; -bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs); -bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs); void StartTimer(); int StartScriptExecuter(char * procName, int msgKey, int * msgID, SETTINGS_IMPL * settings); int ForkAndWait(const std::string & confDir); void KillExecuters(); -//----------------------------------------------------------------------------- -bool StartModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs) -{ -return lhs.GetStartPosition() < rhs.GetStartPosition(); -} -//----------------------------------------------------------------------------- -bool StopModCmp(const PLUGIN_RUNNER & lhs, const PLUGIN_RUNNER & rhs) -{ -return lhs.GetStopPosition() > rhs.GetStopPosition(); -} //----------------------------------------------------------------------------- void StartTimer() { @@ -231,29 +213,14 @@ while (pid != executersPid.end()) int main(int argc, char * argv[]) { SETTINGS_IMPL * settings = NULL; -STORE * dataStore = NULL; -TARIFFS_IMPL * tariffs = NULL; -ADMINS_IMPL * admins = NULL; -USERS_IMPL * users = NULL; -TRAFFCOUNTER_IMPL * traffCnt = NULL; -SERVICES_IMPL * services = NULL; -CORPORATIONS_IMPL * corps = NULL; int msgID = -11; - { - STG_LOGGER & WriteServLog = GetStgLogger(); - WriteServLog.SetLogFileName("/var/log/stargazer.log"); - } - -std::vector modSettings; -std::list modules; - -std::list::iterator modIter; +GetStgLogger().SetLogFileName("/var/log/stargazer.log"); if (getuid()) { printf("You must be root. Exit.\n"); - exit(1); + return 1; } if (argc == 2) @@ -269,7 +236,7 @@ if (settings->ReadSettings()) WriteServLog.SetLogFileName(settings->GetLogFileName()); WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str()); - exit(1); + return -1; } #ifndef NO_DAEMON @@ -280,7 +247,7 @@ if (ForkAndWait(settings->GetConfDir()) < 0) { STG_LOGGER & WriteServLog = GetStgLogger(); WriteServLog("Fork error!"); - exit(1); + return -1; } STG_LOGGER & WriteServLog = GetStgLogger(); @@ -294,7 +261,7 @@ for (size_t i = 0; i < settings->GetExecutersNum(); i++) { STG_LOGGER & WriteServLog = GetStgLogger(); WriteServLog("Start Script Executer error!"); - return 1; + return -1; } if (ret == 1) { @@ -315,6 +282,7 @@ if (!IsStgTimerRunning()) { printfd(__FILE__, "Timer thread not started in 1 sec!\n"); WriteServLog("Timer thread not started in 1 sec!"); + return -1; } EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance()); @@ -322,92 +290,40 @@ EVENT_LOOP & loop(EVENT_LOOP_SINGLETON::GetInstance()); STORE_LOADER storeLoader(*settings); if (storeLoader.Load()) { + printfd(__FILE__, "Storage plugin: '%s'\n", storeLoader.GetStrError().c_str()); WriteServLog("Storage plugin: '%s'", storeLoader.GetStrError().c_str()); - goto exitLblNotStarted; + return -1; } if (loop.Start()) { + printfd(__FILE__, "Event loop not started.\n"); WriteServLog("Event loop not started."); - goto exitLblNotStarted; + return -1; } -dataStore = storeLoader.GetStore(); -WriteServLog("Storage plugin: %s. Loading successfull.", dataStore->GetVersion().c_str()); +STORE & store(storeLoader.GetStore()); +WriteServLog("Storage plugin: %s. Loading successfull.", store.GetVersion().c_str()); -tariffs = new TARIFFS_IMPL(dataStore); -admins = new ADMINS_IMPL(dataStore); -users = new USERS_IMPL(settings, dataStore, tariffs, admins->GetSysAdmin()); -traffCnt = new TRAFFCOUNTER_IMPL(users, settings->GetRulesFileName()); -services = new SERVICES_IMPL(dataStore); -corps = new CORPORATIONS_IMPL(dataStore); -traffCnt->SetMonitorDir(settings->GetMonitorDir()); +ADMINS_IMPL admins(&store); +TARIFFS_IMPL tariffs(&store); +SERVICES_IMPL services(&store); +CORPORATIONS_IMPL corps(&store); +USERS_IMPL users(settings, &store, &tariffs, services, admins.GetSysAdmin()); +TRAFFCOUNTER_IMPL traffCnt(&users, settings->GetRulesFileName()); +traffCnt.SetMonitorDir(settings->GetMonitorDir()); -modSettings = settings->GetModulesSettings(); +if (users.Start()) + return -1; -for (size_t i = 0; i < modSettings.size(); i++) - { - std::string modulePath = settings->GetModulesPath(); - modulePath += "/mod_"; - modulePath += modSettings[i].moduleName; - modulePath += ".so"; - printfd(__FILE__, "Module: %s\n", modulePath.c_str()); - modules.push_back( - PLUGIN_RUNNER(modulePath, - modSettings[i], - admins, - tariffs, - users, - services, - corps, - traffCnt, - dataStore, - settings) - ); - } - -modIter = modules.begin(); - -while (modIter != modules.end()) - { - if (modIter->Load()) - { - WriteServLog("Error loading module '%s': %s", - modIter->GetPlugin()->GetVersion().c_str(), - modIter->GetStrError().c_str()); - goto exitLblNotStarted; - } - ++modIter; - } - -if (users->Start()) - { - goto exitLblNotStarted; - } WriteServLog("Users started successfully."); -if (traffCnt->Start()) - { - goto exitLblNotStarted; - } -WriteServLog("Traffcounter started successfully."); +if (traffCnt.Start()) + return -1; -//Sort by start order -modules.sort(StartModCmp); -modIter = modules.begin(); +WriteServLog("Traffcounter started successfully."); -while (modIter != modules.end()) - { - if (modIter->Start()) - { - WriteServLog("Error starting module '%s': %s", - modIter->GetPlugin()->GetVersion().c_str(), - modIter->GetStrError().c_str()); - goto exitLbl; - } - WriteServLog("Module: '%s'. Start successfull.", modIter->GetPlugin()->GetVersion().c_str()); - ++modIter; - } +STG::PluginManager manager(*settings, store, admins, tariffs, services, corps, users, traffCnt); srandom(static_cast(stgTime)); @@ -430,18 +346,8 @@ while (true) switch (sig) { case SIGHUP: - traffCnt->Reload(); - modIter = modules.begin(); - for (; modIter != modules.end(); ++modIter) - { - if (modIter->Reload()) - { - WriteServLog("Error reloading module '%s': '%s'", modIter->GetPlugin()->GetVersion().c_str(), - modIter->GetStrError().c_str()); - printfd(__FILE__, "Error reloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), - modIter->GetStrError().c_str()); - } - } + traffCnt.Reload(); + manager.reload(); break; case SIGTERM: stop = true; @@ -471,71 +377,16 @@ while (true) break; } -exitLbl: - WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++"); -//Sort by start order -modules.sort(StopModCmp); -modIter = modules.begin(); -while (modIter != modules.end()) - { - std::string name = modIter->GetFileName(); - printfd(__FILE__, "Stopping module '%s'\n", name.c_str()); - if (modIter->Stop()) - { - WriteServLog("Error stopping module '%s': %s", - modIter->GetPlugin()->GetVersion().c_str(), - modIter->GetStrError().c_str()); - printfd(__FILE__, "Error stopping module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str()); - } - else - { - WriteServLog("Module: '%s'. Stop successfull.", modIter->GetPlugin()->GetVersion().c_str()); - } - ++modIter; - } - if (loop.Stop()) - { WriteServLog("Event loop not stopped."); - } -exitLblNotStarted: - -modIter = modules.begin(); -while (modIter != modules.end()) - { - std::string name = modIter->GetFileName(); - if (modIter->IsRunning()) - { - printfd(__FILE__, "Passing module '%s' `cause it's still running\n", name.c_str()); - } - else - { - printfd(__FILE__, "Unloading module '%s'\n", name.c_str()); - if (modIter->Unload()) - { - WriteServLog("Error unloading module '%s': '%s'", - modIter->GetPlugin()->GetVersion().c_str(), - modIter->GetStrError().c_str()); - printfd(__FILE__, "Error unloading module '%s': '%s'\n", modIter->GetPlugin()->GetVersion().c_str(), modIter->GetStrError().c_str()); - } - } - ++modIter; - } - -if (traffCnt) - { - traffCnt->Stop(); +if (!traffCnt.Stop()) WriteServLog("Traffcounter: Stop successfull."); - } -if (users) - { - users->Stop(); +if (!users.Stop()) WriteServLog("Users: Stop successfull."); - } sleep(1); int res = msgctl(msgID, IPC_RMID, NULL); @@ -549,12 +400,6 @@ KillExecuters(); StopStgTimer(); WriteServLog("StgTimer: Stop successfull."); -delete corps; -delete services; -delete traffCnt; -delete users; -delete admins; -delete tariffs; delete settings; WriteServLog("Stg stopped successfully."); diff --git a/projects/stargazer/plugin_mgr.cpp b/projects/stargazer/plugin_mgr.cpp new file mode 100644 index 00000000..69c7efeb --- /dev/null +++ b/projects/stargazer/plugin_mgr.cpp @@ -0,0 +1,115 @@ +/* + * 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 + */ + +#include "plugin_mgr.h" + +#include "plugin_runner.h" + +#include "admins_impl.h" +#include "tariffs_impl.h" +#include "services_impl.h" +#include "corps_impl.h" +#include "users_impl.h" +#include "traffcounter_impl.h" +#include "settings_impl.h" + +#include "stg/logger.h" + +using STG::PluginManager; + +namespace +{ + +bool StartModCmp(const PLUGIN_RUNNER * lhs, const PLUGIN_RUNNER * rhs) +{ + return lhs->GetStartPosition() < rhs->GetStartPosition(); +} + +bool StopModCmp(const PLUGIN_RUNNER * lhs, const PLUGIN_RUNNER * rhs) +{ + return lhs->GetStopPosition() > rhs->GetStopPosition(); +} + +} // namespace anonymous + +PluginManager::PluginManager(const SETTINGS_IMPL& settings, + STORE& store, ADMINS_IMPL& admins, TARIFFS_IMPL& tariffs, + SERVICES_IMPL& services, CORPORATIONS_IMPL& corporations, + USERS_IMPL& users, TRAFFCOUNTER_IMPL& traffcounter) + : m_log(GetStgLogger()) +{ + std::string basePath = settings.GetModulesPath(); + const std::vector & modSettings(settings.GetModulesSettings()); + for (size_t i = 0; i < modSettings.size(); i++) + { + std::string modulePath = basePath + "/mod_" + modSettings[i].moduleName + ".so"; + printfd(__FILE__, "Module: %s\n", modulePath.c_str()); + try + { + m_modules.push_back( + new PLUGIN_RUNNER(modulePath, modSettings[i], admins, tariffs, + users, services, corporations, traffcounter, + store, settings) + ); + } + catch (const PLUGIN_RUNNER::Error & ex) + { + m_log(ex.what()); + printfd(__FILE__, "%s\n", ex.what()); + // TODO: React + } + } + std::sort(m_modules.begin(), m_modules.end(), StartModCmp); + for (size_t i = 0; i < m_modules.size(); ++i) + { + PLUGIN & plugin = m_modules[i]->GetPlugin(); + if (m_modules[i]->Start()) + { + m_log("Failed to start module '%s': '%s'", plugin.GetVersion().c_str(), + plugin.GetStrError().c_str()); + printfd(__FILE__, "Failed to start module '%s': '%s'", plugin.GetVersion().c_str(), + plugin.GetStrError().c_str()); + } + } +} + +PluginManager::~PluginManager() +{ + std::sort(m_modules.begin(), m_modules.end(), StopModCmp); + for (size_t i = 0; i < m_modules.size(); ++i) + m_modules[i]->Stop(); + for (size_t i = 0; i < m_modules.size(); ++i) + delete m_modules[i]; +} + +void PluginManager::reload() +{ + for (size_t i = 0; i < m_modules.size(); ++i) + { + PLUGIN & plugin = m_modules[i]->GetPlugin(); + if (m_modules[i]->Reload()) + { + m_log("Error reloading module '%s': '%s'", plugin.GetVersion().c_str(), + plugin.GetStrError().c_str()); + printfd(__FILE__, "Error reloading module '%s': '%s'\n", plugin.GetVersion().c_str(), + plugin.GetStrError().c_str()); + } + } +} diff --git a/projects/stargazer/plugin_mgr.h b/projects/stargazer/plugin_mgr.h new file mode 100644 index 00000000..79b6cae9 --- /dev/null +++ b/projects/stargazer/plugin_mgr.h @@ -0,0 +1,60 @@ +#ifndef __STG_PLUGIN_MGR_H__ +#define __STG_PLUGIN_MGR_H__ + +/* + * 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 + */ + +#include "stg/module_settings.h" + +#include + +class SETTINGS_IMPL; +class PLUGIN_RUNNER; +class STORE; +class ADMINS_IMPL; +class TARIFFS_IMPL; +class SERVICES_IMPL; +class CORPORATIONS_IMPL; +class USERS_IMPL; +class TRAFFCOUNTER_IMPL; +class STG_LOGGER; + +namespace STG +{ + +class PluginManager +{ + public: + PluginManager(const SETTINGS_IMPL& settings, + STORE& store, ADMINS_IMPL& admins, TARIFFS_IMPL& tariffs, + SERVICES_IMPL& services, CORPORATIONS_IMPL& corporations, + USERS_IMPL& users, TRAFFCOUNTER_IMPL& traffcounter); + ~PluginManager(); + + void reload(); + + private: + std::vector m_modules; + STG_LOGGER & m_log; +}; + +} // namespace STG + +#endif diff --git a/projects/stargazer/plugin_runner.cpp b/projects/stargazer/plugin_runner.cpp index 0948987c..bcf68bee 100644 --- a/projects/stargazer/plugin_runner.cpp +++ b/projects/stargazer/plugin_runner.cpp @@ -16,225 +16,79 @@ /* * 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 "stg/common.h" -#include "stg/traffcounter.h" -#include "plugin_runner.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 std::string & pFileName, +PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & fileName, const MODULE_SETTINGS & ms, - ADMINS_IMPL * a, - TARIFFS_IMPL * t, - USERS_IMPL * u, - SERVICES_IMPL * svc, - CORPORATIONS_IMPL * crp, - TRAFFCOUNTER * tc, - STORE * st, - const SETTINGS_IMPL * s) - : pluginFileName(pFileName), - pluginSettingFileName(), - plugin(NULL), - isPluginLoaded(false), - errorStr(), + ADMINS & admins, + TARIFFS & tariffs, + USERS & users, + SERVICES & services, + CORPORATIONS & corporations, + TRAFFCOUNTER & traffcounter, + STORE & store, + const SETTINGS & settings) + : pluginFileName(fileName), libHandle(NULL), - isRunning(false), - admins(a), - tariffs(t), - users(u), - services(svc), - corps(crp), - 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), - services(rvalue.services), - corps(rvalue.corps), - store(rvalue.store), - traffCnt(rvalue.traffCnt), - stgSettings(rvalue.stgSettings), - modSettings(rvalue.modSettings) + m_plugin(Load(ms, admins, tariffs, users, services, corporations, + traffcounter, store, settings)) { } //----------------------------------------------------------------------------- -PLUGIN_RUNNER & PLUGIN_RUNNER::operator=(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; -services = rvalue.services; -corps = rvalue.corps; -store = rvalue.store; -traffCnt = rvalue.traffCnt; -stgSettings = rvalue.stgSettings; -modSettings = rvalue.modSettings; - -return *this; -} -//----------------------------------------------------------------------------- PLUGIN_RUNNER::~PLUGIN_RUNNER() { -if (isPluginLoaded) +if (dlclose(libHandle)) { - Unload(); + errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror(); + printfd(__FILE__, "PLUGIN_RUNNER::Unload() - %s", errorStr.c_str()); } - -isPluginLoaded = false; -} -//----------------------------------------------------------------------------- -PLUGIN * PLUGIN_RUNNER::GetPlugin() -{ -if (!isPluginLoaded) - { - errorStr = "Plugin '" + pluginFileName + "' is not loaded yet!"; - printfd(__FILE__, "PLUGIN_RUNNER::GetPlugin() - %s\n", errorStr.c_str()); - return NULL; - } - -return plugin; } //----------------------------------------------------------------------------- int PLUGIN_RUNNER::Start() { -if (!isPluginLoaded) - if (Load()) - return -1; - -if (!plugin) - { - errorStr = "Plugin '" + pluginFileName + "' was not created!"; - printfd(__FILE__, "PLUGIN_RUNNER::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); - -if (plugin->Start()) - { - errorStr = plugin->GetStrError(); - return -1; - } - -return 0; +int res = m_plugin.Start(); +errorStr = m_plugin.GetStrError(); +return res; } //----------------------------------------------------------------------------- int PLUGIN_RUNNER::Stop() { -if (!isPluginLoaded) - { - errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!"; - printfd(__FILE__, "PLUGIN_RUNNER::Stop() - %s\n", errorStr.c_str()); - return -1; - } - -if (!plugin) - { - errorStr = "Plugin '" + pluginFileName + "' was not created!"; - printfd(__FILE__, "PLUGIN_RUNNER::Stop() - %s\n", errorStr.c_str()); - return -1; - } - -return plugin->Stop(); +int res = m_plugin.Stop(); +errorStr = m_plugin.GetStrError(); +return res; } //----------------------------------------------------------------------------- int PLUGIN_RUNNER::Reload() { -if (!isPluginLoaded) - { - errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!"; - printfd(__FILE__, "PLUGIN_RUNNER::Reload() - %s\n", errorStr.c_str()); - return -1; - } - -if (!plugin) - { - errorStr = "Plugin '" + pluginFileName + "' was not created!"; - printfd(__FILE__, "PLUGIN_RUNNER::Reload() - %s\n", errorStr.c_str()); - return -1; - } - -int res = plugin->Reload(); -errorStr = plugin->GetStrError(); +int res = m_plugin.Reload(); +errorStr = m_plugin.GetStrError(); return res; } //----------------------------------------------------------------------------- -bool PLUGIN_RUNNER::IsRunning() -{ -if (!isPluginLoaded) - { - errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!"; - printfd(__FILE__, "PLUGIN_RUNNER::IsRunning() - %s\n", errorStr.c_str()); - return false; - } - -if (!plugin) - { - errorStr = "Plugin '" + pluginFileName + "' was not created!"; - printfd(__FILE__, "PLUGIN_RUNNER::IsRunning() - %s\n", errorStr.c_str()); - return false; - } - -return plugin->IsRunning(); -} -//----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Load() +PLUGIN & PLUGIN_RUNNER::Load(const MODULE_SETTINGS & ms, + ADMINS & admins, + TARIFFS & tariffs, + USERS & users, + SERVICES & services, + CORPORATIONS & corporations, + TRAFFCOUNTER & traffcounter, + STORE & store, + const SETTINGS & settings) { -if (isPluginLoaded) - { - errorStr = "Plugin '" + pluginFileName + "' was already loaded!"; - printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - return -1; - } - if (pluginFileName.empty()) { - errorStr = "Empty plugin file name!"; + errorStr = "Empty plugin file name."; printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - return -1; + throw Error(errorStr); } libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW); @@ -243,52 +97,42 @@ if (!libHandle) { errorStr = "Error loading plugin '" + pluginFileName + "': '" + dlerror() + "'"; printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - return -1; + throw Error(errorStr); } -isPluginLoaded = true; - PLUGIN * (*GetPlugin)(); GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin"); if (!GetPlugin) { errorStr = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. " + dlerror(); printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - return -1; + throw Error(errorStr); } -plugin = GetPlugin(); +PLUGIN * plugin = GetPlugin(); if (!plugin) { errorStr = "Failed to create an instance of plugin '" + pluginFileName + "'."; printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - return -1; + throw Error(errorStr); } -plugin->SetSettings(modSettings); +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()) { errorStr = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError(); printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - return -1; + throw Error(errorStr); } -return 0; -} -//----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Unload() -{ -if (isPluginLoaded) - { - if (dlclose(libHandle)) - { - errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror(); - printfd(__FILE__, "PLUGIN_RUNNER::Unload() - %s", errorStr.c_str()); - return -1; - } - plugin = NULL; - isPluginLoaded = false; - } -return 0; +return *plugin; } -//----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugin_runner.h b/projects/stargazer/plugin_runner.h index 8e2272bf..1402d2ef 100644 --- a/projects/stargazer/plugin_runner.h +++ b/projects/stargazer/plugin_runner.h @@ -16,86 +16,79 @@ /* * Author : Boris Mikhailenko - */ - -/* - $Revision: 1.13 $ - $Date: 2010/03/04 12:22:41 $ - $Author: faust $ + * Author : Maxim Mamontov */ #ifndef PLUGIN_RUNNER_H #define PLUGIN_RUNNER_H -#include - -#include "stg/module_settings.h" #include "stg/plugin.h" +#include "stg/module_settings.h" #include "stg/os_int.h" -class SETTINGS_IMPL; -class ADMINS_IMPL; -class TARIFFS_IMPL; -class USERS_IMPL; -class SERVICES_IMPL; -class CORPORATIONS_IMPL; +#include +#include + +class SETTINGS; +class ADMINS; +class TARIFFS; +class USERS; +class SERVICES; +class CORPORATIONS; class TRAFFCOUNTER; class STORE; //----------------------------------------------------------------------------- class PLUGIN_RUNNER { public: + struct Error : public std::runtime_error { + Error(const std::string & msg) : runtime_error(msg) {} + }; + PLUGIN_RUNNER(const std::string & pluginFileName, const MODULE_SETTINGS & ms, - ADMINS_IMPL * admins, - TARIFFS_IMPL * tariffs, - USERS_IMPL * users, - SERVICES_IMPL * services, - CORPORATIONS_IMPL * corporations, - TRAFFCOUNTER * tc, - STORE * store, - const SETTINGS_IMPL * s); - PLUGIN_RUNNER(const PLUGIN_RUNNER & rvalue); + ADMINS & admins, + TARIFFS & tariffs, + USERS & users, + SERVICES & services, + CORPORATIONS & corporations, + TRAFFCOUNTER & traffcounter, + STORE & store, + const SETTINGS & settings); ~PLUGIN_RUNNER(); - PLUGIN_RUNNER & operator=(const PLUGIN_RUNNER & rvalue); - int Start(); int Stop(); int Reload(); int Restart(); - bool IsRunning(); + bool IsRunning() { return m_plugin.IsRunning(); } const std::string & GetStrError() const { return errorStr; } - PLUGIN * GetPlugin(); + PLUGIN & GetPlugin() { return m_plugin; } const std::string & GetFileName() const { return pluginFileName; } - int Load(); - int Unload(); - - uint16_t GetStartPosition() const { return plugin->GetStartPosition(); } - uint16_t GetStopPosition() const { return plugin->GetStopPosition(); } + uint16_t GetStartPosition() const { return m_plugin.GetStartPosition(); } + uint16_t GetStopPosition() const { return m_plugin.GetStopPosition(); } private: - std::string pluginFileName; - std::string pluginSettingFileName; + PLUGIN_RUNNER(const PLUGIN_RUNNER & rvalue); + PLUGIN_RUNNER & operator=(const PLUGIN_RUNNER & rvalue); - PLUGIN * plugin; - bool isPluginLoaded; - std::string errorStr; + PLUGIN & Load(const MODULE_SETTINGS & ms, + ADMINS & admins, + TARIFFS & tariffs, + USERS & users, + SERVICES & services, + CORPORATIONS & corporations, + TRAFFCOUNTER & traffcounter, + STORE & store, + const SETTINGS & settings); + std::string pluginFileName; void * libHandle; - bool isRunning; - ADMINS_IMPL * admins; - TARIFFS_IMPL * tariffs; - USERS_IMPL * users; - SERVICES_IMPL * services; - CORPORATIONS_IMPL * corps; - STORE * store; - TRAFFCOUNTER * traffCnt; - const SETTINGS_IMPL * stgSettings; - MODULE_SETTINGS modSettings; + PLUGIN & m_plugin; + std::string errorStr; }; //----------------------------------------------------------------------------- #endif //PLUGIN_RUNNER_H diff --git a/projects/stargazer/store_loader.h b/projects/stargazer/store_loader.h index fd8b54a3..5245eb57 100644 --- a/projects/stargazer/store_loader.h +++ b/projects/stargazer/store_loader.h @@ -47,7 +47,7 @@ public: bool Load(); bool Unload(); - STORE * GetStore() { return plugin; } + STORE & GetStore() { return *plugin; } const std::string & GetStrError() const { return errorStr; } diff --git a/projects/stargazer/users_impl.cpp b/projects/stargazer/users_impl.cpp index 3dd7638b..97873aeb 100644 --- a/projects/stargazer/users_impl.cpp +++ b/projects/stargazer/users_impl.cpp @@ -51,29 +51,18 @@ extern volatile time_t stgTime; //#define USERS_DEBUG 1 //----------------------------------------------------------------------------- -USERS_IMPL::USERS_IMPL(SETTINGS_IMPL * s, STORE * st, TARIFFS * t, const ADMIN * sa) - : USERS(), - users(), - usersToDelete(), - /*userIPNotifiersBefore(), - userIPNotifiersAfter(),*/ - ipIndex(), - loginIndex(), - settings(s), +USERS_IMPL::USERS_IMPL(SETTINGS_IMPL * s, STORE * st, + TARIFFS * t, SERVICES & svcs, + const ADMIN * sa) + : settings(s), tariffs(t), + m_services(svcs), store(st), sysAdmin(sa), WriteServLog(GetStgLogger()), nonstop(false), isRunning(false), - mutex(), - thread(), - handle(0), - searchDescriptors(), - onAddNotifiers(), - onDelNotifiers(), - onAddNotifiersImpl(), - onDelNotifiersImpl() + handle(0) { pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); @@ -171,7 +160,7 @@ if (store->AddUser(login)) } ////// -USER_IMPL u(settings, store, tariffs, sysAdmin, this); +USER_IMPL u(settings, store, tariffs, sysAdmin, this, m_services); /*struct tm * tms; time_t t = stgTime; @@ -357,7 +346,7 @@ user_iter ui; for (unsigned int i = 0; i < usersList.size(); i++) { - USER_IMPL u(settings, store, tariffs, sysAdmin, this); + USER_IMPL u(settings, store, tariffs, sysAdmin, this, m_services); u.SetLogin(usersList[i]); users.push_front(u); diff --git a/projects/stargazer/users_impl.h b/projects/stargazer/users_impl.h index a8f8d559..0cba7893 100644 --- a/projects/stargazer/users_impl.h +++ b/projects/stargazer/users_impl.h @@ -71,7 +71,9 @@ class USERS_IMPL : private NONCOPYABLE, public USERS { friend class PROPERTY_NOTIFER_IP_AFTER; public: - USERS_IMPL(SETTINGS_IMPL * s, STORE * store, TARIFFS * tariffs, const ADMIN * sysAdmin); + USERS_IMPL(SETTINGS_IMPL * s, STORE * store, + TARIFFS * tariffs, SERVICES & svcs, + const ADMIN * sysAdmin); virtual ~USERS_IMPL(); int FindByName(const std::string & login, USER_PTR * user); @@ -150,6 +152,7 @@ private: SETTINGS_IMPL * settings; TARIFFS * tariffs; + SERVICES & m_services; STORE * store; const ADMIN * sysAdmin; STG_LOGGER & WriteServLog;