]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h
More jthreads.
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / rpcconfig.h
1 #pragma once
2
3 #include "stg/plugin.h"
4 #include "stg/admin_conf.h"
5 #include "stg/module_settings.h"
6 #include "stg/logger.h"
7
8 #include <xmlrpc-c/base.hpp>
9 #include <xmlrpc-c/registry.hpp>
10 #include <xmlrpc-c/server_abyss.hpp>
11
12 #include <ctime>
13 #include <cstdint>
14 #include <string>
15 #include <map>
16 #include <vector>
17
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wshadow"
20 #include <jthread.hpp>
21 #pragma GCC diagnostic pop
22
23 #define RPC_CONFIG_VERSION "Stargazer RPC v. 0.2"
24
25 namespace STG
26 {
27
28 struct Admins;
29 struct Tariffs;
30 struct Users;
31 struct Store;
32
33 }
34
35 class RPC_CONFIG_SETTINGS
36 {
37 public:
38                          RPC_CONFIG_SETTINGS();
39     virtual              ~RPC_CONFIG_SETTINGS() {}
40     const std::string &  GetStrError() const { return errorStr; }
41     int                  ParseSettings(const STG::ModuleSettings & s);
42     uint16_t             GetPort() const { return port; }
43     double               GetCookieTimeout() const { return cookieTimeout; }
44
45 private:
46     std::string  errorStr;
47     uint16_t     port;
48     double       cookieTimeout;
49 };
50
51 struct ADMIN_INFO
52 {
53     ADMIN_INFO()
54         : admin(),
55           accessTime(0),
56           priviledges()
57     {}
58
59     std::string admin;
60     time_t      accessTime;
61     STG::Priv        priviledges;
62 };
63
64 class RPC_CONFIG : public STG::Plugin
65 {
66 public:
67     RPC_CONFIG();
68     ~RPC_CONFIG() override;
69
70     void                SetUsers(STG::Users * u) override { users = u; }
71     void                SetTariffs(STG::Tariffs * t) override { tariffs = t; }
72     void                SetAdmins(STG::Admins * a) override { admins = a; }
73     void                SetStore(STG::Store * s) override { store = s; }
74     void                SetStgSettings(const STG::Settings * s) override;
75     void                SetSettings(const STG::ModuleSettings & s) override { settings = s; }
76     int                 ParseSettings() override;
77
78     int                 Start() override;
79     int                 Stop() override;
80     int                 Reload(const STG::ModuleSettings & /*ms*/) override { return 0; }
81     bool                IsRunning() override { return m_thread.joinable() && !stopped; }
82
83     const std::string & GetStrError() const override { return errorStr; }
84     std::string         GetVersion() const override { return RPC_CONFIG_VERSION; }
85     uint16_t            GetStartPosition() const override { return 20; }
86     uint16_t            GetStopPosition() const override { return 20; }
87
88     bool                GetAdminInfo(const std::string & cookie,
89                                      ADMIN_INFO * info);
90     bool                CheckAdmin(const std::string & login,
91                                    const std::string & password,
92                                    std::string * cookie);
93     bool                LogoutAdmin(const std::string & cookie);
94
95 private:
96     RPC_CONFIG(const RPC_CONFIG & rvalue);
97     RPC_CONFIG & operator=(const RPC_CONFIG & rvalue);
98
99     void                    Run(std::stop_token token);
100     std::string             GetCookie() const;
101     void                    InitiateRegistry();
102
103     mutable std::string     errorStr;
104     RPC_CONFIG_SETTINGS     rpcConfigSettings;
105     STG::Users *                 users;
106     STG::Admins *                admins;
107     STG::Tariffs *               tariffs;
108     STG::Store *                 store;
109     STG::ModuleSettings         settings;
110     int                     fd;
111     xmlrpc_c::registry      rpcRegistry;
112     xmlrpc_c::serverAbyss * rpcServer;
113     bool                    stopped;
114     std::jthread            m_thread;
115     std::map<std::string,
116              ADMIN_INFO>    cookies;
117     size_t                  dayFee;
118     std::vector<std::string> dirNames;
119     STG::PluginLogger           logger;
120 };