]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
Добавление исходников
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / rpcconfig.cpp
1 #include "rpcconfig.h"
2
3 #include <cstdlib>
4 #include <csignal>
5 #include "info_methods.h"
6 #include "users_methods.h"
7 #include "tariffs_methods.h"
8 #include "admins_methods.h"
9 #include "messages_methods.h"
10
11 class RPC_CONFIG_CREATOR
12 {
13 private:
14     RPC_CONFIG * rpcconfig;
15
16 public:
17     RPC_CONFIG_CREATOR()
18         : rpcconfig(new RPC_CONFIG())
19         {
20         };
21     ~RPC_CONFIG_CREATOR()
22         {
23         delete rpcconfig;
24         };
25
26     RPC_CONFIG * GetPlugin()
27         {
28         return rpcconfig;
29         };
30 };
31
32 RPC_CONFIG_CREATOR rpcc;
33
34 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
35     : errorStr(),
36       port(0),
37       cookieTimeout(0)
38 {
39 }
40
41 int RPC_CONFIG_SETTINGS::ParseIntInRange(const string & str,
42                                          int min,
43                                          int max,
44                                          int * val)
45 {
46 if (str2x(str.c_str(), *val))
47     {
48     errorStr = "Incorrect value \'" + str + "\'.";
49     return -1;
50     }
51 if (*val < min || *val > max)
52     {
53     errorStr = "Value \'" + str + "\' out of range.";
54     return -1;
55     }
56 return 0;
57 }
58
59 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
60 {
61 int p;
62 PARAM_VALUE pv;
63 vector<PARAM_VALUE>::const_iterator pvi;
64
65 pv.param = "Port";
66 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
67 if (pvi == s.moduleParams.end())
68     {
69     errorStr = "Parameter \'Port\' not found.";
70     printfd(__FILE__, "Parameter 'Port' not found\n");
71     return -1;
72     }
73 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
74     {
75     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
76     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
77     return -1;
78     }
79 port = p;
80
81 pv.param = "CookieTimeout";
82 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
83 if (pvi == s.moduleParams.end())
84     {
85     cookieTimeout = 1800; // 30 * 60
86     }
87 else
88     {
89     if (str2x(pvi->value[0], cookieTimeout))
90         {
91         errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
92         printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
93         return -1;
94         }
95     }
96
97 return 0;
98 }
99
100 BASE_PLUGIN * GetPlugin()
101 {
102 return rpcc.GetPlugin();
103 }
104
105 RPC_CONFIG::RPC_CONFIG()
106     : rpcServer(NULL)
107 {
108
109 }
110
111 RPC_CONFIG::~RPC_CONFIG()
112 {
113 // delete server
114 delete rpcServer;
115 }
116
117 int RPC_CONFIG::ParseSettings()
118 {
119 int ret = rpcConfigSettings.ParseSettings(settings);
120
121 if (ret)
122     errorStr = rpcConfigSettings.GetStrError();
123
124 return ret;
125 }
126
127 int RPC_CONFIG::Start()
128 {
129 InitiateRegistry();
130 running = true;
131 rpcServer = new xmlrpc_c::serverAbyss(
132         rpcRegistry,
133         rpcConfigSettings.GetPort(),
134         "/var/log/stargazer_rpc.log"
135         );
136 if (pthread_create(&tid, NULL, Run, this))
137     {
138     errorStr = "Failed to create RPC thread";
139     printfd(__FILE__, "Failed to crate RPC thread\n");
140     return -1;
141     }
142 return 0;
143 }
144
145 int RPC_CONFIG::Stop()
146 {
147 running = false;
148 for (int i = 0; i < 5 && !stopped; ++i)
149     usleep(200000);
150 //rpcServer->terminate();
151 if (!stopped)
152     {
153     if (pthread_kill(tid, SIGTERM))
154         {
155         errorStr = "Failed to kill thread";
156         printfd(__FILE__, "Failed to kill thread\n");
157         }
158     for (int i = 0; i < 25 && !stopped; ++i)
159         usleep(200000);
160     if (!stopped)
161         {
162         printfd(__FILE__, "Failed to stop RPC thread\n");
163         errorStr = "Failed to stop RPC thread";
164         return -1;
165         }
166     else
167         {
168         pthread_join(tid, NULL);
169         }
170     }
171 return 0;
172 }
173
174 void * RPC_CONFIG::Run(void * rc)
175 {
176 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
177
178 config->stopped = false;
179 while (config->running)
180     {
181     config->rpcServer->runOnce();
182     }
183 config->stopped = true;
184
185 return NULL;
186 }
187
188 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
189                               ADMIN_INFO * info)
190 {
191 std::map<std::string,
192          ADMIN_INFO>::iterator it;
193
194 it = cookies.find(cookie);
195
196 if (it == cookies.end())
197     {
198     return true;
199     }
200
201 if (difftime(it->second.accessTime, time(NULL)) >
202     rpcConfigSettings.GetCookieTimeout())
203     {
204     cookies.erase(it);
205     return true;
206     }
207
208 // Update access time
209 time(&it->second.accessTime);
210 *info = it->second;
211 return false;
212 }
213
214 bool RPC_CONFIG::CheckAdmin(const std::string & login,
215                             const std::string & password,
216                             std::string * cookie)
217 {
218 ADMIN admin;
219
220 if (!admins->AdminCorrect(login, password, &admin))
221     {
222     return true;
223     }
224
225 ADMIN_INFO info;
226 time(&info.accessTime);
227 info.admin = login;
228 info.priviledges = *admin.GetPriv();
229 *cookie = GetCookie();
230 cookies[*cookie] = info;
231
232 return false;
233 }
234
235 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
236 {
237 std::map<std::string,
238          ADMIN_INFO>::iterator it;
239
240 it = cookies.find(cookie);
241
242 if (it == cookies.end())
243     {
244     return true;
245     }
246
247 cookies.erase(it);
248
249 return false;
250 }
251
252 std::string RPC_CONFIG::GetCookie() const
253 {
254 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
255 std::string cookie;
256
257 for (int i = 0; i < 64; ++i)
258     {
259     cookie += charset[rand() % charset.length()];
260     };
261
262 return cookie;
263 }
264
265 void RPC_CONFIG::InitiateRegistry()
266 {
267 // manage registry
268 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
269             tariffs,
270             users,
271             stgSettings
272             ));
273 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
274
275 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
276             this
277             ));
278 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
279
280 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
281             this
282             ));
283 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
284
285 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
286             this,
287             users
288             ));
289 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
290
291 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
292             this,
293             admins,
294             users
295             ));
296 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
297
298 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
299             this,
300             admins,
301             users
302             ));
303 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
304
305 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
306             this,
307             users
308             ));
309 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
310
311 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
312             this,
313             admins,
314             store,
315             users
316             ));
317 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
318
319 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
320             this,
321             admins,
322             store,
323             users
324             ));
325 rpcRegistry.addMethod("stargazer.add_cash", methodAddCashPtr);
326
327 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
328             this,
329             admins,
330             store,
331             users
332             ));
333 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
334
335 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
336             this,
337             admins,
338             tariffs,
339             store,
340             users
341             ));
342 rpcRegistry.addMethod("stargazer.tariff_change", methodTariffChangePtr);
343
344 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
345             this,
346             tariffs
347             ));
348 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
349
350 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
351             this,
352             admins,
353             tariffs
354             ));
355 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
356
357 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
358             this,
359             tariffs
360             ));
361 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
362
363 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
364             this,
365             admins,
366             tariffs
367             ));
368 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
369
370 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
371             this,
372             admins,
373             tariffs,
374             users
375             ));
376 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
377
378 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
379             this,
380             admins
381             ));
382 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
383
384 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
385             this,
386             admins
387             ));
388 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
389
390 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
391             this,
392             admins
393             ));
394 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
395
396 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
397             this,
398             admins
399             ));
400 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
401
402 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
403             this,
404             admins
405             ));
406 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
407
408 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
409             this,
410             users
411             ));
412 rpcRegistry.addMethod("stargazer.send_message", methodSendMessagePtr);
413
414 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
415             this,
416             users
417             ));
418 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
419 }
420