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 "parser_auth_by.h"
25 #include "parser_server_info.h"
27 #include "stg/common.h"
39 IMPL(const std::string & server, uint16_t port,
40 const std::string & login, const std::string & password);
42 int GetUsers(PARSER_GET_USERS::CALLBACK f, void * data);
43 int GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data);
44 int ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data);
45 int AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data);
46 int SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data);
47 int ServerInfo(SERVER_INFO::CALLBACK f, void * data);
48 int CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data);
50 const std::string & GetStrError() const;
51 static void Start(void * data, const char * el, const char ** attr);
52 static void End(void * data, const char * el);
55 PARSER_GET_USERS parserGetUsers;
56 PARSER_GET_USER parserGetUser;
57 AUTH_BY::PARSER parserAuthBy;
58 SERVER_INFO::PARSER parserServerInfo;
59 PARSER_CHG_USER parserChgUser;
60 PARSER_CHECK_USER parserCheckUser;
61 PARSER_SEND_MESSAGE parserSendMessage;
68 int Exec(const std::string & request, PARSER & cp);
70 static bool AnsRecv(void * data, const std::string & chunk, bool final);
73 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
75 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
77 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
79 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
80 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
81 XML_ErrorString(XML_GetErrorCode(sc->parser)));
82 printf("%s\n", sc->errorMsg.c_str());
89 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
90 const std::string & login, const std::string & password)
91 : pImpl(new IMPL(server, port, login, password))
100 int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
102 return pImpl->GetUsers( f, data );
105 int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
107 return pImpl->GetUser(login, f, data);
110 int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
112 return pImpl->ChgUser(request, f, data);
115 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
117 return pImpl->AuthBy(login, f, data);
120 int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
122 return pImpl->SendMessage(request, f, data);
125 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
127 return pImpl->ServerInfo(f, data);
130 int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
132 return pImpl->CheckUser(login, password, f, data);
135 const std::string & SERVCONF::GetStrError() const
137 return pImpl->GetStrError();
140 //-----------------------------------------------------------------------------
141 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
142 const std::string & login, const std::string & password)
143 : nt( server, port, login, password )
145 parser = XML_ParserCreate(NULL);
146 nt.SetRxCallback(this, AnsRecv);
148 //-----------------------------------------------------------------------------
149 int SERVCONF::IMPL::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
151 parserGetUser.SetCallback(f, data);
152 return Exec("<GetUser login=\"" + login + "\"/>", parserGetUser);
154 //-----------------------------------------------------------------------------
155 int SERVCONF::IMPL::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
157 parserAuthBy.SetCallback(f, data);
158 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parserAuthBy);
160 //-----------------------------------------------------------------------------
161 int SERVCONF::IMPL::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
163 parserGetUsers.SetCallback(f, data);
164 return Exec("<GetUsers/>", parserGetUsers);
166 //-----------------------------------------------------------------------------
167 int SERVCONF::IMPL::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
169 parserServerInfo.SetCallback(f, data);
170 return Exec("<GetServerInfo/>", parserServerInfo);
172 //-----------------------------------------------------------------------------
173 int SERVCONF::IMPL::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
175 parserChgUser.SetCallback(f, data);
176 return Exec(request, parserChgUser);
178 //-----------------------------------------------------------------------------
179 int SERVCONF::IMPL::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
181 parserSendMessage.SetCallback(f, data);
182 return Exec(request, parserSendMessage);
184 //-----------------------------------------------------------------------------
185 int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
187 parserCheckUser.SetCallback(f, data);
188 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parserCheckUser);
190 //-----------------------------------------------------------------------------
191 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
193 PARSER * currParser = static_cast<PARSER *>(data);
194 currParser->ParseStart(el, attr);
196 //-----------------------------------------------------------------------------
197 void SERVCONF::IMPL::End(void * data, const char * el)
199 PARSER * currParser = static_cast<PARSER *>(data);
200 currParser->ParseEnd(el);
202 //-----------------------------------------------------------------------------
203 const std::string & SERVCONF::IMPL::GetStrError() const
207 //-----------------------------------------------------------------------------
208 int SERVCONF::IMPL::Exec(const std::string & request, PARSER & cp)
210 XML_ParserReset(parser, NULL);
211 XML_SetElementHandler(parser, Start, End);
212 XML_SetUserData(parser, &cp);
215 if ((ret = nt.Connect()) != st_ok)
217 errorMsg = nt.GetError();
220 if ((ret = nt.Transact(request.c_str())) != st_ok)
222 errorMsg = nt.GetError();
225 if ((ret = nt.Disconnect()) != st_ok)
227 errorMsg = nt.GetError();