]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
smux.lib added to build process
[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/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 if (!h)
300     {
301     printfd(__FILE__, "users->OpenSearch() error\n");
302     return;
303     }
304
305 while (users->SearchNext(h, &u) == 0)
306     {
307     usersList.push_back(u);
308     SetUserNotifiers(u);
309     if (u->GetProperty().ips.ConstData().OnlyOneIP())
310         {
311         pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
312         }
313     else
314         {
315         uint32_t ip = u->GetCurrIP();
316         if (ip)
317             {
318             pinger.AddIP(ip);
319             }
320         }
321     }
322
323 users->CloseSearch(h);
324 }
325 //-----------------------------------------------------------------------------
326 void PING::AddUser(USER_PTR u)
327 {
328 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
329
330 SetUserNotifiers(u);
331 usersList.push_back(u);
332 }
333 //-----------------------------------------------------------------------------
334 void PING::DelUser(USER_PTR u)
335 {
336 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
337
338 UnSetUserNotifiers(u);
339
340 list<USER_PTR>::iterator users_iter;
341 users_iter = usersList.begin();
342
343 while (users_iter != usersList.end())
344     {
345     if (u == *users_iter)
346         {
347         usersList.erase(users_iter);
348         break;
349         }
350     ++users_iter;
351     }
352 }
353 //-----------------------------------------------------------------------------
354 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
355 {
356 ping.pinger.DelIP(oldIP);
357 if (newIP)
358     {
359     ping.pinger.AddIP(newIP);
360     }
361 }
362 //-----------------------------------------------------------------------------
363 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
364 {
365 if (oldIPS.OnlyOneIP())
366     {
367     ping.pinger.DelIP(oldIPS[0].ip);
368     }
369
370 if (newIPS.OnlyOneIP())
371     {
372     ping.pinger.AddIP(newIPS[0].ip);
373     }
374 }
375 //-----------------------------------------------------------------------------
376 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
377 {
378 ping.AddUser(user);
379 }
380 //-----------------------------------------------------------------------------
381 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
382 {
383 ping.DelUser(user);
384 }
385 //-----------------------------------------------------------------------------