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>
23 $Date: 2010/09/13 05:52:46 $
30 #include "stg/common.h"
31 #include "stg/traffcounter.h"
32 #include "plugin_runner.h"
33 #include "settings_impl.h"
34 #include "admins_impl.h"
35 #include "tariffs_impl.h"
36 #include "users_impl.h"
37 #include "services_impl.h"
38 #include "corps_impl.h"
40 //-----------------------------------------------------------------------------
41 PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName,
42 const MODULE_SETTINGS & ms,
47 CORPORATIONS_IMPL * crp,
50 const SETTINGS_IMPL * s)
51 : pluginFileName(pFileName),
52 pluginSettingFileName(),
54 isPluginLoaded(false),
69 //-----------------------------------------------------------------------------
70 PLUGIN_RUNNER::PLUGIN_RUNNER(const PLUGIN_RUNNER & rvalue)
71 : pluginFileName(rvalue.pluginFileName),
72 pluginSettingFileName(rvalue.pluginSettingFileName),
73 plugin(rvalue.plugin),
74 isPluginLoaded(rvalue.isPluginLoaded),
75 errorStr(rvalue.errorStr),
76 libHandle(rvalue.libHandle),
77 isRunning(rvalue.isRunning),
78 admins(rvalue.admins),
79 tariffs(rvalue.tariffs),
81 services(rvalue.services),
84 traffCnt(rvalue.traffCnt),
85 stgSettings(rvalue.stgSettings),
86 modSettings(rvalue.modSettings)
89 //-----------------------------------------------------------------------------
90 PLUGIN_RUNNER & PLUGIN_RUNNER::operator=(const PLUGIN_RUNNER & rvalue)
92 pluginFileName = rvalue.pluginFileName;
93 pluginSettingFileName = rvalue.pluginSettingFileName;
94 plugin = rvalue.plugin;
95 isPluginLoaded = rvalue.isPluginLoaded;
96 errorStr = rvalue.errorStr;
97 libHandle = rvalue.libHandle;
98 isRunning = rvalue.isRunning;
99 admins = rvalue.admins;
100 tariffs = rvalue.tariffs;
101 users = rvalue.users;
102 services = rvalue.services;
103 corps = rvalue.corps;
104 store = rvalue.store;
105 traffCnt = rvalue.traffCnt;
106 stgSettings = rvalue.stgSettings;
107 modSettings = rvalue.modSettings;
111 //-----------------------------------------------------------------------------
112 PLUGIN_RUNNER::~PLUGIN_RUNNER()
119 isPluginLoaded = false;
121 //-----------------------------------------------------------------------------
122 PLUGIN * PLUGIN_RUNNER::GetPlugin()
126 errorStr = "Plugin '" + pluginFileName + "' is not loaded yet!";
127 printfd(__FILE__, "PLUGIN_LOADER::GetPlugin() - %s\n", errorStr.c_str());
133 //-----------------------------------------------------------------------------
134 int PLUGIN_RUNNER::Start()
142 errorStr = "Plugin '" + pluginFileName + "' was not created!";
143 printfd(__FILE__, "PLUGIN_LOADER::Start() - %s\n", errorStr.c_str());
147 plugin->SetTariffs(tariffs);
148 plugin->SetAdmins(admins);
149 plugin->SetUsers(users);
150 plugin->SetServices(services);
151 plugin->SetCorporations(corps);
152 plugin->SetTraffcounter(traffCnt);
153 plugin->SetStore(store);
154 plugin->SetStgSettings(stgSettings);
158 errorStr = plugin->GetStrError();
164 //-----------------------------------------------------------------------------
165 int PLUGIN_RUNNER::Stop()
169 errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
170 printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str());
176 errorStr = "Plugin '" + pluginFileName + "' was not created!";
177 printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str());
181 return plugin->Stop();
183 //-----------------------------------------------------------------------------
184 int PLUGIN_RUNNER::Reload()
188 errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
189 printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str());
195 errorStr = "Plugin '" + pluginFileName + "' was not created!";
196 printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str());
200 int res = plugin->Reload();
201 errorStr = plugin->GetStrError();
204 //-----------------------------------------------------------------------------
205 bool PLUGIN_RUNNER::IsRunning()
209 errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
210 printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
216 errorStr = "Plugin '" + pluginFileName + "' was not created!";
217 printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
221 return plugin->IsRunning();
223 //-----------------------------------------------------------------------------
224 int PLUGIN_RUNNER::Load()
228 errorStr = "Plugin '" + pluginFileName + "' was already loaded!";
229 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
233 if (pluginFileName.empty())
235 errorStr = "Empty plugin file name!";
236 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
240 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
244 errorStr = "Error loading plugin '"
245 + pluginFileName + "': '" + dlerror() + "'";
246 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
250 isPluginLoaded = true;
252 PLUGIN * (*GetPlugin)();
253 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
256 errorStr = std::string("GetPlugin() not found. ") + dlerror();
257 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
260 plugin = GetPlugin();
264 errorStr = "Plugin was not created!";
265 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
269 plugin->SetSettings(modSettings);
270 if (plugin->ParseSettings())
272 errorStr = plugin->GetStrError();
273 printfd(__FILE__, "PLUGIN_LOADER::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str());
279 //-----------------------------------------------------------------------------
280 int PLUGIN_RUNNER::Unload()
284 if (dlclose(libHandle))
286 errorStr = "Failed to unload plugin '";
287 errorStr += pluginFileName + "': ";
288 errorStr += dlerror();
289 printfd(__FILE__, "PLUGIN_LOADER::Unload() - %s", errorStr.c_str());
293 isPluginLoaded = false;
297 //-----------------------------------------------------------------------------