From: Maxim Mamontov Date: Thu, 8 Aug 2013 14:56:57 +0000 (+0300) Subject: Many changes related to getting user information via sgconf. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/b463b9f9a916554458fc576342125ff2f22efdac?ds=inline Many changes related to getting user information via sgconf. --- diff --git a/projects/sgconf/common_sg.cpp b/projects/sgconf/common_sg.cpp index a15f7588..9bbe62d7 100644 --- a/projects/sgconf/common_sg.cpp +++ b/projects/sgconf/common_sg.cpp @@ -47,10 +47,10 @@ const int usageInfo = 1; const int TO_KOI8 = 0; const int FROM_KOI8 = 1; //----------------------------------------------------------------------------- -struct GetUserCbData +struct GetUserData { - void * data; - bool * result; + REQUEST & request; + bool result; }; //--------------------------------------------------------------------------- struct HelpParams @@ -314,32 +314,29 @@ struct StringReqParams string * value; }; //----------------------------------------------------------------------------- -void RecvUserData(USERDATA * ud, void * d) +void GetUserCallback(const PARSER_GET_USER::INFO & info, void * d) { -GetUserCbData * gucbd; -gucbd = (GetUserCbData *)d; - -bool * result = gucbd->result; +GetUserData * data = static_cast(d); REQUEST * req = (REQUEST *)gucbd->data; -if (ud->login == "") +if (info.login == "") { - *result = false; + data->result = false; return; } -if (!req->cash.res_empty()) - cout << "cash=" << ud->cash << endl; +if (!data->request.cash.res_empty()) + cout << "cash=" << info.cash << endl; -if (!req->credit.res_empty()) - cout << "credit=" << ud->credit << endl; +if (!data->requst.credit.res_empty()) + cout << "credit=" << info.credit << endl; -if (!req->creditExpire.res_empty()) +if (!data->requst.creditExpire.res_empty()) { char buf[32]; struct tm brokenTime; - time_t tt = ud->creditExpire; + time_t tt = info.creditExpire; brokenTime.tm_wday = 0; brokenTime.tm_yday = 0; @@ -355,50 +352,58 @@ if (!req->creditExpire.res_empty()) cout << "creditExpire=" << buf << endl; } -if (!req->down.res_empty()) - cout << "down=" << ud->down << endl; +if (!data->requst.down.res_empty()) + cout << "down=" << info.down << endl; + +if (!data->requst.passive.res_empty()) + cout << "passive=" << info.passive << endl; -if (!req->passive.res_empty()) - cout << "passive=" << ud->passive << endl; +if (!data->requst.disableDetailStat.res_empty()) + cout << "disableDetailStat=" << info.disableDetailStat << endl; -if (!req->disableDetailStat.res_empty()) - cout << "disableDetailStat=" << ud->disableDetailStat << endl; +if (!data->requst.alwaysOnline.res_empty()) + cout << "alwaysOnline=" << info.alwaysOnline << endl; -if (!req->alwaysOnline.res_empty()) - cout << "alwaysOnline=" << ud->alwaysOnline << endl; +if (!data->requst.prepaidTraff.res_empty()) + cout << "prepaidTraff=" << info.prepaidTraff << endl; -if (!req->prepaidTraff.res_empty()) - cout << "prepaidTraff=" << ud->prepaidTraff << endl; +for (int i = 0; i < DIR_NUM; i++) + { + if (!data->requst.up[i].res_empty()) + cout << "session upload for dir" << i << "=" << info.stat.su[i] << endl; + if (!data->requst.down[i].res_empty()) + cout << "session download for dir" << i << "=" << info.stat.sd[i] << endl; + } for (int i = 0; i < DIR_NUM; i++) { - if (!req->u[i].res_empty()) - cout << "u" << i << "=" << ud->stat.mu[i] << endl; - if (!req->d[i].res_empty()) - cout << "d" << i << "=" << ud->stat.md[i] << endl; + if (!data->requst.monthUp[i].res_empty()) + cout << "month upload for dir" << i << "=" << info.stat.mu[i] << endl; + if (!data->requst.monthDown[i].res_empty()) + cout << "month download for dir" << i << "=" << info.stat.md[i] << endl; } for (int i = 0; i < USERDATA_NUM; i++) { - if (!req->ud[i].res_empty()) + if (!data->requst.ud[i].res_empty()) { string str; - ConvertFromKOI8(ud->userData[i], &str); + ConvertFromKOI8(info.userData[i], &str); cout << "userdata" << i << "=" << str << endl; } } StringReqParams strReqParams[] = { - {"note", req->note, &ud->note}, - {"name", req->name, &ud->name}, - {"address", req->address, &ud->address}, - {"email", req->email, &ud->email}, - {"phone", req->phone, &ud->phone}, - {"group", req->group, &ud->group}, - {"tariff", req->tariff, &ud->tariff}, - {"password", req->usrPasswd, &ud->password}, - {"ip", req->ips, &ud->ips} // IP-address of user + {"note", data->requst.note, &info.note}, + {"name", data->requst.name, &info.name}, + {"address", data->requst.address, &info.address}, + {"email", data->requst.email, &info.email}, + {"phone", data->requst.phone, &info.phone}, + {"group", data->requst.group, &info.group}, + {"tariff", data->requst.tariff, &info.tariff}, + {"password", data->requst.usrPasswd, &info.password}, + {"ip", data->requst.ips, &info.ips} // IP-address of user }; for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++) { @@ -409,7 +414,7 @@ for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++) cout << strReqParams[i].name << "=" << str << endl; } } -*result = true; +data->result = true; } //----------------------------------------------------------------------------- void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *) @@ -432,9 +437,9 @@ SERVCONF sc; bool result = false; -sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ -sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ -sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ +sc.SetServer(server.c_str()); +sc.SetPort(port); +sc.SetAdmLogin(admLogin.c_str()); sc.SetAdmPassword(admPasswd.c_str()); // TODO Good variable name :) @@ -473,24 +478,24 @@ int ProcessGetUser(const std::string &server, const std::string &admLogin, const std::string &admPasswd, const std::string &login, - void * data) + REQUEST & request) { SERVCONF sc; bool result = false; -sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ -sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ -sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ +sc.SetServer(server.c_str()); +sc.SetPort(port); +sc.SetAdmLogin(admLogin.c_str()); sc.SetAdmPassword(admPasswd.c_str()); // TODO Good variable name :) GetUserCbData gucbd; -gucbd.data = data; +gucbd.data = &request; gucbd.result = &result; -sc.SetGetUserDataRecvCb(RecvUserData, &gucbd); +sc.SetGetUserCallback(GetUserCallback, &gucbd); sc.GetUser(login.c_str()); if (result) @@ -512,7 +517,7 @@ int ProcessAuthBy(const std::string &server, const std::string &admLogin, const std::string &admPasswd, const std::string &login, - void * data) + REQUEST & request) { SERVCONF sc; diff --git a/projects/sgconf/common_sg.h b/projects/sgconf/common_sg.h index a5cbffad..47069eae 100644 --- a/projects/sgconf/common_sg.h +++ b/projects/sgconf/common_sg.h @@ -50,14 +50,14 @@ int ProcessGetUser(const std::string &server, const std::string &admLogin, const std::string &admPasswd, const std::string &login, - void * data); + REQUEST & request); int ProcessAuthBy(const std::string &server, int port, const std::string &admLogin, const std::string &admPasswd, const std::string &login, - void * data); + REQUEST & request); int ProcessSetUser(const std::string &server, int port, diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 2c02be27..fae181f6 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -66,37 +66,12 @@ struct option long_options_get[] = { {"passive", 0, 0, 'i'}, //passive {"disable-stat",0, 0, 'S'}, //disable detail stat {"always-online",0, 0, 'O'}, //always online -{"u0", 0, 0, 500}, //U0 -{"u1", 0, 0, 501}, //U1 -{"u2", 0, 0, 502}, //U2 -{"u3", 0, 0, 503}, //U3 -{"u4", 0, 0, 504}, //U4 -{"u5", 0, 0, 505}, //U5 -{"u6", 0, 0, 506}, //U6 -{"u7", 0, 0, 507}, //U7 -{"u8", 0, 0, 508}, //U8 -{"u9", 0, 0, 509}, //U9 -{"d0", 0, 0, 600}, //D0 -{"d1", 0, 0, 601}, //D1 -{"d2", 0, 0, 602}, //D2 -{"d3", 0, 0, 603}, //D3 -{"d4", 0, 0, 604}, //D4 -{"d5", 0, 0, 605}, //D5 -{"d6", 0, 0, 606}, //D6 -{"d7", 0, 0, 607}, //D7 -{"d8", 0, 0, 608}, //D8 -{"d9", 0, 0, 609}, //D9 - -{"ud0", 0, 0, 700}, //UserData0 -{"ud1", 0, 0, 701}, //UserData1 -{"ud2", 0, 0, 702}, //UserData2 -{"ud3", 0, 0, 703}, //UserData3 -{"ud4", 0, 0, 704}, //UserData4 -{"ud5", 0, 0, 705}, //UserData5 -{"ud6", 0, 0, 706}, //UserData6 -{"ud7", 0, 0, 707}, //UserData7 -{"ud8", 0, 0, 708}, //UserData8 -{"ud9", 0, 0, 709}, //UserData9 +{"session-upload-dir", 1, 0, 500}, //SU0 +{"session-download-dir", 1, 0, 501}, //SD0 +{"month-upload-dir", 1, 0, 502}, //MU0 +{"month-download-dir", 1, 0, 503}, //MD0 + +{"user-data", 1, 0, 700}, //UserData0 {"prepaid", 0, 0, 'e'}, //prepaid traff {"create", 0, 0, 'n'}, //create @@ -129,37 +104,12 @@ struct option long_options_set[] = { {"passive", 1, 0, 'i'}, //passive {"disable-stat",1, 0, 'S'}, //disable detail stat {"always-online",1, 0, 'O'}, //always online -{"u0", 1, 0, 500}, //U0 -{"u1", 1, 0, 501}, //U1 -{"u2", 1, 0, 502}, //U2 -{"u3", 1, 0, 503}, //U3 -{"u4", 1, 0, 504}, //U4 -{"u5", 1, 0, 505}, //U5 -{"u6", 1, 0, 506}, //U6 -{"u7", 1, 0, 507}, //U7 -{"u8", 1, 0, 508}, //U8 -{"u9", 1, 0, 509}, //U9 -{"d0", 1, 0, 600}, //D0 -{"d1", 1, 0, 601}, //D1 -{"d2", 1, 0, 602}, //D2 -{"d3", 1, 0, 603}, //D3 -{"d4", 1, 0, 604}, //D4 -{"d5", 1, 0, 605}, //D5 -{"d6", 1, 0, 606}, //D6 -{"d7", 1, 0, 607}, //D7 -{"d8", 1, 0, 608}, //D8 -{"d9", 1, 0, 609}, //D9 - -{"ud0", 1, 0, 700}, //UserData -{"ud1", 1, 0, 701}, //UserData1 -{"ud2", 1, 0, 702}, //UserData2 -{"ud3", 1, 0, 703}, //UserData3 -{"ud4", 1, 0, 704}, //UserData4 -{"ud5", 1, 0, 705}, //UserData5 -{"ud6", 1, 0, 706}, //UserData6 -{"ud7", 1, 0, 707}, //UserData7 -{"ud8", 1, 0, 708}, //UserData8 -{"ud9", 1, 0, 709}, //UserData9 +{"session-upload", 1, 0, 500}, //U0 +{"session-download", 1, 0, 501}, //U1 +{"month-upload", 1, 0, 502}, //U2 +{"month-download", 1, 0, 503}, //U3 + +{"user-data", 1, 0, 700}, //UserData {"prepaid", 1, 0, 'e'}, //prepaid traff {"create", 1, 0, 'n'}, //create @@ -792,7 +742,7 @@ while (1) case 'G': //Group req.group = " "; break; - + case 'I': //IP-address of user req.ips = " "; break; @@ -806,45 +756,25 @@ while (1) break; case 500: //U + //printf("U%d\n", c - 500); + req.sessionUp[optarg] = 1; + break; case 501: + //printf("U%d\n", c - 500); + req.sessionDown[optarg] = 1; + break; case 502: - case 503: - case 504: - case 505: - case 506: - case 507: - case 508: - case 509: //printf("U%d\n", c - 500); - req.u[c - 500] = 1; + req.monthUp[optarg] = 1; break; - - case 600: //D - case 601: - case 602: - case 603: - case 604: - case 605: - case 606: - case 607: - case 608: - case 609: - //printf("D%d\n", c - 600); - req.d[c - 600] = 1; + case 503: + //printf("U%d\n", c - 500); + req.monthDown[optarg] = 1; break; case 700: //UserData - case 701: - case 702: - case 703: - case 704: - case 705: - case 706: - case 707: - case 708: - case 709: //printf("UD%d\n", c - 700); - req.ud[c - 700] = " "; + req.ud[optarg] = " "; break; case 800: @@ -879,9 +809,9 @@ if (missedOptionArg || !CheckParametersGet(&req)) } if (req.authBy) - return ProcessAuthBy(req.server, req.port, req.admLogin, req.admPasswd, req.login, &req); + return ProcessAuthBy(req.server, req.port, req.admLogin, req.admPasswd, req.login, req); else - return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, &req); + return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, req); } //----------------------------------------------------------------------------- int mainSet(int argc, char **argv) @@ -1025,46 +955,26 @@ while (1) break; case 500: //U + //printf("U%d\n", c - 500); + req.sesionUpload[optarg] = ParseTraff(argv[optind++]); + break; case 501: + //printf("U%d\n", c - 500); + req.sessionDownload[optarg] = ParseTraff(argv[optind++]); + break; case 502: - case 503: - case 504: - case 505: - case 506: - case 507: - case 508: - case 509: //printf("U%d\n", c - 500); - req.u[c - 500] = ParseTraff(optarg); + req.monthUpload[optarg] = ParseTraff(argv[optind++]); break; - - case 600: //D - case 601: - case 602: - case 603: - case 604: - case 605: - case 606: - case 607: - case 608: - case 609: - //printf("D%d\n", c - 600); - req.d[c - 600] = ParseTraff(optarg); + case 503: + //printf("U%d\n", c - 500); + req.monthDownload[optarg] = ParseTraff(argv[optind++]); break; case 700: //UserData - case 701: - case 702: - case 703: - case 704: - case 705: - case 706: - case 707: - case 708: - case 709: - ParseAnyString(optarg, &str); + ParseAnyString(argv[optind++], &str); //printf("UD%d\n", c - 700); - req.ud[c - 700] = str; + req.userData[optarg] = str; break; case '?': diff --git a/projects/sgconf/request.h b/projects/sgconf/request.h index 61222127..5ce4d093 100644 --- a/projects/sgconf/request.h +++ b/projects/sgconf/request.h @@ -54,12 +54,14 @@ REQUEST() { for (int i = 0; i < DIR_NUM; i++) { - u[i].reset(); - d[i].reset(); + sessionUp[i].reset(); + sessionDown[i].reset(); + monthUp[i].reset(); + monthDown[i].reset(); } for (int i = 0; i < USERDATA_NUM; i++) - ud[i].reset(); + userData[i].reset(); } RESETABLE server; @@ -88,10 +90,13 @@ RESETABLE disableDetailStat; RESETABLE alwaysOnline; RESETABLE prepaidTraff; -RESETABLE u[DIR_NUM]; -RESETABLE d[DIR_NUM]; +RESETABLE sessionUp[DIR_NUM]; +RESETABLE sessionDown[DIR_NUM]; -RESETABLE ud[USERDATA_NUM]; +RESETABLE monthUp[DIR_NUM]; +RESETABLE monthDown[DIR_NUM]; + +RESETABLE userData[USERDATA_NUM]; RESETABLE note; RESETABLE name; diff --git a/stglibs/srvconf.lib/include/stg/parser_get_user.h b/stglibs/srvconf.lib/include/stg/parser_get_user.h index 575b0e46..db074af4 100644 --- a/stglibs/srvconf.lib/include/stg/parser_get_user.h +++ b/stglibs/srvconf.lib/include/stg/parser_get_user.h @@ -35,11 +35,12 @@ class BASE_PROPERTY_PARSER { public: - virtual bool Parse(const char ** attr) = 0; + virtual ~BASE_PROPERTY_PARSER() {} + virtual void Parse(const char ** attr) = 0; }; template -class PROPERTY_PARSER +class PROPERTY_PARSER : public BASE_PROPERTY_PARSER { public: typedef T (* FUNC)(const char **); diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index 714a4e79..f455730e 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -98,7 +98,7 @@ public: void SetAdmLogin(const char * login); void SetAdmPassword(const char * password); - void SetGetUsers(PARSER_GET_USERS::CALLBACK f, void * data); + void SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data); void SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data); void SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data); void SetChgUserCb(RecvChgUserCb_t, void * data); @@ -147,9 +147,9 @@ private: PARSER_CHECK_USER::CALLBACK checkUserCallback; RecvSendMessageCb_t RecvSendMessageCb; + void * getUsersData; void * getUserData; void * authByData; - void * getUsersData; void * serverInfoData; void * chgUserDataCb; void * checkUserData; diff --git a/stglibs/srvconf.lib/parser_get_user.cpp b/stglibs/srvconf.lib/parser_get_user.cpp index 9af85d03..9ba86c14 100644 --- a/stglibs/srvconf.lib/parser_get_user.cpp +++ b/stglibs/srvconf.lib/parser_get_user.cpp @@ -23,6 +23,7 @@ #include "stg/common.h" +#include #include #include @@ -58,24 +59,24 @@ double getValue(const char ** attr) { double value = 0; if (checkValue(attr)) - if (strtodouble2(attr[1], value) == EINVAL) + if (strtodouble2(attr[1], value)) return 0; return value; } template <> -STAT getValue(const char ** attr) +PARSER_GET_USER::STAT getValue(const char ** attr) { -STAT value; +PARSER_GET_USER::STAT value; if (!attr) return value; std::map props; for (size_t i = 0; i < DIR_NUM; ++i) { - props.insert("su" + x2str(i), value.su[i]); - props.insert("sd" + x2str(i), value.sd[i]); - props.insert("mu" + x2str(i), value.mu[i]); - props.insert("md" + x2str(i), value.md[i]); + props.insert(std::pair("su" + x2str(i), value.su[i])); + props.insert(std::pair("sd" + x2str(i), value.sd[i])); + props.insert(std::pair("mu" + x2str(i), value.mu[i])); + props.insert(std::pair("md" + x2str(i), value.md[i])); } size_t pos = 0; while (attr[pos]) @@ -90,24 +91,28 @@ return value; std::string getEncodedValue(const char ** attr) { +std::string value; if (checkValue(attr)) - return Decode21str(attr[1]); -return ""; + Decode21str(value, attr[1]); +return value; } +template +void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, bool encoded = false); + template void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, bool /*encoded*/) { - parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getValue)); + parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, getValue))); } template <> void addParser(PROPERTY_PARSERS & parsers, const std::string & name, std::string & value, bool encoded) { if (encoded) - parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getEncodedValue)); + parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, getEncodedValue))); else - parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getValue)); + parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, getValue))); } void tryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) @@ -182,7 +187,7 @@ void PARSER_GET_USER::ParseUser(const char * el, const char ** attr) { if (strcasecmp(el, "user") == 0) if (strcasecmp(attr[1], "error") == 0) - user.login = ""; + info.login = ""; } //----------------------------------------------------------------------------- void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr) diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 7b2c85ec..3d82860b 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -94,16 +94,16 @@ SERVCONF::SERVCONF() : currParser(NULL), parseDepth(0), error(0), - RecvUserDataCb(NULL), - RecvGetUserDataCb(NULL), + getUsersCallback(NULL), + getUserCallback(NULL), authByCallback(NULL), serverInfoCallback(NULL), RecvChgUserCb(NULL), checkUserCallback(NULL), RecvSendMessageCb(NULL), - getUserDataDataCb(NULL), + getUsersData(NULL), + getUserData(NULL), authByData(NULL), - getUsersDataDataCb(NULL), serverInfoData(NULL), chgUserDataCb(NULL), checkUserData(NULL),