9 #include "stg_locker.h"
10 #include "user_property.h"
11 #include "../../../settings.h"
33 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
41 template <typename varType>
42 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
45 IS_CONTAINS_USER(const USER_PTR & u) : user(u) {}
46 bool operator()(varType notifier) const
48 return notifier.GetUser() == user;
51 const USER_PTR & user;
53 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
58 return pc.GetPlugin();
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 PING_SETTINGS::PING_SETTINGS()
67 //-----------------------------------------------------------------------------
68 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
71 vector<PARAM_VALUE>::const_iterator pvi;
73 pv.param = "PingDelay";
74 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
75 if (pvi == s.moduleParams.end())
77 errorStr = "Parameter \'PingDelay\' not found.";
78 printfd(__FILE__, "Parameter 'PingDelay' not found\n");
81 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
83 errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
84 printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
90 //-----------------------------------------------------------------------------
91 int PING_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val)
93 if (str2x(str.c_str(), *val))
95 errorStr = "Incorrect value \'" + str + "\'.";
98 if (*val < min || *val > max)
100 errorStr = "Value \'" + str + "\' out of range.";
105 //-----------------------------------------------------------------------------
110 onAddUserNotifier(*this),
111 onDelUserNotifier(*this)
113 pthread_mutex_init(&mutex, NULL);
115 //-----------------------------------------------------------------------------
118 pthread_mutex_destroy(&mutex);
120 //-----------------------------------------------------------------------------
121 const std::string PING::GetVersion() const
123 return "Pinger v.1.01";
125 //-----------------------------------------------------------------------------
126 void PING::SetSettings(const MODULE_SETTINGS & s)
130 //-----------------------------------------------------------------------------
131 int PING::ParseSettings()
133 int ret = pingSettings.ParseSettings(settings);
135 errorStr = pingSettings.GetStrError();
138 //-----------------------------------------------------------------------------
139 void PING::SetUsers(USERS * u)
143 //-----------------------------------------------------------------------------
144 const std::string & PING::GetStrError() const
148 //-----------------------------------------------------------------------------
153 users->AddNotifierUserAdd(&onAddUserNotifier);
154 users->AddNotifierUserDel(&onDelUserNotifier);
158 pinger.SetDelayTime(pingSettings.GetPingDelay());
161 if (pthread_create(&thread, NULL, Run, this))
163 errorStr = "Cannot start thread.";
164 printfd(__FILE__, "Cannot start thread\n");
170 //-----------------------------------------------------------------------------
173 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
180 //5 seconds to thread stops itself
181 for (int i = 0; i < 25; i++)
189 //after 5 seconds waiting thread still running. now kill it
192 printfd(__FILE__, "kill PING thread.\n");
193 if (pthread_kill(thread, SIGINT))
195 errorStr = "Cannot kill PING thread.";
196 printfd(__FILE__, "Cannot kill PING thread.\n");
199 printfd(__FILE__, "PING killed\n");
202 users->DelNotifierUserAdd(&onAddUserNotifier);
203 users->DelNotifierUserDel(&onDelUserNotifier);
205 list<USER_PTR>::iterator users_iter;
206 users_iter = usersList.begin();
207 while (users_iter != usersList.end())
209 UnSetUserNotifiers(*users_iter);
215 //-----------------------------------------------------------------------------
216 bool PING::IsRunning()
220 //-----------------------------------------------------------------------------
221 void * PING::Run(void * d)
223 PING * ping = (PING*)d;
224 ping->isRunning = true;
225 list<USER_PTR>::iterator iter;
229 while (ping->nonstop)
231 iter = ping->usersList.begin();
233 STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
234 while (iter != ping->usersList.end())
236 if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
238 ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
239 if (ping->pinger.GetIPTime(ip, &t) == 0)
242 (*iter)->UpdatePingTime(t);
247 ip = (*iter)->GetCurrIP();
250 if (ping->pinger.GetIPTime(ip, &t) == 0)
253 (*iter)->UpdatePingTime(t);
260 for (int i = 0; i < 100; i++)
264 usleep((10000*ping->pingSettings.GetPingDelay())/3 + 50000);
268 ping->isRunning = false;
271 //-----------------------------------------------------------------------------
272 uint16_t PING::GetStartPosition() const
276 //-----------------------------------------------------------------------------
277 uint16_t PING::GetStopPosition() const
281 //-----------------------------------------------------------------------------
282 void PING::SetUserNotifiers(USER_PTR u)
284 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
285 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
287 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
288 ChgIPNotifierList.push_front(ChgIPNotifier);
290 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
291 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
293 //-----------------------------------------------------------------------------
294 void PING::UnSetUserNotifiers(USER_PTR u)
297 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
298 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
300 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
301 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
303 currIPter = find_if(ChgCurrIPNotifierList.begin(),
304 ChgCurrIPNotifierList.end(),
305 IsContainsUserCurrIP);
307 if (currIPter != ChgCurrIPNotifierList.end())
309 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
310 ChgCurrIPNotifierList.erase(currIPter);
312 // --- CurrIP end ---
315 IPIter = find_if(ChgIPNotifierList.begin(),
316 ChgIPNotifierList.end(),
319 if (IPIter != ChgIPNotifierList.end())
321 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
322 ChgIPNotifierList.erase(IPIter);
326 //-----------------------------------------------------------------------------
327 void PING::GetUsers()
329 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
332 int h = users->OpenSearch();
335 printfd(__FILE__, "users->OpenSearch() error\n");
339 while (users->SearchNext(h, &u) == 0)
341 usersList.push_back(u);
343 if (u->GetProperty().ips.ConstData().OnlyOneIP())
345 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
349 uint32_t ip = u->GetCurrIP();
357 users->CloseSearch(h);
359 //-----------------------------------------------------------------------------
360 void PING::AddUser(USER_PTR u)
362 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
365 usersList.push_back(u);
367 //-----------------------------------------------------------------------------
368 void PING::DelUser(USER_PTR u)
370 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
372 UnSetUserNotifiers(u);
374 list<USER_PTR>::iterator users_iter;
375 users_iter = usersList.begin();
377 while (users_iter != usersList.end())
379 if (u == *users_iter)
381 usersList.erase(users_iter);
387 //-----------------------------------------------------------------------------
388 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
390 ping.pinger.DelIP(oldIP);
393 ping.pinger.AddIP(newIP);
396 //-----------------------------------------------------------------------------
397 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
399 if (oldIPS.OnlyOneIP())
401 ping.pinger.DelIP(oldIPS[0].ip);
404 if (newIPS.OnlyOneIP())
406 ping.pinger.AddIP(newIPS[0].ip);
409 //-----------------------------------------------------------------------------
410 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
414 //-----------------------------------------------------------------------------
415 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
419 //-----------------------------------------------------------------------------