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)
79 pthread_mutex_init(&mutex, NULL);
81 //-----------------------------------------------------------------------------
84 pthread_mutex_destroy(&mutex);
86 //-----------------------------------------------------------------------------
87 int PING::ParseSettings()
89 int ret = pingSettings.ParseSettings(settings);
91 errorStr = pingSettings.GetStrError();
94 //-----------------------------------------------------------------------------
99 users->AddNotifierUserAdd(&onAddUserNotifier);
100 users->AddNotifierUserDel(&onDelUserNotifier);
104 pinger.SetDelayTime(pingSettings.GetPingDelay());
107 if (pthread_create(&thread, NULL, Run, this))
109 errorStr = "Cannot start thread.";
110 printfd(__FILE__, "Cannot start thread\n");
116 //-----------------------------------------------------------------------------
119 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
126 //5 seconds to thread stops itself
127 struct timespec ts = {0, 200000000};
128 for (int i = 0; i < 25; i++)
133 nanosleep(&ts, NULL);
136 //after 5 seconds waiting thread still running. now kill it
139 printfd(__FILE__, "kill PING thread.\n");
140 if (pthread_kill(thread, SIGINT))
142 errorStr = "Cannot kill PING thread.";
143 printfd(__FILE__, "Cannot kill PING thread.\n");
146 printfd(__FILE__, "PING killed\n");
149 users->DelNotifierUserAdd(&onAddUserNotifier);
150 users->DelNotifierUserDel(&onDelUserNotifier);
152 list<USER_PTR>::iterator users_iter;
153 users_iter = usersList.begin();
154 while (users_iter != usersList.end())
156 UnSetUserNotifiers(*users_iter);
162 //-----------------------------------------------------------------------------
163 bool PING::IsRunning()
167 //-----------------------------------------------------------------------------
168 void * PING::Run(void * d)
170 PING * ping = (PING *)d;
171 ping->isRunning = true;
173 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
175 while (ping->nonstop)
177 list<USER_PTR>::iterator iter = ping->usersList.begin();
179 STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
180 while (iter != ping->usersList.end())
182 if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
184 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
186 if (ping->pinger.GetIPTime(ip, &t) == 0)
189 (*iter)->UpdatePingTime(t);
194 uint32_t ip = (*iter)->GetCurrIP();
198 if (ping->pinger.GetIPTime(ip, &t) == 0)
201 (*iter)->UpdatePingTime(t);
208 struct timespec ts = {delay / 1000000000, delay % 1000000000};
209 for (int i = 0; i < 100; i++)
213 nanosleep(&ts, NULL);
218 ping->isRunning = false;
221 //-----------------------------------------------------------------------------
222 void PING::SetUserNotifiers(USER_PTR u)
224 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
225 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
227 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
228 ChgIPNotifierList.push_front(ChgIPNotifier);
230 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
231 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
233 //-----------------------------------------------------------------------------
234 void PING::UnSetUserNotifiers(USER_PTR u)
237 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
238 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
240 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
241 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
243 currIPter = find_if(ChgCurrIPNotifierList.begin(),
244 ChgCurrIPNotifierList.end(),
245 IsContainsUserCurrIP);
247 if (currIPter != ChgCurrIPNotifierList.end())
249 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
250 ChgCurrIPNotifierList.erase(currIPter);
252 // --- CurrIP end ---
255 IPIter = find_if(ChgIPNotifierList.begin(),
256 ChgIPNotifierList.end(),
259 if (IPIter != ChgIPNotifierList.end())
261 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
262 ChgIPNotifierList.erase(IPIter);
266 //-----------------------------------------------------------------------------
267 void PING::GetUsers()
269 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
272 int h = users->OpenSearch();
273 assert(h && "USERS::OpenSearch is always correct");
275 while (users->SearchNext(h, &u) == 0)
277 usersList.push_back(u);
279 if (u->GetProperty().ips.ConstData().OnlyOneIP())
281 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
285 uint32_t ip = u->GetCurrIP();
293 users->CloseSearch(h);
295 //-----------------------------------------------------------------------------
296 void PING::AddUser(USER_PTR u)
298 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
301 usersList.push_back(u);
303 //-----------------------------------------------------------------------------
304 void PING::DelUser(USER_PTR u)
306 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
308 UnSetUserNotifiers(u);
310 list<USER_PTR>::iterator users_iter;
311 users_iter = usersList.begin();
313 while (users_iter != usersList.end())
315 if (u == *users_iter)
317 usersList.erase(users_iter);
323 //-----------------------------------------------------------------------------
324 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
326 ping.pinger.DelIP(oldIP);
329 ping.pinger.AddIP(newIP);
332 //-----------------------------------------------------------------------------
333 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
335 if (oldIPS.OnlyOneIP())
337 ping.pinger.DelIP(oldIPS[0].ip);
340 if (newIPS.OnlyOneIP())
342 ping.pinger.AddIP(newIPS[0].ip);
345 //-----------------------------------------------------------------------------
346 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
350 //-----------------------------------------------------------------------------
351 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
355 //-----------------------------------------------------------------------------