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