]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
[NY] Simplified SERVCONF interface.
[stg.git] / stglibs / srvconf.lib / servconf.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #include "stg/servconf.h"
22
23 #include "stg/common.h"
24
25 #include <cstdio>
26 #include <cstring>
27
28 namespace
29 {
30
31 void ElementStart(void * data, const char * el, const char ** attr)
32 {
33 static_cast<SERVCONF *>(data)->Start(el, attr);
34 }
35
36 void ElementEnd(void * data, const char * el)
37 {
38 static_cast<SERVCONF *>(data)->End(el);
39 }
40
41 } // namespace anonymous
42
43 bool AnsRecv(void * data, const std::string & chunk, bool final)
44 {
45 SERVCONF * sc = static_cast<SERVCONF *>(data);
46
47 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
48     {
49     strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
50               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
51               XML_ErrorString(XML_GetErrorCode(sc->parser)));
52     printf("%s\n", sc->errorMsg.c_str());
53     return false;
54     }
55
56 return true;
57 }
58
59 //-----------------------------------------------------------------------------
60 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
61                    const std::string & login, const std::string & password)
62     : currParser(NULL),
63       nt( server, port, login, password )
64 {
65 parser = XML_ParserCreate(NULL);
66 nt.SetRxCallback(this, AnsRecv);
67 }
68 //-----------------------------------------------------------------------------
69 int SERVCONF::GetUser(const std::string & login, PARSER_GET_USER::CALLBACK f, void * data)
70 {
71 parserGetUser.SetCallback(f, data);
72 return Exec("<GetUser login=\"" + login + "\"/>", parserGetUser);
73 }
74 //-----------------------------------------------------------------------------
75 int SERVCONF::AuthBy(const std::string & login, PARSER_AUTH_BY::CALLBACK f, void * data)
76 {
77 parserAuthBy.SetCallback(f, data);
78 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parserAuthBy);
79 }
80 //-----------------------------------------------------------------------------
81 int SERVCONF::GetUsers(PARSER_GET_USERS::CALLBACK f, void * data)
82 {
83 parserGetUsers.SetCallback(f, data);
84 return Exec("<GetUsers/>", parserGetUsers);
85 }
86 //-----------------------------------------------------------------------------
87 int SERVCONF::ServerInfo(PARSER_SERVER_INFO::CALLBACK f, void * data)
88 {
89 parserServerInfo.SetCallback(f, data);
90 return Exec("<GetServerInfo/>", parserServerInfo);
91 }
92 //-----------------------------------------------------------------------------
93 int SERVCONF::ChgUser(const std::string & request, PARSER_CHG_USER::CALLBACK f, void * data)
94 {
95 parserChgUser.SetCallback(f, data);
96 return Exec(request, parserChgUser);
97 }
98 //-----------------------------------------------------------------------------
99 int SERVCONF::SendMessage(const std::string & request, PARSER_SEND_MESSAGE::CALLBACK f, void * data)
100 {
101 parserSendMessage.SetCallback(f, data);
102 return Exec(request, parserSendMessage);
103 }
104 //-----------------------------------------------------------------------------
105 int SERVCONF::CheckUser(const std::string & login, const std::string & password, PARSER_CHECK_USER::CALLBACK f, void * data)
106 {
107 parserCheckUser.SetCallback(f, data);
108 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parserCheckUser);
109 }
110 //-----------------------------------------------------------------------------
111 void SERVCONF::Start(const char * el, const char ** attr)
112 {
113 currParser->ParseStart(el, attr);
114 }
115 //-----------------------------------------------------------------------------
116 void SERVCONF::End(const char * el)
117 {
118 currParser->ParseEnd(el);
119 }
120 //-----------------------------------------------------------------------------
121 const std::string & SERVCONF::GetStrError() const
122 {
123 return errorMsg;
124 }
125 //-----------------------------------------------------------------------------
126 int SERVCONF::Exec(const std::string & request, PARSER & cp)
127 {
128 currParser = &cp;
129
130 XML_ParserReset(parser, NULL);
131 XML_SetElementHandler(parser, ElementStart, ElementEnd);
132 XML_SetUserData(parser, this);
133
134 int ret = 0;
135 if ((ret = nt.Connect()) != st_ok)
136     {
137     errorMsg = nt.GetError();
138     return ret;
139     }
140 if ((ret = nt.Transact(request.c_str())) != st_ok)
141     {
142     errorMsg = nt.GetError();
143     return ret;
144     }
145 if ((ret = nt.Disconnect()) != st_ok)
146     {
147     errorMsg = nt.GetError();
148     return ret;
149     }
150
151 return st_ok;
152 }