X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/9dc2984179d989f8774ea7ff6d72c81d376b5a2c..fe97f14f9caa463e2bc6b81124ce5e9553bc924d:/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp diff --git a/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp b/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp index eb8fbcde..5a030989 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/configproto.cpp @@ -14,194 +14,284 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* - * Date: 27.10.2002 - */ - /* * Author : Boris Mikhailenko - */ - - /* - $Revision: 1.22 $ - $Date: 2010/10/04 20:24:14 $ - $Author: faust $ + * Author : Maxim Mamontov */ #include "configproto.h" -#include "parser.h" -#include "parser_auth_by.h" +#include "conn.h" + +#include "parser_server_info.h" +#include "parser_admins.h" +#include "parser_tariffs.h" +#include "parser_users.h" +#include "parser_message.h" #include "parser_user_info.h" +#include "parser_auth_by.h" -#include "stg/users.h" -#include "stg/admins.h" -#include "stg/tariffs.h" -#include "stg/logger.h" #include "stg/common.h" +#include "stg/logger.h" -#include +#include +#include +#include +#include +#include +#include -//----------------------------------------------------------------------------- -void ParseXMLStart(void *data, const char *el, const char **attr) +#include +#include +#include +#include + +namespace SP = STG::PARSER; + +namespace { -CONFIGPROTO * cp = static_cast(data); -if (cp->currParser) +struct IsFinished : public std::unary_function +{ + result_type operator()(const argument_type & arg) { - //cp->currParser->SetCurrAdmin(*cp->currAdmin); - cp->currParser->Start(data, el, attr); + return (arg->IsDone() && !arg->IsKeepAlive()) || !arg->IsOk(); } -else +}; + +struct RemoveConn : public std::unary_function +{ + result_type operator()(const argument_type & arg) { - for (size_t i = 0; i < cp->dataParser.size(); i++) - { - //cp->dataParser[i]->SetCurrAdmin(*cp->currAdmin); - //cp->dataParser[i]->Reset(); - if (cp->dataParser[i]->Start(data, el, attr) == 0) - { - cp->currParser = cp->dataParser[i]; - break; - } - else - { - //cp->dataParser[i]->Reset(); - } - } + delete arg; } +}; + } -//----------------------------------------------------------------------------- -void ParseXMLEnd(void *data, const char *el) + +CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l) + : m_settings(NULL), + m_admins(NULL), + m_tariffs(NULL), + m_users(NULL), + m_port(0), + m_running(false), + m_stopped(true), + m_logger(l), + m_listenSocket(-1) +{ +} + +CONFIGPROTO::~CONFIGPROTO() { -CONFIGPROTO * cp = static_cast(data); -if (cp->currParser) + std::for_each(m_conns.begin(), m_conns.end(), RemoveConn()); +} + +int CONFIGPROTO::Prepare() +{ + sigset_t sigmask, oldmask; + sigemptyset(&sigmask); + sigaddset(&sigmask, SIGINT); + sigaddset(&sigmask, SIGTERM); + sigaddset(&sigmask, SIGUSR1); + sigaddset(&sigmask, SIGHUP); + pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask); + + m_listenSocket = socket(PF_INET, SOCK_STREAM, 0); + + if (m_listenSocket < 0) { - if (cp->currParser->End(data, el) == 0) - { - cp->dataAnswer = cp->currParser->GetAnswer(); - cp->currParser = NULL; - } + m_errorStr = std::string("Cannot create listen socket: '") + strerror(errno) + "'."; + m_logger(m_errorStr); + return -1; } -else + + struct sockaddr_in listenAddr; + listenAddr.sin_family = PF_INET; + listenAddr.sin_port = htons(m_port); + listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); // TODO: arbitrary address + + int dummy = 1; + + if (setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &dummy, 4) != 0) { - for (size_t i = 0; i < cp->dataParser.size(); i++) - { - if (cp->dataParser[i]->End(data, el) == 0) - { - cp->dataAnswer = cp->currParser->GetAnswer(); - cp->currParser = NULL; - break; - } - } + m_errorStr = std::string("Failed to set SO_REUSEADDR to the listen socket: '") + strerror(errno) + "'."; + m_logger(m_errorStr); + return -1; + } + + if (bind(m_listenSocket, reinterpret_cast(&listenAddr), sizeof(listenAddr)) == -1) + { + m_errorStr = std::string("Cannot bind listen socket: '") + strerror(errno) + "'."; + m_logger(m_errorStr); + return -1; + } + + if (listen(m_listenSocket, 64) == -1) // TODO: backlog length + { + m_errorStr = std::string("Failed to start listening for connections: '") + strerror(errno) + "'."; + m_logger(m_errorStr); + return -1; } + + RegisterParsers(); + + m_running = true; + m_stopped = false; + return 0; } -//----------------------------------------------------------------------------- -CONFIGPROTO::CONFIGPROTO(PLUGIN_LOGGER & l) - : adminIP(0), - port(0), - nonstop(true), - state(0), - currAdmin(NULL), - logger(l), - listenSocket(-1), - admins(NULL), - currParser(NULL) + +int CONFIGPROTO::Stop() { -/*dataParser.push_back(new PARSER_GET_SERVER_INFO); - -dataParser.push_back(new PARSER_GET_USERS); -dataParser.push_back(new PARSER_GET_USER); -dataParser.push_back(new PARSER_CHG_USER); -dataParser.push_back(new PARSER_ADD_USER); -dataParser.push_back(new PARSER_DEL_USER); -dataParser.push_back(new PARSER_CHECK_USER); -dataParser.push_back(new PARSER_SEND_MESSAGE); -dataParser.push_back(new PARSER_AUTH_BY); -dataParser.push_back(new PARSER_USER_INFO); - -dataParser.push_back(new PARSER_GET_TARIFFS); -dataParser.push_back(new PARSER_ADD_TARIFF); -dataParser.push_back(new PARSER_DEL_TARIFF); -dataParser.push_back(new PARSER_CHG_TARIFF); - -dataParser.push_back(new PARSER_GET_ADMINS); -dataParser.push_back(new PARSER_CHG_ADMIN); -dataParser.push_back(new PARSER_DEL_ADMIN); -dataParser.push_back(new PARSER_ADD_ADMIN);*/ - -xmlParser = XML_ParserCreate(NULL); - -if (!xmlParser) + m_running = false; + for (int i = 0; i < 5 && !m_stopped; ++i) { - logger("Couldn't allocate memory for parser."); - exit(1); + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } + if (!m_stopped) + { + m_errorStr = "Cannot stop listenign thread."; + m_logger(m_errorStr); + return -1; + } + + shutdown(m_listenSocket, SHUT_RDWR); + close(m_listenSocket); + return 0; } -//----------------------------------------------------------------------------- -CONFIGPROTO::~CONFIGPROTO() + +void CONFIGPROTO::Run() { -for (size_t i = 0; i < dataParser.size(); ++i) - delete dataParser[i]; -XML_ParserFree(xmlParser); + while (m_running) + { + fd_set fds; + + BuildFDSet(fds); + + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 500000; + + int res = select(MaxFD() + 1, &fds, NULL, NULL, &tv); + if (res < 0) + { + m_errorStr = std::string("'select' is failed: '") + strerror(errno) + "'."; + m_logger(m_errorStr); + break; + } + if (!m_running) + break; + if (res > 0) + HandleEvents(fds); + + CleanupConns(); + } + m_stopped = true; } -//----------------------------------------------------------------------------- -int CONFIGPROTO::ParseCommand() + +void CONFIGPROTO::RegisterParsers() { -std::list::iterator n; -int done = 0; -char str[9]; + assert(m_settings != NULL); + assert(m_store != NULL); + assert(m_admins != NULL); + assert(m_users != NULL); + assert(m_tariffs != NULL); -if (requestList.empty()) - return 0; + SP::GET_SERVER_INFO::FACTORY::Register(m_registry, *m_settings, *m_users, *m_tariffs); -n = requestList.begin(); + SP::GET_ADMINS::FACTORY::Register(m_registry, *m_admins); + SP::ADD_ADMIN::FACTORY::Register(m_registry, *m_admins); + SP::DEL_ADMIN::FACTORY::Register(m_registry, *m_admins); + SP::CHG_ADMIN::FACTORY::Register(m_registry, *m_admins); -strncpy(str, (*n).c_str(), 8); -str[8] = 0; + SP::GET_TARIFFS::FACTORY::Register(m_registry, *m_tariffs); + SP::ADD_TARIFF::FACTORY::Register(m_registry, *m_tariffs); + SP::DEL_TARIFF::FACTORY::Register(m_registry, *m_tariffs, *m_users); + SP::CHG_TARIFF::FACTORY::Register(m_registry, *m_tariffs); -XML_ParserReset(xmlParser, NULL); -XML_SetElementHandler(xmlParser, ParseXMLStart, ParseXMLEnd); -XML_SetUserData(xmlParser, this); + SP::GET_USERS::FACTORY::Register(m_registry, *m_users); + SP::GET_USER::FACTORY::Register(m_registry, *m_users); + SP::ADD_USER::FACTORY::Register(m_registry, *m_users); + SP::DEL_USER::FACTORY::Register(m_registry, *m_users); + SP::CHG_USER::FACTORY::Register(m_registry, *m_users, *m_store, *m_tariffs); + SP::CHECK_USER::FACTORY::Register(m_registry, *m_users); -while(nonstop) - { - strncpy(str, (*n).c_str(), 8); - str[8] = 0; - size_t len = strlen(str); + SP::SEND_MESSAGE::FACTORY::Register(m_registry, *m_users); - ++n; - if (n == requestList.end()) - done = 1; - --n; + SP::AUTH_BY::FACTORY::Register(m_registry, *m_users); - if (XML_Parse(xmlParser, (*n).c_str(), static_cast(len), done) == XML_STATUS_ERROR) - { - logger("Invalid configuration request"); - printfd(__FILE__, "Parse error at line %d:\n%s\n", - XML_GetCurrentLineNumber(xmlParser), - XML_ErrorString(XML_GetErrorCode(xmlParser))); - if (currParser) - { - printfd(__FILE__, "Parser reset\n"); - //currParser->Reset(); - currParser = NULL; - } + SP::USER_INFO::FACTORY::Register(m_registry, *m_users); +} - return -1; - } +int CONFIGPROTO::MaxFD() const +{ + int maxFD = m_listenSocket; + for (size_t i = 0; i < m_conns.size(); ++i) + if (maxFD < m_conns[i]->Sock()) + maxFD = m_conns[i]->Sock(); + return maxFD; +} + +void CONFIGPROTO::BuildFDSet(fd_set & fds) const +{ + for (size_t i = 0; i < m_conns.size(); ++i) + FD_SET(m_conns[i]->Sock(), &fds); +} + +void CONFIGPROTO::CleanupConns() +{ + std::vector::iterator pos; + pos = std::remove_if(m_conns.begin(), m_conns.end(), IsFinished()); + if (pos == m_conns.end()) + return; + std::for_each(pos, m_conns.end(), RemoveConn()); + m_conns.erase(pos, m_conns.end()); +} + +void CONFIGPROTO::HandleEvents(const fd_set & fds) +{ + if (FD_ISSET(m_listenSocket, &fds)) + AcceptConnection(); + else + { + for (size_t i = 0; i < m_conns.size(); ++i) + if (FD_ISSET(m_conns[i]->Sock(), &fds)) + m_conns[i]->Read(); + } +} - if (done) - return 0; +void CONFIGPROTO::AcceptConnection() +{ + struct sockaddr_in outerAddr; + socklen_t outerAddrLen(sizeof(outerAddr)); + int sock = accept(m_listenSocket, reinterpret_cast(&outerAddr), &outerAddrLen); - ++n; + if (sock < 0) + { + m_errorStr = std::string("Failed to accept connection: '") + strerror(errno) + "'."; + printfd(__FILE__, "%s", m_errorStr.c_str()); + m_logger(m_errorStr); + return; } -return 0; + assert(m_admins != NULL); + + try + { + m_conns.push_back(new STG::Conn(m_registry, *m_admins, sock, outerAddr)); + printfd(__FILE__, "New connection from %s:%d\n", inet_ntostring(m_conns.back()->IP()).c_str(), m_conns.back()->Port()); + } + catch (const STG::Conn::Error & error) + { + // Unlikely. + m_logger(std::string("Failed to create new client connection: '") + error.what() + "'."); + } } -//----------------------------------------------------------------------------- -void CONFIGPROTO::SetAdmins(ADMINS * a) +/* +void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip) { -admins = a; + m_logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str()); } -//----------------------------------------------------------------------------- +*/