]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/ping/ping.cpp
00bb362d98151a56b7354cca9dd7922ae03ec2c1
[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       logger(GetPluginLogger(GetStgLogger(), "ping"))
79 {
80 pthread_mutex_init(&mutex, NULL);
81 }
82 //-----------------------------------------------------------------------------
83 PING::~PING()
84 {
85 pthread_mutex_destroy(&mutex);
86 }
87 //-----------------------------------------------------------------------------
88 int PING::ParseSettings()
89 {
90 int ret = pingSettings.ParseSettings(settings);
91 if (ret)
92     errorStr = pingSettings.GetStrError();
93 return ret;
94 }
95 //-----------------------------------------------------------------------------
96 int PING::Start()
97 {
98 GetUsers();
99
100 users->AddNotifierUserAdd(&onAddUserNotifier);
101 users->AddNotifierUserDel(&onDelUserNotifier);
102
103 nonstop = true;
104
105 pinger.SetDelayTime(pingSettings.GetPingDelay());
106 pinger.Start();
107
108 if (pthread_create(&thread, NULL, Run, this))
109     {
110     errorStr = "Cannot start thread.";
111     printfd(__FILE__, "Cannot start thread\n");
112     return -1;
113     }
114
115 return 0;
116 }
117 //-----------------------------------------------------------------------------
118 int PING::Stop()
119 {
120 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
121
122 if (!isRunning)
123     return 0;
124
125 pinger.Stop();
126 nonstop = false;
127 //5 seconds to thread stops itself
128 struct timespec ts = {0, 200000000};
129 for (int i = 0; i < 25; i++)
130     {
131     if (!isRunning)
132         break;
133
134     nanosleep(&ts, NULL);
135     }
136
137 users->DelNotifierUserAdd(&onAddUserNotifier);
138 users->DelNotifierUserDel(&onDelUserNotifier);
139
140 list<USER_PTR>::iterator users_iter;
141 users_iter = usersList.begin();
142 while (users_iter != usersList.end())
143     {
144     UnSetUserNotifiers(*users_iter);
145     ++users_iter;
146     }
147
148 if (isRunning)
149     return -1;
150
151 return 0;
152 }
153 //-----------------------------------------------------------------------------
154 bool PING::IsRunning()
155 {
156 return isRunning;
157 }
158 //-----------------------------------------------------------------------------
159 void * PING::Run(void * d)
160 {
161 sigset_t signalSet;
162 sigfillset(&signalSet);
163 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
164
165 PING * ping = static_cast<PING *>(d);
166 ping->isRunning = true;
167
168 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
169  
170 while (ping->nonstop)
171     {
172     list<USER_PTR>::iterator iter = ping->usersList.begin();
173         {
174         STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
175         while (iter != ping->usersList.end())
176             {
177             if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
178                 {
179                 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
180                 time_t t;
181                 if (ping->pinger.GetIPTime(ip, &t) == 0)
182                     {
183                     if (t)
184                         (*iter)->UpdatePingTime(t);
185                     }
186                 }
187             else
188                 {
189                 uint32_t ip = (*iter)->GetCurrIP();
190                 if (ip)
191                     {
192                     time_t t;
193                     if (ping->pinger.GetIPTime(ip, &t) == 0)
194                         {
195                         if (t)
196                             (*iter)->UpdatePingTime(t);
197                         }
198                     }
199                 }
200             ++iter;
201             }
202         }
203     struct timespec ts = {delay / 1000000000, delay % 1000000000};
204     for (int i = 0; i < 100; i++)
205         {
206         if (ping->nonstop)
207             {
208             nanosleep(&ts, NULL);
209             }
210         }
211     }
212
213 ping->isRunning = false;
214 return NULL;
215 }
216 //-----------------------------------------------------------------------------
217 void PING::SetUserNotifiers(USER_PTR u)
218 {
219 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
220 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
221
222 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
223 ChgIPNotifierList.push_front(ChgIPNotifier);
224
225 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
226 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
227 }
228 //-----------------------------------------------------------------------------
229 void PING::UnSetUserNotifiers(USER_PTR u)
230 {
231 // ---          CurrIP              ---
232 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
233 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
234
235 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
236 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
237
238 currIPter = find_if(ChgCurrIPNotifierList.begin(),
239                     ChgCurrIPNotifierList.end(),
240                     IsContainsUserCurrIP);
241
242 if (currIPter != ChgCurrIPNotifierList.end())
243     {
244     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
245     ChgCurrIPNotifierList.erase(currIPter);
246     }
247 // ---         CurrIP end          ---
248
249 // ---          IP              ---
250 IPIter = find_if(ChgIPNotifierList.begin(),
251                  ChgIPNotifierList.end(),
252                  IsContainsUserIP);
253
254 if (IPIter != ChgIPNotifierList.end())
255     {
256     IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
257     ChgIPNotifierList.erase(IPIter);
258     }
259 // ---          IP end          ---
260 }
261 //-----------------------------------------------------------------------------
262 void PING::GetUsers()
263 {
264 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
265
266 USER_PTR u;
267 int h = users->OpenSearch();
268 assert(h && "USERS::OpenSearch is always correct");
269
270 while (users->SearchNext(h, &u) == 0)
271     {
272     usersList.push_back(u);
273     SetUserNotifiers(u);
274     if (u->GetProperty().ips.ConstData().OnlyOneIP())
275         {
276         pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
277         }
278     else
279         {
280         uint32_t ip = u->GetCurrIP();
281         if (ip)
282             {
283             pinger.AddIP(ip);
284             }
285         }
286     }
287
288 users->CloseSearch(h);
289 }
290 //-----------------------------------------------------------------------------
291 void PING::AddUser(USER_PTR u)
292 {
293 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
294
295 SetUserNotifiers(u);
296 usersList.push_back(u);
297 }
298 //-----------------------------------------------------------------------------
299 void PING::DelUser(USER_PTR u)
300 {
301 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
302
303 UnSetUserNotifiers(u);
304
305 list<USER_PTR>::iterator users_iter;
306 users_iter = usersList.begin();
307
308 while (users_iter != usersList.end())
309     {
310     if (u == *users_iter)
311         {
312         usersList.erase(users_iter);
313         break;
314         }
315     ++users_iter;
316     }
317 }
318 //-----------------------------------------------------------------------------
319 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
320 {
321 ping.pinger.DelIP(oldIP);
322 if (newIP)
323     {
324     ping.pinger.AddIP(newIP);
325     }
326 }
327 //-----------------------------------------------------------------------------
328 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
329 {
330 if (oldIPS.OnlyOneIP())
331     {
332     ping.pinger.DelIP(oldIPS[0].ip);
333     }
334
335 if (newIPS.OnlyOneIP())
336     {
337     ping.pinger.AddIP(newIPS[0].ip);
338     }
339 }
340 //-----------------------------------------------------------------------------
341 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
342 {
343 ping.AddUser(user);
344 }
345 //-----------------------------------------------------------------------------
346 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
347 {
348 ping.DelUser(user);
349 }
350 //-----------------------------------------------------------------------------