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>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "stg/servconf.h"
26 #include "parsers/simple.h"
28 #include "parsers/server_info.h"
30 #include "parsers/get_admins.h"
31 #include "parsers/get_admin.h"
32 #include "parsers/chg_admin.h"
34 #include "parsers/get_tariffs.h"
35 #include "parsers/get_tariff.h"
36 #include "parsers/chg_tariff.h"
38 #include "parsers/auth_by.h"
39 #include "parsers/get_users.h"
40 #include "parsers/get_user.h"
41 #include "parsers/chg_user.h"
43 #include "parsers/get_services.h"
44 #include "parsers/get_service.h"
45 #include "parsers/chg_service.h"
47 #include "parsers/get_corporations.h"
48 #include "parsers/get_corp.h"
49 #include "parsers/chg_corp.h"
51 #include "parsers/base.h"
53 #include "stg/common.h"
65 IMPL(const std::string & server, uint16_t port,
66 const std::string & login, const std::string & password);
68 const std::string & GetStrError() const;
69 static void Start(void * data, const char * el, const char ** attr);
70 static void End(void * data, const char * el);
72 int RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data);
74 template <class P, typename C>
75 int Exec(const std::string & request, C callback, void * data)
78 return ExecImpl(request, cp);
81 template <class P, typename C>
82 int Exec(const std::string & tag, const std::string & request, C callback, void * data)
84 P cp(tag, callback, data);
85 return ExecImpl(request, cp);
94 static bool ParserRecv(void * data, const std::string & chunk, bool final);
95 static bool SimpleRecv(void * data, const std::string & chunk, bool final);
96 int ExecImpl(const std::string & request, PARSER & cp);
99 bool SERVCONF::IMPL::ParserRecv(void * data, const std::string & chunk, bool final)
101 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
103 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
105 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
106 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
107 XML_ErrorString(XML_GetErrorCode(sc->parser)));
108 printf("%s\n", sc->errorMsg.c_str());
115 bool SERVCONF::IMPL::SimpleRecv(void * data, const std::string & chunk, bool /*final*/)
117 *static_cast<std::string *>(data) += chunk;
121 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
122 const std::string & login, const std::string & password)
123 : pImpl(new IMPL(server, port, login, password))
127 SERVCONF::~SERVCONF()
132 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
134 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
137 int SERVCONF::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data)
139 return pImpl->RawXML(request, f, data);
144 int SERVCONF::GetAdmins(GET_ADMINS::CALLBACK f, void * data)
146 return pImpl->Exec<GET_ADMINS::PARSER>("<GetAdmins/>", f, data);
149 int SERVCONF::GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data)
151 return pImpl->Exec<GET_ADMIN::PARSER>("<GetAdmin login=\"" + login + "\"/>", f, data);
154 int SERVCONF::ChgAdmin(const ADMIN_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
156 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
159 int SERVCONF::AddAdmin(const std::string & login,
160 const ADMIN_CONF_RES & conf,
161 SIMPLE::CALLBACK f, void * data)
163 int res = pImpl->Exec<SIMPLE::PARSER>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
166 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
169 int SERVCONF::DelAdmin(const std::string & login, SIMPLE::CALLBACK f, void * data)
171 return pImpl->Exec<SIMPLE::PARSER>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
176 int SERVCONF::GetTariffs(GET_TARIFFS::CALLBACK f, void * data)
178 return pImpl->Exec<GET_TARIFFS::PARSER>("<GetTariffs/>", f, data);
181 int SERVCONF::GetTariff(const std::string & name, GET_TARIFF::CALLBACK f, void * data)
183 return pImpl->Exec<GET_TARIFF::PARSER>("<GetTariff name=\"" + name + "\"/>", f, data);
186 int SERVCONF::ChgTariff(const TARIFF_DATA_RES & tariffData, SIMPLE::CALLBACK f, void * data)
188 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + tariffData.tariffConf.name.data() + "\">" + CHG_TARIFF::Serialize(tariffData) + "</SetTariff>", f, data);
191 int SERVCONF::AddTariff(const std::string & name,
192 const TARIFF_DATA_RES & tariffData,
193 SIMPLE::CALLBACK f, void * data)
195 int res = pImpl->Exec<SIMPLE::PARSER>("AddTariff", "<AddTariff name=\"" + name + "\"/>", f, data);
198 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + name + "\">" + CHG_TARIFF::Serialize(tariffData) + "</SetTariff>", f, data);
201 int SERVCONF::DelTariff(const std::string & name, SIMPLE::CALLBACK f, void * data)
203 return pImpl->Exec<SIMPLE::PARSER>("DelTariff", "<DelTariff name=\"" + name + "\"/>", f, data);
208 int SERVCONF::GetUsers(GET_USERS::CALLBACK f, void * data)
210 return pImpl->Exec<GET_USERS::PARSER>("<GetUsers/>", f, data);
213 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
215 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
218 int SERVCONF::ChgUser(const std::string & login,
219 const USER_CONF_RES & conf,
220 const USER_STAT_RES & stat,
221 SIMPLE::CALLBACK f, void * data)
223 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat) + "</SetUser>", f, data);
226 int SERVCONF::DelUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
228 return pImpl->Exec<SIMPLE::PARSER>("DelUser", "<DelUser login=\"" + login + "\"/>", f, data);
231 int SERVCONF::AddUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
233 return pImpl->Exec<SIMPLE::PARSER>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
236 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
238 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
241 int SERVCONF::SendMessage(const std::string & login, const std::string & text, SIMPLE::CALLBACK f, void * data)
243 return pImpl->Exec<SIMPLE::PARSER>("SendMessage", "<Message login=\"" + login + "\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"" + Encode12str(text) + "\"/>", f, data);
246 int SERVCONF::CheckUser(const std::string & login, const std::string & password, SIMPLE::CALLBACK f, void * data)
248 return pImpl->Exec<SIMPLE::PARSER>("CheckUser", "<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
253 int SERVCONF::GetServices(GET_SERVICES::CALLBACK f, void * data)
255 return pImpl->Exec<GET_SERVICES::PARSER>("<GetServices/>", f, data);
258 int SERVCONF::GetService(const std::string & name, GET_SERVICE::CALLBACK f, void * data)
260 return pImpl->Exec<GET_SERVICE::PARSER>("<GetService name=\"" + name + "\"/>", f, data);
263 int SERVCONF::ChgService(const SERVICE_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
265 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService name=\"" + conf.name.data() + "\">" + CHG_SERVICE::Serialize(conf) + "</SetService>", f, data);
268 int SERVCONF::AddService(const std::string & name,
269 const SERVICE_CONF_RES & conf,
270 SIMPLE::CALLBACK f, void * data)
272 int res = pImpl->Exec<SIMPLE::PARSER>("AddService", "<AddService name=\"" + name + "\"/>", f, data);
275 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService name=\"" + name + "\">" + CHG_SERVICE::Serialize(conf) + "</SetService>", f, data);
278 int SERVCONF::DelService(const std::string & name, SIMPLE::CALLBACK f, void * data)
280 return pImpl->Exec<SIMPLE::PARSER>("DelService", "<DelService name=\"" + name + "\"/>", f, data);
283 // -- Corporations --
285 int SERVCONF::GetCorporations(GET_CORPORATIONS::CALLBACK f, void * data)
287 return pImpl->Exec<GET_CORPORATIONS::PARSER>("<GetCorporations/>", f, data);
290 int SERVCONF::GetCorp(const std::string & name, GET_CORP::CALLBACK f, void * data)
292 return pImpl->Exec<GET_CORP::PARSER>("<GetCorp name=\"" + name + "\"/>", f, data);
295 int SERVCONF::ChgCorp(const CORP_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
297 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + conf.name.data() + "\">" + CHG_CORP::Serialize(conf) + "</SetCorp>", f, data);
300 int SERVCONF::AddCorp(const std::string & name,
301 const CORP_CONF_RES & conf,
302 SIMPLE::CALLBACK f, void * data)
304 int res = pImpl->Exec<SIMPLE::PARSER>("AddCorp", "<AddCorp name=\"" + name + "\"/>", f, data);
307 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + name + "\">" + CHG_CORP::Serialize(conf) + "</SetCorp>", f, data);
310 int SERVCONF::DelCorp(const std::string & name, SIMPLE::CALLBACK f, void * data)
312 return pImpl->Exec<SIMPLE::PARSER>("DelCorp", "<DelCorp name=\"" + name + "\"/>", f, data);
315 const std::string & SERVCONF::GetStrError() const
317 return pImpl->GetStrError();
320 //-----------------------------------------------------------------------------
321 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
322 const std::string & login, const std::string & password)
323 : nt( server, port, login, password )
325 parser = XML_ParserCreate(NULL);
326 nt.SetRxCallback(this, ParserRecv);
328 //-----------------------------------------------------------------------------
329 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
331 PARSER * currParser = static_cast<PARSER *>(data);
332 currParser->ParseStart(el, attr);
334 //-----------------------------------------------------------------------------
335 void SERVCONF::IMPL::End(void * data, const char * el)
337 PARSER * currParser = static_cast<PARSER *>(data);
338 currParser->ParseEnd(el);
340 //-----------------------------------------------------------------------------
341 const std::string & SERVCONF::IMPL::GetStrError() const
345 //-----------------------------------------------------------------------------
346 int SERVCONF::IMPL::ExecImpl(const std::string & request, PARSER & cp)
348 XML_ParserReset(parser, NULL);
349 XML_SetElementHandler(parser, Start, End);
350 XML_SetUserData(parser, &cp);
353 if ((ret = nt.Connect()) != st_ok)
355 errorMsg = nt.GetError();
358 if ((ret = nt.Transact(request.c_str())) != st_ok)
360 errorMsg = nt.GetError();
363 if ((ret = nt.Disconnect()) != st_ok)
365 errorMsg = nt.GetError();
372 int SERVCONF::IMPL::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data)
374 std::string response;
375 nt.SetRxCallback(&response, SimpleRecv);
377 if ((ret = nt.Connect()) != st_ok)
379 nt.SetRxCallback(this, ParserRecv);
380 errorMsg = nt.GetError();
381 f(false, errorMsg, "", data);
384 if ((ret = nt.Transact(request.c_str())) != st_ok)
386 nt.SetRxCallback(this, ParserRecv);
387 errorMsg = nt.GetError();
388 f(false, errorMsg, "", data);
391 if ((ret = nt.Disconnect()) != st_ok)
393 nt.SetRxCallback(this, ParserRecv);
394 errorMsg = nt.GetError();
395 f(false, errorMsg, "", data);
398 nt.SetRxCallback(this, ParserRecv);
399 f(true, "", response, data);