/*
* 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"
+using STG::PluginRunner;
+using STG::Plugin;
//-----------------------------------------------------------------------------
-PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName,
- 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(),
+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),
- services(svc),
- corps(crp),
- store(st),
- traffCnt(tc),
- stgSettings(s),
- modSettings(ms)
+ m_plugin(load(ms, admins, tariffs, users, services, corporations,
+ traffcounter, store, settings))
{
}
//-----------------------------------------------------------------------------
-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)
+PluginRunner::~PluginRunner()
{
-}
-//-----------------------------------------------------------------------------
-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)
- {
- Unload();
- }
-
-isPluginLoaded = false;
-}
-//-----------------------------------------------------------------------------
-PLUGIN * PLUGIN_RUNNER::GetPlugin()
-{
-if (!isPluginLoaded)
+ delete &m_plugin;
+ if (dlclose(libHandle))
{
- errorStr = "Plugin '" + pluginFileName + "' is not loaded yet!";
- printfd(__FILE__, "PLUGIN_LOADER::GetPlugin() - %s\n", errorStr.c_str());
- return NULL;
+ errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror();
+ printfd(__FILE__, "PluginRunner::Unload() - %s", errorStr.c_str());
}
-
-return plugin;
}
//-----------------------------------------------------------------------------
-int PLUGIN_RUNNER::Start()
+int PluginRunner::Start()
{
-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);
-
-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()
+int PluginRunner::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();
-
-return 0;
+ int res = m_plugin.Stop();
+ errorStr = m_plugin.GetStrError();
+ return res;
}
//-----------------------------------------------------------------------------
-int PLUGIN_RUNNER::Reload()
+int PluginRunner::Reload(const ModuleSettings& ms)
{
-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;
+ int res = m_plugin.Reload(ms);
+ errorStr = m_plugin.GetStrError();
+ return res;
}
//-----------------------------------------------------------------------------
-bool PLUGIN_RUNNER::IsRunning()
+Plugin & PluginRunner::load(const ModuleSettings& ms,
+ Admins& admins,
+ Tariffs& tariffs,
+ Users& users,
+ Services& services,
+ Corporations& corporations,
+ TraffCounter& traffcounter,
+ Store& store,
+ const Settings& settings)
{
-if (!isPluginLoaded)
+ if (pluginFileName.empty())
{
- errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
- printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
- return false;
+ const std::string msg = "Empty plugin file name.";
+ printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str());
+ throw Error(msg);
}
-if (!plugin)
+ if (access(pluginFileName.c_str(), R_OK))
{
- errorStr = "Plugin '" + pluginFileName + "' was not created!";
- printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
- return false;
+ const std::string msg = "Plugin file '" + pluginFileName + "' is missing or inaccessible.";
+ printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str());
+ throw Error(msg);
}
-return plugin->IsRunning();
-}
-//-----------------------------------------------------------------------------
-int PLUGIN_RUNNER::Load()
-{
-if (isPluginLoaded)
- {
- errorStr = "Plugin '" + pluginFileName + "' was already loaded!";
- printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
- return -1;
- }
+ libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
-if (pluginFileName.empty())
+ if (!libHandle)
{
- errorStr = "Empty plugin file name!";
- printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
- 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);
}
-libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
-
-if (!libHandle)
+ using Getter = Plugin* (*)();
+ auto GetPlugin = reinterpret_cast<Getter>(dlsym(libHandle, "GetPlugin"));
+ if (!GetPlugin)
{
- errorStr = "Error loading plugin '"
- + pluginFileName + "': '" + dlerror() + "'";
- printfd(__FILE__, "PLUGIN_LOADER::Load() - %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);
}
-isPluginLoaded = true;
+ Plugin* plugin = GetPlugin();
-PLUGIN * (*GetPlugin)();
-GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
-if (!GetPlugin)
+ if (!plugin)
{
- errorStr = std::string("GetPlugin() not found. ") + dlerror();
- printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
- 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);
}
-plugin = GetPlugin();
-if (!plugin)
- {
- errorStr = "Plugin was not created!";
- printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
- return -1;
- }
+ 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);
-plugin->SetSettings(modSettings);
-if (plugin->ParseSettings())
+ if (plugin->ParseSettings())
{
- errorStr = plugin->GetStrError();
- printfd(__FILE__, "PLUGIN_LOADER::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str());
- return -1;
+ 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;
+ return *plugin;
}
-//-----------------------------------------------------------------------------
-int PLUGIN_RUNNER::Unload()
-{
-if (isPluginLoaded)
- {
- if (dlclose(libHandle))
- {
- errorStr = "Failed to unload plugin '";
- errorStr += pluginFileName + "': ";
- errorStr += dlerror();
- printfd(__FILE__, "PLUGIN_LOADER::Unload() - %s", errorStr.c_str());
- return -1;
- }
- plugin = NULL;
- isPluginLoaded = false;
- }
-return 0;
-}
-//-----------------------------------------------------------------------------