]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
352371202bb433f2526172662789a3291e936665
[stg.git] / projects / stargazer / plugins / other / ping / ping.cpp
1 #include "ping.h"
2
3 #include "stg/user.h"
4 #include "stg/locker.h"
5 #include "stg/user_property.h"
6
7 #include <cstdio>
8 #include <cassert>
9 #include <csignal>
10 #include <ctime>
11 #include <algorithm>
12
13 namespace
14 {
15 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 template <typename varType>
19 class HAS_USER: public std::binary_function<varType, UserPtr, bool>
20 {
21 public:
22     explicit HAS_USER(const UserPtr & u) : user(u) {}
23     bool operator()(varType notifier) const
24         {
25         return notifier.GetUser() == user;
26         }
27 private:
28     const UserPtr & user;
29 };
30 }
31
32 extern "C" STG::Plugin* GetPlugin()
33 {
34     static PING plugin;
35     return &plugin;
36 }
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 int PING_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
41 {
42 STG::ParamValue pv;
43 std::vector<STG::ParamValue>::const_iterator pvi;
44
45 pv.param = "PingDelay";
46 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
47 if (pvi == s.moduleParams.end() || pvi->value.empty())
48     {
49     errorStr = "Parameter \'PingDelay\' not found.";
50     printfd(__FILE__, "Parameter 'PingDelay' not found\n");
51     return -1;
52     }
53 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay) != 0)
54     {
55     errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
56     printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
57     return -1;
58     }
59
60 return 0;
61 }
62 //-----------------------------------------------------------------------------
63 PING::PING()
64     : users(nullptr),
65       isRunning(false),
66       onAddUserNotifier(*this),
67       onDelUserNotifier(*this),
68       logger(STG::PluginLogger::get("ping"))
69 {
70 }
71 //-----------------------------------------------------------------------------
72 int PING::ParseSettings()
73 {
74 auto ret = pingSettings.ParseSettings(settings);
75 if (ret != 0)
76     errorStr = pingSettings.GetStrError();
77 return ret;
78 }
79 //-----------------------------------------------------------------------------
80 int PING::Start()
81 {
82 GetUsers();
83
84 users->AddNotifierUserAdd(&onAddUserNotifier);
85 users->AddNotifierUserDel(&onDelUserNotifier);
86
87 pinger.SetDelayTime(pingSettings.GetPingDelay());
88 pinger.Start();
89
90 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
91
92 return 0;
93 }
94 //-----------------------------------------------------------------------------
95 int PING::Stop()
96 {
97 std::lock_guard lock(m_mutex);
98
99 if (!m_thread.joinable())
100     return 0;
101
102 pinger.Stop();
103 m_thread.request_stop();
104 //5 seconds to thread stops itself
105 struct timespec ts = {0, 200000000};
106 for (int i = 0; i < 25; i++)
107     {
108     if (!isRunning)
109         break;
110
111     nanosleep(&ts, nullptr);
112     }
113
114 users->DelNotifierUserAdd(&onAddUserNotifier);
115 users->DelNotifierUserDel(&onDelUserNotifier);
116
117 std::list<UserPtr>::iterator users_iter;
118 users_iter = usersList.begin();
119 while (users_iter != usersList.end())
120     {
121     UnSetUserNotifiers(*users_iter);
122     ++users_iter;
123     }
124
125 if (isRunning)
126     m_thread.detach();
127 else
128     m_thread.join();
129
130 return 0;
131 }
132 //-----------------------------------------------------------------------------
133 bool PING::IsRunning()
134 {
135 return isRunning;
136 }
137 //-----------------------------------------------------------------------------
138 void PING::Run(std::stop_token token)
139 {
140 sigset_t signalSet;
141 sigfillset(&signalSet);
142 pthread_sigmask(SIG_BLOCK, &signalSet, nullptr);
143
144 isRunning = true;
145
146 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
147
148 while (!token.stop_requested())
149     {
150     auto iter = usersList.begin();
151         {
152         std::lock_guard lock(m_mutex);
153         while (iter != usersList.end())
154             {
155             if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
156                 {
157                 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
158                 time_t t;
159                 if (pinger.GetIPTime(ip, &t) == 0)
160                     {
161                     if (t != 0)
162                         (*iter)->UpdatePingTime(t);
163                     }
164                 }
165             else
166                 {
167                 uint32_t ip = (*iter)->GetCurrIP();
168                 if (ip != 0)
169                     {
170                     time_t t;
171                     if (pinger.GetIPTime(ip, &t) == 0)
172                         {
173                         if (t != 0)
174                             (*iter)->UpdatePingTime(t);
175                         }
176                     }
177                 }
178             ++iter;
179             }
180         }
181     struct timespec ts = {delay / 1000000000, delay % 1000000000};
182     for (int i = 0; i < 100; i++)
183         {
184         if (!token.stop_requested())
185             {
186             nanosleep(&ts, nullptr);
187             }
188         }
189     }
190
191 isRunning = false;
192 }
193 //-----------------------------------------------------------------------------
194 void PING::SetUserNotifiers(UserPtr u)
195 {
196 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
197 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
198
199 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
200 ChgIPNotifierList.push_front(ChgIPNotifier);
201
202 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
203 u->GetProperties().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
204 }
205 //-----------------------------------------------------------------------------
206 void PING::UnSetUserNotifiers(UserPtr u)
207 {
208 // ---          CurrIP              ---
209 HAS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
210 HAS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
211
212 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
213 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
214
215 currIPter = find_if(ChgCurrIPNotifierList.begin(),
216                     ChgCurrIPNotifierList.end(),
217                     IsContainsUserCurrIP);
218
219 if (currIPter != ChgCurrIPNotifierList.end())
220     {
221     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
222     ChgCurrIPNotifierList.erase(currIPter);
223     }
224 // ---         CurrIP end          ---
225
226 // ---          IP              ---
227 IPIter = find_if(ChgIPNotifierList.begin(),
228                  ChgIPNotifierList.end(),
229                  IsContainsUserIP);
230
231 if (IPIter != ChgIPNotifierList.end())
232     {
233     IPIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*IPIter));
234     ChgIPNotifierList.erase(IPIter);
235     }
236 // ---          IP end          ---
237 }
238 //-----------------------------------------------------------------------------
239 void PING::GetUsers()
240 {
241 std::lock_guard lock(m_mutex);
242
243 UserPtr u;
244 int h = users->OpenSearch();
245 assert(h && "USERS::OpenSearch is always correct");
246
247 while (users->SearchNext(h, &u) == 0)
248     {
249     usersList.push_back(u);
250     SetUserNotifiers(u);
251     if (u->GetProperties().ips.ConstData().onlyOneIP())
252         {
253         pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
254         }
255     else
256         {
257         uint32_t ip = u->GetCurrIP();
258         if (ip != 0)
259             pinger.AddIP(ip);
260         }
261     }
262
263 users->CloseSearch(h);
264 }
265 //-----------------------------------------------------------------------------
266 void PING::AddUser(UserPtr u)
267 {
268 std::lock_guard lock(m_mutex);
269
270 SetUserNotifiers(u);
271 usersList.push_back(u);
272 }
273 //-----------------------------------------------------------------------------
274 void PING::DelUser(UserPtr u)
275 {
276 std::lock_guard lock(m_mutex);
277
278 UnSetUserNotifiers(u);
279
280 std::list<UserPtr>::iterator users_iter;
281 users_iter = usersList.begin();
282
283 while (users_iter != usersList.end())
284     {
285     if (u == *users_iter)
286         {
287         usersList.erase(users_iter);
288         break;
289         }
290     ++users_iter;
291     }
292 }
293 //-----------------------------------------------------------------------------
294 void CHG_CURRIP_NOTIFIER_PING::notify(const uint32_t & oldIP, const uint32_t & newIP)
295 {
296 ping.pinger.DelIP(oldIP);
297 if (newIP != 0)
298     ping.pinger.AddIP(newIP);
299 }
300 //-----------------------------------------------------------------------------
301 void CHG_IPS_NOTIFIER_PING::notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS)
302 {
303 if (oldIPS.onlyOneIP())
304     ping.pinger.DelIP(oldIPS[0].ip);
305
306 if (newIPS.onlyOneIP())
307     ping.pinger.AddIP(newIPS[0].ip);
308 }
309 //-----------------------------------------------------------------------------
310 void ADD_USER_NONIFIER_PING::notify(const UserPtr & user)
311 {
312 ping.AddUser(user);
313 }
314 //-----------------------------------------------------------------------------
315 void DEL_USER_NONIFIER_PING::notify(const UserPtr & user)
316 {
317 ping.DelUser(user);
318 }
319 //-----------------------------------------------------------------------------