]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/configproto.h
Use std::jthread and C++17.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / configproto.h
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  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #pragma once
23
24 #include "parser.h"
25
26 #include "stg/module_settings.h"
27
28 #include <string>
29 #include <deque>
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wshadow"
32 #include <jthread.hpp>
33 #pragma GCC diagnostic pop
34 #include <cstdint>
35
36 #include <sys/select.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39
40 namespace STG
41 {
42
43 struct Settings;
44 struct Admins;
45 struct Tariffs;
46 struct Users;
47 struct Services;
48 struct Corporations;
49 struct Store;
50 class PluginLogger;
51
52 class Conn;
53
54 }
55
56 class CONFIGPROTO {
57 public:
58     explicit CONFIGPROTO(STG::PluginLogger & l);
59     ~CONFIGPROTO();
60
61     void SetPort(uint16_t port) { m_port = port; }
62     void SetBindAddress(const std::string & address) { m_bindAddress = address; }
63     void SetSettings(const STG::Settings * settings) { m_settings = settings; }
64     void SetAdmins(STG::Admins * admins) { m_admins = admins; }
65     void SetTariffs(STG::Tariffs * tariffs) { m_tariffs = tariffs; }
66     void SetUsers(STG::Users * users) { m_users = users; }
67     void SetStore(STG::Store * store) { m_store = store; }
68     void SetServices(STG::Services * services) { m_services = services; }
69     void SetCorporations(STG::Corporations * corporations) { m_corporations = corporations; }
70
71     int Prepare();
72     int Stop();
73     const std::string & GetStrError() const { return m_errorStr; }
74     void Run(std::stop_token token);
75
76 private:
77     CONFIGPROTO(const CONFIGPROTO & rvalue);
78     CONFIGPROTO & operator=(const CONFIGPROTO & rvalue);
79
80     const STG::Settings * m_settings;
81     STG::Admins *         m_admins;
82     STG::Tariffs *        m_tariffs;
83     STG::Users *          m_users;
84     STG::Services *       m_services;
85     STG::Corporations *   m_corporations;
86     STG::Store *          m_store;
87
88     uint16_t         m_port;
89     std::string      m_bindAddress;
90     bool             m_stopped;
91     STG::PluginLogger &   m_logger;
92     int              m_listenSocket;
93
94     std::string      m_errorStr;
95
96     BASE_PARSER::REGISTRY m_registry;
97     std::deque<STG::Conn *> m_conns;
98
99     bool Bind();
100
101     void RegisterParsers();
102
103     int MaxFD() const;
104     void BuildFDSet(fd_set & fds) const;
105     void CleanupConns();
106     void HandleEvents(const fd_set & fds);
107     void AcceptConnection();
108 };