]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
53f7b2f02b8bdf929d540f98358347ac69ffa6e2
[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 else
132     m_thread.join();
133
134 return 0;
135 }
136 //-----------------------------------------------------------------------------
137 bool PING::IsRunning()
138 {
139 return isRunning;
140 }
141 //-----------------------------------------------------------------------------
142 void PING::Run(std::stop_token token)
143 {
144 sigset_t signalSet;
145 sigfillset(&signalSet);
146 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
147
148 isRunning = true;
149
150 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
151
152 while (!token.stop_requested())
153     {
154     std::list<UserPtr>::iterator iter = usersList.begin();
155         {
156         std::lock_guard lock(m_mutex);
157         while (iter != usersList.end())
158             {
159             if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
160                 {
161                 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
162                 time_t t;
163                 if (pinger.GetIPTime(ip, &t) == 0)
164                     {
165                     if (t)
166                         (*iter)->UpdatePingTime(t);
167                     }
168                 }
169             else
170                 {
171                 uint32_t ip = (*iter)->GetCurrIP();
172                 if (ip)
173                     {
174                     time_t t;
175                     if (pinger.GetIPTime(ip, &t) == 0)
176                         {
177                         if (t)
178                             (*iter)->UpdatePingTime(t);
179                         }
180                     }
181                 }
182             ++iter;
183             }
184         }
185     struct timespec ts = {delay / 1000000000, delay % 1000000000};
186     for (int i = 0; i < 100; i++)
187         {
188         if (!token.stop_requested())
189             {
190             nanosleep(&ts, NULL);
191             }
192         }
193     }
194
195 isRunning = false;
196 }
197 //-----------------------------------------------------------------------------
198 void PING::SetUserNotifiers(UserPtr u)
199 {
200 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
201 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
202
203 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
204 ChgIPNotifierList.push_front(ChgIPNotifier);
205
206 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
207 u->GetProperties().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
208 }
209 //-----------------------------------------------------------------------------
210 void PING::UnSetUserNotifiers(UserPtr u)
211 {
212 // ---          CurrIP              ---
213 HAS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
214 HAS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
215
216 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
217 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
218
219 currIPter = find_if(ChgCurrIPNotifierList.begin(),
220                     ChgCurrIPNotifierList.end(),
221                     IsContainsUserCurrIP);
222
223 if (currIPter != ChgCurrIPNotifierList.end())
224     {
225     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
226     ChgCurrIPNotifierList.erase(currIPter);
227     }
228 // ---         CurrIP end          ---
229
230 // ---          IP              ---
231 IPIter = find_if(ChgIPNotifierList.begin(),
232                  ChgIPNotifierList.end(),
233                  IsContainsUserIP);
234
235 if (IPIter != ChgIPNotifierList.end())
236     {
237     IPIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*IPIter));
238     ChgIPNotifierList.erase(IPIter);
239     }
240 // ---          IP end          ---
241 }
242 //-----------------------------------------------------------------------------
243 void PING::GetUsers()
244 {
245 std::lock_guard lock(m_mutex);
246
247 UserPtr u;
248 int h = users->OpenSearch();
249 assert(h && "USERS::OpenSearch is always correct");
250
251 while (users->SearchNext(h, &u) == 0)
252     {
253     usersList.push_back(u);
254     SetUserNotifiers(u);
255     if (u->GetProperties().ips.ConstData().onlyOneIP())
256         {
257         pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
258         }
259     else
260         {
261         uint32_t ip = u->GetCurrIP();
262         if (ip)
263             pinger.AddIP(ip);
264         }
265     }
266
267 users->CloseSearch(h);
268 }
269 //-----------------------------------------------------------------------------
270 void PING::AddUser(UserPtr u)
271 {
272 std::lock_guard lock(m_mutex);
273
274 SetUserNotifiers(u);
275 usersList.push_back(u);
276 }
277 //-----------------------------------------------------------------------------
278 void PING::DelUser(UserPtr u)
279 {
280 std::lock_guard lock(m_mutex);
281
282 UnSetUserNotifiers(u);
283
284 std::list<UserPtr>::iterator users_iter;
285 users_iter = usersList.begin();
286
287 while (users_iter != usersList.end())
288     {
289     if (u == *users_iter)
290         {
291         usersList.erase(users_iter);
292         break;
293         }
294     ++users_iter;
295     }
296 }
297 //-----------------------------------------------------------------------------
298 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
299 {
300 ping.pinger.DelIP(oldIP);
301 if (newIP)
302     ping.pinger.AddIP(newIP);
303 }
304 //-----------------------------------------------------------------------------
305 void CHG_IPS_NOTIFIER_PING::Notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS)
306 {
307 if (oldIPS.onlyOneIP())
308     ping.pinger.DelIP(oldIPS[0].ip);
309
310 if (newIPS.onlyOneIP())
311     ping.pinger.AddIP(newIPS[0].ip);
312 }
313 //-----------------------------------------------------------------------------
314 void ADD_USER_NONIFIER_PING::Notify(const UserPtr & user)
315 {
316 ping.AddUser(user);
317 }
318 //-----------------------------------------------------------------------------
319 void DEL_USER_NONIFIER_PING::Notify(const UserPtr & user)
320 {
321 ping.DelUser(user);
322 }
323 //-----------------------------------------------------------------------------