8 #include "stg/locker.h"
9 #include "stg/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 //-----------------------------------------------------------------------------
94 onAddUserNotifier(*this),
95 onDelUserNotifier(*this)
97 pthread_mutex_init(&mutex, NULL);
99 //-----------------------------------------------------------------------------
102 pthread_mutex_destroy(&mutex);
104 //-----------------------------------------------------------------------------
105 const std::string PING::GetVersion() const
107 return "Pinger v.1.01";
109 //-----------------------------------------------------------------------------
110 void PING::SetSettings(const MODULE_SETTINGS & s)
114 //-----------------------------------------------------------------------------
115 int PING::ParseSettings()
117 int ret = pingSettings.ParseSettings(settings);
119 errorStr = pingSettings.GetStrError();
122 //-----------------------------------------------------------------------------
123 void PING::SetUsers(USERS * u)
127 //-----------------------------------------------------------------------------
128 const std::string & PING::GetStrError() const
132 //-----------------------------------------------------------------------------
137 users->AddNotifierUserAdd(&onAddUserNotifier);
138 users->AddNotifierUserDel(&onDelUserNotifier);
142 pinger.SetDelayTime(pingSettings.GetPingDelay());
145 if (pthread_create(&thread, NULL, Run, this))
147 errorStr = "Cannot start thread.";
148 printfd(__FILE__, "Cannot start thread\n");
154 //-----------------------------------------------------------------------------
157 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
164 //5 seconds to thread stops itself
165 struct timespec ts = {0, 200000000};
166 for (int i = 0; i < 25; i++)
171 nanosleep(&ts, NULL);
174 //after 5 seconds waiting thread still running. now kill it
177 printfd(__FILE__, "kill PING thread.\n");
178 if (pthread_kill(thread, SIGINT))
180 errorStr = "Cannot kill PING thread.";
181 printfd(__FILE__, "Cannot kill PING thread.\n");
184 printfd(__FILE__, "PING killed\n");
187 users->DelNotifierUserAdd(&onAddUserNotifier);
188 users->DelNotifierUserDel(&onDelUserNotifier);
190 list<USER_PTR>::iterator users_iter;
191 users_iter = usersList.begin();
192 while (users_iter != usersList.end())
194 UnSetUserNotifiers(*users_iter);
200 //-----------------------------------------------------------------------------
201 bool PING::IsRunning()
205 //-----------------------------------------------------------------------------
206 void * PING::Run(void * d)
208 PING * ping = (PING *)d;
209 ping->isRunning = true;
211 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
213 while (ping->nonstop)
215 list<USER_PTR>::iterator iter = ping->usersList.begin();
217 STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
218 while (iter != ping->usersList.end())
220 if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
222 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
224 if (ping->pinger.GetIPTime(ip, &t) == 0)
227 (*iter)->UpdatePingTime(t);
232 uint32_t ip = (*iter)->GetCurrIP();
236 if (ping->pinger.GetIPTime(ip, &t) == 0)
239 (*iter)->UpdatePingTime(t);
246 struct timespec ts = {delay / 1000000000, delay % 1000000000};
247 for (int i = 0; i < 100; i++)
251 nanosleep(&ts, NULL);
256 ping->isRunning = false;
259 //-----------------------------------------------------------------------------
260 uint16_t PING::GetStartPosition() const
264 //-----------------------------------------------------------------------------
265 uint16_t PING::GetStopPosition() const
269 //-----------------------------------------------------------------------------
270 void PING::SetUserNotifiers(USER_PTR u)
272 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
273 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
275 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
276 ChgIPNotifierList.push_front(ChgIPNotifier);
278 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
279 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
281 //-----------------------------------------------------------------------------
282 void PING::UnSetUserNotifiers(USER_PTR u)
285 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
286 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
288 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
289 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
291 currIPter = find_if(ChgCurrIPNotifierList.begin(),
292 ChgCurrIPNotifierList.end(),
293 IsContainsUserCurrIP);
295 if (currIPter != ChgCurrIPNotifierList.end())
297 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
298 ChgCurrIPNotifierList.erase(currIPter);
300 // --- CurrIP end ---
303 IPIter = find_if(ChgIPNotifierList.begin(),
304 ChgIPNotifierList.end(),
307 if (IPIter != ChgIPNotifierList.end())
309 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
310 ChgIPNotifierList.erase(IPIter);
314 //-----------------------------------------------------------------------------
315 void PING::GetUsers()
317 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
320 int h = users->OpenSearch();
323 printfd(__FILE__, "users->OpenSearch() error\n");
327 while (users->SearchNext(h, &u) == 0)
329 usersList.push_back(u);
331 if (u->GetProperty().ips.ConstData().OnlyOneIP())
333 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
337 uint32_t ip = u->GetCurrIP();
345 users->CloseSearch(h);
347 //-----------------------------------------------------------------------------
348 void PING::AddUser(USER_PTR u)
350 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
353 usersList.push_back(u);
355 //-----------------------------------------------------------------------------
356 void PING::DelUser(USER_PTR u)
358 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
360 UnSetUserNotifiers(u);
362 list<USER_PTR>::iterator users_iter;
363 users_iter = usersList.begin();
365 while (users_iter != usersList.end())
367 if (u == *users_iter)
369 usersList.erase(users_iter);
375 //-----------------------------------------------------------------------------
376 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
378 ping.pinger.DelIP(oldIP);
381 ping.pinger.AddIP(newIP);
384 //-----------------------------------------------------------------------------
385 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
387 if (oldIPS.OnlyOneIP())
389 ping.pinger.DelIP(oldIPS[0].ip);
392 if (newIPS.OnlyOneIP())
394 ping.pinger.AddIP(newIPS[0].ip);
397 //-----------------------------------------------------------------------------
398 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
402 //-----------------------------------------------------------------------------
403 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
407 //-----------------------------------------------------------------------------