From: Maxim Mamontov Date: Tue, 23 Sep 2014 15:17:37 +0000 (+0300) Subject: Solved some TODOs. X-Git-Tag: 2.409~254 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/3cc4710818c810ca4e899cfd3d52f091815017d2?ds=sidebyside Solved some TODOs. Conflicts: projects/stargazer/plugins/configuration/sgconfig/parser_user_info.cpp projects/stargazer/plugins/configuration/sgconfig/parser_user_info.h --- diff --git a/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp b/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp index d9f87eca..fc164900 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp @@ -158,10 +158,7 @@ void CONFIGPROTO::Run() if (!m_running) break; if (res > 0) - { - printfd(__FILE__, "Something happend - received %d events.\n", res); HandleEvents(fds); - } CleanupConns(); } @@ -248,7 +245,6 @@ int CONFIGPROTO::MaxFD() const void CONFIGPROTO::BuildFDSet(fd_set & fds) const { - printfd(__FILE__, "Building fd set for %d connections.\n", m_conns.size()); FD_ZERO(&fds); FD_SET(m_listenSocket, &fds); std::deque::const_iterator it; @@ -258,8 +254,6 @@ void CONFIGPROTO::BuildFDSet(fd_set & fds) const void CONFIGPROTO::CleanupConns() { - size_t old = m_conns.size(); - std::deque::iterator pos; for (pos = m_conns.begin(); pos != m_conns.end(); ++pos) if (((*pos)->IsDone() && !(*pos)->IsKeepAlive()) || !(*pos)->IsOk()) @@ -270,9 +264,6 @@ void CONFIGPROTO::CleanupConns() pos = std::remove(m_conns.begin(), m_conns.end(), static_cast(NULL)); m_conns.erase(pos, m_conns.end()); - - if (m_conns.size() < old) - printfd(__FILE__, "Cleaned up %d connections.\n", old - m_conns.size()); } void CONFIGPROTO::HandleEvents(const fd_set & fds) @@ -284,17 +275,12 @@ void CONFIGPROTO::HandleEvents(const fd_set & fds) std::deque::iterator it; for (it = m_conns.begin(); it != m_conns.end(); ++it) if (FD_ISSET((*it)->Sock(), &fds)) - { - printfd(__FILE__, "Reading data from %s:%d.\n", inet_ntostring((*it)->IP()).c_str(), (*it)->Port()); (*it)->Read(); - } } } void CONFIGPROTO::AcceptConnection() { - printfd(__FILE__, "Accepting new connection.\n"); - struct sockaddr_in outerAddr; socklen_t outerAddrLen(sizeof(outerAddr)); int sock = accept(m_listenSocket, reinterpret_cast(&outerAddr), &outerAddrLen); @@ -311,7 +297,7 @@ void CONFIGPROTO::AcceptConnection() try { - m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr)); + m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr, m_logger)); printfd(__FILE__, "New connection from %s:%d. Total connections: %d\n", inet_ntostring(m_conns.back()->IP()).c_str(), m_conns.back()->Port(), m_conns.size()); } catch (const STG::Conn::Error & error) @@ -320,9 +306,3 @@ void CONFIGPROTO::AcceptConnection() m_logger(std::string("Failed to create new client connection: '") + error.what() + "'."); } } -/* -void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip) -{ - m_logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str()); -} -*/ diff --git a/projects/stargazer/plugins/configuration/sgconfig/conn.cpp b/projects/stargazer/plugins/configuration/sgconfig/conn.cpp index abcc677b..8bba49be 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/conn.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/conn.cpp @@ -22,6 +22,7 @@ #include "stg/admins.h" #include "stg/admin.h" +#include "stg/logger.h" #include "stg/blowfish.h" #include "stg/bfstream.h" #include "stg/common.h" @@ -44,7 +45,8 @@ const char Conn::OK_LOGINS[] = "OKLS"; const char Conn::ERR_LOGINS[] = "ERLS"; Conn::Conn(const BASE_PARSER::REGISTRY & registry, - ADMINS & admins, int sock, const sockaddr_in& addr) + ADMINS & admins, int sock, const sockaddr_in& addr, + PLUGIN_LOGGER & logger) : m_registry(registry), m_admins(admins), m_admin(NULL), @@ -57,6 +59,7 @@ Conn::Conn(const BASE_PARSER::REGISTRY & registry, m_buffer(m_header), m_bufferSize(sizeof(m_header)), m_stream(NULL), + m_logger(logger), m_dataState(false, *this) { if (m_xmlParser == NULL) @@ -80,16 +83,14 @@ bool Conn::Read() ssize_t res = read(m_sock, m_buffer, m_bufferSize); if (res < 0) { - printfd(__FILE__, "Failed to read data from %s:%d: '%s'.\n", inet_ntostring(IP()).c_str(), Port(), strerror(errno)); m_state = ERROR; - // TODO: log it + Log(__FILE__, "Failed to read data from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Reason: '" + strerror(errno) + "'"); return false; } if (res == 0 && m_state != DATA) // EOF is ok for data. { - printfd(__FILE__, "Failed to read data from %s:%d: 'EOF'.\n", inet_ntostring(IP()).c_str(), Port()); m_state = ERROR; - // TODO: log it + Log(__FILE__, "Failed to read data from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Unexpected EOF."); return false; } m_bufferSize -= res; @@ -101,7 +102,8 @@ bool Conn::WriteAnswer(const void* buffer, size_t size) ssize_t res = write(m_sock, buffer, size); if (res < 0) { - // TODO: log it + m_state = ERROR; + Log(__FILE__, "Failed to write data to " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Reason: '" + strerror(errno) + "'."); return false; } return true; @@ -109,7 +111,7 @@ bool Conn::WriteAnswer(const void* buffer, size_t size) BASE_PARSER * Conn::GetParser(const std::string & tag) const { - BASE_PARSER::REGISTRY::const_iterator it = m_registry.find(tag); + BASE_PARSER::REGISTRY::const_iterator it = m_registry.find(ToLower(tag)); if (it == m_registry.end()) return NULL; return it->second->create(*m_admin); @@ -117,7 +119,6 @@ BASE_PARSER * Conn::GetParser(const std::string & tag) const bool Conn::HandleBuffer(size_t size) { - printfd(__FILE__, "Got %d bytes. State: %s.\n", size, (m_state == DATA ? "DATA" : (m_state == HEADER ? "HEADER" : (m_state == LOGIN ? "LOGIN" : (m_state == CRYPTO_LOGIN ? "CRYPTO_LOGIN" : (m_state == DONE ? "DONE" : "ERROR")))))); if (m_state == DATA) return HandleData(size); @@ -139,9 +140,8 @@ bool Conn::HandleHeader() { if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0) { - printfd(__FILE__, "Wrong header from %s:%d.\n", inet_ntostring(IP()).c_str(), Port()); + Log(__FILE__, "Received invalid header from " + inet_ntostring(IP()) + ":" + x2str(Port()) + "."); WriteAnswer(ERR_HEADER, sizeof(ERR_HEADER) - 1); // Without \0 - // TODO: log it m_state = ERROR; return false; } @@ -155,9 +155,9 @@ bool Conn::HandleLogin() { if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error. { - printfd(__FILE__, "Wrong login ('%s') from %s:%d.\n", m_login, inet_ntostring(IP()).c_str(), Port()); + std::string login(m_login, strnlen(m_login, sizeof(m_login))); + Log(__FILE__, "Received invalid login '" + ToPrintable(login) + "' from " + inet_ntostring(IP()) + ":" + x2str(Port()) + "."); WriteAnswer(ERR_LOGIN, sizeof(ERR_LOGIN) - 1); // Without \0 - // TODO: log it m_state = ERROR; return false; } @@ -177,9 +177,8 @@ bool Conn::HandleCryptoLogin() if (strncmp(m_login, login, sizeof(login)) != 0) { - printfd(__FILE__, "Wrong password from %s:%d: '%s' != '%s'.\n", inet_ntostring(IP()).c_str(), Port(), login, m_login); + Log(__FILE__, "Attempt to connect with wrong password from " + m_admin->GetLogin() + "@" + inet_ntostring(IP()) + ":" + x2str(Port()) + "."); WriteAnswer(ERR_LOGINS, sizeof(ERR_LOGINS) - 1); // Without \0 - // TODO: log it m_state = ERROR; return false; } @@ -209,7 +208,7 @@ bool Conn::DataCallback(const void * block, size_t size, void * data) if (XML_Parse(state.conn.m_xmlParser, xml, length, state.final) == XML_STATUS_ERROR) { - // TODO: log it + state.conn.Log(__FILE__, "Received invalid XML from " + state.conn.m_admin->GetLogin() + "@" + inet_ntostring(state.conn.IP()) + ":" + x2str(state.conn.Port()) + "."); printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d\n", static_cast(XML_GetCurrentLineNumber(state.conn.m_xmlParser)), static_cast(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)), @@ -223,7 +222,7 @@ bool Conn::DataCallback(const void * block, size_t size, void * data) { if (!state.conn.WriteResponse()) { - // TODO: log it + state.conn.Log(__FILE__, "Failed to write response to " + state.conn.m_admin->GetLogin() + "@" + inet_ntostring(state.conn.IP()) + ":" + x2str(state.conn.Port()) + "."); state.conn.m_state = ERROR; return false; } @@ -243,13 +242,10 @@ void Conn::ParseXMLStart(void * data, const char * el, const char ** attr) if (conn.m_parser == NULL) { - printfd(__FILE__, "Failed to find a suitable parser for '%s'.\n", el); - // TODO: log it + conn.Log(__FILE__, "Received unknown command '" + std::string(el) + "' from " + conn.m_admin->GetLogin() + "@" + inet_ntostring(conn.IP()) + ":" + x2str(conn.Port()) + "."); conn.m_state = ERROR; return; } - else - printfd(__FILE__, "Using parser '%s'.\n", conn.m_parser->GetTag().c_str()); conn.m_parser->Start(data, el, attr); } @@ -261,7 +257,7 @@ void Conn::ParseXMLEnd(void * data, const char * el) if (conn.m_parser == NULL) { - // TODO: log it + // No need to log it. conn.m_state = ERROR; return; } @@ -277,7 +273,7 @@ bool Conn::WriteResponse() answer = m_parser->GetAnswer(); else answer = ""; - printfd(__FILE__, "Writing %d bytes of answer: '%s'\n", answer.length(), answer.c_str()); + printfd(__FILE__, "Writing %d bytes of answer.\n", answer.length()); stream.Put(answer.c_str(), answer.length() + 1 /* including \0 */, true /* final */); return stream.IsOk(); } @@ -288,3 +284,9 @@ bool Conn::WriteCallback(const void * block, size_t size, void * data) Conn & conn = *static_cast(data); return WriteAll(conn.m_sock, block, size);; } + +void Conn::Log(const char * file, const std::string & message) +{ + printfd(file, "%s\n", message.c_str()); + m_logger(message); +} diff --git a/projects/stargazer/plugins/configuration/sgconfig/conn.h b/projects/stargazer/plugins/configuration/sgconfig/conn.h index 8f76b7e9..e3cb3f0c 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/conn.h +++ b/projects/stargazer/plugins/configuration/sgconfig/conn.h @@ -39,6 +39,7 @@ class USERS; class TARIFFS; class ADMIN; class BASE_PARSER; +class PLUGIN_LOGGER; namespace STG { @@ -54,7 +55,8 @@ class Conn }; Conn(const BASE_PARSER::REGISTRY & registry, - ADMINS & admins, int sock, const sockaddr_in& addr); + ADMINS & admins, int sock, const sockaddr_in& addr, + PLUGIN_LOGGER & logger); ~Conn(); int Sock() const { return m_sock; } @@ -100,6 +102,7 @@ class Conn char m_cryptoLogin[ADM_LOGIN_LEN]; // Without \0 char m_data[1024]; STG::DECRYPT_STREAM * m_stream; + PLUGIN_LOGGER & m_logger; BASE_PARSER * GetParser(const std::string & tag) const; @@ -113,6 +116,8 @@ class Conn bool WriteAnswer(const void* buffer, size_t size); bool WriteResponse(); + void Log(const char * file, const std::string & message); + struct DataState { DataState(bool f, Conn & c) : final(f), conn(c) {} diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp index 740d86a7..0db867bb 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp @@ -22,7 +22,6 @@ #include "parser_admins.h" #include "stg/admins.h" -#include "stg/common.h" #include // strcasecmp diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_admins.h b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.h index c68789c2..3b99e642 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_admins.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.h @@ -24,6 +24,7 @@ #include "parser.h" +#include "stg/common.h" #include "stg/resetable.h" #include @@ -45,7 +46,7 @@ class GET_ADMINS: public BASE_PARSER FACTORY(const ADMINS & admins) : m_admins(admins) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_ADMINS(admin, m_admins); } static void Register(REGISTRY & registry, const ADMINS & admins) - { registry[tag] = new FACTORY(admins); } + { registry[ToLower(tag)] = new FACTORY(admins); } private: const ADMINS & m_admins; }; @@ -70,7 +71,7 @@ class ADD_ADMIN: public BASE_PARSER FACTORY(ADMINS & admins) : m_admins(admins) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new ADD_ADMIN(admin, m_admins); } static void Register(REGISTRY & registry, ADMINS & admins) - { registry[tag] = new FACTORY(admins); } + { registry[ToLower(tag)] = new FACTORY(admins); } private: ADMINS & m_admins; }; @@ -97,7 +98,7 @@ class DEL_ADMIN: public BASE_PARSER FACTORY(ADMINS & admins) : m_admins(admins) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new DEL_ADMIN(admin, m_admins); } static void Register(REGISTRY & registry, ADMINS & admins) - { registry[tag] = new FACTORY(admins); } + { registry[ToLower(tag)] = new FACTORY(admins); } private: ADMINS & m_admins; }; @@ -124,7 +125,7 @@ class CHG_ADMIN: public BASE_PARSER FACTORY(ADMINS & admins) : m_admins(admins) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new CHG_ADMIN(admin, m_admins); } static void Register(REGISTRY & registry, ADMINS & admins) - { registry[tag] = new FACTORY(admins); } + { registry[ToLower(tag)] = new FACTORY(admins); } private: ADMINS & m_admins; }; diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h b/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h index d36ca1c4..9a2e81a8 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h @@ -23,6 +23,8 @@ #include "parser.h" +#include "stg/common.h" + #include class ADMIN; @@ -42,7 +44,7 @@ class AUTH_BY : public BASE_PARSER FACTORY(const USERS & users) : m_users(users) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new AUTH_BY(admin, m_users); } static void Register(REGISTRY & registry, const USERS & users) - { registry[tag] = new FACTORY(users); } + { registry[ToLower(tag)] = new FACTORY(users); } private: const USERS & m_users; }; diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp index c93ac437..06489a42 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp @@ -22,7 +22,6 @@ #include "parser_message.h" #include "stg/users.h" -#include "stg/common.h" extern volatile time_t stgTime; // So sad... diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_message.h b/projects/stargazer/plugins/configuration/sgconfig/parser_message.h index 6a5f8f4e..4caa354c 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_message.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_message.h @@ -25,6 +25,7 @@ #include "parser.h" #include "stg/message.h" +#include "stg/common.h" #include #include @@ -46,7 +47,7 @@ class SEND_MESSAGE: public BASE_PARSER FACTORY(USERS & users) : m_users(users) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new SEND_MESSAGE(admin, m_users); } static void Register(REGISTRY & registry, USERS & users) - { registry[tag] = new FACTORY(users); } + { registry[ToLower(tag)] = new FACTORY(users); } private: USERS & m_users; }; diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp index 0cb215d0..1873b9bb 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp @@ -24,7 +24,6 @@ #include "stg/settings.h" #include "stg/users.h" #include "stg/tariffs.h" -#include "stg/common.h" #include "stg/version.h" #include "stg/const.h" diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h b/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h index 1b76659f..094caf38 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h @@ -24,6 +24,8 @@ #include "parser.h" +#include "stg/common.h" + class ADMIN; class SETTINGS; class USERS; @@ -43,7 +45,7 @@ class GET_SERVER_INFO: public BASE_PARSER { : m_settings(settings), m_users(users), m_tariffs(tariffs) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_SERVER_INFO(admin, m_settings, m_users, m_tariffs); } static void Register(REGISTRY & registry, const SETTINGS & settings, const USERS & users, const TARIFFS & tariffs) - { registry[tag] = new FACTORY(settings, users, tariffs); } + { registry[ToLower(tag)] = new FACTORY(settings, users, tariffs); } private: const SETTINGS & m_settings; const USERS & m_users; diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp index 4763e97f..cc32b6fc 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp @@ -23,7 +23,6 @@ #include "stg/tariffs.h" #include "stg/users.h" -#include "stg/common.h" #include "stg/resetable.h" #include // snprintf diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h index 5dad5eff..6425cca1 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h @@ -25,6 +25,7 @@ #include "parser.h" #include "stg/tariff_conf.h" +#include "stg/common.h" #include @@ -46,7 +47,7 @@ class GET_TARIFFS: public BASE_PARSER FACTORY(const TARIFFS & tariffs) : m_tariffs(tariffs) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_TARIFFS(admin, m_tariffs); } static void Register(REGISTRY & registry, const TARIFFS & tariffs) - { registry[tag] = new FACTORY(tariffs); } + { registry[ToLower(tag)] = new FACTORY(tariffs); } private: const TARIFFS & m_tariffs; }; @@ -71,7 +72,7 @@ class ADD_TARIFF: public BASE_PARSER FACTORY(TARIFFS & tariffs) : m_tariffs(tariffs) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new ADD_TARIFF(admin, m_tariffs); } static void Register(REGISTRY & registry, TARIFFS & tariffs) - { registry[tag] = new FACTORY(tariffs); } + { registry[ToLower(tag)] = new FACTORY(tariffs); } private: TARIFFS & m_tariffs; }; @@ -98,7 +99,7 @@ class DEL_TARIFF: public BASE_PARSER FACTORY(TARIFFS & tariffs, const USERS & users) : m_tariffs(tariffs), m_users(users) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new DEL_TARIFF(admin, m_users, m_tariffs); } static void Register(REGISTRY & registry, TARIFFS & tariffs, const USERS & users) - { registry[tag] = new FACTORY(tariffs, users); } + { registry[ToLower(tag)] = new FACTORY(tariffs, users); } private: TARIFFS & m_tariffs; const USERS & m_users; @@ -127,7 +128,7 @@ class CHG_TARIFF: public BASE_PARSER FACTORY(TARIFFS & tariffs) : m_tariffs(tariffs) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new CHG_TARIFF(admin, m_tariffs); } static void Register(REGISTRY & registry, TARIFFS & tariffs) - { registry[tag] = new FACTORY(tariffs); } + { registry[ToLower(tag)] = new FACTORY(tariffs); } private: TARIFFS & m_tariffs; }; diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp index f596bfbf..cd66ec9e 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp @@ -26,7 +26,6 @@ #include "stg/user_property.h" #include "stg/user_conf.h" #include "stg/user_stat.h" -#include "stg/common.h" #include #include diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_users.h b/projects/stargazer/plugins/configuration/sgconfig/parser_users.h index 4d03b7e8..0ef1cb95 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_users.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_users.h @@ -26,6 +26,7 @@ #include "stg/user_conf.h" #include "stg/user_stat.h" +#include "stg/common.h" #include "stg/resetable.h" #include @@ -50,7 +51,7 @@ class GET_USERS: public BASE_PARSER FACTORY(USERS & users) : m_users(users) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_USERS(admin, m_users); } static void Register(REGISTRY & registry, USERS & users) - { registry[tag] = new FACTORY(users); } + { registry[ToLower(tag)] = new FACTORY(users); } private: USERS & m_users; }; @@ -78,7 +79,7 @@ class GET_USER: public BASE_PARSER FACTORY(const USERS & users) : m_users(users) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_USER(admin, m_users); } static void Register(REGISTRY & registry, const USERS & users) - { registry[tag] = new FACTORY(users); } + { registry[ToLower(tag)] = new FACTORY(users); } private: const USERS & m_users; }; @@ -105,7 +106,7 @@ class ADD_USER: public BASE_PARSER FACTORY(USERS & users) : m_users(users) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new ADD_USER(admin, m_users); } static void Register(REGISTRY & registry, USERS & users) - { registry[tag] = new FACTORY(users); } + { registry[ToLower(tag)] = new FACTORY(users); } private: USERS & m_users; }; @@ -134,7 +135,7 @@ class CHG_USER: public BASE_PARSER {} virtual BASE_PARSER * create(const ADMIN & admin) { return new CHG_USER(admin, m_users, m_store, m_tariffs); } static void Register(REGISTRY & registry, USERS & users, STORE & store, const TARIFFS & tariffs) - { registry[tag] = new FACTORY(users, store, tariffs); } + { registry[ToLower(tag)] = new FACTORY(users, store, tariffs); } private: USERS & m_users; STORE & m_store; @@ -180,7 +181,7 @@ class DEL_USER: public BASE_PARSER FACTORY(USERS & users) : m_users(users) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new DEL_USER(admin, m_users); } static void Register(REGISTRY & registry, USERS & users) - { registry[tag] = new FACTORY(users); } + { registry[ToLower(tag)] = new FACTORY(users); } private: USERS & m_users; }; @@ -209,7 +210,7 @@ class CHECK_USER: public BASE_PARSER FACTORY(const USERS & users) : m_users(users) {} virtual BASE_PARSER * create(const ADMIN & admin) { return new CHECK_USER(admin, m_users); } static void Register(REGISTRY & registry, const USERS & users) - { registry[tag] = new FACTORY(users); } + { registry[ToLower(tag)] = new FACTORY(users); } private: const USERS & m_users; }; diff --git a/stglibs/common.lib/common.cpp b/stglibs/common.lib/common.cpp index 5bd66c87..1068cfa1 100644 --- a/stglibs/common.lib/common.cpp +++ b/stglibs/common.lib/common.cpp @@ -1047,3 +1047,16 @@ while (done < size) } return true; } + +std::string ToPrintable(const std::string & src) +{ + std::string dest; + + for (size_t i = 0; i < src.size(); ++i) + if (std::isprint(src[i])) + dest += src[i]; + else + dest += "\\" + x2str(src[i]); + + return dest; +} diff --git a/stglibs/common.lib/include/stg/common.h b/stglibs/common.lib/include/stg/common.h index 0791f093..fe3b3af2 100644 --- a/stglibs/common.lib/include/stg/common.h +++ b/stglibs/common.lib/include/stg/common.h @@ -142,6 +142,8 @@ bool WaitPackets(int sd); bool ReadAll(int sd, void * dest, size_t size); bool WriteAll(int sd, const void * source, size_t size); +std::string ToPrintable(const std::string & src); + //----------------------------------------------------------------------------- int str2x(const std::string & str, int32_t & x); int str2x(const std::string & str, uint32_t & x);