]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
a8dda2a680ee510474c9e7169d86a9659119feb5
[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))
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(NULL),
65       isRunning(false),
66       onAddUserNotifier(*this),
67       onDelUserNotifier(*this),
68       logger(STG::PluginLogger::get("ping"))
69 {
70 }
71 //-----------------------------------------------------------------------------
72 PING::~PING()
73 {
74 }
75 //-----------------------------------------------------------------------------
76 int PING::ParseSettings()
77 {
78 int ret = pingSettings.ParseSettings(settings);
79 if (ret)
80     errorStr = pingSettings.GetStrError();
81 return ret;
82 }
83 //-----------------------------------------------------------------------------
84 int PING::Start()
85 {
86 GetUsers();
87
88 users->AddNotifierUserAdd(&onAddUserNotifier);
89 users->AddNotifierUserDel(&onDelUserNotifier);
90
91 pinger.SetDelayTime(pingSettings.GetPingDelay());
92 pinger.Start();
93
94 m_thread = std::jthread([this](auto token){ Run(token); });
95
96 return 0;
97 }
98 //-----------------------------------------------------------------------------
99 int PING::Stop()
100 {
101 std::lock_guard lock(m_mutex);
102
103 if (!m_thread.joinable())
104     return 0;
105
106 pinger.Stop();
107 m_thread.request_stop();
108 //5 seconds to thread stops itself
109 struct timespec ts = {0, 200000000};
110 for (int i = 0; i < 25; i++)
111     {
112     if (!isRunning)
113         break;
114
115     nanosleep(&ts, NULL);
116     }
117
118 users->DelNotifierUserAdd(&onAddUserNotifier);
119 users->DelNotifierUserDel(&onDelUserNotifier);
120
121 std::list<UserPtr>::iterator users_iter;
122 users_iter = usersList.begin();
123 while (users_iter != usersList.end())
124     {
125     UnSetUserNotifiers(*users_iter);
126     ++users_iter;
127     }
128
129 if (isRunning)
130     m_thread.detach();
131
132 return 0;
133 }
134 //-----------------------------------------------------------------------------
135 bool PING::IsRunning()
136 {
137 return isRunning;
138 }
139 //-----------------------------------------------------------------------------
140 void PING::Run(std::stop_token token)
141 {
142 sigset_t signalSet;
143 sigfillset(&signalSet);
144 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
145
146 isRunning = true;
147
148 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
149
150 while (!token.stop_requested())
151     {
152     std::list<UserPtr>::iterator iter = usersList.begin();
153         {
154         std::lock_guard lock(m_mutex);
155         while (iter != usersList.end())
156             {
157             if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
158                 {
159                 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
160                 time_t t;
161                 if (pinger.GetIPTime(ip, &t) == 0)
162                     {
163                     if (t)
164                         (*iter)->UpdatePingTime(t);
165                     }
166                 }
167             else
168                 {
169                 uint32_t ip = (*iter)->GetCurrIP();
170                 if (ip)
171                     {
172                     time_t t;
173                     if (pinger.GetIPTime(ip, &t) == 0)
174                         {
175                         if (t)
176                             (*iter)->UpdatePingTime(t);
177                         }
178                     }
179                 }
180             ++iter;
181             }
182         }
183     struct timespec ts = {delay / 1000000000, delay % 1000000000};
184     for (int i = 0; i < 100; i++)
185         {
186         if (!token.stop_requested())
187             {
188             nanosleep(&ts, NULL);
189             }
190         }
191     }
192
193 isRunning = false;
194 }
195 //-----------------------------------------------------------------------------
196 void PING::SetUserNotifiers(UserPtr u)
197 {
198 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
199 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
200
201 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
202 ChgIPNotifierList.push_front(ChgIPNotifier);
203
204 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
205 u->GetProperties().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
206 }
207 //-----------------------------------------------------------------------------
208 void PING::UnSetUserNotifiers(UserPtr u)
209 {
210 // ---          CurrIP              ---
211 HAS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
212 HAS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
213
214 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
215 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
216
217 currIPter = find_if(ChgCurrIPNotifierList.begin(),
218                     ChgCurrIPNotifierList.end(),
219                     IsContainsUserCurrIP);
220
221 if (currIPter != ChgCurrIPNotifierList.end())
222     {
223     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
224     ChgCurrIPNotifierList.erase(currIPter);
225     }
226 // ---         CurrIP end          ---
227
228 // ---          IP              ---
229 IPIter = find_if(ChgIPNotifierList.begin(),
230                  ChgIPNotifierList.end(),
231                  IsContainsUserIP);
232
233 if (IPIter != ChgIPNotifierList.end())
234     {
235     IPIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*IPIter));
236     ChgIPNotifierList.erase(IPIter);
237     }
238 // ---          IP end          ---
239 }
240 //-----------------------------------------------------------------------------
241 void PING::GetUsers()
242 {
243 std::lock_guard lock(m_mutex);
244
245 UserPtr u;
246 int h = users->OpenSearch();
247 assert(h && "USERS::OpenSearch is always correct");
248
249 while (users->SearchNext(h, &u) == 0)
250     {
251     usersList.push_back(u);
252     SetUserNotifiers(u);
253     if (u->GetProperties().ips.ConstData().onlyOneIP())
254         {
255         pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
256         }
257     else
258         {
259         uint32_t ip = u->GetCurrIP();
260         if (ip)
261             pinger.AddIP(ip);
262         }
263     }
264
265 users->CloseSearch(h);
266 }
267 //-----------------------------------------------------------------------------
268 void PING::AddUser(UserPtr u)
269 {
270 std::lock_guard lock(m_mutex);
271
272 SetUserNotifiers(u);
273 usersList.push_back(u);
274 }
275 //-----------------------------------------------------------------------------
276 void PING::DelUser(UserPtr u)
277 {
278 std::lock_guard lock(m_mutex);
279
280 UnSetUserNotifiers(u);
281
282 std::list<UserPtr>::iterator users_iter;
283 users_iter = usersList.begin();
284
285 while (users_iter != usersList.end())
286     {
287     if (u == *users_iter)
288         {
289         usersList.erase(users_iter);
290         break;
291         }
292     ++users_iter;
293     }
294 }
295 //-----------------------------------------------------------------------------
296 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
297 {
298 ping.pinger.DelIP(oldIP);
299 if (newIP)
300     ping.pinger.AddIP(newIP);
301 }
302 //-----------------------------------------------------------------------------
303 void CHG_IPS_NOTIFIER_PING::Notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS)
304 {
305 if (oldIPS.onlyOneIP())
306     ping.pinger.DelIP(oldIPS[0].ip);
307
308 if (newIPS.onlyOneIP())
309     ping.pinger.AddIP(newIPS[0].ip);
310 }
311 //-----------------------------------------------------------------------------
312 void ADD_USER_NONIFIER_PING::Notify(const UserPtr & user)
313 {
314 ping.AddUser(user);
315 }
316 //-----------------------------------------------------------------------------
317 void DEL_USER_NONIFIER_PING::Notify(const UserPtr & user)
318 {
319 ping.DelUser(user);
320 }
321 //-----------------------------------------------------------------------------