]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
Hide or add proper copy ctor and assignement operator, initialize
[stg.git] / projects / stargazer / plugins / other / ping / ping.cpp
1 #include <cstdio>
2 #include <cassert>
3 #include <csignal>
4 #include <ctime>
5 #include <algorithm>
6
7 #include "stg/user.h"
8 #include "stg/locker.h"
9 #include "stg/user_property.h"
10 #include "stg/plugin_creator.h"
11 #include "ping.h"
12
13 PLUGIN_CREATOR<PING> pc;
14 //-----------------------------------------------------------------------------
15 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
17 PLUGIN * GetPlugin()
18 {
19 return pc.GetPlugin();
20 }
21 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
24 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
25 template <typename varType>
26 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
27 {
28 public:
29     IS_CONTAINS_USER(const USER_PTR & u) : user(u) {}
30     bool operator()(varType notifier) const
31         {
32         return notifier.GetUser() == user;
33         };
34 private:
35     const USER_PTR & user;
36 };
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
41 {
42 PARAM_VALUE pv;
43 vector<PARAM_VALUE>::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())
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     : errorStr(),
65       pingSettings(),
66       settings(),
67       users(NULL),
68       usersList(),
69       thread(),
70       mutex(),
71       nonstop(false),
72       isRunning(false),
73       pinger(),
74       ChgCurrIPNotifierList(),
75       ChgIPNotifierList(),
76       onAddUserNotifier(*this),
77       onDelUserNotifier(*this)
78 {
79 pthread_mutex_init(&mutex, NULL);
80 }
81 //-----------------------------------------------------------------------------
82 PING::~PING()
83 {
84 pthread_mutex_destroy(&mutex);
85 }
86 //-----------------------------------------------------------------------------
87 int PING::ParseSettings()
88 {
89 int ret = pingSettings.ParseSettings(settings);
90 if (ret)
91     errorStr = pingSettings.GetStrError();
92 return ret;
93 }
94 //-----------------------------------------------------------------------------
95 int PING::Start()
96 {
97 GetUsers();
98
99 users->AddNotifierUserAdd(&onAddUserNotifier);
100 users->AddNotifierUserDel(&onDelUserNotifier);
101
102 nonstop = true;
103
104 pinger.SetDelayTime(pingSettings.GetPingDelay());
105 pinger.Start();
106
107 if (pthread_create(&thread, NULL, Run, this))
108     {
109     errorStr = "Cannot start thread.";
110     printfd(__FILE__, "Cannot start thread\n");
111     return -1;
112     }
113
114 return 0;
115 }
116 //-----------------------------------------------------------------------------
117 int PING::Stop()
118 {
119 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
120
121 if (!isRunning)
122     return 0;
123
124 pinger.Stop();
125 nonstop = false;
126 //5 seconds to thread stops itself
127 struct timespec ts = {0, 200000000};
128 for (int i = 0; i < 25; i++)
129     {
130     if (!isRunning)
131         break;
132
133     nanosleep(&ts, NULL);
134     }
135
136 //after 5 seconds waiting thread still running. now kill it
137 if (isRunning)
138     {
139     printfd(__FILE__, "kill PING thread.\n");
140     if (pthread_kill(thread, SIGINT))
141         {
142         errorStr = "Cannot kill PING thread.";
143         printfd(__FILE__, "Cannot kill PING thread.\n");
144         return -1;
145         }
146     printfd(__FILE__, "PING killed\n");
147     }
148
149 users->DelNotifierUserAdd(&onAddUserNotifier);
150 users->DelNotifierUserDel(&onDelUserNotifier);
151
152 list<USER_PTR>::iterator users_iter;
153 users_iter = usersList.begin();
154 while (users_iter != usersList.end())
155     {
156     UnSetUserNotifiers(*users_iter);
157     ++users_iter;
158     }
159
160 return 0;
161 }
162 //-----------------------------------------------------------------------------
163 bool PING::IsRunning()
164 {
165 return isRunning;
166 }
167 //-----------------------------------------------------------------------------
168 void * PING::Run(void * d)
169 {
170 PING * ping = (PING *)d;
171 ping->isRunning = true;
172
173 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
174  
175 while (ping->nonstop)
176     {
177     list<USER_PTR>::iterator iter = ping->usersList.begin();
178         {
179         STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
180         while (iter != ping->usersList.end())
181             {
182             if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
183                 {
184                 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
185                 time_t t;
186                 if (ping->pinger.GetIPTime(ip, &t) == 0)
187                     {
188                     if (t)
189                         (*iter)->UpdatePingTime(t);
190                     }
191                 }
192             else
193                 {
194                 uint32_t ip = (*iter)->GetCurrIP();
195                 if (ip)
196                     {
197                     time_t t;
198                     if (ping->pinger.GetIPTime(ip, &t) == 0)
199                         {
200                         if (t)
201                             (*iter)->UpdatePingTime(t);
202                         }
203                     }
204                 }
205             ++iter;
206             }
207         }
208     struct timespec ts = {delay / 1000000000, delay % 1000000000};
209     for (int i = 0; i < 100; i++)
210         {
211         if (ping->nonstop)
212             {
213             nanosleep(&ts, NULL);
214             }
215         }
216     }
217
218 ping->isRunning = false;
219 return NULL;
220 }
221 //-----------------------------------------------------------------------------
222 void PING::SetUserNotifiers(USER_PTR u)
223 {
224 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
225 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
226
227 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
228 ChgIPNotifierList.push_front(ChgIPNotifier);
229
230 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
231 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
232 }
233 //-----------------------------------------------------------------------------
234 void PING::UnSetUserNotifiers(USER_PTR u)
235 {
236 // ---          CurrIP              ---
237 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
238 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
239
240 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
241 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
242
243 currIPter = find_if(ChgCurrIPNotifierList.begin(),
244                     ChgCurrIPNotifierList.end(),
245                     IsContainsUserCurrIP);
246
247 if (currIPter != ChgCurrIPNotifierList.end())
248     {
249     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
250     ChgCurrIPNotifierList.erase(currIPter);
251     }
252 // ---         CurrIP end          ---
253
254 // ---          IP              ---
255 IPIter = find_if(ChgIPNotifierList.begin(),
256                  ChgIPNotifierList.end(),
257                  IsContainsUserIP);
258
259 if (IPIter != ChgIPNotifierList.end())
260     {
261     IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
262     ChgIPNotifierList.erase(IPIter);
263     }
264 // ---          IP end          ---
265 }
266 //-----------------------------------------------------------------------------
267 void PING::GetUsers()
268 {
269 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
270
271 USER_PTR u;
272 int h = users->OpenSearch();
273 assert(h && "USERS::OpenSearch is always correct");
274
275 while (users->SearchNext(h, &u) == 0)
276     {
277     usersList.push_back(u);
278     SetUserNotifiers(u);
279     if (u->GetProperty().ips.ConstData().OnlyOneIP())
280         {
281         pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
282         }
283     else
284         {
285         uint32_t ip = u->GetCurrIP();
286         if (ip)
287             {
288             pinger.AddIP(ip);
289             }
290         }
291     }
292
293 users->CloseSearch(h);
294 }
295 //-----------------------------------------------------------------------------
296 void PING::AddUser(USER_PTR u)
297 {
298 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
299
300 SetUserNotifiers(u);
301 usersList.push_back(u);
302 }
303 //-----------------------------------------------------------------------------
304 void PING::DelUser(USER_PTR u)
305 {
306 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
307
308 UnSetUserNotifiers(u);
309
310 list<USER_PTR>::iterator users_iter;
311 users_iter = usersList.begin();
312
313 while (users_iter != usersList.end())
314     {
315     if (u == *users_iter)
316         {
317         usersList.erase(users_iter);
318         break;
319         }
320     ++users_iter;
321     }
322 }
323 //-----------------------------------------------------------------------------
324 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
325 {
326 ping.pinger.DelIP(oldIP);
327 if (newIP)
328     {
329     ping.pinger.AddIP(newIP);
330     }
331 }
332 //-----------------------------------------------------------------------------
333 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
334 {
335 if (oldIPS.OnlyOneIP())
336     {
337     ping.pinger.DelIP(oldIPS[0].ip);
338     }
339
340 if (newIPS.OnlyOneIP())
341     {
342     ping.pinger.AddIP(newIPS[0].ip);
343     }
344 }
345 //-----------------------------------------------------------------------------
346 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
347 {
348 ping.AddUser(user);
349 }
350 //-----------------------------------------------------------------------------
351 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
352 {
353 ping.DelUser(user);
354 }
355 //-----------------------------------------------------------------------------