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"
27 #include "parsers/get_container.h"
29 #include "parsers/server_info.h"
31 #include "parsers/get_admin.h"
32 #include "parsers/chg_admin.h"
34 #include "parsers/get_tariff.h"
35 #include "parsers/chg_tariff.h"
37 #include "parsers/auth_by.h"
38 #include "parsers/get_user.h"
39 #include "parsers/chg_user.h"
41 #include "parsers/get_service.h"
42 #include "parsers/chg_service.h"
44 #include "parsers/get_corp.h"
45 #include "parsers/chg_corp.h"
47 #include "parsers/base.h"
49 #include "stg/common.h"
61 IMPL(const std::string & server, uint16_t port,
62 const std::string & login, const std::string & password);
63 IMPL(const std::string & server, uint16_t port,
64 const std::string & localAddress, uint16_t localPort,
65 const std::string & login, const std::string & password);
66 ~IMPL() { XML_ParserFree(parser); }
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)
77 P cp(callback, data, encoding);
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, encoding);
85 return ExecImpl(request, cp);
95 static bool ParserRecv(const std::string & chunk, bool final, void * data);
96 static bool SimpleRecv(const std::string & chunk, bool final, void * data);
97 int ExecImpl(const std::string & request, PARSER & cp);
100 bool SERVCONF::IMPL::ParserRecv(const std::string & chunk, bool final, void * data)
102 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
104 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
106 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
107 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
108 XML_ErrorString(XML_GetErrorCode(sc->parser)));
109 printf("%s\n", sc->errorMsg.c_str());
116 bool SERVCONF::IMPL::SimpleRecv(const std::string & chunk, bool /*final*/, void * data)
118 *static_cast<std::string *>(data) += chunk;
122 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
123 const std::string & login, const std::string & password)
124 : pImpl(new IMPL(server, port, login, password))
128 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
129 const std::string & localAddress, uint16_t localPort,
130 const std::string & login, const std::string & password)
131 : pImpl(new IMPL(server, port, localAddress, localPort, login, password))
135 SERVCONF::~SERVCONF()
140 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
142 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
145 int SERVCONF::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data)
147 return pImpl->RawXML(request, f, data);
152 int SERVCONF::GetAdmins(GET_CONTAINER::CALLBACK<GET_ADMIN::INFO>::TYPE f, void * data)
154 return pImpl->Exec<GET_CONTAINER::PARSER<GET_ADMIN::PARSER> >("admins", "<GetAdmins/>", f, data);
157 int SERVCONF::GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data)
159 return pImpl->Exec<GET_ADMIN::PARSER>("<GetAdmin login=\"" + login + "\"/>", f, data);
162 int SERVCONF::ChgAdmin(const ADMIN_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
164 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf, encoding) + "/>", f, data);
167 int SERVCONF::AddAdmin(const std::string & login,
168 const ADMIN_CONF_RES & conf,
169 SIMPLE::CALLBACK f, void * data)
171 int res = pImpl->Exec<SIMPLE::PARSER>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
174 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf, encoding) + "/>", f, data);
177 int SERVCONF::DelAdmin(const std::string & login, SIMPLE::CALLBACK f, void * data)
179 return pImpl->Exec<SIMPLE::PARSER>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
184 int SERVCONF::GetTariffs(GET_CONTAINER::CALLBACK<GET_TARIFF::INFO>::TYPE f, void * data)
186 return pImpl->Exec<GET_CONTAINER::PARSER<GET_TARIFF::PARSER> >("tariffs", "<GetTariffs/>", f, data);
189 int SERVCONF::GetTariff(const std::string & name, GET_TARIFF::CALLBACK f, void * data)
191 return pImpl->Exec<GET_TARIFF::PARSER>("<GetTariff name=\"" + name + "\"/>", f, data);
194 int SERVCONF::ChgTariff(const TARIFF_DATA_RES & tariffData, SIMPLE::CALLBACK f, void * data)
196 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + tariffData.tariffConf.name.data() + "\">" + CHG_TARIFF::Serialize(tariffData, encoding) + "</SetTariff>", f, data);
199 int SERVCONF::AddTariff(const std::string & name,
200 const TARIFF_DATA_RES & tariffData,
201 SIMPLE::CALLBACK f, void * data)
203 int res = pImpl->Exec<SIMPLE::PARSER>("AddTariff", "<AddTariff name=\"" + name + "\"/>", f, data);
206 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + name + "\">" + CHG_TARIFF::Serialize(tariffData, encoding) + "</SetTariff>", f, data);
209 int SERVCONF::DelTariff(const std::string & name, SIMPLE::CALLBACK f, void * data)
211 return pImpl->Exec<SIMPLE::PARSER>("DelTariff", "<DelTariff name=\"" + name + "\"/>", f, data);
216 int SERVCONF::GetUsers(GET_CONTAINER::CALLBACK<GET_USER::INFO>::TYPE f, void * data)
218 return pImpl->Exec<GET_CONTAINER::PARSER<GET_USER::PARSER> >("users", "<GetUsers/>", f, data);
221 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
223 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
226 int SERVCONF::ChgUser(const std::string & login,
227 const USER_CONF_RES & conf,
228 const USER_STAT_RES & stat,
229 SIMPLE::CALLBACK f, void * data)
231 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat, encoding) + "</SetUser>", f, data);
234 int SERVCONF::DelUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
236 return pImpl->Exec<SIMPLE::PARSER>("DelUser", "<DelUser login=\"" + login + "\"/>", f, data);
239 int SERVCONF::AddUser(const std::string & login,
240 const USER_CONF_RES & conf,
241 const USER_STAT_RES & stat,
242 SIMPLE::CALLBACK f, void * data)
244 int res = pImpl->Exec<SIMPLE::PARSER>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
247 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat, encoding) + "</SetUser>", f, data);
250 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
252 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
255 int SERVCONF::SendMessage(const std::string & login, const std::string & text, SIMPLE::CALLBACK f, void * data)
257 return pImpl->Exec<SIMPLE::PARSER>("SendMessageResult", "<Message login=\"" + login + "\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"" + Encode12str(text) + "\"/>", f, data);
260 int SERVCONF::CheckUser(const std::string & login, const std::string & password, SIMPLE::CALLBACK f, void * data)
262 return pImpl->Exec<SIMPLE::PARSER>("CheckUser", "<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
267 int SERVCONF::GetServices(GET_CONTAINER::CALLBACK<GET_SERVICE::INFO>::TYPE f, void * data)
269 return pImpl->Exec<GET_CONTAINER::PARSER<GET_SERVICE::PARSER> >("services", "<GetServices/>", f, data);
272 int SERVCONF::GetService(const std::string & name, GET_SERVICE::CALLBACK f, void * data)
274 return pImpl->Exec<GET_SERVICE::PARSER>("<GetService name=\"" + name + "\"/>", f, data);
277 int SERVCONF::ChgService(const SERVICE_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
279 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService name=\"" + conf.name.data() + "\">" + CHG_SERVICE::Serialize(conf, encoding) + "</SetService>", f, data);
282 int SERVCONF::AddService(const std::string & name,
283 const SERVICE_CONF_RES & conf,
284 SIMPLE::CALLBACK f, void * data)
286 int res = pImpl->Exec<SIMPLE::PARSER>("AddService", "<AddService name=\"" + name + "\"/>", f, data);
289 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService name=\"" + name + "\">" + CHG_SERVICE::Serialize(conf, encoding) + "</SetService>", f, data);
292 int SERVCONF::DelService(const std::string & name, SIMPLE::CALLBACK f, void * data)
294 return pImpl->Exec<SIMPLE::PARSER>("DelService", "<DelService name=\"" + name + "\"/>", f, data);
297 // -- Corporations --
299 int SERVCONF::GetCorporations(GET_CONTAINER::CALLBACK<GET_CORP::INFO>::TYPE f, void * data)
301 return pImpl->Exec<GET_CONTAINER::PARSER<GET_CORP::PARSER> >("corporations", "<GetCorporations/>", f, data);
304 int SERVCONF::GetCorp(const std::string & name, GET_CORP::CALLBACK f, void * data)
306 return pImpl->Exec<GET_CORP::PARSER>("<GetCorp name=\"" + name + "\"/>", f, data);
309 int SERVCONF::ChgCorp(const CORP_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
311 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + conf.name.data() + "\">" + CHG_CORP::Serialize(conf, encoding) + "</SetCorp>", f, data);
314 int SERVCONF::AddCorp(const std::string & name,
315 const CORP_CONF_RES & conf,
316 SIMPLE::CALLBACK f, void * data)
318 int res = pImpl->Exec<SIMPLE::PARSER>("AddCorp", "<AddCorp name=\"" + name + "\"/>", f, data);
321 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + name + "\">" + CHG_CORP::Serialize(conf, encoding) + "</SetCorp>", f, data);
324 int SERVCONF::DelCorp(const std::string & name, SIMPLE::CALLBACK f, void * data)
326 return pImpl->Exec<SIMPLE::PARSER>("DelCorp", "<DelCorp name=\"" + name + "\"/>", f, data);
329 const std::string & SERVCONF::GetStrError() const
331 return pImpl->GetStrError();
334 //-----------------------------------------------------------------------------
335 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
336 const std::string & login, const std::string & password)
337 : nt(server, port, login, password)
339 setlocale(LC_ALL, "");
340 encoding = nl_langinfo(CODESET);
341 parser = XML_ParserCreate(NULL);
343 //-----------------------------------------------------------------------------
344 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
345 const std::string & localAddress, uint16_t localPort,
346 const std::string & login, const std::string & password)
347 : nt(server, port, localAddress, localPort, login, password)
349 setlocale(LC_ALL, "");
350 encoding = nl_langinfo(CODESET);
351 parser = XML_ParserCreate(NULL);
353 //-----------------------------------------------------------------------------
354 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
356 PARSER * currParser = static_cast<PARSER *>(data);
357 currParser->ParseStart(el, attr);
359 //-----------------------------------------------------------------------------
360 void SERVCONF::IMPL::End(void * data, const char * el)
362 PARSER * currParser = static_cast<PARSER *>(data);
363 currParser->ParseEnd(el);
365 //-----------------------------------------------------------------------------
366 const std::string & SERVCONF::IMPL::GetStrError() const
370 //-----------------------------------------------------------------------------
371 int SERVCONF::IMPL::ExecImpl(const std::string & request, PARSER & cp)
373 XML_ParserReset(parser, NULL);
374 XML_SetElementHandler(parser, Start, End);
375 XML_SetUserData(parser, &cp);
378 if ((ret = nt.Connect()) != st_ok)
380 errorMsg = nt.GetError();
381 cp.Failure(errorMsg);
384 if ((ret = nt.Transact(request, ParserRecv, this)) != st_ok)
386 errorMsg = nt.GetError();
387 cp.Failure(errorMsg);
395 int SERVCONF::IMPL::RawXML(const std::string & request, RAW_XML::CALLBACK callback, void * data)
398 if ((ret = nt.Connect()) != st_ok)
400 errorMsg = nt.GetError();
401 callback(false, errorMsg, "", data);
404 std::string response;
405 if ((ret = nt.Transact(request, SimpleRecv, &response)) != st_ok)
407 errorMsg = nt.GetError();
408 callback(false, errorMsg, "", data);
413 callback(true, "", response, data);