1 #include <unistd.h> // TODO: usleep
9 #include "stg/common.h"
10 #include "stg/admin.h"
11 #include "stg/module_settings.h"
12 #include "stg/settings.h"
13 #include "stg/plugin_creator.h"
15 #include "rpcconfig.h"
16 #include "info_methods.h"
17 #include "users_methods.h"
18 #include "tariffs_methods.h"
19 #include "admins_methods.h"
20 #include "messages_methods.h"
22 PLUGIN_CREATOR<RPC_CONFIG> rpcc;
24 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
31 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
35 std::vector<PARAM_VALUE>::const_iterator pvi;
38 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
39 if (pvi == s.moduleParams.end())
41 errorStr = "Parameter \'Port\' not found.";
42 printfd(__FILE__, "Parameter 'Port' not found\n");
45 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
47 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
48 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
53 pv.param = "CookieTimeout";
54 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55 if (pvi == s.moduleParams.end())
57 cookieTimeout = 1800; // 30 * 60
61 if (str2x(pvi->value[0], cookieTimeout))
63 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
64 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
74 return rpcc.GetPlugin();
77 RPC_CONFIG::RPC_CONFIG()
89 RPC_CONFIG::~RPC_CONFIG()
95 int RPC_CONFIG::ParseSettings()
97 int ret = rpcConfigSettings.ParseSettings(settings);
100 errorStr = rpcConfigSettings.GetStrError();
105 void RPC_CONFIG::SetStgSettings(const SETTINGS * settings)
107 dayFee = settings->GetDayFee();
108 dirNames.erase(dirNames.begin(), dirNames.end());
109 for (size_t i = 0; i < DIR_NUM; ++i) {
110 dirNames.push_back(settings->GetDirName(i));
114 int RPC_CONFIG::Start()
118 rpcServer = new xmlrpc_c::serverAbyss(
120 rpcConfigSettings.GetPort(),
121 "/var/log/stargazer_rpc.log"
123 if (pthread_create(&tid, NULL, Run, this))
125 errorStr = "Failed to create RPC thread";
126 printfd(__FILE__, "Failed to crate RPC thread\n");
132 int RPC_CONFIG::Stop()
135 for (int i = 0; i < 5 && !stopped; ++i)
137 //rpcServer->terminate();
140 if (pthread_kill(tid, SIGTERM))
142 errorStr = "Failed to kill thread";
143 printfd(__FILE__, "Failed to kill thread\n");
145 for (int i = 0; i < 25 && !stopped; ++i)
149 printfd(__FILE__, "Failed to stop RPC thread\n");
150 errorStr = "Failed to stop RPC thread";
155 pthread_join(tid, NULL);
161 void * RPC_CONFIG::Run(void * rc)
163 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
165 config->stopped = false;
166 while (config->running)
168 config->rpcServer->runOnce();
170 config->stopped = true;
175 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
178 std::map<std::string,
179 ADMIN_INFO>::iterator it;
181 it = cookies.find(cookie);
183 if (it == cookies.end())
188 if (difftime(it->second.accessTime, time(NULL)) >
189 rpcConfigSettings.GetCookieTimeout())
195 // Update access time
196 time(&it->second.accessTime);
201 bool RPC_CONFIG::CheckAdmin(const std::string & login,
202 const std::string & password,
203 std::string * cookie)
205 ADMIN * admin = NULL;
207 if (!admins->Correct(login, password, &admin))
213 time(&info.accessTime);
215 info.priviledges = *admin->GetPriv();
216 *cookie = GetCookie();
217 cookies[*cookie] = info;
222 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
224 std::map<std::string,
225 ADMIN_INFO>::iterator it;
227 it = cookies.find(cookie);
229 if (it == cookies.end())
239 std::string RPC_CONFIG::GetCookie() const
241 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
244 for (int i = 0; i < 64; ++i)
246 cookie += charset[rand() % charset.length()];
252 void RPC_CONFIG::InitiateRegistry()
255 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
261 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
263 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
266 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
268 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
271 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
273 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
277 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
279 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
284 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
286 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
291 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
293 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
297 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
299 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
306 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
308 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
314 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
316 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
322 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
324 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
331 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
333 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
337 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
339 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
344 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
346 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
350 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
352 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
357 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
359 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
365 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
367 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
371 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
373 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
377 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
379 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
383 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
385 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
389 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
391 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
395 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
397 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
401 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
403 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
407 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);