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 struct timespec ts = {0, 200000000};
181 for (int i = 0; i < 25; i++)
186 nanosleep(&ts, NULL);
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;
226 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
228 while (ping->nonstop)
230 list<USER_PTR>::iterator 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 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
239 if (ping->pinger.GetIPTime(ip, &t) == 0)
242 (*iter)->UpdatePingTime(t);
247 uint32_t ip = (*iter)->GetCurrIP();
251 if (ping->pinger.GetIPTime(ip, &t) == 0)
254 (*iter)->UpdatePingTime(t);
261 struct timespec ts = {delay / 1000000000, delay % 1000000000};
262 for (int i = 0; i < 100; i++)
266 nanosleep(&ts, NULL);
271 ping->isRunning = false;
274 //-----------------------------------------------------------------------------
275 uint16_t PING::GetStartPosition() const
279 //-----------------------------------------------------------------------------
280 uint16_t PING::GetStopPosition() const
284 //-----------------------------------------------------------------------------
285 void PING::SetUserNotifiers(USER_PTR u)
287 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
288 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
290 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
291 ChgIPNotifierList.push_front(ChgIPNotifier);
293 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
294 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
296 //-----------------------------------------------------------------------------
297 void PING::UnSetUserNotifiers(USER_PTR u)
300 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
301 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
303 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
304 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
306 currIPter = find_if(ChgCurrIPNotifierList.begin(),
307 ChgCurrIPNotifierList.end(),
308 IsContainsUserCurrIP);
310 if (currIPter != ChgCurrIPNotifierList.end())
312 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
313 ChgCurrIPNotifierList.erase(currIPter);
315 // --- CurrIP end ---
318 IPIter = find_if(ChgIPNotifierList.begin(),
319 ChgIPNotifierList.end(),
322 if (IPIter != ChgIPNotifierList.end())
324 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
325 ChgIPNotifierList.erase(IPIter);
329 //-----------------------------------------------------------------------------
330 void PING::GetUsers()
332 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
335 int h = users->OpenSearch();
338 printfd(__FILE__, "users->OpenSearch() error\n");
342 while (users->SearchNext(h, &u) == 0)
344 usersList.push_back(u);
346 if (u->GetProperty().ips.ConstData().OnlyOneIP())
348 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
352 uint32_t ip = u->GetCurrIP();
360 users->CloseSearch(h);
362 //-----------------------------------------------------------------------------
363 void PING::AddUser(USER_PTR u)
365 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
368 usersList.push_back(u);
370 //-----------------------------------------------------------------------------
371 void PING::DelUser(USER_PTR u)
373 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
375 UnSetUserNotifiers(u);
377 list<USER_PTR>::iterator users_iter;
378 users_iter = usersList.begin();
380 while (users_iter != usersList.end())
382 if (u == *users_iter)
384 usersList.erase(users_iter);
390 //-----------------------------------------------------------------------------
391 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
393 ping.pinger.DelIP(oldIP);
396 ping.pinger.AddIP(newIP);
399 //-----------------------------------------------------------------------------
400 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
402 if (oldIPS.OnlyOneIP())
404 ping.pinger.DelIP(oldIPS[0].ip);
407 if (newIPS.OnlyOneIP())
409 ping.pinger.AddIP(newIPS[0].ip);
412 //-----------------------------------------------------------------------------
413 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
417 //-----------------------------------------------------------------------------
418 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
422 //-----------------------------------------------------------------------------