]> git.stg.codes - stg.git/blob - projects/stargazer/plugin_runner.cpp
Fix compilation issues
[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 #include <signal.h>
30
31 #include "plugin_runner.h"
32 #include "common.h"
33 #include "conffiles.h"
34
35 //-----------------------------------------------------------------------------
36 PLUGIN_RUNNER::PLUGIN_RUNNER(const std::string & pFileName,
37                              const MODULE_SETTINGS & ms,
38                              ADMINS * a,
39                              TARIFFS * t,
40                              USERS * u,
41                              TRAFFCOUNTER * tc,
42                              STORE * st,
43                              const SETTINGS * s)
44     : pluginFileName(pFileName),
45       pluginSettingFileName(),
46       plugin(NULL),
47       isPluginLoaded(false),
48       errorStr(),
49       libHandle(NULL),
50       isRunning(false),
51       admins(a),
52       tariffs(t),
53       users(u),
54       store(st),
55       traffCnt(tc),
56       stgSettings(s),
57       modSettings(ms)
58 {
59 }
60 //-----------------------------------------------------------------------------
61 PLUGIN_RUNNER::PLUGIN_RUNNER(const PLUGIN_RUNNER & rvalue)
62     : pluginFileName(rvalue.pluginFileName),
63       pluginSettingFileName(rvalue.pluginSettingFileName),
64       plugin(rvalue.plugin),
65       isPluginLoaded(rvalue.isPluginLoaded),
66       errorStr(rvalue.errorStr),
67       libHandle(rvalue.libHandle),
68       isRunning(rvalue.isRunning),
69       admins(rvalue.admins),
70       tariffs(rvalue.tariffs),
71       users(rvalue.users),
72       store(rvalue.store),
73       traffCnt(rvalue.traffCnt),
74       stgSettings(rvalue.stgSettings),
75       modSettings(rvalue.modSettings)
76 {
77 }
78 //-----------------------------------------------------------------------------
79 PLUGIN_RUNNER & PLUGIN_RUNNER::operator=(const PLUGIN_RUNNER & rvalue)
80 {
81 pluginFileName = rvalue.pluginFileName;
82 pluginSettingFileName = rvalue.pluginSettingFileName;
83 plugin = rvalue.plugin;
84 isPluginLoaded = rvalue.isPluginLoaded;
85 errorStr = rvalue.errorStr;
86 libHandle = rvalue.libHandle;
87 isRunning = rvalue.isRunning;
88 admins = rvalue.admins;
89 tariffs = rvalue.tariffs;
90 users = rvalue.users;
91 store = rvalue.store;
92 traffCnt = rvalue.traffCnt;
93 stgSettings = rvalue.stgSettings;
94 modSettings = rvalue.modSettings;
95
96 return *this;
97 }
98 //-----------------------------------------------------------------------------
99 PLUGIN_RUNNER::~PLUGIN_RUNNER()
100 {
101 if (isPluginLoaded)
102     {
103     Unload();
104     }
105
106 isPluginLoaded = 0;
107 }
108 //-----------------------------------------------------------------------------
109 PLUGIN * PLUGIN_RUNNER::GetPlugin()
110 {
111 return plugin;
112 }
113 //-----------------------------------------------------------------------------
114 int PLUGIN_RUNNER::Start()
115 {
116 if (!isPluginLoaded)
117     if (Load())
118         return -1;
119
120 plugin->SetTariffs(tariffs);
121 plugin->SetAdmins(admins);
122 plugin->SetUsers(users);
123 plugin->SetTraffcounter(traffCnt);
124 plugin->SetStore(store);
125 plugin->SetStgSettings(stgSettings);
126
127 if (plugin->Start())
128     {
129     errorStr = plugin->GetStrError();
130     return -1;
131     }
132 return 0;
133 }
134 //-----------------------------------------------------------------------------
135 int PLUGIN_RUNNER::Stop()
136 {
137 plugin->Stop();
138
139 //if (Unload())
140 //    return -1;
141 return 0;
142 }
143 //-----------------------------------------------------------------------------
144 int PLUGIN_RUNNER::Reload()
145 {
146 int res = plugin->Reload();
147 errorStr = plugin->GetStrError();
148 return res;
149 }
150 //-----------------------------------------------------------------------------
151 bool PLUGIN_RUNNER::IsRunning()
152 {
153 if (!isPluginLoaded)
154     return false;
155 return plugin->IsRunning();
156 }
157 //-----------------------------------------------------------------------------
158 int PLUGIN_RUNNER::Load()
159 {
160 if (!pluginFileName.size())
161     {
162     errorStr = "Plugin loading failed. No plugin";
163     printfd(__FILE__, "%s\n", errorStr.c_str());
164     return -1;
165     }
166
167 libHandle = dlopen(pluginFileName.c_str(), RTLD_NOW);
168
169 if (!libHandle)
170     {
171     errorStr = std::string("Plugin loading failed. ") + dlerror();
172     printfd(__FILE__, "%s\n", errorStr.c_str());
173     return -1;
174     }
175
176 PLUGIN * (*GetPlugin)();
177 GetPlugin = (PLUGIN * (*)())dlsym(libHandle, "GetPlugin");
178 if (!GetPlugin)
179     {
180     errorStr = std::string("GetPlugin() not found. ") + dlerror();
181     return -1;
182     }
183 plugin = GetPlugin();
184 isPluginLoaded++;
185
186 if (!plugin)
187     {
188     errorStr = "Plugin was not created!";
189     printfd(__FILE__, "%s\n", errorStr.c_str());
190     return -1;
191     }
192
193 plugin->SetSettings(modSettings);
194 printfd(__FILE__, "Plugin %s parsesettings\n", plugin->GetVersion().c_str());
195 if (plugin->ParseSettings())
196     {
197     errorStr = "Plugin \'" + plugin->GetVersion() + "\' error: " + plugin->GetStrError();
198     return -1;
199     }
200
201 return 0;
202 }
203 //-----------------------------------------------------------------------------
204 int PLUGIN_RUNNER::Unload()
205 {
206 if (isPluginLoaded)
207     {
208     if (dlclose(libHandle))
209         {
210         errorStr = dlerror();
211         printfd(__FILE__, "Error unloading plugin '%s': '%s'", pluginFileName.c_str(), dlerror());
212         return -1;
213         }
214     isPluginLoaded--;
215     }
216 return 0;
217 }
218 //-----------------------------------------------------------------------------
219 const std::string & PLUGIN_RUNNER::GetStrError() const
220 {
221 return errorStr;
222 }
223 //-----------------------------------------------------------------------------
224 uint16_t PLUGIN_RUNNER::GetStartPosition() const
225 {
226 return plugin->GetStartPosition();
227 }
228 //-----------------------------------------------------------------------------
229 uint16_t PLUGIN_RUNNER::GetStopPosition() const
230 {
231 return plugin->GetStopPosition();
232 }
233 //-----------------------------------------------------------------------------