X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/55a204e46261850f1a26ebafc45fb3ba6f0604c6..f4333737c7e347efe9258ccf85c1237e03be619c:/projects/stargazer/plugin_runner.cpp?ds=sidebyside diff --git a/projects/stargazer/plugin_runner.cpp b/projects/stargazer/plugin_runner.cpp index bcf68bee..8938950f 100644 --- a/projects/stargazer/plugin_runner.cpp +++ b/projects/stargazer/plugin_runner.cpp @@ -26,113 +26,130 @@ #include #include +using STG::PluginRunner; +using STG::Plugin; + //----------------------------------------------------------------------------- -PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & fileName, - const MODULE_SETTINGS & ms, - ADMINS & admins, - TARIFFS & tariffs, - USERS & users, - SERVICES & services, - CORPORATIONS & corporations, - TRAFFCOUNTER & traffcounter, - STORE & store, - const SETTINGS & settings) +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), - m_plugin(Load(ms, admins, tariffs, users, services, corporations, + m_plugin(load(ms, admins, tariffs, users, services, corporations, traffcounter, store, settings)) { } //----------------------------------------------------------------------------- -PLUGIN_RUNNER::~PLUGIN_RUNNER() +PluginRunner::~PluginRunner() { -if (dlclose(libHandle)) + delete &m_plugin; + if (dlclose(libHandle)) { - errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror(); - printfd(__FILE__, "PLUGIN_RUNNER::Unload() - %s", errorStr.c_str()); + errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror(); + printfd(__FILE__, "PluginRunner::Unload() - %s", errorStr.c_str()); } } //----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Start() +int PluginRunner::Start() { -int res = m_plugin.Start(); -errorStr = m_plugin.GetStrError(); -return res; + int res = m_plugin.Start(); + errorStr = m_plugin.GetStrError(); + return res; } //----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Stop() +int PluginRunner::Stop() { -int res = m_plugin.Stop(); -errorStr = m_plugin.GetStrError(); -return res; + int res = m_plugin.Stop(); + errorStr = m_plugin.GetStrError(); + return res; } //----------------------------------------------------------------------------- -int PLUGIN_RUNNER::Reload() +int PluginRunner::Reload(const ModuleSettings& ms) { -int res = m_plugin.Reload(); -errorStr = m_plugin.GetStrError(); -return res; + int res = m_plugin.Reload(ms); + errorStr = m_plugin.GetStrError(); + return res; } //----------------------------------------------------------------------------- -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) +Plugin & PluginRunner::load(const ModuleSettings& ms, + Admins& admins, + Tariffs& tariffs, + Users& users, + Services& services, + Corporations& corporations, + TraffCounter& traffcounter, + Store& store, + const Settings& settings) { -if (pluginFileName.empty()) + if (pluginFileName.empty()) { - errorStr = "Empty plugin file name."; - printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - throw Error(errorStr); + const std::string msg = "Empty plugin file name."; + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); } -libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW); + if (access(pluginFileName.c_str(), R_OK)) + { + const std::string msg = "Plugin file '" + pluginFileName + "' is missing or inaccessible."; + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); + } -if (!libHandle) + libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW); + + if (!libHandle) { - errorStr = "Error loading plugin '" + pluginFileName + "': '" + dlerror() + "'"; - printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - throw Error(errorStr); + 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); } -PLUGIN * (*GetPlugin)(); -GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin"); -if (!GetPlugin) + using Getter = Plugin* (*)(); + auto GetPlugin = reinterpret_cast(dlsym(libHandle, "GetPlugin")); + if (!GetPlugin) { - errorStr = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. " + dlerror(); - printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - throw Error(errorStr); + const std::string msg = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. "; + printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str()); + throw Error(msg); } -PLUGIN * plugin = GetPlugin(); -if (!plugin) + Plugin* plugin = GetPlugin(); + + if (!plugin) { - errorStr = "Failed to create an instance of plugin '" + pluginFileName + "'."; - printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - throw Error(errorStr); + 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->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(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()) + if (plugin->ParseSettings()) { - errorStr = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError(); - printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str()); - throw Error(errorStr); + 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 *plugin; + return *plugin; }