2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "plugin_runner.h"
24 #include "stg/common.h"
29 using STG::PluginRunner;
32 //-----------------------------------------------------------------------------
33 PluginRunner::PluginRunner(const std::string& fileName,
34 const std::string& name,
35 const ModuleSettings& ms,
40 Corporations& corporations,
41 TraffCounter& traffcounter,
43 const Settings& settings)
44 : pluginFileName(fileName),
47 m_plugin(load(ms, admins, tariffs, users, services, corporations,
48 traffcounter, store, settings))
51 //-----------------------------------------------------------------------------
52 PluginRunner::~PluginRunner()
55 if (dlclose(libHandle))
57 errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror();
58 printfd(__FILE__, "PluginRunner::Unload() - %s", errorStr.c_str());
61 //-----------------------------------------------------------------------------
62 int PluginRunner::Start()
64 int res = m_plugin.Start();
65 errorStr = m_plugin.GetStrError();
68 //-----------------------------------------------------------------------------
69 int PluginRunner::Stop()
71 int res = m_plugin.Stop();
72 errorStr = m_plugin.GetStrError();
75 //-----------------------------------------------------------------------------
76 int PluginRunner::Reload(const ModuleSettings& ms)
78 int res = m_plugin.Reload(ms);
79 errorStr = m_plugin.GetStrError();
82 //-----------------------------------------------------------------------------
83 Plugin & PluginRunner::load(const ModuleSettings& ms,
88 Corporations& corporations,
89 TraffCounter& traffcounter,
91 const Settings& settings)
93 if (pluginFileName.empty())
95 const std::string msg = "Empty plugin file name.";
96 printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str());
100 if (access(pluginFileName.c_str(), R_OK))
102 const std::string msg = "Plugin file '" + pluginFileName + "' is missing or inaccessible.";
103 printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str());
107 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
111 std::string msg = "Error loading plugin '" + pluginFileName + "'";
112 const char* error = dlerror();
114 msg = msg + ": '" + error + "'";
115 printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str());
119 using Getter = Plugin* (*)();
120 auto GetPlugin = reinterpret_cast<Getter>(dlsym(libHandle, "GetPlugin"));
123 const std::string msg = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. ";
124 printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str());
128 Plugin* plugin = GetPlugin();
132 const std::string msg = "Failed to create an instance of plugin '" + pluginFileName + "'.";
133 printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str());
137 plugin->SetSettings(ms);
138 plugin->SetTariffs(&tariffs);
139 plugin->SetAdmins(&admins);
140 plugin->SetUsers(&users);
141 plugin->SetServices(&services);
142 plugin->SetCorporations(&corporations);
143 plugin->SetTraffcounter(&traffcounter);
144 plugin->SetStore(&store);
145 plugin->SetStgSettings(&settings);
147 if (plugin->ParseSettings())
149 const std::string msg = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError();
150 printfd(__FILE__, "PluginRunner::load() - %s\n", msg.c_str());