7 #include "stg/common.h"
9 #include "stg/module_settings.h"
10 #include "stg/settings.h"
11 #include "stg/plugin_creator.h"
13 #include "rpcconfig.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 PLUGIN_CREATOR<RPC_CONFIG> rpcc;
22 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
29 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
33 std::vector<PARAM_VALUE>::const_iterator pvi;
36 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
37 if (pvi == s.moduleParams.end())
39 errorStr = "Parameter \'Port\' not found.";
40 printfd(__FILE__, "Parameter 'Port' not found\n");
43 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
45 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
46 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
51 pv.param = "CookieTimeout";
52 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
53 if (pvi == s.moduleParams.end())
55 cookieTimeout = 1800; // 30 * 60
59 if (str2x(pvi->value[0], cookieTimeout))
61 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
62 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
72 return rpcc.GetPlugin();
75 RPC_CONFIG::RPC_CONFIG()
94 RPC_CONFIG::~RPC_CONFIG()
100 int RPC_CONFIG::ParseSettings()
102 int ret = rpcConfigSettings.ParseSettings(settings);
105 errorStr = rpcConfigSettings.GetStrError();
110 void RPC_CONFIG::SetStgSettings(const SETTINGS * settings)
112 dayFee = settings->GetDayFee();
113 dirNames.erase(dirNames.begin(), dirNames.end());
114 for (size_t i = 0; i < DIR_NUM; ++i) {
115 dirNames.push_back(settings->GetDirName(i));
119 int RPC_CONFIG::Start()
123 rpcServer = new xmlrpc_c::serverAbyss(
125 rpcConfigSettings.GetPort(),
126 "/var/log/stargazer_rpc.log"
128 if (pthread_create(&tid, NULL, Run, this))
130 errorStr = "Failed to create RPC thread";
131 printfd(__FILE__, "Failed to crate RPC thread\n");
137 int RPC_CONFIG::Stop()
140 for (int i = 0; i < 5 && !stopped; ++i)
142 struct timespec ts = {0, 200000000};
143 nanosleep(&ts, NULL);
145 //rpcServer->terminate();
148 if (pthread_kill(tid, SIGTERM))
150 errorStr = "Failed to kill thread";
151 printfd(__FILE__, "Failed to kill thread\n");
153 for (int i = 0; i < 25 && !stopped; ++i)
155 struct timespec ts = {0, 200000000};
156 nanosleep(&ts, NULL);
160 printfd(__FILE__, "Failed to stop RPC thread\n");
161 errorStr = "Failed to stop RPC thread";
166 pthread_join(tid, NULL);
172 void * RPC_CONFIG::Run(void * rc)
174 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
176 config->stopped = false;
177 while (config->running)
179 config->rpcServer->runOnce();
181 config->stopped = true;
186 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
189 std::map<std::string,
190 ADMIN_INFO>::iterator it;
192 it = cookies.find(cookie);
194 if (it == cookies.end())
199 if (difftime(it->second.accessTime, time(NULL)) >
200 rpcConfigSettings.GetCookieTimeout())
206 // Update access time
207 time(&it->second.accessTime);
212 bool RPC_CONFIG::CheckAdmin(const std::string & login,
213 const std::string & password,
214 std::string * cookie)
216 ADMIN * admin = NULL;
218 if (!admins->Correct(login, password, &admin))
224 time(&info.accessTime);
226 info.priviledges = *admin->GetPriv();
227 *cookie = GetCookie();
228 cookies[*cookie] = info;
233 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
235 std::map<std::string,
236 ADMIN_INFO>::iterator it;
238 it = cookies.find(cookie);
240 if (it == cookies.end())
250 std::string RPC_CONFIG::GetCookie() const
252 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
255 for (int i = 0; i < 64; ++i)
257 cookie += charset[rand() % charset.length()];
263 void RPC_CONFIG::InitiateRegistry()
266 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
272 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
274 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
277 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
279 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
282 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
284 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
288 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
290 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
295 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
297 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
302 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
304 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
308 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
310 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
317 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
319 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
325 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
327 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
333 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
335 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
342 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
344 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
348 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
350 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
355 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
357 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
361 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
363 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
368 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
370 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
376 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
378 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
382 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
384 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
388 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
390 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
394 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
396 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
400 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
402 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
406 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
408 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
412 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
414 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
418 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);