]> git.stg.codes - stg.git/blob - projects/stargazer/plugin_runner.cpp
Better error reporting from plugin runner.
[stg.git] / projects / stargazer / plugin_runner.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22  $Revision: 1.17 $
23  $Date: 2010/09/13 05:52:46 $
24  $Author: faust $
25  */
26
27 #include <dlfcn.h>
28 #include <unistd.h>
29
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"
39
40 //-----------------------------------------------------------------------------
41 PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName,
42                              const MODULE_SETTINGS & ms,
43                              ADMINS_IMPL * a,
44                              TARIFFS_IMPL * t,
45                              USERS_IMPL * u,
46                              SERVICES_IMPL * svc,
47                              CORPORATIONS_IMPL * crp,
48                              TRAFFCOUNTER * tc,
49                              STORE * st,
50                              const SETTINGS_IMPL * s)
51     : pluginFileName(pFileName),
52       pluginSettingFileName(),
53       plugin(NULL),
54       isPluginLoaded(false),
55       errorStr(),
56       libHandle(NULL),
57       isRunning(false),
58       admins(a),
59       tariffs(t),
60       users(u),
61       services(svc),
62       corps(crp),
63       store(st),
64       traffCnt(tc),
65       stgSettings(s),
66       modSettings(ms)
67 {
68 }
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),
80       users(rvalue.users),
81       services(rvalue.services),
82       corps(rvalue.corps),
83       store(rvalue.store),
84       traffCnt(rvalue.traffCnt),
85       stgSettings(rvalue.stgSettings),
86       modSettings(rvalue.modSettings)
87 {
88 }
89 //-----------------------------------------------------------------------------
90 PLUGIN_RUNNER & PLUGIN_RUNNER::operator=(const PLUGIN_RUNNER & rvalue)
91 {
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;
108
109 return *this;
110 }
111 //-----------------------------------------------------------------------------
112 PLUGIN_RUNNER::~PLUGIN_RUNNER()
113 {
114 if (isPluginLoaded)
115     {
116     Unload();
117     }
118
119 isPluginLoaded = false;
120 }
121 //-----------------------------------------------------------------------------
122 PLUGIN * PLUGIN_RUNNER::GetPlugin()
123 {
124 if (!isPluginLoaded)
125     {
126     errorStr = "Plugin '" + pluginFileName + "' is not loaded yet!";
127     printfd(__FILE__, "PLUGIN_RUNNER::GetPlugin() - %s\n", errorStr.c_str());
128     return NULL;
129     }
130
131 return plugin;
132 }
133 //-----------------------------------------------------------------------------
134 int PLUGIN_RUNNER::Start()
135 {
136 if (!isPluginLoaded)
137     if (Load())
138         return -1;
139
140 if (!plugin)
141     {
142     errorStr = "Plugin '" + pluginFileName + "' was not created!";
143     printfd(__FILE__, "PLUGIN_RUNNER::Start() - %s\n", errorStr.c_str());
144     return -1;
145     }
146
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);
155
156 if (plugin->Start())
157     {
158     errorStr = plugin->GetStrError();
159     return -1;
160     }
161
162 return 0;
163 }
164 //-----------------------------------------------------------------------------
165 int PLUGIN_RUNNER::Stop()
166 {
167 if (!isPluginLoaded)
168     {
169     errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
170     printfd(__FILE__, "PLUGIN_RUNNER::Stop() - %s\n", errorStr.c_str());
171     return -1;
172     }
173
174 if (!plugin)
175     {
176     errorStr = "Plugin '" + pluginFileName + "' was not created!";
177     printfd(__FILE__, "PLUGIN_RUNNER::Stop() - %s\n", errorStr.c_str());
178     return -1;
179     }
180
181 return plugin->Stop();
182 }
183 //-----------------------------------------------------------------------------
184 int PLUGIN_RUNNER::Reload()
185 {
186 if (!isPluginLoaded)
187     {
188     errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
189     printfd(__FILE__, "PLUGIN_RUNNER::Reload() - %s\n", errorStr.c_str());
190     return -1;
191     }
192
193 if (!plugin)
194     {
195     errorStr = "Plugin '" + pluginFileName + "' was not created!";
196     printfd(__FILE__, "PLUGIN_RUNNER::Reload() - %s\n", errorStr.c_str());
197     return -1;
198     }
199
200 int res = plugin->Reload();
201 errorStr = plugin->GetStrError();
202 return res;
203 }
204 //-----------------------------------------------------------------------------
205 bool PLUGIN_RUNNER::IsRunning()
206 {
207 if (!isPluginLoaded)
208     {
209     errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
210     printfd(__FILE__, "PLUGIN_RUNNER::IsRunning() - %s\n", errorStr.c_str());
211     return false;
212     }
213
214 if (!plugin)
215     {
216     errorStr = "Plugin '" + pluginFileName + "' was not created!";
217     printfd(__FILE__, "PLUGIN_RUNNER::IsRunning() - %s\n", errorStr.c_str());
218     return false;
219     }
220
221 return plugin->IsRunning();
222 }
223 //-----------------------------------------------------------------------------
224 int PLUGIN_RUNNER::Load()
225 {
226 if (isPluginLoaded)
227     {
228     errorStr = "Plugin '" + pluginFileName + "' was already loaded!";
229     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
230     return -1;
231     }
232
233 if (pluginFileName.empty())
234     {
235     errorStr = "Empty plugin file name!";
236     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
237     return -1;
238     }
239
240 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
241
242 if (!libHandle)
243     {
244     errorStr = "Error loading plugin '" + pluginFileName + "': '" + dlerror() + "'";
245     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
246     return -1;
247     }
248
249 isPluginLoaded = true;
250
251 PLUGIN * (*GetPlugin)();
252 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
253 if (!GetPlugin)
254     {
255     errorStr = "Plugin '" + pluginFileName + "' does not have GetPlugin() function. " + dlerror();
256     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
257     return -1;
258     }
259 plugin = GetPlugin();
260
261 if (!plugin)
262     {
263     errorStr = "Failed to create an instance of plugin '" + pluginFileName + "'.";
264     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
265     return -1;
266     }
267
268 plugin->SetSettings(modSettings);
269 if (plugin->ParseSettings())
270     {
271     errorStr = "Plugin '" + pluginFileName + "' is unable to parse settings. " + plugin->GetStrError();
272     printfd(__FILE__, "PLUGIN_RUNNER::Load() - %s\n", errorStr.c_str());
273     return -1;
274     }
275
276 return 0;
277 }
278 //-----------------------------------------------------------------------------
279 int PLUGIN_RUNNER::Unload()
280 {
281 if (isPluginLoaded)
282     {
283     if (dlclose(libHandle))
284         {
285         errorStr = "Failed to unload plugin '" + pluginFileName + "': " + dlerror();
286         printfd(__FILE__, "PLUGIN_RUNNER::Unload() - %s", errorStr.c_str());
287         return -1;
288         }
289     plugin = NULL;
290     isPluginLoaded = false;
291     }
292 return 0;
293 }
294 //-----------------------------------------------------------------------------