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"
 
  26 #include "stg/common.h"
 
  38     IMPL(const std::string & server, uint16_t port,
 
  39          const std::string & login, const std::string & password);
 
  41     int GetUsers(PARSER_GET_USERS::CALLBACK f, void * data);
 
  42     int GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data);
 
  43     int ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data);
 
  44     int AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data);
 
  45     int SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data);
 
  46     int ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data);
 
  47     int CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data);
 
  49     const std::string & GetStrError() const;
 
  50     static void Start(void * data, const char * el, const char ** attr);
 
  51     static void End(void * data, const char * el);
 
  54     PARSER_GET_USERS parserGetUsers;
 
  55     PARSER_GET_USER parserGetUser;
 
  56     AUTH_BY::PARSER parserAuthBy;
 
  57     PARSER_SERVER_INFO  parserServerInfo;
 
  58     PARSER_CHG_USER parserChgUser;
 
  59     PARSER_CHECK_USER parserCheckUser;
 
  60     PARSER_SEND_MESSAGE parserSendMessage;
 
  67     int Exec(const std::string & request, PARSER & cp);
 
  69     static bool AnsRecv(void * data, const std::string & chunk, bool final);
 
  72 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
 
  74 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
 
  76 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
 
  78     strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
 
  79               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
 
  80               XML_ErrorString(XML_GetErrorCode(sc->parser)));
 
  81     printf("%s\n", sc->errorMsg.c_str());
 
  88 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
 
  89                    const std::string & login, const std::string & password)
 
  90     : pImpl(new IMPL(server, port, login, password))
 
  99 int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
 
 101     return pImpl->GetUsers( f, data );
 
 104 int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
 
 106     return pImpl->GetUser(login, f, data);
 
 109 int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
 
 111     return pImpl->ChgUser(request, f, data);
 
 114 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
 
 116     return pImpl->AuthBy(login, f, data);
 
 119 int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
 
 121     return pImpl->SendMessage(request, f, data);
 
 124 int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
 
 126     return pImpl->ServerInfo(f, data);
 
 129 int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
 
 131     return pImpl->CheckUser(login, password, f, data);
 
 134 const std::string & SERVCONF::GetStrError() const
 
 136     return pImpl->GetStrError();
 
 139 //-----------------------------------------------------------------------------
 
 140 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
 
 141                      const std::string & login, const std::string & password)
 
 142     : nt( server, port, login, password )
 
 144 parser = XML_ParserCreate(NULL);
 
 145 nt.SetRxCallback(this, AnsRecv);
 
 147 //-----------------------------------------------------------------------------
 
 148 int SERVCONF::IMPL::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
 
 150 parserGetUser.SetCallback(f, data);
 
 151 return Exec("<GetUser login=\"" + login + "\"/>", parserGetUser);
 
 153 //-----------------------------------------------------------------------------
 
 154 int SERVCONF::IMPL::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
 
 156 parserAuthBy.SetCallback(f, data);
 
 157 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parserAuthBy);
 
 159 //-----------------------------------------------------------------------------
 
 160 int SERVCONF::IMPL::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
 
 162 parserGetUsers.SetCallback(f, data);
 
 163 return Exec("<GetUsers/>", parserGetUsers);
 
 165 //-----------------------------------------------------------------------------
 
 166 int SERVCONF::IMPL::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
 
 168 parserServerInfo.SetCallback(f, data);
 
 169 return Exec("<GetServerInfo/>", parserServerInfo);
 
 171 //-----------------------------------------------------------------------------
 
 172 int SERVCONF::IMPL::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
 
 174 parserChgUser.SetCallback(f, data);
 
 175 return Exec(request, parserChgUser);
 
 177 //-----------------------------------------------------------------------------
 
 178 int SERVCONF::IMPL::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
 
 180 parserSendMessage.SetCallback(f, data);
 
 181 return Exec(request, parserSendMessage);
 
 183 //-----------------------------------------------------------------------------
 
 184 int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
 
 186 parserCheckUser.SetCallback(f, data);
 
 187 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parserCheckUser);
 
 189 //-----------------------------------------------------------------------------
 
 190 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
 
 192 PARSER * currParser = static_cast<PARSER *>(data);
 
 193 currParser->ParseStart(el, attr);
 
 195 //-----------------------------------------------------------------------------
 
 196 void SERVCONF::IMPL::End(void * data, const char * el)
 
 198 PARSER * currParser = static_cast<PARSER *>(data);
 
 199 currParser->ParseEnd(el);
 
 201 //-----------------------------------------------------------------------------
 
 202 const std::string & SERVCONF::IMPL::GetStrError() const
 
 206 //-----------------------------------------------------------------------------
 
 207 int SERVCONF::IMPL::Exec(const std::string & request, PARSER & cp)
 
 209 XML_ParserReset(parser, NULL);
 
 210 XML_SetElementHandler(parser, Start, End);
 
 211 XML_SetUserData(parser, &cp);
 
 214 if ((ret = nt.Connect()) != st_ok)
 
 216     errorMsg = nt.GetError();
 
 219 if ((ret = nt.Transact(request.c_str())) != st_ok)
 
 221     errorMsg = nt.GetError();
 
 224 if ((ret = nt.Disconnect()) != st_ok)
 
 226     errorMsg = nt.GetError();