]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
Fix ping plugin compilation errors
[stg.git] / projects / stargazer / plugins / other / ping / ping.cpp
1 #include <stdio.h>
2 #include <signal.h>
3
4 #include <ctime>
5 #include <algorithm>
6
7 #include "stg/user.h"
8 #include "stg/stg_locker.h"
9 #include "stg/user_property.h"
10 #include "ping.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 struct timespec ts = {0, 200000000};
181 for (int i = 0; i < 25; i++)
182     {
183     if (!isRunning)
184         break;
185
186     nanosleep(&ts, NULL);
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
226 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
227  
228 while (ping->nonstop)
229     {
230     list<USER_PTR>::iterator 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                 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
238                 time_t t;
239                 if (ping->pinger.GetIPTime(ip, &t) == 0)
240                     {
241                     if (t)
242                         (*iter)->UpdatePingTime(t);
243                     }
244                 }
245             else
246                 {
247                 uint32_t ip = (*iter)->GetCurrIP();
248                 if (ip)
249                     {
250                     time_t t;
251                     if (ping->pinger.GetIPTime(ip, &t) == 0)
252                         {
253                         if (t)
254                             (*iter)->UpdatePingTime(t);
255                         }
256                     }
257                 }
258             ++iter;
259             }
260         }
261     struct timespec ts = {delay / 1000000000, delay % 1000000000};
262     for (int i = 0; i < 100; i++)
263         {
264         if (ping->nonstop)
265             {
266             nanosleep(&ts, NULL);
267             }
268         }
269     }
270
271 ping->isRunning = false;
272 return NULL;
273 }
274 //-----------------------------------------------------------------------------
275 uint16_t PING::GetStartPosition() const
276 {
277 return 100;
278 }
279 //-----------------------------------------------------------------------------
280 uint16_t PING::GetStopPosition() const
281 {
282 return 100;
283 }
284 //-----------------------------------------------------------------------------
285 void PING::SetUserNotifiers(USER_PTR u)
286 {
287 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
288 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
289
290 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
291 ChgIPNotifierList.push_front(ChgIPNotifier);
292
293 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
294 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
295 }
296 //-----------------------------------------------------------------------------
297 void PING::UnSetUserNotifiers(USER_PTR u)
298 {
299 // ---          CurrIP              ---
300 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
301 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
302
303 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
304 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
305
306 currIPter = find_if(ChgCurrIPNotifierList.begin(),
307                     ChgCurrIPNotifierList.end(),
308                     IsContainsUserCurrIP);
309
310 if (currIPter != ChgCurrIPNotifierList.end())
311     {
312     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
313     ChgCurrIPNotifierList.erase(currIPter);
314     }
315 // ---         CurrIP end          ---
316
317 // ---          IP              ---
318 IPIter = find_if(ChgIPNotifierList.begin(),
319                  ChgIPNotifierList.end(),
320                  IsContainsUserIP);
321
322 if (IPIter != ChgIPNotifierList.end())
323     {
324     IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
325     ChgIPNotifierList.erase(IPIter);
326     }
327 // ---          IP end          ---
328 }
329 //-----------------------------------------------------------------------------
330 void PING::GetUsers()
331 {
332 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
333
334 USER_PTR u;
335 int h = users->OpenSearch();
336 if (!h)
337     {
338     printfd(__FILE__, "users->OpenSearch() error\n");
339     return;
340     }
341
342 while (users->SearchNext(h, &u) == 0)
343     {
344     usersList.push_back(u);
345     SetUserNotifiers(u);
346     if (u->GetProperty().ips.ConstData().OnlyOneIP())
347         {
348         pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
349         }
350     else
351         {
352         uint32_t ip = u->GetCurrIP();
353         if (ip)
354             {
355             pinger.AddIP(ip);
356             }
357         }
358     }
359
360 users->CloseSearch(h);
361 }
362 //-----------------------------------------------------------------------------
363 void PING::AddUser(USER_PTR u)
364 {
365 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
366
367 SetUserNotifiers(u);
368 usersList.push_back(u);
369 }
370 //-----------------------------------------------------------------------------
371 void PING::DelUser(USER_PTR u)
372 {
373 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
374
375 UnSetUserNotifiers(u);
376
377 list<USER_PTR>::iterator users_iter;
378 users_iter = usersList.begin();
379
380 while (users_iter != usersList.end())
381     {
382     if (u == *users_iter)
383         {
384         usersList.erase(users_iter);
385         break;
386         }
387     ++users_iter;
388     }
389 }
390 //-----------------------------------------------------------------------------
391 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
392 {
393 ping.pinger.DelIP(oldIP);
394 if (newIP)
395     {
396     ping.pinger.AddIP(newIP);
397     }
398 }
399 //-----------------------------------------------------------------------------
400 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
401 {
402 if (oldIPS.OnlyOneIP())
403     {
404     ping.pinger.DelIP(oldIPS[0].ip);
405     }
406
407 if (newIPS.OnlyOneIP())
408     {
409     ping.pinger.AddIP(newIPS[0].ip);
410     }
411 }
412 //-----------------------------------------------------------------------------
413 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
414 {
415 ping.AddUser(user);
416 }
417 //-----------------------------------------------------------------------------
418 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
419 {
420 ping.DelUser(user);
421 }
422 //-----------------------------------------------------------------------------