#include <cstdio>
#include <cstring>
#include <string>
-#include <list>
#include <sstream>
#include "stg/common.h"
-#include "stg/netunit.h"
#include "request.h"
#include "common_sg.h"
#include "sg_error_codes.h"
-using namespace std;
+namespace
+{
-time_t stgTime;
+template <typename T>
+struct ARRAY_TYPE;
+
+template <typename T>
+struct ARRAY_TYPE<T[]>
+{
+typedef T type;
+};
+
+template <typename T, size_t N>
+struct ARRAY_TYPE<T[N]>
+{
+typedef T type;
+};
+
+template <typename T>
+bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE<T>::type & value)
+{
+size_t pos = 0;
+if (str2x(index, pos))
+ return false;
+array[pos] = value;
+return true;
+}
-int ParseReplyGet(void * data, list<string> * ans);
-//int ParseReplySet(void * data, list<string> * ans);
+} // namespace anonymous
+
+time_t stgTime;
struct option long_options_get[] = {
{"server", 1, 0, 's'}, //Server
{"passive", 0, 0, 'i'}, //passive
{"disable-stat",0, 0, 'S'}, //disable detail stat
{"always-online",0, 0, 'O'}, //always online
-{"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
+{"session-upload", 1, 0, 500}, //SU0
+{"session-download", 1, 0, 501}, //SD0
+{"month-upload", 1, 0, 502}, //MU0
+{"month-download", 1, 0, 503}, //MD0
{"user-data", 1, 0, 700}, //UserData0
int dPresent = false;
for (int i = 0; i < DIR_NUM; i++)
{
- if (!req->u[i].res_empty())
+ if (!req->monthUpload[i].res_empty())
{
if (!uPresent && !dPresent)
{
}
stringstream ss;
- ss << req->u[i].const_data();
+ ss << req->monthUpload[i].const_data();
//sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
strcat(r, str);
}
- if (!req->d[i].res_empty())
+ if (!req->monthDownload[i].res_empty())
{
if (!uPresent && !dPresent)
{
}
stringstream ss;
- ss << req->d[i].const_data();
+ ss << req->monthDownload[i].const_data();
+ sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
+ strcat(r, str);
+ }
+ if (!req->sessionUpload[i].res_empty())
+ {
+ if (!uPresent && !dPresent)
+ {
+ sprintf(str, "<traff ");
+ strcat(r, str);
+ uPresent = true;
+ }
+
+ stringstream ss;
+ ss << req->sessionUpload[i].const_data();
+ //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
+ sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
+ strcat(r, str);
+ }
+ if (!req->sessionDownload[i].res_empty())
+ {
+ if (!uPresent && !dPresent)
+ {
+ sprintf(str, "<traff ");
+ strcat(r, str);
+ dPresent = true;
+ }
+
+ stringstream ss;
+ ss << req->sessionDownload[i].const_data();
sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
strcat(r, str);
}
for (int i = 0; i < USERDATA_NUM; i++)
{
- if (!req->ud[i].res_empty())
+ if (!req->userData[i].res_empty())
{
string ud;
- Encode12str(ud, req->ud[i]);
+ Encode12str(ud, req->userData[i]);
sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
strcat(r, str);
}
//-----------------------------------------------------------------------------
int CheckParameters(REQUEST * req)
{
-int u = false;
-int d = false;
-int ud = false;
-int a = !req->admLogin.res_empty()
+bool su = false;
+bool sd = false;
+bool mu = false;
+bool md = false;
+bool ud = false;
+bool a = !req->admLogin.res_empty()
&& !req->admPasswd.res_empty()
&& !req->server.res_empty()
&& !req->port.res_empty()
&& !req->login.res_empty();
-int b = !req->cash.res_empty()
+bool b = !req->cash.res_empty()
|| !req->setCash.res_empty()
|| !req->credit.res_empty()
|| !req->prepaidTraff.res_empty()
for (int i = 0; i < DIR_NUM; i++)
{
- if (req->u[i].res_empty())
+ if (req->sessionUpload[i].res_empty())
+ {
+ su = true;
+ break;
+ }
+ }
+
+for (int i = 0; i < DIR_NUM; i++)
+ {
+ if (req->sessionDownload[i].res_empty())
+ {
+ sd = true;
+ break;
+ }
+ }
+
+for (int i = 0; i < DIR_NUM; i++)
+ {
+ if (req->monthUpload[i].res_empty())
{
- u = true;
+ mu = true;
break;
}
}
for (int i = 0; i < DIR_NUM; i++)
{
- if (req->d[i].res_empty())
+ if (req->monthDownload[i].res_empty())
{
- d = true;
+ md = true;
break;
}
}
for (int i = 0; i < DIR_NUM; i++)
{
- if (req->ud[i].res_empty())
+ if (req->userData[i].res_empty())
{
ud = true;
break;
//printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
-return a && (b || u || d || ud);
+return a && (b || su || sd || mu || md || ud);
}
//-----------------------------------------------------------------------------
int CheckParametersGet(REQUEST * req)
return CheckParameters(req);
}
//-----------------------------------------------------------------------------
-int mainGet(int argc, char **argv)
+bool mainGet(int argc, char **argv)
{
int c;
REQUEST req;
req.group = " ";
break;
- case 'I': //IP-address of user
- req.ips = " ";
- break;
+ case 'I': //IP-address of user
+ req.ips = " ";
+ break;
case 'S': //Detail stat status
req.disableDetailStat = " ";
break;
case 500: //U
- //printf("U%d\n", c - 500);
- req.sessionUp[optarg] = 1;
+ SetArrayItem(req.sessionUpload, optarg, 1);
+ //req.sessionUpload[optarg] = 1;
break;
case 501:
- //printf("U%d\n", c - 500);
- req.sessionDown[optarg] = 1;
+ SetArrayItem(req.sessionDownload, optarg, 1);
+ //req.sessionDownload[optarg] = 1;
break;
case 502:
- //printf("U%d\n", c - 500);
- req.monthUp[optarg] = 1;
+ SetArrayItem(req.monthUpload, optarg, 1);
+ //req.monthUpload[optarg] = 1;
break;
case 503:
- //printf("U%d\n", c - 500);
- req.monthDown[optarg] = 1;
+ SetArrayItem(req.monthDownload, optarg, 1);
+ //req.monthDownload[optarg] = 1;
break;
case 700: //UserData
- //printf("UD%d\n", c - 700);
- req.ud[optarg] = " ";
+ SetArrayItem(req.userData, optarg, std::string(" "));
+ //req.userData[optarg] = " ";
break;
case 800:
case '?':
case ':':
- //printf ("Unknown option \n");
missedOptionArg = true;
break;
}
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);
else
return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, req);
}
//-----------------------------------------------------------------------------
-int mainSet(int argc, char **argv)
+bool mainSet(int argc, char **argv)
{
string str;
break;
case 500: //U
- //printf("U%d\n", c - 500);
- req.sesionUpload[optarg] = ParseTraff(argv[optind++]);
+ SetArrayItem(req.sessionUpload, optarg, ParseTraff(argv[optind++]));
+ //req.sessionUpload[optarg] = ParseTraff(argv[optind++]);
break;
case 501:
- //printf("U%d\n", c - 500);
- req.sessionDownload[optarg] = ParseTraff(argv[optind++]);
+ SetArrayItem(req.sessionDownload, optarg, ParseTraff(argv[optind++]));
+ //req.sessionDownload[optarg] = ParseTraff(argv[optind++]);
break;
case 502:
- //printf("U%d\n", c - 500);
- req.monthUpload[optarg] = ParseTraff(argv[optind++]);
+ SetArrayItem(req.monthUpload, optarg, ParseTraff(argv[optind++]));
+ //req.monthUpload[optarg] = ParseTraff(argv[optind++]);
break;
case 503:
- //printf("U%d\n", c - 500);
- req.monthDownload[optarg] = ParseTraff(argv[optind++]);
+ SetArrayItem(req.monthDownload, optarg, ParseTraff(argv[optind++]));
+ //req.monthDownload[optarg] = ParseTraff(argv[optind++]);
break;
case 700: //UserData
ParseAnyString(argv[optind++], &str);
- //printf("UD%d\n", c - 700);
- req.userData[optarg] = str;
+ SetArrayItem(req.userData, optarg, str);
+ //req.userData[optarg] = str;
break;
case '?':
- //printf("Missing option argument\n");
missedOptionArg = true;
break;
case ':':
- //printf("Missing option argument\n");
missedOptionArg = true;
break;
memset(rstr, 0, rLen);
CreateRequestSet(&req, rstr);
-return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr, NULL, isMessage);
+if (isMessage)
+ return ProcessSendMessage(req.server, req.port, req.admLogin, req.admPasswd, rstr);
+
+return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr);
}
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
else if (strcmp(argv[1], "set") == 0)
{
//printf("set\n");
- return mainSet(argc - 1, argv + 1);
+ if (mainSet(argc - 1, argv + 1) )
+ return 0;
+ return -1;
}
else
{