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 //-----------------------------------------------------------------------------
30 PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & fileName,
31 const std::string & name,
32 const MODULE_SETTINGS & ms,
37 CORPORATIONS & corporations,
38 TRAFFCOUNTER & traffcounter,
40 const SETTINGS & settings)
41 : pluginFileName(fileName),
44 m_plugin(Load(ms, admins, tariffs, users, services, corporations,
45 traffcounter, store, settings))
48 //-----------------------------------------------------------------------------
49 PLUGIN_RUNNER::~PLUGIN_RUNNER()
51 if (dlclose(libHandle))
53 errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror();
54 printfd(__FILE__, "PLUGIN_RUNNER::Unload() - %s", errorStr.c_str());
57 //-----------------------------------------------------------------------------
58 int PLUGIN_RUNNER::Start()
60 int res = m_plugin.Start();
61 errorStr = m_plugin.GetStrError();
64 //-----------------------------------------------------------------------------
65 int PLUGIN_RUNNER::Stop()
67 int res = m_plugin.Stop();
68 errorStr = m_plugin.GetStrError();
71 //-----------------------------------------------------------------------------
72 int PLUGIN_RUNNER::Reload()
74 int res = m_plugin.Reload();
75 errorStr = m_plugin.GetStrError();
78 //-----------------------------------------------------------------------------
79 PLUGIN & PLUGIN_RUNNER::Load(const MODULE_SETTINGS & ms,
84 CORPORATIONS & corporations,
85 TRAFFCOUNTER & traffcounter,
87 const SETTINGS & settings)
89 if (pluginFileName.empty())
91 const std::string msg = "Empty plugin file name.";
92 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
96 if (access(pluginFileName.c_str(), R_OK))
98 const std::string msg = "Plugin file '" + pluginFileName + "' is missing or inaccessible.";
99 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
103 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
107 std::string msg = "Error loading plugin '" + pluginFileName + "'";
108 const char* error = dlerror();
110 msg = msg + ": '" + error + "'";
111 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
115 PLUGIN * (*GetPlugin)();
116 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
119 const std::string msg = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. ";
120 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
123 PLUGIN * plugin = GetPlugin();
127 const std::string msg = "Failed to create an instance of plugin '" + pluginFileName + "'.";
128 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());
132 plugin->SetSettings(ms);
133 plugin->SetTariffs(&tariffs);
134 plugin->SetAdmins(&admins);
135 plugin->SetUsers(&users);
136 plugin->SetServices(&services);
137 plugin->SetCorporations(&corporations);
138 plugin->SetTraffcounter(&traffcounter);
139 plugin->SetStore(&store);
140 plugin->SetStgSettings(&settings);
142 if (plugin->ParseSettings())
144 const std::string msg = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError();
145 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", msg.c_str());