]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
Fix compilation issues
[stg.git] / projects / stargazer / plugins / other / ping / ping.cpp
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <signal.h>
4
5 #include <algorithm>
6
7 #include "ping.h"
8 #include "user.h"
9 #include "stg_locker.h"
10 #include "../../../settings.h"
11 #include "../../../user_property.h"
12
13 class PING_CREATOR
14 {
15 private:
16     PING * ping;
17
18 public:
19     PING_CREATOR()
20         : ping(new PING())
21         {
22         };
23     ~PING_CREATOR()
24         {
25         delete ping;
26         };
27
28     PING * GetPlugin()
29         {
30         return ping;
31         };
32 };
33 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
36 PING_CREATOR pc;
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
41 template <typename varType>
42 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
43 {
44 public:
45     IS_CONTAINS_USER(const USER_PTR & u) : user(u) {}
46     bool operator()(varType notifier) const
47         {
48         return notifier.GetUser() == user;
49         };
50 private:
51     const USER_PTR & user;
52 };
53 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
56 PLUGIN * GetPlugin()
57 {
58 return pc.GetPlugin();
59 }
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 PING_SETTINGS::PING_SETTINGS()
64     : pingDelay(0)
65 {
66 }
67 //-----------------------------------------------------------------------------
68 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
69 {
70 PARAM_VALUE pv;
71 vector<PARAM_VALUE>::const_iterator pvi;
72
73 pv.param = "PingDelay";
74 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
75 if (pvi == s.moduleParams.end())
76     {
77     errorStr = "Parameter \'PingDelay\' not found.";
78     printfd(__FILE__, "Parameter 'PingDelay' not found\n");
79     return -1;
80     }
81 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
82     {
83     errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
84     printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
85     return -1;
86     }
87
88 return 0;
89 }
90 //-----------------------------------------------------------------------------
91 int PING_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val)
92 {
93 if (str2x(str.c_str(), *val))
94     {
95     errorStr = "Incorrect value \'" + str + "\'.";
96     return -1;
97     }
98 if (*val < min || *val > max)
99     {
100     errorStr = "Value \'" + str + "\' out of range.";
101     return -1;
102     }
103 return 0;
104 }
105 //-----------------------------------------------------------------------------
106 PING::PING()
107     : users(NULL),
108       nonstop(false),
109       isRunning(false),
110       onAddUserNotifier(*this),
111       onDelUserNotifier(*this)
112 {
113 pthread_mutex_init(&mutex, NULL);
114 }
115 //-----------------------------------------------------------------------------
116 PING::~PING()
117 {
118 pthread_mutex_destroy(&mutex);
119 }
120 //-----------------------------------------------------------------------------
121 const std::string PING::GetVersion() const
122 {
123 return "Pinger v.1.01";
124 }
125 //-----------------------------------------------------------------------------
126 void PING::SetSettings(const MODULE_SETTINGS & s)
127 {
128 settings = s;
129 }
130 //-----------------------------------------------------------------------------
131 int PING::ParseSettings()
132 {
133 int ret = pingSettings.ParseSettings(settings);
134 if (ret)
135     errorStr = pingSettings.GetStrError();
136 return ret;
137 }
138 //-----------------------------------------------------------------------------
139 void PING::SetUsers(USERS * u)
140 {
141 users = u;
142 }
143 //-----------------------------------------------------------------------------
144 const std::string & PING::GetStrError() const
145 {
146 return errorStr;
147 }
148 //-----------------------------------------------------------------------------
149 int PING::Start()
150 {
151 GetUsers();
152
153 users->AddNotifierUserAdd(&onAddUserNotifier);
154 users->AddNotifierUserDel(&onDelUserNotifier);
155
156 nonstop = true;
157
158 pinger.SetDelayTime(pingSettings.GetPingDelay());
159 pinger.Start();
160
161 if (pthread_create(&thread, NULL, Run, this))
162     {
163     errorStr = "Cannot start thread.";
164     printfd(__FILE__, "Cannot start thread\n");
165     return -1;
166     }
167
168 return 0;
169 }
170 //-----------------------------------------------------------------------------
171 int PING::Stop()
172 {
173 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
174
175 if (!isRunning)
176     return 0;
177
178 pinger.Stop();
179 nonstop = false;
180 //5 seconds to thread stops itself
181 for (int i = 0; i < 25; i++)
182     {
183     if (!isRunning)
184         break;
185
186     usleep(200000);
187     }
188
189 //after 5 seconds waiting thread still running. now kill it
190 if (isRunning)
191     {
192     printfd(__FILE__, "kill PING thread.\n");
193     if (pthread_kill(thread, SIGINT))
194         {
195         errorStr = "Cannot kill PING thread.";
196         printfd(__FILE__, "Cannot kill PING thread.\n");
197         return -1;
198         }
199     printfd(__FILE__, "PING killed\n");
200     }
201
202 users->DelNotifierUserAdd(&onAddUserNotifier);
203 users->DelNotifierUserDel(&onDelUserNotifier);
204
205 list<USER_PTR>::iterator users_iter;
206 users_iter = usersList.begin();
207 while (users_iter != usersList.end())
208     {
209     UnSetUserNotifiers(*users_iter);
210     users_iter++;
211     }
212
213 return 0;
214 }
215 //-----------------------------------------------------------------------------
216 bool PING::IsRunning()
217 {
218 return isRunning;
219 }
220 //-----------------------------------------------------------------------------
221 void * PING::Run(void * d)
222 {
223 PING * ping = (PING*)d;
224 ping->isRunning = true;
225 list<USER_PTR>::iterator iter;
226 uint32_t ip;
227 time_t t;
228
229 while (ping->nonstop)
230     {
231     iter = ping->usersList.begin();
232         {
233         STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
234         while (iter != ping->usersList.end())
235             {
236             if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
237                 {
238                 ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
239                 if (ping->pinger.GetIPTime(ip, &t) == 0)
240                     {
241                     if (t)
242                         (*iter)->UpdatePingTime(t);
243                     }
244                 }
245             else
246                 {
247                 ip = (*iter)->GetCurrIP();
248                 if (ip)
249                     {
250                     if (ping->pinger.GetIPTime(ip, &t) == 0)
251                         {
252                         if (t)
253                             (*iter)->UpdatePingTime(t);
254                         }
255                     }
256                 }
257             iter++;
258             }
259         }
260     for (int i = 0; i < 100; i++)
261         {
262         if (ping->nonstop)
263             {
264             usleep((10000*ping->pingSettings.GetPingDelay())/3 + 50000);
265             }
266         }
267     }
268 ping->isRunning = false;
269 return NULL;
270 }
271 //-----------------------------------------------------------------------------
272 uint16_t PING::GetStartPosition() const
273 {
274 return 100;
275 }
276 //-----------------------------------------------------------------------------
277 uint16_t PING::GetStopPosition() const
278 {
279 return 100;
280 }
281 //-----------------------------------------------------------------------------
282 void PING::SetUserNotifiers(USER_PTR u)
283 {
284 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
285 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
286
287 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
288 ChgIPNotifierList.push_front(ChgIPNotifier);
289
290 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
291 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
292 }
293 //-----------------------------------------------------------------------------
294 void PING::UnSetUserNotifiers(USER_PTR u)
295 {
296 // ---          CurrIP              ---
297 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
298 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
299
300 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
301 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
302
303 currIPter = find_if(ChgCurrIPNotifierList.begin(),
304                     ChgCurrIPNotifierList.end(),
305                     IsContainsUserCurrIP);
306
307 if (currIPter != ChgCurrIPNotifierList.end())
308     {
309     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
310     ChgCurrIPNotifierList.erase(currIPter);
311     }
312 // ---         CurrIP end          ---
313
314 // ---          IP              ---
315 IPIter = find_if(ChgIPNotifierList.begin(),
316                  ChgIPNotifierList.end(),
317                  IsContainsUserIP);
318
319 if (IPIter != ChgIPNotifierList.end())
320     {
321     IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
322     ChgIPNotifierList.erase(IPIter);
323     }
324 // ---          IP end          ---
325 }
326 //-----------------------------------------------------------------------------
327 void PING::GetUsers()
328 {
329 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
330
331 USER_PTR u;
332 int h = users->OpenSearch();
333 if (!h)
334     {
335     printfd(__FILE__, "users->OpenSearch() error\n");
336     return;
337     }
338
339 while (users->SearchNext(h, &u) == 0)
340     {
341     usersList.push_back(u);
342     SetUserNotifiers(u);
343     if (u->GetProperty().ips.ConstData().OnlyOneIP())
344         {
345         pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
346         }
347     else
348         {
349         uint32_t ip = u->GetCurrIP();
350         if (ip)
351             {
352             pinger.AddIP(ip);
353             }
354         }
355     }
356
357 users->CloseSearch(h);
358 }
359 //-----------------------------------------------------------------------------
360 void PING::AddUser(USER_PTR u)
361 {
362 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
363
364 SetUserNotifiers(u);
365 usersList.push_back(u);
366 }
367 //-----------------------------------------------------------------------------
368 void PING::DelUser(USER_PTR u)
369 {
370 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
371
372 UnSetUserNotifiers(u);
373
374 list<USER_PTR>::iterator users_iter;
375 users_iter = usersList.begin();
376
377 while (users_iter != usersList.end())
378     {
379     if (u == *users_iter)
380         {
381         usersList.erase(users_iter);
382         break;
383         }
384     users_iter++;
385     }
386 }
387 //-----------------------------------------------------------------------------
388 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
389 {
390 ping.pinger.DelIP(oldIP);
391 if (newIP)
392     {
393     ping.pinger.AddIP(newIP);
394     }
395 }
396 //-----------------------------------------------------------------------------
397 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
398 {
399 if (oldIPS.OnlyOneIP())
400     {
401     ping.pinger.DelIP(oldIPS[0].ip);
402     }
403
404 if (newIPS.OnlyOneIP())
405     {
406     ping.pinger.AddIP(newIPS[0].ip);
407     }
408 }
409 //-----------------------------------------------------------------------------
410 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
411 {
412 ping.AddUser(user);
413 }
414 //-----------------------------------------------------------------------------
415 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
416 {
417 ping.DelUser(user);
418 }
419 //-----------------------------------------------------------------------------