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 //-----------------------------------------------------------------------------
32 void ElementStart(void *data, const char *el, const char **attr)
34 SERVCONF * sc = static_cast<SERVCONF *>(data);
37 //-----------------------------------------------------------------------------
38 void ElementEnd(void * data, const char * el)
40 SERVCONF * sc = static_cast<SERVCONF *>(data);
44 } // namespace anonymous
46 bool AnsRecv(void * data, const std::string & chunk, bool final)
48 SERVCONF * sc = static_cast<SERVCONF *>(data);
49 printf("Chunk: '%s', length: %d, final: %d\n", chunk.c_str(), chunk.length(), final);
51 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
53 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
54 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
55 XML_ErrorString(XML_GetErrorCode(sc->parser)));
56 printf("%s\n", sc->errorMsg.c_str());
63 //-----------------------------------------------------------------------------
64 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
65 const std::string & login, const std::string & password)
67 nt( server, port, login, password )
69 parser = XML_ParserCreate(NULL);
70 nt.SetRxCallback(this, AnsRecv);
72 //-----------------------------------------------------------------------------
73 int SERVCONF::GetUser(const char * l)
76 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
78 currParser = &parserGetUser;
82 //-----------------------------------------------------------------------------
83 int SERVCONF::AuthBy(const char * l)
86 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
88 currParser = &parserAuthBy;
92 //-----------------------------------------------------------------------------
93 int SERVCONF::GetUsers()
95 char request[] = "<GetUsers/>";
97 currParser = &parserGetUsers;
101 //-----------------------------------------------------------------------------
102 int SERVCONF::ServerInfo()
104 char request[] = "<GetServerInfo/>";
106 currParser = &parserServerInfo;
108 return Exec(request);
110 //-----------------------------------------------------------------------------
111 int SERVCONF::ChgUser(const char * request)
113 currParser = &parserChgUser;
115 return Exec(request);
117 //-----------------------------------------------------------------------------
118 int SERVCONF::SendMessage(const char * request)
120 currParser = &parserSendMessage;
122 return Exec(request);
124 //-----------------------------------------------------------------------------
125 int SERVCONF::CheckUser(const char * login, const char * password)
128 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
130 currParser = &parserCheckUser;
132 return Exec(request);
134 //-----------------------------------------------------------------------------
135 int SERVCONF::Start(const char * el, const char ** attr)
137 currParser->ParseStart(el, attr);
140 //-----------------------------------------------------------------------------
141 void SERVCONF::End(const char * el)
143 currParser->ParseEnd(el);
145 //-----------------------------------------------------------------------------
146 void SERVCONF::SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data)
148 parserGetUsers.SetCallback(f, data);
150 //-----------------------------------------------------------------------------
151 void SERVCONF::SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data)
153 parserGetUser.SetCallback(f, data);
155 //-----------------------------------------------------------------------------
156 void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
158 parserAuthBy.SetCallback(f, data);
160 //-----------------------------------------------------------------------------
161 void SERVCONF::SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data)
163 parserServerInfo.SetCallback(f, data);
165 //-----------------------------------------------------------------------------
166 void SERVCONF::SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data)
168 parserChgUser.SetCallback(f, data);
170 //-----------------------------------------------------------------------------
171 void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data)
173 parserCheckUser.SetCallback(f, data);
175 //-----------------------------------------------------------------------------
176 void SERVCONF::SetSendMessageCallback(PARSER_SEND_MESSAGE::CALLBACK f, void * data)
178 parserSendMessage.SetCallback(f, data);
180 //-----------------------------------------------------------------------------
181 const std::string & SERVCONF::GetStrError() const
185 //-----------------------------------------------------------------------------
186 int SERVCONF::Exec(const char * request)
188 XML_ParserReset(parser, NULL);
189 XML_SetElementHandler(parser, ElementStart, ElementEnd);
190 XML_SetUserData(parser, this);
193 if ((ret = nt.Connect()) != st_ok)
195 errorMsg = nt.GetError();
198 if ((ret = nt.Transact(request)) != st_ok)
200 errorMsg = nt.GetError();
203 if ((ret = nt.Disconnect()) != st_ok)
205 errorMsg = nt.GetError();