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());
185 //-----------------------------------------------------------------------------
186 int PLUGIN_RUNNER::Reload()
190 errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
191 printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str());
197 errorStr = "Plugin '" + pluginFileName + "' was not created!";
198 printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str());
202 int res = plugin->Reload();
203 errorStr = plugin->GetStrError();
206 //-----------------------------------------------------------------------------
207 bool PLUGIN_RUNNER::IsRunning()
211 errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
212 printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
218 errorStr = "Plugin '" + pluginFileName + "' was not created!";
219 printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
223 return plugin->IsRunning();
225 //-----------------------------------------------------------------------------
226 int PLUGIN_RUNNER::Load()
230 errorStr = "Plugin '" + pluginFileName + "' was already loaded!";
231 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
235 if (pluginFileName.empty())
237 errorStr = "Empty plugin file name!";
238 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
242 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
246 errorStr = "Error loading plugin '"
247 + pluginFileName + "': '" + dlerror() + "'";
248 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
252 isPluginLoaded = true;
254 PLUGIN * (*GetPlugin)();
255 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
258 errorStr = std::string("GetPlugin() not found. ") + dlerror();
259 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
262 plugin = GetPlugin();
266 errorStr = "Plugin was not created!";
267 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
271 plugin->SetSettings(modSettings);
272 if (plugin->ParseSettings())
274 errorStr = plugin->GetStrError();
275 printfd(__FILE__, "PLUGIN_LOADER::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str());
281 //-----------------------------------------------------------------------------
282 int PLUGIN_RUNNER::Unload()
286 if (dlclose(libHandle))
288 errorStr = "Failed to unload plugin '";
289 errorStr += pluginFileName + "': ";
290 errorStr += dlerror();
291 printfd(__FILE__, "PLUGIN_LOADER::Unload() - %s", errorStr.c_str());
295 isPluginLoaded = false;
299 //-----------------------------------------------------------------------------