9 #include "stg_locker.h"
10 #include "user_property.h"
32 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
40 template <typename varType>
41 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
44 IS_CONTAINS_USER(const USER_PTR & u) : user(u) {}
45 bool operator()(varType notifier) const
47 return notifier.GetUser() == user;
50 const USER_PTR & user;
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
57 return pc.GetPlugin();
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 PING_SETTINGS::PING_SETTINGS()
66 //-----------------------------------------------------------------------------
67 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
70 vector<PARAM_VALUE>::const_iterator pvi;
72 pv.param = "PingDelay";
73 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
74 if (pvi == s.moduleParams.end())
76 errorStr = "Parameter \'PingDelay\' not found.";
77 printfd(__FILE__, "Parameter 'PingDelay' not found\n");
80 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
82 errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
83 printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
89 //-----------------------------------------------------------------------------
90 int PING_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val)
92 if (str2x(str.c_str(), *val))
94 errorStr = "Incorrect value \'" + str + "\'.";
97 if (*val < min || *val > max)
99 errorStr = "Value \'" + str + "\' out of range.";
104 //-----------------------------------------------------------------------------
109 onAddUserNotifier(*this),
110 onDelUserNotifier(*this)
112 pthread_mutex_init(&mutex, NULL);
114 //-----------------------------------------------------------------------------
117 pthread_mutex_destroy(&mutex);
119 //-----------------------------------------------------------------------------
120 const std::string PING::GetVersion() const
122 return "Pinger v.1.01";
124 //-----------------------------------------------------------------------------
125 void PING::SetSettings(const MODULE_SETTINGS & s)
129 //-----------------------------------------------------------------------------
130 int PING::ParseSettings()
132 int ret = pingSettings.ParseSettings(settings);
134 errorStr = pingSettings.GetStrError();
137 //-----------------------------------------------------------------------------
138 void PING::SetUsers(USERS * u)
142 //-----------------------------------------------------------------------------
143 const std::string & PING::GetStrError() const
147 //-----------------------------------------------------------------------------
152 users->AddNotifierUserAdd(&onAddUserNotifier);
153 users->AddNotifierUserDel(&onDelUserNotifier);
157 pinger.SetDelayTime(pingSettings.GetPingDelay());
160 if (pthread_create(&thread, NULL, Run, this))
162 errorStr = "Cannot start thread.";
163 printfd(__FILE__, "Cannot start thread\n");
169 //-----------------------------------------------------------------------------
172 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
179 //5 seconds to thread stops itself
180 for (int i = 0; i < 25; i++)
188 //after 5 seconds waiting thread still running. now kill it
191 printfd(__FILE__, "kill PING thread.\n");
192 if (pthread_kill(thread, SIGINT))
194 errorStr = "Cannot kill PING thread.";
195 printfd(__FILE__, "Cannot kill PING thread.\n");
198 printfd(__FILE__, "PING killed\n");
201 users->DelNotifierUserAdd(&onAddUserNotifier);
202 users->DelNotifierUserDel(&onDelUserNotifier);
204 list<USER_PTR>::iterator users_iter;
205 users_iter = usersList.begin();
206 while (users_iter != usersList.end())
208 UnSetUserNotifiers(*users_iter);
214 //-----------------------------------------------------------------------------
215 bool PING::IsRunning()
219 //-----------------------------------------------------------------------------
220 void * PING::Run(void * d)
222 PING * ping = (PING*)d;
223 ping->isRunning = true;
224 list<USER_PTR>::iterator iter;
228 while (ping->nonstop)
230 iter = ping->usersList.begin();
232 STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
233 while (iter != ping->usersList.end())
235 if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
237 ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
238 if (ping->pinger.GetIPTime(ip, &t) == 0)
241 (*iter)->UpdatePingTime(t);
246 ip = (*iter)->GetCurrIP();
249 if (ping->pinger.GetIPTime(ip, &t) == 0)
252 (*iter)->UpdatePingTime(t);
259 for (int i = 0; i < 100; i++)
263 usleep((10000*ping->pingSettings.GetPingDelay())/3 + 50000);
267 ping->isRunning = false;
270 //-----------------------------------------------------------------------------
271 uint16_t PING::GetStartPosition() const
275 //-----------------------------------------------------------------------------
276 uint16_t PING::GetStopPosition() const
280 //-----------------------------------------------------------------------------
281 void PING::SetUserNotifiers(USER_PTR u)
283 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
284 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
286 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
287 ChgIPNotifierList.push_front(ChgIPNotifier);
289 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
290 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
292 //-----------------------------------------------------------------------------
293 void PING::UnSetUserNotifiers(USER_PTR u)
296 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
297 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
299 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
300 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
302 currIPter = find_if(ChgCurrIPNotifierList.begin(),
303 ChgCurrIPNotifierList.end(),
304 IsContainsUserCurrIP);
306 if (currIPter != ChgCurrIPNotifierList.end())
308 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
309 ChgCurrIPNotifierList.erase(currIPter);
311 // --- CurrIP end ---
314 IPIter = find_if(ChgIPNotifierList.begin(),
315 ChgIPNotifierList.end(),
318 if (IPIter != ChgIPNotifierList.end())
320 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
321 ChgIPNotifierList.erase(IPIter);
325 //-----------------------------------------------------------------------------
326 void PING::GetUsers()
328 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
331 int h = users->OpenSearch();
334 printfd(__FILE__, "users->OpenSearch() error\n");
338 while (users->SearchNext(h, &u) == 0)
340 usersList.push_back(u);
342 if (u->GetProperty().ips.ConstData().OnlyOneIP())
344 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
348 uint32_t ip = u->GetCurrIP();
356 users->CloseSearch(h);
358 //-----------------------------------------------------------------------------
359 void PING::AddUser(USER_PTR u)
361 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
364 usersList.push_back(u);
366 //-----------------------------------------------------------------------------
367 void PING::DelUser(USER_PTR u)
369 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
371 UnSetUserNotifiers(u);
373 list<USER_PTR>::iterator users_iter;
374 users_iter = usersList.begin();
376 while (users_iter != usersList.end())
378 if (u == *users_iter)
380 usersList.erase(users_iter);
386 //-----------------------------------------------------------------------------
387 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
389 ping.pinger.DelIP(oldIP);
392 ping.pinger.AddIP(newIP);
395 //-----------------------------------------------------------------------------
396 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
398 if (oldIPS.OnlyOneIP())
400 ping.pinger.DelIP(oldIPS[0].ip);
403 if (newIPS.OnlyOneIP())
405 ping.pinger.AddIP(newIPS[0].ip);
408 //-----------------------------------------------------------------------------
409 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
413 //-----------------------------------------------------------------------------
414 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
418 //-----------------------------------------------------------------------------