10 #include "module_settings.h"
11 #include "../../../settings.h"
14 #include "info_methods.h"
15 #include "users_methods.h"
16 #include "tariffs_methods.h"
17 #include "admins_methods.h"
18 #include "messages_methods.h"
20 class RPC_CONFIG_CREATOR {
22 RPC_CONFIG * rpcconfig;
26 : rpcconfig(new RPC_CONFIG())
34 RPC_CONFIG * GetPlugin()
40 RPC_CONFIG_CREATOR rpcc;
42 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
49 int RPC_CONFIG_SETTINGS::ParseIntInRange(const std::string & str,
54 if (str2x(str.c_str(), *val))
56 errorStr = "Incorrect value \'" + str + "\'.";
59 if (*val < min || *val > max)
61 errorStr = "Value \'" + str + "\' out of range.";
67 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
71 std::vector<PARAM_VALUE>::const_iterator pvi;
74 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
75 if (pvi == s.moduleParams.end())
77 errorStr = "Parameter \'Port\' not found.";
78 printfd(__FILE__, "Parameter 'Port' not found\n");
81 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
83 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
84 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
89 pv.param = "CookieTimeout";
90 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
91 if (pvi == s.moduleParams.end())
93 cookieTimeout = 1800; // 30 * 60
97 if (str2x(pvi->value[0], cookieTimeout))
99 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
100 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
110 return rpcc.GetPlugin();
113 RPC_CONFIG::RPC_CONFIG()
125 RPC_CONFIG::~RPC_CONFIG()
131 int RPC_CONFIG::ParseSettings()
133 int ret = rpcConfigSettings.ParseSettings(settings);
136 errorStr = rpcConfigSettings.GetStrError();
141 void RPC_CONFIG::SetStgSettings(const SETTINGS * settings)
143 dayFee = settings->GetDayFee();
144 dirNames.erase(dirNames.begin(), dirNames.end());
145 for (size_t i = 0; i < DIR_NUM; ++i) {
146 dirNames.push_back(settings->GetDirName(i));
150 int RPC_CONFIG::Start()
154 rpcServer = new xmlrpc_c::serverAbyss(
156 rpcConfigSettings.GetPort(),
157 "/var/log/stargazer_rpc.log"
159 if (pthread_create(&tid, NULL, Run, this))
161 errorStr = "Failed to create RPC thread";
162 printfd(__FILE__, "Failed to crate RPC thread\n");
168 int RPC_CONFIG::Stop()
171 for (int i = 0; i < 5 && !stopped; ++i)
173 //rpcServer->terminate();
176 if (pthread_kill(tid, SIGTERM))
178 errorStr = "Failed to kill thread";
179 printfd(__FILE__, "Failed to kill thread\n");
181 for (int i = 0; i < 25 && !stopped; ++i)
185 printfd(__FILE__, "Failed to stop RPC thread\n");
186 errorStr = "Failed to stop RPC thread";
191 pthread_join(tid, NULL);
197 void * RPC_CONFIG::Run(void * rc)
199 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
201 config->stopped = false;
202 while (config->running)
204 config->rpcServer->runOnce();
206 config->stopped = true;
211 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
214 std::map<std::string,
215 ADMIN_INFO>::iterator it;
217 it = cookies.find(cookie);
219 if (it == cookies.end())
224 if (difftime(it->second.accessTime, time(NULL)) >
225 rpcConfigSettings.GetCookieTimeout())
231 // Update access time
232 time(&it->second.accessTime);
237 bool RPC_CONFIG::CheckAdmin(const std::string & login,
238 const std::string & password,
239 std::string * cookie)
241 ADMIN * admin = NULL;
243 if (!admins->AdminCorrect(login, password, &admin))
249 time(&info.accessTime);
251 info.priviledges = *admin->GetPriv();
252 *cookie = GetCookie();
253 cookies[*cookie] = info;
258 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
260 std::map<std::string,
261 ADMIN_INFO>::iterator it;
263 it = cookies.find(cookie);
265 if (it == cookies.end())
275 std::string RPC_CONFIG::GetCookie() const
277 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
280 for (int i = 0; i < 64; ++i)
282 cookie += charset[rand() % charset.length()];
288 void RPC_CONFIG::InitiateRegistry()
291 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
297 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
299 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
302 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
304 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
307 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
309 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
313 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
315 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
320 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
322 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
327 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
329 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
333 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
335 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
342 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
344 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
350 rpcRegistry.addMethod("stargazer.add_cash", methodAddCashPtr);
352 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
358 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
360 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
367 rpcRegistry.addMethod("stargazer.tariff_change", methodTariffChangePtr);
369 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
373 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
375 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
380 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
382 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
386 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
388 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
393 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
395 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
401 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
403 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
407 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
409 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
413 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
415 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
419 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
421 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
425 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
427 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
431 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
433 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
437 rpcRegistry.addMethod("stargazer.send_message", methodSendMessagePtr);
439 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
443 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);