const int TO_KOI8 = 0;
const int FROM_KOI8 = 1;
//-----------------------------------------------------------------------------
+struct ResultData
+{
+ bool result;
+ std::string reason;
+};
+//-----------------------------------------------------------------------------
struct GetUserData
{
GetUserData(REQUEST & req, bool res) : request(req), result(res) {}
REQUEST & request;
bool result;
+ std::string reason;
};
//---------------------------------------------------------------------------
struct HelpParams
ConvertKOI8(src, dst, FROM_KOI8);
}
//-----------------------------------------------------------------------------
-int RecvSetUserAnswer(const char * ans, void * d)
+void SendMessageCallback(bool result, const std::string & reason, void * d)
{
-GetUserData * data = static_cast<GetUserData *>(d);
+ResultData * data = static_cast<ResultData *>(d);
+data->result = result;
+data->reason = reason;
+}
+//-----------------------------------------------------------------------------
+void RecvSetUserAnswer(bool result, const std::string & reason, void * d)
+{
+ResultData * data = static_cast<ResultData *>(d);
+data->result = result;
+data->reason = reason;
+}
+//-----------------------------------------------------------------------------
+void RecvAuthByData(bool result, const std::string & reason,
+ const PARSER_AUTH_BY::INFO & list, void * d)
+{
+ResultData * data = static_cast<ResultData *>(d);
+data->result = result;
+data->reason = reason;
-std::cout << ans << std::endl;
+if (!result)
+ return;
-data->result = (strcasecmp("Ok", ans) == 0);
+for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
+ std::cout << *it << "\n";
-return 0;
+std::cout << std::endl;
}
//-----------------------------------------------------------------------------
struct StringReqParams
const std::string * value;
};
//-----------------------------------------------------------------------------
-void GetUserCallback(const PARSER_GET_USER::INFO & info, void * d)
+void GetUserCallback(bool result, const std::string& reason, const PARSER_GET_USER::INFO & info, void * d)
{
GetUserData * data = static_cast<GetUserData *>(d);
+data->result = false;
+data->reason = reason;
+
+if (!result)
+ return;
if (info.login == "")
{
data->result = false;
+ data->reason = "Invalid login.";
return;
}
data->result = true;
}
//-----------------------------------------------------------------------------
-void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *)
-{
-for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
- cout << *it << "\n";
-cout << endl;
-}
-//-----------------------------------------------------------------------------
-int ProcessSetUser(const std::string &server,
- int port,
- const std::string &admLogin,
- const std::string &admPasswd,
- const std::string &str,
- bool isMessage)
+bool ProcessSetUser(const std::string & server,
+ int port,
+ const std::string & admLogin,
+ const std::string & admPasswd,
+ const std::string & str)
{
SERVCONF sc;
sc.SetAdmLogin(admLogin.c_str());
sc.SetAdmPassword(admPasswd.c_str());
-REQUEST request;
-GetUserData cbdata(request, false);
+ResultData data;
+sc.SetChgUserCallback(RecvSetUserAnswer, &data);
+int res = sc.ChgUser(str.c_str());
-int res = 0;
-if (isMessage)
+if (res == st_ok && data.result)
{
- sc.SetSendMessageCallback(RecvSetUserAnswer, &cbdata);
- res = sc.SendMessage(str.c_str());
+ printf("Ok\n");
+ return false;
}
+
+printf("Error\n");
+if (res != st_ok)
+ printf("%s\n", sc.GetStrError().c_str());
else
- {
- sc.SetChgUserCallback(RecvSetUserAnswer, &cbdata);
- res = sc.ChgUser(str.c_str());
- }
+ printf("%s\n", data.reason.c_str());
+return true;
+}
+//-----------------------------------------------------------------------------
+bool ProcessSendMessage(const std::string & server, uint16_t port,
+ const std::string & login, const std::string & password,
+ const std::string & requestString)
+{
+SERVCONF sc;
+
+sc.SetServer(server.c_str());
+sc.SetPort(port);
+sc.SetAdmLogin(login.c_str());
+sc.SetAdmPassword(password.c_str());
+
+ResultData data;
+sc.SetSendMessageCallback(SendMessageCallback, &data);
+int res = sc.SendMessage(requestString.c_str());
-if (res == st_ok && cbdata.result)
+if (res == st_ok && data.result)
{
printf("Ok\n");
- return 0;
- }
-else
- {
- printf("Error\n");
- if (res != st_ok)
- printf("%s\n", sc.GetStrError().c_str());
- return -1;
+ return true;
}
-return 0;
+printf("Error\n");
+if (res != st_ok)
+ printf("%s\n", sc.GetStrError().c_str());
+else
+ printf("%s\n", data.reason.c_str());
+return false;
}
//-----------------------------------------------------------------------------
-int ProcessGetUser(const std::string &server,
- int port,
- const std::string &admLogin,
- const std::string &admPasswd,
- const std::string &login,
- REQUEST & request)
+bool ProcessGetUser(const std::string &server,
+ int port,
+ const std::string &admLogin,
+ const std::string &admPasswd,
+ const std::string &login,
+ REQUEST & request)
{
SERVCONF sc;
if (res && data.result)
{
printf("Ok\n");
- return 0;
- }
-else
- {
- printf("Error\n");
- return -1;
+ return true;
}
-return 0;
+printf("Error\n");
+if (!res)
+ printf("%s\n", sc.GetStrError().c_str());
+else
+ printf("%s\n", data.reason.c_str());
+return false;
}
//-----------------------------------------------------------------------------
-int ProcessAuthBy(const std::string &server,
- int port,
- const std::string &admLogin,
- const std::string &admPasswd,
- const std::string &login)
+bool ProcessAuthBy(const std::string &server,
+ int port,
+ const std::string &admLogin,
+ const std::string &admPasswd,
+ const std::string &login)
{
SERVCONF sc;
sc.SetAdmLogin(admLogin.c_str());
sc.SetAdmPassword(admPasswd.c_str());
-sc.SetAuthByCallback(RecvAuthByData, NULL);
+ResultData data;
+sc.SetAuthByCallback(RecvAuthByData, &data);
bool res = (sc.AuthBy(login.c_str()) == st_ok);
-if (!res)
+if (res && data.result)
{
- printf("Error\n");
- return -1;
+ printf("Ok\n");
+ return true;
}
-printf("Ok\n");
-return 0;
+printf("Error\n");
+if (!res)
+ printf("%s\n", sc.GetStrError().c_str());
+else
+ printf("%s\n", data.reason.c_str());
+return false;
}
//-----------------------------------------------------------------------------
void ConvertFromKOI8(const std::string & src, std::string * dst);
void ConvertToKOI8(const std::string & src, std::string * dst);
-int ProcessGetUser(const std::string &server,
+bool ProcessGetUser(const std::string & server,
+ int port,
+ const std::string & admLogin,
+ const std::string & admPasswd,
+ const std::string & login,
+ REQUEST & request);
+
+bool ProcessAuthBy(const std::string & server,
int port,
- const std::string &admLogin,
- const std::string &admPasswd,
- const std::string &login,
- REQUEST & request);
+ const std::string & admLogin,
+ const std::string & admPasswd,
+ const std::string & login);
-int ProcessAuthBy(const std::string &server,
- int port,
- const std::string &admLogin,
- const std::string &admPasswd,
- const std::string &login);
+bool ProcessSetUser(const std::string & server,
+ int port,
+ const std::string & admLogin,
+ const std::string & admPasswd,
+ const std::string & str);
-int ProcessSetUser(const std::string &server,
- int port,
- const std::string &admLogin,
- const std::string &admPasswd,
- const std::string &str,
- bool isMessage = false);
+bool ProcessSendMessage(const std::string & server, uint16_t port,
+ const std::string & login, const std::string & password,
+ const std::string & requestString);
#endif
return CheckParameters(req);
}
//-----------------------------------------------------------------------------
-int mainGet(int argc, char **argv)
+bool mainGet(int argc, char **argv)
{
int c;
REQUEST req;
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;
memset(rstr, 0, rLen);
CreateRequestSet(&req, rstr);
-return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr, 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
{
{
public:
typedef std::vector<std::string> INFO;
- typedef void (* CALLBACK)(const INFO & info, void * data);
+ typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data);
PARSER_AUTH_BY();
int ParseStart(const char *el, const char **attr);
CALLBACK callback;
void * data;
int depth;
+ bool parsingAnswer;
INFO info;
+ std::string error;
};
#endif
class PARSER_CHG_USER: public PARSER
{
public:
- typedef int (* CALLBACK)(bool result, const std::string& reason, void * data);
+ typedef void (* CALLBACK)(bool result, const std::string& reason, void * data);
PARSER_CHG_USER();
int ParseStart(const char * el, const char ** attr);
void * data;
INFO info;
int depth;
+ bool parsingAnswer;
std::string error;
void ParseUser(const char *el, const char **attr);
PARSER_GET_USER userParser;
INFO info;
int depth;
+ bool parsingAnswer;
std::string error;
void AddUser(const PARSER_GET_USER::INFO & userInfo);
void SetError(const std::string & e) { error = e; }
- void ParseUsers(const char * el, const char ** attr);
- static void UserCallback(const PARSER_GET_USER::INFO & info, void * data);
+ static void UserCallback(bool result, const std::string& reason, const PARSER_GET_USER::INFO & info, void * data);
};
#endif
class PARSER_SEND_MESSAGE: public PARSER
{
public:
- typedef int (* CALLBACK)(bool result, const std::string& reason, void * data);
+ typedef void (* CALLBACK)(bool result, const std::string& reason, void * data);
PARSER_SEND_MESSAGE();
int ParseStart(const char * el, const char ** attr);
CALLBACK callback;
void * data;
int depth;
+ bool parsingAnswer;
INFO info;
std::string error;
};
bool CheckValue(const char ** attr);
template <typename T>
+inline
bool GetValue(const char ** attr, T & value)
{
if (CheckValue(attr))
}
template <>
+inline
bool GetValue<std::string>(const char ** attr, std::string & value)
{
if (!CheckValue(attr))
}
template <>
+inline
bool GetValue<double>(const char ** attr, double & value)
{
if (CheckValue(attr))
void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>);
template <typename T>
+inline
void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func)
{
parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
PARSER_AUTH_BY::PARSER_AUTH_BY()
: callback(NULL),
data(NULL),
- depth(0)
+ depth(0),
+ parsingAnswer(false)
{
}
//-----------------------------------------------------------------------------
depth++;
if (depth == 1)
{
- if (strcasecmp(el, "AuthorizedBy") != 0)
- info.clear();
+ if (strcasecmp(el, "AuthorizedBy") == 0)
+ if (attr && attr[0] && attr[1])
+ {
+ if (strcasecmp(attr[1], "error") == 0)
+ {
+ if (attr[2] && attr[3])
+ error = attr[3];
+ else
+ error = "User not found.";
+ }
+ else
+ parsingAnswer = true;
+ }
}
else
{
if (depth == 2)
{
- if (strcasecmp(el, "Auth") == 0)
+ if (parsingAnswer && strcasecmp(el, "Auth") == 0)
{
if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "name") == 0)
info.push_back(attr[1]);
void PARSER_AUTH_BY::ParseEnd(const char * /*el*/)
{
depth--;
-if (depth == 0 && callback)
- callback(info, data);
+if (depth == 0)
+ {
+ if (callback)
+ callback(error.empty(), error, info, data);
+ info.clear();
+ error.clear();
+ }
}
//-----------------------------------------------------------------------------
void PARSER_AUTH_BY::SetCallback(CALLBACK f, void * d)
{
}
//-----------------------------------------------------------------------------
-int PARSER_CHECK_USER::ParseStart(const char *el, const char **attr)
+int PARSER_CHECK_USER::ParseStart(const char * el, const char ** attr)
{
depth++;
if (depth == 1)
PARSER_GET_USER::PARSER_GET_USER()
: callback(NULL),
data(NULL),
- depth(0)
+ depth(0),
+ parsingAnswer(false)
{
AddParser(propertyParsers, "login", info.login);
AddParser(propertyParsers, "password", info.password);
if (depth == 1)
ParseUser(el, attr);
-if (depth == 2)
+if (depth == 2 && parsingAnswer)
ParseUserParams(el, attr);
return 0;
void PARSER_GET_USER::ParseEnd(const char * /*el*/)
{
depth--;
-if (depth == 0)
+if (depth == 0 && parsingAnswer)
{
if (callback)
callback(error.empty(), error, info, data);
error.clear();
+ parsingAnswer = false;
}
}
//-----------------------------------------------------------------------------
void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
{
if (strcasecmp(el, "user") == 0)
- if (strcasecmp(attr[1], "error") == 0)
- error = "User not found.";
+ if (attr && attr[0] && attr[1])
+ {
+ if (strcasecmp(attr[1], "error") == 0)
+ {
+ if (attr[2] && attr[3])
+ error = attr[3];
+ else
+ error = "User not found.";
+ }
+ else
+ parsingAnswer = true;
+ }
+ else
+ parsingAnswer = true;
}
//-----------------------------------------------------------------------------
void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
PARSER_GET_USERS::PARSER_GET_USERS()
: callback(NULL),
data(NULL),
- depth(0)
+ depth(0),
+ parsingAnswer(false)
{
userParser.SetCallback(&PARSER_GET_USERS::UserCallback, this);
}
int PARSER_GET_USERS::ParseStart(const char * el, const char ** attr)
{
depth++;
-if (depth == 1)
- ParseUsers(el, attr);
+if (depth == 1 && strcasecmp(el, "users") == 0)
+ parsingAnswer = true;
-if (depth > 1)
+if (depth > 1 && parsingAnswer)
userParser.ParseStart(el, attr);
return 0;
void PARSER_GET_USERS::ParseEnd(const char * el)
{
depth--;
-if (depth > 0)
+if (depth > 0 && parsingAnswer)
userParser.ParseEnd(el);
-if (depth == 0)
+if (depth == 0 && parsingAnswer)
{
if (callback)
callback(error.empty(), error, info, data);
error.clear();
- }
-}
-//-----------------------------------------------------------------------------
-void PARSER_GET_USERS::ParseUsers(const char * el, const char ** /*attr*/)
-{
-if (strcasecmp(el, "users") == 0)
info.clear();
+ parsingAnswer = false;
+ }
}
//-----------------------------------------------------------------------------
void PARSER_GET_USERS::AddUser(const PARSER_GET_USER::INFO & userInfo)
{
}
//-----------------------------------------------------------------------------
-int PARSER_SEND_MESSAGE::ParseStart(const char * el, const char ** attr)
+int PARSER_SEND_MESSAGE::ParseStart(const char * el, const char ** attr)
{
depth++;
if (depth == 1)
PARSER_SERVER_INFO::PARSER_SERVER_INFO()
: callback(NULL),
data(NULL),
- depth(0)
+ depth(0),
+ parsingAnswer(false)
{
AddParser(propertyParsers, "uname", info.uname);
AddParser(propertyParsers, "version", info.version);
{
depth++;
if (depth == 1)
- if (strcasecmp(el, "ServerInfo") != 0)
- error = "Invalid response.";
+ if (strcasecmp(el, "ServerInfo") == 0)
+ parsingAnswer = true;
else
- if (depth == 2)
+ if (depth == 2 && parsingAnswer)
if (!TryParse(propertyParsers, ToLower(el), attr))
error = "Invalid parameter.";
return 0;
void PARSER_SERVER_INFO::ParseEnd(const char * /*el*/)
{
depth--;
-if (depth == 0)
+if (depth == 0 && parsingAnswer)
{
if (callback)
callback(error.empty(), error, info, data);
error.clear();
+ parsingAnswer = false;
}
}
//-----------------------------------------------------------------------------
return true;
}
-bool GetIPValue(const char ** attr, uint32_t value)
+bool GetIPValue(const char ** attr, uint32_t & value)
{
if (!CheckValue(attr))
return false;