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