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);
161 printfd(__FILE__, "Failed to stop RPC thread\n");
162 errorStr = "Failed to stop RPC thread";
167 pthread_join(tid, NULL);
173 void * RPC_CONFIG::Run(void * rc)
176 sigfillset(&signalSet);
177 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
179 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
181 config->stopped = false;
182 while (config->running)
184 config->rpcServer->runOnce();
186 config->stopped = true;
191 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
194 std::map<std::string,
195 ADMIN_INFO>::iterator it;
197 it = cookies.find(cookie);
199 if (it == cookies.end())
204 if (difftime(it->second.accessTime, time(NULL)) >
205 rpcConfigSettings.GetCookieTimeout())
211 // Update access time
212 time(&it->second.accessTime);
217 bool RPC_CONFIG::CheckAdmin(const std::string & login,
218 const std::string & password,
219 std::string * cookie)
221 ADMIN * admin = NULL;
223 if (!admins->Correct(login, password, &admin))
229 time(&info.accessTime);
231 info.priviledges = *admin->GetPriv();
232 *cookie = GetCookie();
233 cookies[*cookie] = info;
238 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
240 std::map<std::string,
241 ADMIN_INFO>::iterator it;
243 it = cookies.find(cookie);
245 if (it == cookies.end())
255 std::string RPC_CONFIG::GetCookie() const
257 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
260 for (int i = 0; i < 64; ++i)
262 cookie += charset[rand() % charset.length()];
268 void RPC_CONFIG::InitiateRegistry()
271 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
277 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
279 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
282 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
284 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
287 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
289 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
293 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
295 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
300 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
302 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
307 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
309 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
313 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
315 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
322 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
324 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
330 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
332 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
338 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
340 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
347 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
349 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
353 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
355 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
360 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
362 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
366 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
368 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
373 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
375 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
381 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
383 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
387 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
389 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
393 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
395 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
399 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
401 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
405 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
407 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
411 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
413 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
417 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
419 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
423 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);