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"
38 //-----------------------------------------------------------------------------
39 PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName,
40 const MODULE_SETTINGS & ms,
46 const SETTINGS_IMPL * s)
47 : pluginFileName(pFileName),
48 pluginSettingFileName(),
50 isPluginLoaded(false),
63 //-----------------------------------------------------------------------------
64 PLUGIN_RUNNER::PLUGIN_RUNNER(const PLUGIN_RUNNER & rvalue)
65 : pluginFileName(rvalue.pluginFileName),
66 pluginSettingFileName(rvalue.pluginSettingFileName),
67 plugin(rvalue.plugin),
68 isPluginLoaded(rvalue.isPluginLoaded),
69 errorStr(rvalue.errorStr),
70 libHandle(rvalue.libHandle),
71 isRunning(rvalue.isRunning),
72 admins(rvalue.admins),
73 tariffs(rvalue.tariffs),
76 traffCnt(rvalue.traffCnt),
77 stgSettings(rvalue.stgSettings),
78 modSettings(rvalue.modSettings)
81 //-----------------------------------------------------------------------------
82 PLUGIN_RUNNER & PLUGIN_RUNNER::operator=(const PLUGIN_RUNNER & rvalue)
84 pluginFileName = rvalue.pluginFileName;
85 pluginSettingFileName = rvalue.pluginSettingFileName;
86 plugin = rvalue.plugin;
87 isPluginLoaded = rvalue.isPluginLoaded;
88 errorStr = rvalue.errorStr;
89 libHandle = rvalue.libHandle;
90 isRunning = rvalue.isRunning;
91 admins = rvalue.admins;
92 tariffs = rvalue.tariffs;
95 traffCnt = rvalue.traffCnt;
96 stgSettings = rvalue.stgSettings;
97 modSettings = rvalue.modSettings;
101 //-----------------------------------------------------------------------------
102 PLUGIN_RUNNER::~PLUGIN_RUNNER()
109 isPluginLoaded = false;
111 //-----------------------------------------------------------------------------
112 PLUGIN * PLUGIN_RUNNER::GetPlugin()
116 errorStr = "Plugin '" + pluginFileName + "' is not loaded yet!";
117 printfd(__FILE__, "PLUGIN_LOADER::GetPlugin() - %s\n", errorStr.c_str());
123 //-----------------------------------------------------------------------------
124 int PLUGIN_RUNNER::Start()
132 errorStr = "Plugin '" + pluginFileName + "' was not created!";
133 printfd(__FILE__, "PLUGIN_LOADER::Start() - %s\n", errorStr.c_str());
137 plugin->SetTariffs(tariffs);
138 plugin->SetAdmins(admins);
139 plugin->SetUsers(users);
140 plugin->SetTraffcounter(traffCnt);
141 plugin->SetStore(store);
142 plugin->SetStgSettings(stgSettings);
146 errorStr = plugin->GetStrError();
152 //-----------------------------------------------------------------------------
153 int PLUGIN_RUNNER::Stop()
157 errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
158 printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str());
164 errorStr = "Plugin '" + pluginFileName + "' was not created!";
165 printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str());
173 //-----------------------------------------------------------------------------
174 int PLUGIN_RUNNER::Reload()
178 errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
179 printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str());
185 errorStr = "Plugin '" + pluginFileName + "' was not created!";
186 printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str());
190 int res = plugin->Reload();
191 errorStr = plugin->GetStrError();
194 //-----------------------------------------------------------------------------
195 bool PLUGIN_RUNNER::IsRunning()
199 errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
200 printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
206 errorStr = "Plugin '" + pluginFileName + "' was not created!";
207 printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
211 return plugin->IsRunning();
213 //-----------------------------------------------------------------------------
214 int PLUGIN_RUNNER::Load()
218 errorStr = "Plugin '" + pluginFileName + "' was already loaded!";
219 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
223 if (pluginFileName.empty())
225 errorStr = "Empty plugin file name!";
226 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
230 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
234 errorStr = "Error loading plugin '"
235 + pluginFileName + "': '" + dlerror() + "'";
236 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
240 isPluginLoaded = true;
242 PLUGIN * (*GetPlugin)();
243 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
246 errorStr = std::string("GetPlugin() not found. ") + dlerror();
247 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
250 plugin = GetPlugin();
254 errorStr = "Plugin was not created!";
255 printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
259 plugin->SetSettings(modSettings);
260 if (plugin->ParseSettings())
262 errorStr = plugin->GetStrError();
263 printfd(__FILE__, "PLUGIN_LOADER::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str());
269 //-----------------------------------------------------------------------------
270 int PLUGIN_RUNNER::Unload()
274 if (dlclose(libHandle))
276 errorStr = "Failed to unload plugin '";
277 errorStr += pluginFileName + "': ";
278 errorStr += dlerror();
279 printfd(__FILE__, "PLUGIN_LOADER::Unload() - %s", errorStr.c_str());
283 isPluginLoaded = false;
287 //-----------------------------------------------------------------------------