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 void ElementStart(void * data, const char * el, const char ** attr)
33 SERVCONF * sc = static_cast<SERVCONF *>(data);
37 void ElementEnd(void * data, const char * el)
39 SERVCONF * sc = static_cast<SERVCONF *>(data);
43 } // namespace anonymous
45 bool AnsRecv(void * data, const std::string & chunk, bool final)
47 SERVCONF * sc = static_cast<SERVCONF *>(data);
49 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
51 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
52 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
53 XML_ErrorString(XML_GetErrorCode(sc->parser)));
54 printf("%s\n", sc->errorMsg.c_str());
61 //-----------------------------------------------------------------------------
62 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
63 const std::string & login, const std::string & password)
65 nt( server, port, login, password )
67 parser = XML_ParserCreate(NULL);
68 nt.SetRxCallback(this, AnsRecv);
70 //-----------------------------------------------------------------------------
71 int SERVCONF::GetUser(const char * l)
74 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
76 return Exec(request, parserGetUser);
78 //-----------------------------------------------------------------------------
79 int SERVCONF::AuthBy(const char * l)
82 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
84 return Exec(request, parserAuthBy);
86 //-----------------------------------------------------------------------------
87 int SERVCONF::GetUsers()
89 char request[] = "<GetUsers/>";
91 return Exec(request, parserGetUsers);
93 //-----------------------------------------------------------------------------
94 int SERVCONF::ServerInfo()
96 char request[] = "<GetServerInfo/>";
98 return Exec(request, parserServerInfo);
100 //-----------------------------------------------------------------------------
101 int SERVCONF::ChgUser(const char * request)
103 return Exec(request, parserChgUser);
105 //-----------------------------------------------------------------------------
106 int SERVCONF::SendMessage(const char * request)
108 return Exec(request, parserSendMessage);
110 //-----------------------------------------------------------------------------
111 int SERVCONF::CheckUser(const char * login, const char * password)
114 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
116 return Exec(request, parserCheckUser);
118 //-----------------------------------------------------------------------------
119 int SERVCONF::Start(const char * el, const char ** attr)
121 currParser->ParseStart(el, attr);
124 //-----------------------------------------------------------------------------
125 void SERVCONF::End(const char * el)
127 currParser->ParseEnd(el);
129 //-----------------------------------------------------------------------------
130 void SERVCONF::SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data)
132 parserGetUsers.SetCallback(f, data);
134 //-----------------------------------------------------------------------------
135 void SERVCONF::SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data)
137 parserGetUser.SetCallback(f, data);
139 //-----------------------------------------------------------------------------
140 void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
142 parserAuthBy.SetCallback(f, data);
144 //-----------------------------------------------------------------------------
145 void SERVCONF::SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data)
147 parserServerInfo.SetCallback(f, data);
149 //-----------------------------------------------------------------------------
150 void SERVCONF::SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data)
152 parserChgUser.SetCallback(f, data);
154 //-----------------------------------------------------------------------------
155 void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data)
157 parserCheckUser.SetCallback(f, data);
159 //-----------------------------------------------------------------------------
160 void SERVCONF::SetSendMessageCallback(PARSER_SEND_MESSAGE::CALLBACK f, void * data)
162 parserSendMessage.SetCallback(f, data);
164 //-----------------------------------------------------------------------------
165 const std::string & SERVCONF::GetStrError() const
169 //-----------------------------------------------------------------------------
170 int SERVCONF::Exec(const char * request, PARSER & cp)
174 XML_ParserReset(parser, NULL);
175 XML_SetElementHandler(parser, ElementStart, ElementEnd);
176 XML_SetUserData(parser, this);
179 if ((ret = nt.Connect()) != st_ok)
181 errorMsg = nt.GetError();
184 if ((ret = nt.Transact(request)) != st_ok)
186 errorMsg = nt.GetError();
189 if ((ret = nt.Disconnect()) != st_ok)
191 errorMsg = nt.GetError();