7 #include "stg/common.h"
9 #include "stg/module_settings.h"
10 #include "stg/settings.h"
12 #include "rpcconfig.h"
13 #include "info_methods.h"
14 #include "users_methods.h"
15 #include "tariffs_methods.h"
16 #include "admins_methods.h"
17 #include "messages_methods.h"
19 class RPC_CONFIG_CREATOR {
21 RPC_CONFIG * rpcconfig;
25 : rpcconfig(new RPC_CONFIG())
33 RPC_CONFIG * GetPlugin()
39 RPC_CONFIG_CREATOR rpcc;
41 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
48 int RPC_CONFIG_SETTINGS::ParseIntInRange(const std::string & str,
53 if (str2x(str.c_str(), *val))
55 errorStr = "Incorrect value \'" + str + "\'.";
58 if (*val < min || *val > max)
60 errorStr = "Value \'" + str + "\' out of range.";
66 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
70 std::vector<PARAM_VALUE>::const_iterator pvi;
73 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
74 if (pvi == s.moduleParams.end())
76 errorStr = "Parameter \'Port\' not found.";
77 printfd(__FILE__, "Parameter 'Port' not found\n");
80 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
82 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
83 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
88 pv.param = "CookieTimeout";
89 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
90 if (pvi == s.moduleParams.end())
92 cookieTimeout = 1800; // 30 * 60
96 if (str2x(pvi->value[0], cookieTimeout))
98 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
99 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
109 return rpcc.GetPlugin();
112 RPC_CONFIG::RPC_CONFIG()
124 RPC_CONFIG::~RPC_CONFIG()
130 int RPC_CONFIG::ParseSettings()
132 int ret = rpcConfigSettings.ParseSettings(settings);
135 errorStr = rpcConfigSettings.GetStrError();
140 void RPC_CONFIG::SetStgSettings(const SETTINGS * settings)
142 dayFee = settings->GetDayFee();
143 dirNames.erase(dirNames.begin(), dirNames.end());
144 for (size_t i = 0; i < DIR_NUM; ++i) {
145 dirNames.push_back(settings->GetDirName(i));
149 int RPC_CONFIG::Start()
153 rpcServer = new xmlrpc_c::serverAbyss(
155 rpcConfigSettings.GetPort(),
156 "/var/log/stargazer_rpc.log"
158 if (pthread_create(&tid, NULL, Run, this))
160 errorStr = "Failed to create RPC thread";
161 printfd(__FILE__, "Failed to crate RPC thread\n");
167 int RPC_CONFIG::Stop()
170 for (int i = 0; i < 5 && !stopped; ++i)
172 //rpcServer->terminate();
175 if (pthread_kill(tid, SIGTERM))
177 errorStr = "Failed to kill thread";
178 printfd(__FILE__, "Failed to kill thread\n");
180 for (int i = 0; i < 25 && !stopped; ++i)
184 printfd(__FILE__, "Failed to stop RPC thread\n");
185 errorStr = "Failed to stop RPC thread";
190 pthread_join(tid, NULL);
196 void * RPC_CONFIG::Run(void * rc)
198 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
200 config->stopped = false;
201 while (config->running)
203 config->rpcServer->runOnce();
205 config->stopped = true;
210 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
213 std::map<std::string,
214 ADMIN_INFO>::iterator it;
216 it = cookies.find(cookie);
218 if (it == cookies.end())
223 if (difftime(it->second.accessTime, time(NULL)) >
224 rpcConfigSettings.GetCookieTimeout())
230 // Update access time
231 time(&it->second.accessTime);
236 bool RPC_CONFIG::CheckAdmin(const std::string & login,
237 const std::string & password,
238 std::string * cookie)
240 ADMIN * admin = NULL;
242 if (!admins->AdminCorrect(login, password, &admin))
248 time(&info.accessTime);
250 info.priviledges = *admin->GetPriv();
251 *cookie = GetCookie();
252 cookies[*cookie] = info;
257 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
259 std::map<std::string,
260 ADMIN_INFO>::iterator it;
262 it = cookies.find(cookie);
264 if (it == cookies.end())
274 std::string RPC_CONFIG::GetCookie() const
276 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
279 for (int i = 0; i < 64; ++i)
281 cookie += charset[rand() % charset.length()];
287 void RPC_CONFIG::InitiateRegistry()
290 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
296 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
298 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
301 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
303 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
306 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
308 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
312 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
314 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
319 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
321 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
326 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
328 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
332 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
334 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
341 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
343 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
349 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
351 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
357 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
359 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
366 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
368 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
372 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
374 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
379 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
381 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
385 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
387 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
392 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
394 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
400 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
402 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
406 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
408 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
412 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
414 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
418 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
420 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
424 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
426 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
430 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
432 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
436 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
438 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
442 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);