2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
21 #include "stg/servconf.h"
23 #include "stg/common.h"
31 void ElementStart(void * data, const char * el, const char ** attr)
33 static_cast<SERVCONF *>(data)->Start(el, attr);
36 void ElementEnd(void * data, const char * el)
38 static_cast<SERVCONF *>(data)->End(el);
41 } // namespace anonymous
43 bool AnsRecv(void * data, const std::string & chunk, bool final)
45 SERVCONF * sc = static_cast<SERVCONF *>(data);
47 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
49 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
50 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
51 XML_ErrorString(XML_GetErrorCode(sc->parser)));
52 printf("%s\n", sc->errorMsg.c_str());
59 //-----------------------------------------------------------------------------
60 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
61 const std::string & login, const std::string & password)
63 nt( server, port, login, password )
65 parser = XML_ParserCreate(NULL);
66 nt.SetRxCallback(this, AnsRecv);
68 //-----------------------------------------------------------------------------
69 int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
71 parserGetUser.SetCallback(f, data);
72 return Exec("<GetUser login=\"" + login + "\"/>", parserGetUser);
74 //-----------------------------------------------------------------------------
75 int SERVCONF::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data)
77 parserAuthBy.SetCallback(f, data);
78 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parserAuthBy);
80 //-----------------------------------------------------------------------------
81 int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
83 parserGetUsers.SetCallback(f, data);
84 return Exec("<GetUsers/>", parserGetUsers);
86 //-----------------------------------------------------------------------------
87 int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
89 parserServerInfo.SetCallback(f, data);
90 return Exec("<GetServerInfo/>", parserServerInfo);
92 //-----------------------------------------------------------------------------
93 int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
95 parserChgUser.SetCallback(f, data);
96 return Exec(request, parserChgUser);
98 //-----------------------------------------------------------------------------
99 int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
101 parserSendMessage.SetCallback(f, data);
102 return Exec(request, parserSendMessage);
104 //-----------------------------------------------------------------------------
105 int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
107 parserCheckUser.SetCallback(f, data);
108 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parserCheckUser);
110 //-----------------------------------------------------------------------------
111 void SERVCONF::Start(const char * el, const char ** attr)
113 currParser->ParseStart(el, attr);
115 //-----------------------------------------------------------------------------
116 void SERVCONF::End(const char * el)
118 currParser->ParseEnd(el);
120 //-----------------------------------------------------------------------------
121 const std::string & SERVCONF::GetStrError() const
125 //-----------------------------------------------------------------------------
126 int SERVCONF::Exec(const std::string & request, PARSER & cp)
130 XML_ParserReset(parser, NULL);
131 XML_SetElementHandler(parser, ElementStart, ElementEnd);
132 XML_SetUserData(parser, this);
135 if ((ret = nt.Connect()) != st_ok)
137 errorMsg = nt.GetError();
140 if ((ret = nt.Transact(request.c_str())) != st_ok)
142 errorMsg = nt.GetError();
145 if ((ret = nt.Disconnect()) != st_ok)
147 errorMsg = nt.GetError();