]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.h
Use std::jthread and C++17.
[stg.git] / projects / stargazer / plugins / other / ping / ping.h
1 #pragma once
2
3 #include "stg/plugin.h"
4 #include "stg/module_settings.h"
5 #include "stg/notifer.h"
6 #include "stg/user_ips.h"
7 #include "stg/pinger.h"
8 #include "stg/users.h"
9 #include "stg/logger.h"
10
11 #include <string>
12 #include <list>
13 #include <mutex>
14 #pragma GCC diagnostic push
15 #pragma GCC diagnostic ignored "-Wshadow"
16 #include <jthread.hpp>
17 #pragma GCC diagnostic pop
18 #include <cstdint>
19
20 class PING;
21
22 namespace STG
23 {
24 struct USER;
25 struct SETTINGS;
26 }
27
28 using UserPtr = STG::User*;
29 //-----------------------------------------------------------------------------*/
30 class CHG_CURRIP_NOTIFIER_PING: public STG::PropertyNotifierBase<uint32_t> {
31 public:
32     CHG_CURRIP_NOTIFIER_PING(const PING & p, UserPtr u)
33         : user(u), ping(p) {}
34     void Notify(const uint32_t & oldIP, const uint32_t & newIP);
35     UserPtr GetUser() const { return user; }
36
37 private:
38     CHG_CURRIP_NOTIFIER_PING & operator=(const CHG_CURRIP_NOTIFIER_PING &);
39
40     UserPtr user;
41     const PING & ping;
42 };
43 //-----------------------------------------------------------------------------
44 class CHG_IPS_NOTIFIER_PING: public STG::PropertyNotifierBase<STG::UserIPs> {
45 public:
46     CHG_IPS_NOTIFIER_PING(const PING & p, UserPtr u)
47         : user(u), ping(p) {}
48     void Notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS);
49     UserPtr GetUser() const { return user; }
50
51 private:
52     CHG_IPS_NOTIFIER_PING & operator=(const CHG_IPS_NOTIFIER_PING &);
53
54     UserPtr user;
55     const PING & ping;
56 };
57 //-----------------------------------------------------------------------------
58 class ADD_USER_NONIFIER_PING: public STG::NotifierBase<UserPtr> {
59 public:
60     explicit ADD_USER_NONIFIER_PING(PING & p) : ping(p) {}
61     void Notify(const UserPtr & user);
62
63 private:
64     ADD_USER_NONIFIER_PING(const ADD_USER_NONIFIER_PING &);
65     ADD_USER_NONIFIER_PING & operator=(const ADD_USER_NONIFIER_PING &);
66
67     PING & ping;
68 };
69 //-----------------------------------------------------------------------------
70 class DEL_USER_NONIFIER_PING: public STG::NotifierBase<UserPtr> {
71 public:
72     explicit DEL_USER_NONIFIER_PING(PING & p) : ping(p) {}
73     void Notify(const UserPtr & user);
74
75 private:
76     DEL_USER_NONIFIER_PING(const DEL_USER_NONIFIER_PING &);
77     DEL_USER_NONIFIER_PING & operator=(const DEL_USER_NONIFIER_PING &);
78
79     PING & ping;
80 };
81 //-----------------------------------------------------------------------------
82 class PING_SETTINGS {
83 public:
84     PING_SETTINGS() : pingDelay(0) {}
85     const std::string & GetStrError() const { return errorStr; }
86     int ParseSettings(const STG::ModuleSettings & s);
87     int GetPingDelay() const { return pingDelay; }
88 private:
89     int pingDelay;
90     mutable std::string errorStr;
91 };
92 //-----------------------------------------------------------------------------
93 class PING : public STG::Plugin {
94 friend class CHG_CURRIP_NOTIFIER_PING;
95 friend class CHG_IPS_NOTIFIER_PING;
96 public:
97     PING();
98     ~PING() override;
99
100     void SetUsers(STG::Users * u) override { users = u; }
101     void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
102     int ParseSettings() override;
103
104     int Start() override;
105     int Stop() override;
106     int Reload(const STG::ModuleSettings & /*ms*/) override { return 0; }
107     bool IsRunning() override;
108
109     const std::string & GetStrError() const override { return errorStr; }
110     std::string GetVersion() const override { return "Pinger v.1.01"; }
111     uint16_t GetStartPosition() const override { return 10; }
112     uint16_t GetStopPosition() const override { return 10; }
113
114     void AddUser(UserPtr u);
115     void DelUser(UserPtr u);
116
117 private:
118     explicit PING(const PING & rvalue);
119     PING & operator=(const PING & rvalue);
120
121     void GetUsers();
122     void SetUserNotifiers(UserPtr u);
123     void UnSetUserNotifiers(UserPtr u);
124     void Run(std::stop_token token);
125
126     mutable std::string errorStr;
127     PING_SETTINGS pingSettings;
128     STG::ModuleSettings settings;
129     STG::Users * users;
130     std::list<UserPtr> usersList;
131
132     std::jthread m_thread;
133     std::mutex m_mutex;
134     bool isRunning;
135     mutable STG_PINGER pinger;
136
137     std::list<CHG_CURRIP_NOTIFIER_PING> ChgCurrIPNotifierList;
138     std::list<CHG_IPS_NOTIFIER_PING> ChgIPNotifierList;
139
140     ADD_USER_NONIFIER_PING onAddUserNotifier;
141     DEL_USER_NONIFIER_PING onDelUserNotifier;
142
143     STG::PluginLogger logger;
144 };