]> git.stg.codes - stg.git/blob - projects/stargazer/plugin_runner.cpp
Fix compilation issues after splitting SETTINGS into interface and
[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 "plugin_runner.h"
31 #include "common.h"
32 #include "settings_impl.h"
33 #include "admins_impl.h"
34 #include "tariffs_impl.h"
35 #include "users_impl.h"
36 #include "traffcounter.h"
37
38 //-----------------------------------------------------------------------------
39 PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName,
40                              const MODULE_SETTINGS & ms,
41                              ADMINS_IMPL * a,
42                              TARIFFS_IMPL * t,
43                              USERS_IMPL * u,
44                              TRAFFCOUNTER * tc,
45                              STORE * st,
46                              const SETTINGS_IMPL * s)
47     : pluginFileName(pFileName),
48       pluginSettingFileName(),
49       plugin(NULL),
50       isPluginLoaded(false),
51       errorStr(),
52       libHandle(NULL),
53       isRunning(false),
54       admins(a),
55       tariffs(t),
56       users(u),
57       store(st),
58       traffCnt(tc),
59       stgSettings(s),
60       modSettings(ms)
61 {
62 }
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),
74       users(rvalue.users),
75       store(rvalue.store),
76       traffCnt(rvalue.traffCnt),
77       stgSettings(rvalue.stgSettings),
78       modSettings(rvalue.modSettings)
79 {
80 }
81 //-----------------------------------------------------------------------------
82 PLUGIN_RUNNER & PLUGIN_RUNNER::operator=(const PLUGIN_RUNNER & rvalue)
83 {
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;
93 users = rvalue.users;
94 store = rvalue.store;
95 traffCnt = rvalue.traffCnt;
96 stgSettings = rvalue.stgSettings;
97 modSettings = rvalue.modSettings;
98
99 return *this;
100 }
101 //-----------------------------------------------------------------------------
102 PLUGIN_RUNNER::~PLUGIN_RUNNER()
103 {
104 if (isPluginLoaded)
105     {
106     Unload();
107     }
108
109 isPluginLoaded = false;
110 }
111 //-----------------------------------------------------------------------------
112 PLUGIN * PLUGIN_RUNNER::GetPlugin()
113 {
114 if (!isPluginLoaded)
115     {
116     errorStr = "Plugin '" + pluginFileName + "' is not loaded yet!";
117     printfd(__FILE__, "PLUGIN_LOADER::GetPlugin() - %s\n", errorStr.c_str());
118     return NULL;
119     }
120
121 return plugin;
122 }
123 //-----------------------------------------------------------------------------
124 int PLUGIN_RUNNER::Start()
125 {
126 if (!isPluginLoaded)
127     if (Load())
128         return -1;
129
130 if (!plugin)
131     {
132     errorStr = "Plugin '" + pluginFileName + "' was not created!";
133     printfd(__FILE__, "PLUGIN_LOADER::Start() - %s\n", errorStr.c_str());
134     return -1;
135     }
136
137 plugin->SetTariffs(tariffs);
138 plugin->SetAdmins(admins);
139 plugin->SetUsers(users);
140 plugin->SetTraffcounter(traffCnt);
141 plugin->SetStore(store);
142 plugin->SetStgSettings(stgSettings);
143
144 if (plugin->Start())
145     {
146     errorStr = plugin->GetStrError();
147     return -1;
148     }
149
150 return 0;
151 }
152 //-----------------------------------------------------------------------------
153 int PLUGIN_RUNNER::Stop()
154 {
155 if (!isPluginLoaded)
156     {
157     errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
158     printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str());
159     return -1;
160     }
161
162 if (!plugin)
163     {
164     errorStr = "Plugin '" + pluginFileName + "' was not created!";
165     printfd(__FILE__, "PLUGIN_LOADER::Stop() - %s\n", errorStr.c_str());
166     return -1;
167     }
168
169 plugin->Stop();
170
171 return 0;
172 }
173 //-----------------------------------------------------------------------------
174 int PLUGIN_RUNNER::Reload()
175 {
176 if (!isPluginLoaded)
177     {
178     errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
179     printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str());
180     return -1;
181     }
182
183 if (!plugin)
184     {
185     errorStr = "Plugin '" + pluginFileName + "' was not created!";
186     printfd(__FILE__, "PLUGIN_LOADER::Reload() - %s\n", errorStr.c_str());
187     return -1;
188     }
189
190 int res = plugin->Reload();
191 errorStr = plugin->GetStrError();
192 return res;
193 }
194 //-----------------------------------------------------------------------------
195 bool PLUGIN_RUNNER::IsRunning()
196 {
197 if (!isPluginLoaded)
198     {
199     errorStr = "Plugin '" + pluginFileName + "' was not loaded yet!";
200     printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
201     return false;
202     }
203
204 if (!plugin)
205     {
206     errorStr = "Plugin '" + pluginFileName + "' was not created!";
207     printfd(__FILE__, "PLUGIN_LOADER::IsRunning() - %s\n", errorStr.c_str());
208     return false;
209     }
210
211 return plugin->IsRunning();
212 }
213 //-----------------------------------------------------------------------------
214 int PLUGIN_RUNNER::Load()
215 {
216 if (isPluginLoaded)
217     {
218     errorStr = "Plugin '" + pluginFileName + "' was already loaded!";
219     printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
220     return -1;
221     }
222
223 if (pluginFileName.empty())
224     {
225     errorStr = "Empty plugin file name!";
226     printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
227     return -1;
228     }
229
230 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
231
232 if (!libHandle)
233     {
234     errorStr = "Error loading plugin '"
235         + pluginFileName + "': '" + dlerror() + "'";
236     printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
237     return -1;
238     }
239
240 isPluginLoaded = true;
241
242 PLUGIN * (*GetPlugin)();
243 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
244 if (!GetPlugin)
245     {
246     errorStr = std::string("GetPlugin() not found. ") + dlerror();
247     printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
248     return -1;
249     }
250 plugin = GetPlugin();
251
252 if (!plugin)
253     {
254     errorStr = "Plugin was not created!";
255     printfd(__FILE__, "PLUGIN_LOADER::Load() - %s\n", errorStr.c_str());
256     return -1;
257     }
258
259 plugin->SetSettings(modSettings);
260 if (plugin->ParseSettings())
261     {
262     errorStr = plugin->GetStrError();
263     printfd(__FILE__, "PLUGIN_LOADER::Load() - Failed to parse settings. Plugin reports: '%s'\n", errorStr.c_str());
264     return -1;
265     }
266
267 return 0;
268 }
269 //-----------------------------------------------------------------------------
270 int PLUGIN_RUNNER::Unload()
271 {
272 if (isPluginLoaded)
273     {
274     if (dlclose(libHandle))
275         {
276         errorStr = "Failed to unload plugin '";
277         errorStr += pluginFileName + "': ";
278         errorStr += dlerror();
279         printfd(__FILE__, "PLUGIN_LOADER::Unload() - %s", errorStr.c_str());
280         return -1;
281         }
282     plugin = NULL;
283     isPluginLoaded = false;
284     }
285 return 0;
286 }
287 //-----------------------------------------------------------------------------