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 MODULE_SETTINGS & ms,
36 CORPORATIONS & corporations,
37 TRAFFCOUNTER & traffcounter,
39 const SETTINGS & settings)
40 : pluginFileName(fileName),
42 m_plugin(Load(ms, admins, tariffs, users, services, corporations,
43 traffcounter, store, settings))
46 //-----------------------------------------------------------------------------
47 PLUGIN_RUNNER::~PLUGIN_RUNNER()
49 if (dlclose(libHandle))
51 errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror();
52 printfd(__FILE__, "PLUGIN_RUNNER::Unload() - %s", errorStr.c_str());
55 //-----------------------------------------------------------------------------
56 int PLUGIN_RUNNER::Start()
58 int res = m_plugin.Start();
59 errorStr = m_plugin.GetStrError();
62 //-----------------------------------------------------------------------------
63 int PLUGIN_RUNNER::Stop()
65 int res = m_plugin.Stop();
66 errorStr = m_plugin.GetStrError();
69 //-----------------------------------------------------------------------------
70 int PLUGIN_RUNNER::Reload()
72 int res = m_plugin.Reload();
73 errorStr = m_plugin.GetStrError();
76 //-----------------------------------------------------------------------------
77 PLUGIN & PLUGIN_RUNNER::Load(const MODULE_SETTINGS & ms,
82 CORPORATIONS & corporations,
83 TRAFFCOUNTER & traffcounter,
85 const SETTINGS & settings)
87 if (pluginFileName.empty())
89 errorStr = "Empty plugin file name.";
90 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
91 throw Error(errorStr);
94 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
98 errorStr = "Error loading plugin '" + pluginFileName + "': '" + dlerror() + "'";
99 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
100 throw Error(errorStr);
103 PLUGIN * (*GetPlugin)();
104 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
107 errorStr = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. " + dlerror();
108 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
109 throw Error(errorStr);
111 PLUGIN * plugin = GetPlugin();
115 errorStr = "Failed to create an instance of plugin '" + pluginFileName + "'.";
116 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
117 throw Error(errorStr);
120 plugin->SetSettings(ms);
121 plugin->SetTariffs(&tariffs);
122 plugin->SetAdmins(&admins);
123 plugin->SetUsers(&users);
124 plugin->SetServices(&services);
125 plugin->SetCorporations(&corporations);
126 plugin->SetTraffcounter(&traffcounter);
127 plugin->SetStore(&store);
128 plugin->SetStgSettings(&settings);
130 if (plugin->ParseSettings())
132 errorStr = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError();
133 printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
134 throw Error(errorStr);