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