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"
24 #include "stg/servconf_types.h"
26 #include "stg/common.h"
36 IMPL(const std::string & server, uint16_t port,
37 const std::string & login, const std::string & password);
39 int GetUsers(PARSER_GET_USERS::CALLBACK f, void * data);
40 int GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data);
41 int ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data);
42 int AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data);
43 int SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data);
44 int ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data);
45 int CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data);
47 const std::string & GetStrError() const;
48 static void Start(void * data, const char * el, const char ** attr);
49 static void End(void * data, const char * el);
52 PARSER_GET_USERS parserGetUsers;
53 PARSER_GET_USER parserGetUser;
54 PARSER_AUTH_BY parserAuthBy;
55 PARSER_SERVER_INFO parserServerInfo;
56 PARSER_CHG_USER parserChgUser;
57 PARSER_CHECK_USER parserCheckUser;
58 PARSER_SEND_MESSAGE parserSendMessage;
65 int Exec(const std::string & request, PARSER & cp);
67 static bool AnsRecv(void * data, const std::string & chunk, bool final);
73 void ElementStart(void * data, const char * el, const char ** attr)
75 static_cast<SERVCONF::IMPL *>(data)->Start(el, attr);
78 void ElementEnd(void * data, const char * el)
80 static_cast<SERVCONF::IMPL *>(data)->End(el);
83 } // namespace anonymous
86 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
88 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
90 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
92 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
93 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
94 XML_ErrorString(XML_GetErrorCode(sc->parser)));
95 printf("%s\n", sc->errorMsg.c_str());
102 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
103 const std::string & login, const std::string & password)
104 : pImpl(new IMPL(server, port, login, password))
108 SERVCONF::~SERVCONF()
113 int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
115 return pImpl->GetUsers( f, data );
118 int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
120 return pImpl->GetUser(login, f, data);
123 int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
125 return pImpl->ChgUser(request, f, data);
128 int SERVCONF::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data)
130 return pImpl->AuthBy(login, f, data);
133 int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
135 return pImpl->SendMessage(request, f, data);
138 int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
140 return pImpl->ServerInfo(f, data);
143 int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
145 return pImpl->CheckUser(login, password, f, data);
148 const std::string & SERVCONF::GetStrError() const
150 return pImpl->GetStrError();
153 //-----------------------------------------------------------------------------
154 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
155 const std::string & login, const std::string & password)
156 : nt( server, port, login, password )
158 parser = XML_ParserCreate(NULL);
159 nt.SetRxCallback(this, AnsRecv);
161 //-----------------------------------------------------------------------------
162 int SERVCONF::IMPL::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
164 parserGetUser.SetCallback(f, data);
165 return Exec("<GetUser login=\"" + login + "\"/>", parserGetUser);
167 //-----------------------------------------------------------------------------
168 int SERVCONF::IMPL::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data)
170 parserAuthBy.SetCallback(f, data);
171 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parserAuthBy);
173 //-----------------------------------------------------------------------------
174 int SERVCONF::IMPL::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
176 parserGetUsers.SetCallback(f, data);
177 return Exec("<GetUsers/>", parserGetUsers);
179 //-----------------------------------------------------------------------------
180 int SERVCONF::IMPL::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
182 parserServerInfo.SetCallback(f, data);
183 return Exec("<GetServerInfo/>", parserServerInfo);
185 //-----------------------------------------------------------------------------
186 int SERVCONF::IMPL::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
188 parserChgUser.SetCallback(f, data);
189 return Exec(request, parserChgUser);
191 //-----------------------------------------------------------------------------
192 int SERVCONF::IMPL::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
194 parserSendMessage.SetCallback(f, data);
195 return Exec(request, parserSendMessage);
197 //-----------------------------------------------------------------------------
198 int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
200 parserCheckUser.SetCallback(f, data);
201 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parserCheckUser);
203 //-----------------------------------------------------------------------------
204 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
206 PARSER * currParser = static_cast<PARSER *>(data);
207 currParser->ParseStart(el, attr);
209 //-----------------------------------------------------------------------------
210 void SERVCONF::IMPL::End(void * data, const char * el)
212 PARSER * currParser = static_cast<PARSER *>(data);
213 currParser->ParseEnd(el);
215 //-----------------------------------------------------------------------------
216 const std::string & SERVCONF::IMPL::GetStrError() const
220 //-----------------------------------------------------------------------------
221 int SERVCONF::IMPL::Exec(const std::string & request, PARSER & cp)
223 XML_ParserReset(parser, NULL);
224 XML_SetElementHandler(parser, Start, End);
225 XML_SetUserData(parser, &cp);
228 if ((ret = nt.Connect()) != st_ok)
230 errorMsg = nt.GetError();
233 if ((ret = nt.Transact(request.c_str())) != st_ok)
235 errorMsg = nt.GetError();
238 if ((ret = nt.Disconnect()) != st_ok)
240 errorMsg = nt.GetError();