5 #include "info_methods.h"
6 #include "users_methods.h"
7 #include "tariffs_methods.h"
8 #include "admins_methods.h"
9 #include "messages_methods.h"
11 class RPC_CONFIG_CREATOR
14 RPC_CONFIG * rpcconfig;
18 : rpcconfig(new RPC_CONFIG())
26 RPC_CONFIG * GetPlugin()
32 RPC_CONFIG_CREATOR rpcc;
34 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
41 int RPC_CONFIG_SETTINGS::ParseIntInRange(const string & str,
46 if (str2x(str.c_str(), *val))
48 errorStr = "Incorrect value \'" + str + "\'.";
51 if (*val < min || *val > max)
53 errorStr = "Value \'" + str + "\' out of range.";
59 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
63 vector<PARAM_VALUE>::const_iterator pvi;
66 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
67 if (pvi == s.moduleParams.end())
69 errorStr = "Parameter \'Port\' not found.";
70 printfd(__FILE__, "Parameter 'Port' not found\n");
73 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
75 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
76 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
81 pv.param = "CookieTimeout";
82 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
83 if (pvi == s.moduleParams.end())
85 cookieTimeout = 1800; // 30 * 60
89 if (str2x(pvi->value[0], cookieTimeout))
91 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
92 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
100 BASE_PLUGIN * GetPlugin()
102 return rpcc.GetPlugin();
105 RPC_CONFIG::RPC_CONFIG()
111 RPC_CONFIG::~RPC_CONFIG()
117 int RPC_CONFIG::ParseSettings()
119 int ret = rpcConfigSettings.ParseSettings(settings);
122 errorStr = rpcConfigSettings.GetStrError();
127 int RPC_CONFIG::Start()
131 rpcServer = new xmlrpc_c::serverAbyss(
133 rpcConfigSettings.GetPort(),
134 "/var/log/stargazer_rpc.log"
136 if (pthread_create(&tid, NULL, Run, this))
138 errorStr = "Failed to create RPC thread";
139 printfd(__FILE__, "Failed to crate RPC thread\n");
145 int RPC_CONFIG::Stop()
148 for (int i = 0; i < 5 && !stopped; ++i)
150 //rpcServer->terminate();
153 if (pthread_kill(tid, SIGTERM))
155 errorStr = "Failed to kill thread";
156 printfd(__FILE__, "Failed to kill thread\n");
158 for (int i = 0; i < 25 && !stopped; ++i)
162 printfd(__FILE__, "Failed to stop RPC thread\n");
163 errorStr = "Failed to stop RPC thread";
168 pthread_join(tid, NULL);
174 void * RPC_CONFIG::Run(void * rc)
176 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
178 config->stopped = false;
179 while (config->running)
181 config->rpcServer->runOnce();
183 config->stopped = true;
188 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
191 std::map<std::string,
192 ADMIN_INFO>::iterator it;
194 it = cookies.find(cookie);
196 if (it == cookies.end())
201 if (difftime(it->second.accessTime, time(NULL)) >
202 rpcConfigSettings.GetCookieTimeout())
208 // Update access time
209 time(&it->second.accessTime);
214 bool RPC_CONFIG::CheckAdmin(const std::string & login,
215 const std::string & password,
216 std::string * cookie)
220 if (!admins->AdminCorrect(login, password, &admin))
226 time(&info.accessTime);
228 info.priviledges = *admin.GetPriv();
229 *cookie = GetCookie();
230 cookies[*cookie] = info;
235 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
237 std::map<std::string,
238 ADMIN_INFO>::iterator it;
240 it = cookies.find(cookie);
242 if (it == cookies.end())
252 std::string RPC_CONFIG::GetCookie() const
254 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
257 for (int i = 0; i < 64; ++i)
259 cookie += charset[rand() % charset.length()];
265 void RPC_CONFIG::InitiateRegistry()
268 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
273 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
275 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
278 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
280 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
283 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
285 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
289 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
291 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
296 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
298 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
303 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
305 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
309 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
311 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_cash", methodAddCashPtr);
327 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
333 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
335 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
342 rpcRegistry.addMethod("stargazer.tariff_change", 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_message", methodSendMessagePtr);
414 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
418 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);