]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
[NY Flight] Improvements in parser dispatching.
[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 SERVCONF * sc = static_cast<SERVCONF *>(data);
34 sc->Start(el, attr);
35 }
36
37 void ElementEnd(void * data, const char * el)
38 {
39 SERVCONF * sc = static_cast<SERVCONF *>(data);
40 sc->End(el);
41 }
42
43 } // namespace anonymous
44
45 bool AnsRecv(void * data, const std::string & chunk, bool final)
46 {
47 SERVCONF * sc = static_cast<SERVCONF *>(data);
48
49 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
50     {
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());
55     return false;
56     }
57
58 return true;
59 }
60
61 //-----------------------------------------------------------------------------
62 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
63                    const std::string & login, const std::string & password)
64     : currParser(NULL),
65       nt( server, port, login, password )
66 {
67 parser = XML_ParserCreate(NULL);
68 nt.SetRxCallback(this, AnsRecv);
69 }
70 //-----------------------------------------------------------------------------
71 int SERVCONF::GetUser(const char * l)
72 {
73 char request[255];
74 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
75
76 return Exec(request, parserGetUser);
77 }
78 //-----------------------------------------------------------------------------
79 int SERVCONF::AuthBy(const char * l)
80 {
81 char request[255];
82 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
83
84 return Exec(request, parserAuthBy);
85 }
86 //-----------------------------------------------------------------------------
87 int SERVCONF::GetUsers()
88 {
89 char request[] = "<GetUsers/>";
90
91 return Exec(request, parserGetUsers);
92 }
93 //-----------------------------------------------------------------------------
94 int SERVCONF::ServerInfo()
95 {
96 char request[] = "<GetServerInfo/>";
97
98 return Exec(request, parserServerInfo);
99 }
100 //-----------------------------------------------------------------------------
101 int SERVCONF::ChgUser(const char * request)
102 {
103 return Exec(request, parserChgUser);
104 }
105 //-----------------------------------------------------------------------------
106 int SERVCONF::SendMessage(const char * request)
107 {
108 return Exec(request, parserSendMessage);
109 }
110 //-----------------------------------------------------------------------------
111 int SERVCONF::CheckUser(const char * login, const char * password)
112 {
113 char request[255];
114 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
115
116 return Exec(request, parserCheckUser);
117 }
118 //-----------------------------------------------------------------------------
119 int SERVCONF::Start(const char * el, const char ** attr)
120 {
121 currParser->ParseStart(el, attr);
122 return 0;
123 }
124 //-----------------------------------------------------------------------------
125 void SERVCONF::End(const char * el)
126 {
127 currParser->ParseEnd(el);
128 }
129 //-----------------------------------------------------------------------------
130 void SERVCONF::SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data)
131 {
132 parserGetUsers.SetCallback(f, data);
133 }
134 //-----------------------------------------------------------------------------
135 void SERVCONF::SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data)
136 {
137 parserGetUser.SetCallback(f, data);
138 }
139 //-----------------------------------------------------------------------------
140 void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
141 {
142 parserAuthBy.SetCallback(f, data);
143 }
144 //-----------------------------------------------------------------------------
145 void SERVCONF::SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data)
146 {
147 parserServerInfo.SetCallback(f, data);
148 }
149 //-----------------------------------------------------------------------------
150 void SERVCONF::SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data)
151 {
152 parserChgUser.SetCallback(f, data);
153 }
154 //-----------------------------------------------------------------------------
155 void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data)
156 {
157 parserCheckUser.SetCallback(f, data);
158 }
159 //-----------------------------------------------------------------------------
160 void SERVCONF::SetSendMessageCallback(PARSER_SEND_MESSAGE::CALLBACK f, void * data)
161 {
162 parserSendMessage.SetCallback(f, data);
163 }
164 //-----------------------------------------------------------------------------
165 const std::string & SERVCONF::GetStrError() const
166 {
167 return errorMsg;
168 }
169 //-----------------------------------------------------------------------------
170 int SERVCONF::Exec(const char * request, PARSER & cp)
171 {
172 currParser = &cp;
173
174 XML_ParserReset(parser, NULL);
175 XML_SetElementHandler(parser, ElementStart, ElementEnd);
176 XML_SetUserData(parser, this);
177
178 int ret = 0;
179 if ((ret = nt.Connect()) != st_ok)
180     {
181     errorMsg = nt.GetError();
182     return ret;
183     }
184 if ((ret = nt.Transact(request)) != st_ok)
185     {
186     errorMsg = nt.GetError();
187     return ret;
188     }
189 if ((ret = nt.Disconnect()) != st_ok)
190     {
191     errorMsg = nt.GetError();
192     return ret;
193     }
194
195 return st_ok;
196 }