./main.cpp \
./pidfile.cpp \
./plugin_runner.cpp \
+ ./plugin_mgr.cpp \
./settings_impl.cpp \
./services_impl.cpp \
./stg_timer.cpp \
* Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
*/
- /*
- $Revision: 1.124 $
- $Date: 2010/10/04 20:19:12 $
- $Author: faust $
- */
-
-#include <unistd.h>
-#include <sys/ipc.h>
-#include <sys/msg.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/stat.h> // S_IRUSR
-#include <fcntl.h> // create
-
-#include <csignal>
-#include <cerrno>
-#include <cstdio>
-#include <cstdlib> // srandom, exit
-#include <fstream>
-#include <vector>
-#include <set>
-#include <list>
-
-#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 <fstream>
+#include <vector>
+#include <set>
+#include <csignal>
+#include <cerrno>
+#include <cstdio>
+#include <cstdlib> // srandom, exit
+
+#include <unistd.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h> // S_IRUSR
+#include <fcntl.h> // create
#ifdef DEBUG
- #define MAIN_DEBUG (1)
#define NO_DAEMON (1)
#endif
{
std::set<pid_t> 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()
{
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<MODULE_SETTINGS> modSettings;
-std::list<PLUGIN_RUNNER> modules;
-
-std::list<PLUGIN_RUNNER>::iterator modIter;
+GetStgLogger().SetLogFileName("/var/log/stargazer.log");
if (getuid())
{
printf("You must be root. Exit.\n");
- exit(1);
+ return 1;
}
if (argc == 2)
WriteServLog.SetLogFileName(settings->GetLogFileName());
WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
- exit(1);
+ return -1;
}
#ifndef NO_DAEMON
{
STG_LOGGER & WriteServLog = GetStgLogger();
WriteServLog("Fork error!");
- exit(1);
+ return -1;
}
STG_LOGGER & WriteServLog = GetStgLogger();
{
STG_LOGGER & WriteServLog = GetStgLogger();
WriteServLog("Start Script Executer error!");
- return 1;
+ return -1;
}
if (ret == 1)
{
{
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());
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<unsigned int>(stgTime));
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;
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);
StopStgTimer();
WriteServLog("StgTimer: Stop successfull.");
-delete corps;
-delete services;
-delete traffCnt;
-delete users;
-delete admins;
-delete tariffs;
delete settings;
WriteServLog("Stg stopped successfully.");
--- /dev/null
+/*
+ * 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 <faust@stargazer.dp.ua>
+ */
+
+#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<MODULE_SETTINGS> & 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());
+ }
+ }
+}
--- /dev/null
+#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 <faust@stargazer.dp.ua>
+ */
+
+#include "stg/module_settings.h"
+
+#include <vector>
+
+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<PLUGIN_RUNNER*> m_modules;
+ STG_LOGGER & m_log;
+};
+
+} // namespace STG
+
+#endif
/*
* Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
+ * Author : Maxim Mamontov <faust@stargazer.dp.ua>
*/
-/*
- $Revision: 1.17 $
- $Date: 2010/09/13 05:52:46 $
- $Author: faust $
- */
+#include "plugin_runner.h"
+
+#include "stg/common.h"
#include <dlfcn.h>
#include <unistd.h>
-#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);
{
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;
}
-//-----------------------------------------------------------------------------
/*
* Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
- */
-
-/*
- $Revision: 1.13 $
- $Date: 2010/03/04 12:22:41 $
- $Author: faust $
+ * Author : Maxim Mamontov <faust@stargazer.dp.ua>
*/
#ifndef PLUGIN_RUNNER_H
#define PLUGIN_RUNNER_H
-#include <string>
-
-#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 <string>
+#include <stdexcept>
+
+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
bool Load();
bool Unload();
- STORE * GetStore() { return plugin; }
+ STORE & GetStore() { return *plugin; }
const std::string & GetStrError() const { return errorStr; }
//#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);
}
//////
-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;
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);
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);
SETTINGS_IMPL * settings;
TARIFFS * tariffs;
+ SERVICES & m_services;
STORE * store;
const ADMIN * sysAdmin;
STG_LOGGER & WriteServLog;