]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
Move authorization from USER to USERS
[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 PING_SETTINGS::PING_SETTINGS()
41     : pingDelay(0)
42 {
43 }
44 //-----------------------------------------------------------------------------
45 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
46 {
47 PARAM_VALUE pv;
48 vector<PARAM_VALUE>::const_iterator pvi;
49
50 pv.param = "PingDelay";
51 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
52 if (pvi == s.moduleParams.end())
53     {
54     errorStr = "Parameter \'PingDelay\' not found.";
55     printfd(__FILE__, "Parameter 'PingDelay' not found\n");
56     return -1;
57     }
58 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
59     {
60     errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
61     printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
62     return -1;
63     }
64
65 return 0;
66 }
67 //-----------------------------------------------------------------------------
68 PING::PING()
69     : users(NULL),
70       nonstop(false),
71       isRunning(false),
72       onAddUserNotifier(*this),
73       onDelUserNotifier(*this)
74 {
75 pthread_mutex_init(&mutex, NULL);
76 }
77 //-----------------------------------------------------------------------------
78 PING::~PING()
79 {
80 pthread_mutex_destroy(&mutex);
81 }
82 //-----------------------------------------------------------------------------
83 const std::string PING::GetVersion() const
84 {
85 return "Pinger v.1.01";
86 }
87 //-----------------------------------------------------------------------------
88 void PING::SetSettings(const MODULE_SETTINGS & s)
89 {
90 settings = s;
91 }
92 //-----------------------------------------------------------------------------
93 int PING::ParseSettings()
94 {
95 int ret = pingSettings.ParseSettings(settings);
96 if (ret)
97     errorStr = pingSettings.GetStrError();
98 return ret;
99 }
100 //-----------------------------------------------------------------------------
101 void PING::SetUsers(USERS * u)
102 {
103 users = u;
104 }
105 //-----------------------------------------------------------------------------
106 const std::string & PING::GetStrError() const
107 {
108 return errorStr;
109 }
110 //-----------------------------------------------------------------------------
111 int PING::Start()
112 {
113 GetUsers();
114
115 users->AddNotifierUserAdd(&onAddUserNotifier);
116 users->AddNotifierUserDel(&onDelUserNotifier);
117
118 nonstop = true;
119
120 pinger.SetDelayTime(pingSettings.GetPingDelay());
121 pinger.Start();
122
123 if (pthread_create(&thread, NULL, Run, this))
124     {
125     errorStr = "Cannot start thread.";
126     printfd(__FILE__, "Cannot start thread\n");
127     return -1;
128     }
129
130 return 0;
131 }
132 //-----------------------------------------------------------------------------
133 int PING::Stop()
134 {
135 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
136
137 if (!isRunning)
138     return 0;
139
140 pinger.Stop();
141 nonstop = false;
142 //5 seconds to thread stops itself
143 struct timespec ts = {0, 200000000};
144 for (int i = 0; i < 25; i++)
145     {
146     if (!isRunning)
147         break;
148
149     nanosleep(&ts, NULL);
150     }
151
152 //after 5 seconds waiting thread still running. now kill it
153 if (isRunning)
154     {
155     printfd(__FILE__, "kill PING thread.\n");
156     if (pthread_kill(thread, SIGINT))
157         {
158         errorStr = "Cannot kill PING thread.";
159         printfd(__FILE__, "Cannot kill PING thread.\n");
160         return -1;
161         }
162     printfd(__FILE__, "PING killed\n");
163     }
164
165 users->DelNotifierUserAdd(&onAddUserNotifier);
166 users->DelNotifierUserDel(&onDelUserNotifier);
167
168 list<USER_PTR>::iterator users_iter;
169 users_iter = usersList.begin();
170 while (users_iter != usersList.end())
171     {
172     UnSetUserNotifiers(*users_iter);
173     ++users_iter;
174     }
175
176 return 0;
177 }
178 //-----------------------------------------------------------------------------
179 bool PING::IsRunning()
180 {
181 return isRunning;
182 }
183 //-----------------------------------------------------------------------------
184 void * PING::Run(void * d)
185 {
186 PING * ping = (PING *)d;
187 ping->isRunning = true;
188
189 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
190  
191 while (ping->nonstop)
192     {
193     list<USER_PTR>::iterator iter = ping->usersList.begin();
194         {
195         STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
196         while (iter != ping->usersList.end())
197             {
198             if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
199                 {
200                 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
201                 time_t t;
202                 if (ping->pinger.GetIPTime(ip, &t) == 0)
203                     {
204                     if (t)
205                         (*iter)->UpdatePingTime(t);
206                     }
207                 }
208             else
209                 {
210                 uint32_t ip = (*iter)->GetCurrIP();
211                 if (ip)
212                     {
213                     time_t t;
214                     if (ping->pinger.GetIPTime(ip, &t) == 0)
215                         {
216                         if (t)
217                             (*iter)->UpdatePingTime(t);
218                         }
219                     }
220                 }
221             ++iter;
222             }
223         }
224     struct timespec ts = {delay / 1000000000, delay % 1000000000};
225     for (int i = 0; i < 100; i++)
226         {
227         if (ping->nonstop)
228             {
229             nanosleep(&ts, NULL);
230             }
231         }
232     }
233
234 ping->isRunning = false;
235 return NULL;
236 }
237 //-----------------------------------------------------------------------------
238 uint16_t PING::GetStartPosition() const
239 {
240 return 100;
241 }
242 //-----------------------------------------------------------------------------
243 uint16_t PING::GetStopPosition() const
244 {
245 return 100;
246 }
247 //-----------------------------------------------------------------------------
248 void PING::SetUserNotifiers(USER_PTR u)
249 {
250 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
251 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
252
253 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
254 ChgIPNotifierList.push_front(ChgIPNotifier);
255
256 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
257 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
258 }
259 //-----------------------------------------------------------------------------
260 void PING::UnSetUserNotifiers(USER_PTR u)
261 {
262 // ---          CurrIP              ---
263 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
264 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
265
266 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
267 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
268
269 currIPter = find_if(ChgCurrIPNotifierList.begin(),
270                     ChgCurrIPNotifierList.end(),
271                     IsContainsUserCurrIP);
272
273 if (currIPter != ChgCurrIPNotifierList.end())
274     {
275     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
276     ChgCurrIPNotifierList.erase(currIPter);
277     }
278 // ---         CurrIP end          ---
279
280 // ---          IP              ---
281 IPIter = find_if(ChgIPNotifierList.begin(),
282                  ChgIPNotifierList.end(),
283                  IsContainsUserIP);
284
285 if (IPIter != ChgIPNotifierList.end())
286     {
287     IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
288     ChgIPNotifierList.erase(IPIter);
289     }
290 // ---          IP end          ---
291 }
292 //-----------------------------------------------------------------------------
293 void PING::GetUsers()
294 {
295 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
296
297 USER_PTR u;
298 int h = users->OpenSearch();
299 assert(h && "USERS::OpenSearch is always correct");
300
301 while (users->SearchNext(h, &u) == 0)
302     {
303     usersList.push_back(u);
304     SetUserNotifiers(u);
305     if (u->GetProperty().ips.ConstData().OnlyOneIP())
306         {
307         pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
308         }
309     else
310         {
311         uint32_t ip = u->GetCurrIP();
312         if (ip)
313             {
314             pinger.AddIP(ip);
315             }
316         }
317     }
318
319 users->CloseSearch(h);
320 }
321 //-----------------------------------------------------------------------------
322 void PING::AddUser(USER_PTR u)
323 {
324 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
325
326 SetUserNotifiers(u);
327 usersList.push_back(u);
328 }
329 //-----------------------------------------------------------------------------
330 void PING::DelUser(USER_PTR u)
331 {
332 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
333
334 UnSetUserNotifiers(u);
335
336 list<USER_PTR>::iterator users_iter;
337 users_iter = usersList.begin();
338
339 while (users_iter != usersList.end())
340     {
341     if (u == *users_iter)
342         {
343         usersList.erase(users_iter);
344         break;
345         }
346     ++users_iter;
347     }
348 }
349 //-----------------------------------------------------------------------------
350 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
351 {
352 ping.pinger.DelIP(oldIP);
353 if (newIP)
354     {
355     ping.pinger.AddIP(newIP);
356     }
357 }
358 //-----------------------------------------------------------------------------
359 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
360 {
361 if (oldIPS.OnlyOneIP())
362     {
363     ping.pinger.DelIP(oldIPS[0].ip);
364     }
365
366 if (newIPS.OnlyOneIP())
367     {
368     ping.pinger.AddIP(newIPS[0].ip);
369     }
370 }
371 //-----------------------------------------------------------------------------
372 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
373 {
374 ping.AddUser(user);
375 }
376 //-----------------------------------------------------------------------------
377 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
378 {
379 ping.DelUser(user);
380 }
381 //-----------------------------------------------------------------------------