]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.h
Simplify notifiers.
[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) override;
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) override;
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) override;
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) override;
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
99     void SetUsers(STG::Users * u) override { users = u; }
100     void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
101     int ParseSettings() override;
102
103     int Start() override;
104     int Stop() override;
105     int Reload(const STG::ModuleSettings & /*ms*/) override { return 0; }
106     bool IsRunning() override;
107
108     const std::string & GetStrError() const override { return errorStr; }
109     std::string GetVersion() const override { return "Pinger v.1.01"; }
110     uint16_t GetStartPosition() const override { return 10; }
111     uint16_t GetStopPosition() const override { return 10; }
112
113     void AddUser(UserPtr u);
114     void DelUser(UserPtr u);
115
116 private:
117     explicit PING(const PING & rvalue);
118     PING & operator=(const PING & rvalue);
119
120     void GetUsers();
121     void SetUserNotifiers(UserPtr u);
122     void UnSetUserNotifiers(UserPtr u);
123     void Run(std::stop_token token);
124
125     mutable std::string errorStr;
126     PING_SETTINGS pingSettings;
127     STG::ModuleSettings settings;
128     STG::Users * users;
129     std::list<UserPtr> usersList;
130
131     std::jthread m_thread;
132     std::mutex m_mutex;
133     bool isRunning;
134     mutable STG_PINGER pinger;
135
136     std::list<CHG_CURRIP_NOTIFIER_PING> ChgCurrIPNotifierList;
137     std::list<CHG_IPS_NOTIFIER_PING> ChgIPNotifierList;
138
139     ADD_USER_NONIFIER_PING onAddUserNotifier;
140     DEL_USER_NONIFIER_PING onDelUserNotifier;
141
142     STG::PluginLogger logger;
143 };