8 #include "stg/locker.h"
9 #include "stg/user_property.h"
10 #include "stg/plugin_creator.h"
13 PLUGIN_CREATOR<PING> pc;
14 //-----------------------------------------------------------------------------
15 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
19 return pc.GetPlugin();
21 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
24 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
25 template <typename varType>
26 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
29 IS_CONTAINS_USER(const USER_PTR & u) : user(u) {}
30 bool operator()(varType notifier) const
32 return notifier.GetUser() == user;
35 const USER_PTR & user;
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
43 vector<PARAM_VALUE>::const_iterator pvi;
45 pv.param = "PingDelay";
46 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
47 if (pvi == s.moduleParams.end())
49 errorStr = "Parameter \'PingDelay\' not found.";
50 printfd(__FILE__, "Parameter 'PingDelay' not found\n");
53 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
55 errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
56 printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
62 //-----------------------------------------------------------------------------
74 ChgCurrIPNotifierList(),
76 onAddUserNotifier(*this),
77 onDelUserNotifier(*this),
78 logger(GetPluginLogger(GetStgLogger(), "ping"))
80 pthread_mutex_init(&mutex, NULL);
82 //-----------------------------------------------------------------------------
85 pthread_mutex_destroy(&mutex);
87 //-----------------------------------------------------------------------------
88 int PING::ParseSettings()
90 int ret = pingSettings.ParseSettings(settings);
92 errorStr = pingSettings.GetStrError();
95 //-----------------------------------------------------------------------------
100 users->AddNotifierUserAdd(&onAddUserNotifier);
101 users->AddNotifierUserDel(&onDelUserNotifier);
105 pinger.SetDelayTime(pingSettings.GetPingDelay());
108 if (pthread_create(&thread, NULL, Run, this))
110 errorStr = "Cannot start thread.";
111 logger("Cannot create thread.");
112 printfd(__FILE__, "Cannot start thread\n");
118 //-----------------------------------------------------------------------------
121 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
128 //5 seconds to thread stops itself
129 struct timespec ts = {0, 200000000};
130 for (int i = 0; i < 25; i++)
135 nanosleep(&ts, NULL);
138 users->DelNotifierUserAdd(&onAddUserNotifier);
139 users->DelNotifierUserDel(&onDelUserNotifier);
141 list<USER_PTR>::iterator users_iter;
142 users_iter = usersList.begin();
143 while (users_iter != usersList.end())
145 UnSetUserNotifiers(*users_iter);
154 //-----------------------------------------------------------------------------
155 bool PING::IsRunning()
159 //-----------------------------------------------------------------------------
160 void * PING::Run(void * d)
163 sigfillset(&signalSet);
164 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
166 PING * ping = static_cast<PING *>(d);
167 ping->isRunning = true;
169 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
171 while (ping->nonstop)
173 list<USER_PTR>::iterator iter = ping->usersList.begin();
175 STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
176 while (iter != ping->usersList.end())
178 if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
180 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
182 if (ping->pinger.GetIPTime(ip, &t) == 0)
185 (*iter)->UpdatePingTime(t);
190 uint32_t ip = (*iter)->GetCurrIP();
194 if (ping->pinger.GetIPTime(ip, &t) == 0)
197 (*iter)->UpdatePingTime(t);
204 struct timespec ts = {delay / 1000000000, delay % 1000000000};
205 for (int i = 0; i < 100; i++)
209 nanosleep(&ts, NULL);
214 ping->isRunning = false;
217 //-----------------------------------------------------------------------------
218 void PING::SetUserNotifiers(USER_PTR u)
220 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
221 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
223 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
224 ChgIPNotifierList.push_front(ChgIPNotifier);
226 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
227 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
229 //-----------------------------------------------------------------------------
230 void PING::UnSetUserNotifiers(USER_PTR u)
233 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
234 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
236 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
237 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
239 currIPter = find_if(ChgCurrIPNotifierList.begin(),
240 ChgCurrIPNotifierList.end(),
241 IsContainsUserCurrIP);
243 if (currIPter != ChgCurrIPNotifierList.end())
245 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
246 ChgCurrIPNotifierList.erase(currIPter);
248 // --- CurrIP end ---
251 IPIter = find_if(ChgIPNotifierList.begin(),
252 ChgIPNotifierList.end(),
255 if (IPIter != ChgIPNotifierList.end())
257 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
258 ChgIPNotifierList.erase(IPIter);
262 //-----------------------------------------------------------------------------
263 void PING::GetUsers()
265 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
268 int h = users->OpenSearch();
269 assert(h && "USERS::OpenSearch is always correct");
271 while (users->SearchNext(h, &u) == 0)
273 usersList.push_back(u);
275 if (u->GetProperty().ips.ConstData().OnlyOneIP())
277 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
281 uint32_t ip = u->GetCurrIP();
289 users->CloseSearch(h);
291 //-----------------------------------------------------------------------------
292 void PING::AddUser(USER_PTR u)
294 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
297 usersList.push_back(u);
299 //-----------------------------------------------------------------------------
300 void PING::DelUser(USER_PTR u)
302 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
304 UnSetUserNotifiers(u);
306 list<USER_PTR>::iterator users_iter;
307 users_iter = usersList.begin();
309 while (users_iter != usersList.end())
311 if (u == *users_iter)
313 usersList.erase(users_iter);
319 //-----------------------------------------------------------------------------
320 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
322 ping.pinger.DelIP(oldIP);
325 ping.pinger.AddIP(newIP);
328 //-----------------------------------------------------------------------------
329 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
331 if (oldIPS.OnlyOneIP())
333 ping.pinger.DelIP(oldIPS[0].ip);
336 if (newIPS.OnlyOneIP())
338 ping.pinger.AddIP(newIPS[0].ip);
341 //-----------------------------------------------------------------------------
342 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
346 //-----------------------------------------------------------------------------
347 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
351 //-----------------------------------------------------------------------------