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 "parsers/auth_by.h"
25 #include "parsers/server_info.h"
26 #include "parsers/check_user.h"
27 #include "parsers/get_users.h"
28 #include "parsers/get_user.h"
29 #include "parsers/chg_user.h"
30 #include "parsers/send_message.h"
31 #include "parsers/base.h"
33 #include "stg/common.h"
45 IMPL(const std::string & server, uint16_t port,
46 const std::string & login, const std::string & password);
48 const std::string & GetStrError() const;
49 static void Start(void * data, const char * el, const char ** attr);
50 static void End(void * data, const char * el);
52 template <class P, typename C>
53 int Exec(const std::string & request, C callback, void * data);
61 static bool AnsRecv(void * data, const std::string & chunk, bool final);
64 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
66 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
68 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
70 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
71 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
72 XML_ErrorString(XML_GetErrorCode(sc->parser)));
73 printf("%s\n", sc->errorMsg.c_str());
80 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
81 const std::string & login, const std::string & password)
82 : pImpl(new IMPL(server, port, login, password))
91 int SERVCONF::GetUsers(GET_USERS::CALLBACK f, void * data)
93 return pImpl->Exec<GET_USERS::PARSER>("<GetUsers/>", f, data);
96 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
98 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
101 int SERVCONF::ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data)
103 return pImpl->Exec<CHG_USER::PARSER>(request, f, data);
106 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
108 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
111 int SERVCONF::SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data)
113 return pImpl->Exec<SEND_MESSAGE::PARSER>(request, f, data);
116 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
118 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
121 int SERVCONF::CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data)
123 return pImpl->Exec<CHECK_USER::PARSER>("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
126 const std::string & SERVCONF::GetStrError() const
128 return pImpl->GetStrError();
131 //-----------------------------------------------------------------------------
132 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
133 const std::string & login, const std::string & password)
134 : nt( server, port, login, password )
136 parser = XML_ParserCreate(NULL);
137 nt.SetRxCallback(this, AnsRecv);
139 //-----------------------------------------------------------------------------
140 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
142 PARSER * currParser = static_cast<PARSER *>(data);
143 currParser->ParseStart(el, attr);
145 //-----------------------------------------------------------------------------
146 void SERVCONF::IMPL::End(void * data, const char * el)
148 PARSER * currParser = static_cast<PARSER *>(data);
149 currParser->ParseEnd(el);
151 //-----------------------------------------------------------------------------
152 const std::string & SERVCONF::IMPL::GetStrError() const
156 //-----------------------------------------------------------------------------
157 template <class P, typename C>
158 int SERVCONF::IMPL::Exec(const std::string & request, C callback, void * data)
160 P cp(callback, data);
161 XML_ParserReset(parser, NULL);
162 XML_SetElementHandler(parser, Start, End);
163 XML_SetUserData(parser, &cp);
166 if ((ret = nt.Connect()) != st_ok)
168 errorMsg = nt.GetError();
171 if ((ret = nt.Transact(request.c_str())) != st_ok)
173 errorMsg = nt.GetError();
176 if ((ret = nt.Disconnect()) != st_ok)
178 errorMsg = nt.GetError();