]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
Merge remote-tracking branch 'github/master'
[stg.git] / projects / stargazer / plugins / other / ping / ping.cpp
1 #include "ping.h"
2
3 #include "stg/user.h"
4 #include "stg/user_property.h"
5
6 #include <algorithm>
7 #include <cstdio>
8 #include <cassert>
9 #include <csignal>
10 #include <ctime>
11
12 using STG::PING;
13 using STG::PING_SETTINGS;
14
15 namespace
16 {
17
18 extern "C" STG::Plugin* GetPlugin()
19 {
20     static PING plugin;
21     return &plugin;
22 }
23
24 }
25 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
28 int PING_SETTINGS::ParseSettings(const ModuleSettings & s)
29 {
30 ParamValue pv;
31 std::vector<ParamValue>::const_iterator pvi;
32
33 pv.param = "PingDelay";
34 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
35 if (pvi == s.moduleParams.end() || pvi->value.empty())
36     {
37     errorStr = "Parameter \'PingDelay\' not found.";
38     printfd(__FILE__, "Parameter 'PingDelay' not found\n");
39     return -1;
40     }
41 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay) != 0)
42     {
43     errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
44     printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
45     return -1;
46     }
47
48 return 0;
49 }
50 //-----------------------------------------------------------------------------
51 PING::PING()
52     : users(nullptr),
53       isRunning(false),
54       logger(PluginLogger::get("ping"))
55 {
56 }
57 //-----------------------------------------------------------------------------
58 int PING::ParseSettings()
59 {
60 auto ret = pingSettings.ParseSettings(settings);
61 if (ret != 0)
62     errorStr = pingSettings.GetStrError();
63 return ret;
64 }
65 //-----------------------------------------------------------------------------
66 int PING::Start()
67 {
68 GetUsers();
69
70 m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); });
71 m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
72
73 m_pinger.SetDelayTime(pingSettings.GetPingDelay());
74 m_pinger.Start();
75
76 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
77
78 return 0;
79 }
80 //-----------------------------------------------------------------------------
81 int PING::Stop()
82 {
83 std::lock_guard lock(m_mutex);
84
85 if (!m_thread.joinable())
86     return 0;
87
88 m_pinger.Stop();
89 m_thread.request_stop();
90 //5 seconds to thread stops itself
91 struct timespec ts = {0, 200000000};
92 for (int i = 0; i < 25; i++)
93     {
94     if (!isRunning)
95         break;
96
97     nanosleep(&ts, nullptr);
98     }
99
100 m_onAddUserConn.disconnect();
101 m_onDelUserConn.disconnect();
102
103 m_conns.clear();
104
105 if (isRunning)
106     m_thread.detach();
107 else
108     m_thread.join();
109
110 return 0;
111 }
112 //-----------------------------------------------------------------------------
113 bool PING::IsRunning()
114 {
115 return isRunning;
116 }
117 //-----------------------------------------------------------------------------
118 void PING::Run(std::stop_token token)
119 {
120 sigset_t signalSet;
121 sigfillset(&signalSet);
122 pthread_sigmask(SIG_BLOCK, &signalSet, nullptr);
123
124 isRunning = true;
125
126 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
127
128 while (!token.stop_requested())
129     {
130     auto iter = usersList.begin();
131         {
132         std::lock_guard lock(m_mutex);
133         while (iter != usersList.end())
134             {
135             if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
136                 {
137                 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
138                 time_t t;
139                 if (m_pinger.GetIPTime(ip, t) == 0)
140                     {
141                     if (t != 0)
142                         (*iter)->UpdatePingTime(t);
143                     }
144                 }
145             else
146                 {
147                 uint32_t ip = (*iter)->GetCurrIP();
148                 if (ip != 0)
149                     {
150                     time_t t;
151                     if (m_pinger.GetIPTime(ip, t) == 0)
152                         {
153                         if (t != 0)
154                             (*iter)->UpdatePingTime(t);
155                         }
156                     }
157                 }
158             ++iter;
159             }
160         }
161     struct timespec ts = {delay / 1000000000, delay % 1000000000};
162     for (int i = 0; i < 100; i++)
163         {
164         if (!token.stop_requested())
165             {
166             nanosleep(&ts, nullptr);
167             }
168         }
169     }
170
171 isRunning = false;
172 }
173 //-----------------------------------------------------------------------------
174 void PING::SetUserNotifiers(UserPtr u)
175 {
176     m_conns.emplace_back(
177         u->GetID(),
178         u->afterCurrIPChange([this](auto oldVal, auto newVal){ updateCurrIP(oldVal, newVal); }),
179         u->GetProperties().ips.afterChange([this](const auto& oldVal, const auto& newVal){ updateIPs(oldVal, newVal); })
180     );
181 }
182 //-----------------------------------------------------------------------------
183 void PING::UnSetUserNotifiers(UserPtr u)
184 {
185     m_conns.erase(std::remove_if(m_conns.begin(), m_conns.end(),
186                                  [u](const auto& c){ return std::get<0>(c) == u->GetID(); }),
187                   m_conns.end());
188 }
189 //-----------------------------------------------------------------------------
190 void PING::GetUsers()
191 {
192 std::lock_guard lock(m_mutex);
193
194 UserPtr u;
195 int h = users->OpenSearch();
196 assert(h && "USERS::OpenSearch is always correct");
197
198 while (users->SearchNext(h, &u) == 0)
199     {
200     usersList.push_back(u);
201     SetUserNotifiers(u);
202     if (u->GetProperties().ips.ConstData().onlyOneIP())
203         {
204         m_pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
205         }
206     else
207         {
208         uint32_t ip = u->GetCurrIP();
209         if (ip != 0)
210             m_pinger.AddIP(ip);
211         }
212     }
213
214 users->CloseSearch(h);
215 }
216 //-----------------------------------------------------------------------------
217 void PING::AddUser(UserPtr u)
218 {
219 std::lock_guard lock(m_mutex);
220
221 SetUserNotifiers(u);
222 usersList.push_back(u);
223 }
224 //-----------------------------------------------------------------------------
225 void PING::DelUser(UserPtr u)
226 {
227 std::lock_guard lock(m_mutex);
228
229 UnSetUserNotifiers(u);
230
231 std::list<UserPtr>::iterator users_iter;
232 users_iter = usersList.begin();
233
234 while (users_iter != usersList.end())
235     {
236     if (u == *users_iter)
237         {
238         usersList.erase(users_iter);
239         break;
240         }
241     ++users_iter;
242     }
243 }
244 //-----------------------------------------------------------------------------
245 void PING::updateCurrIP(uint32_t oldVal, uint32_t newVal)
246 {
247     m_pinger.DelIP(oldVal);
248     if (newVal != 0)
249         m_pinger.AddIP(newVal);
250 }
251 //-----------------------------------------------------------------------------
252 void PING::updateIPs(const UserIPs& oldVal, const UserIPs& newVal)
253 {
254     if (oldVal.onlyOneIP())
255         m_pinger.DelIP(oldVal[0].ip);
256
257     if (newVal.onlyOneIP())
258         m_pinger.AddIP(newVal[0].ip);
259 }