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