8 #include "stg/locker.h"
9 #include "stg/user_property.h"
10 #include "stg/plugin_creator.h"
15 PLUGIN_CREATOR<PING> pc;
17 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
19 //-----------------------------------------------------------------------------
20 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
21 template <typename varType>
22 class IS_CONTAINS_USER: public std::binary_function<varType, USER_PTR, bool>
25 IS_CONTAINS_USER(const USER_PTR & u) : user(u) {}
26 bool operator()(varType notifier) const
28 return notifier.GetUser() == user;
31 const USER_PTR & user;
35 extern "C" PLUGIN * GetPlugin();
36 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
41 return pc.GetPlugin();
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
49 std::vector<PARAM_VALUE>::const_iterator pvi;
51 pv.param = "PingDelay";
52 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
53 if (pvi == s.moduleParams.end())
55 errorStr = "Parameter \'PingDelay\' not found.";
56 printfd(__FILE__, "Parameter 'PingDelay' not found\n");
59 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
61 errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
62 printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
68 //-----------------------------------------------------------------------------
80 ChgCurrIPNotifierList(),
82 onAddUserNotifier(*this),
83 onDelUserNotifier(*this),
84 logger(GetPluginLogger(GetStgLogger(), "ping"))
86 pthread_mutex_init(&mutex, NULL);
88 //-----------------------------------------------------------------------------
91 pthread_mutex_destroy(&mutex);
93 //-----------------------------------------------------------------------------
94 int PING::ParseSettings()
96 int ret = pingSettings.ParseSettings(settings);
98 errorStr = pingSettings.GetStrError();
101 //-----------------------------------------------------------------------------
106 users->AddNotifierUserAdd(&onAddUserNotifier);
107 users->AddNotifierUserDel(&onDelUserNotifier);
111 pinger.SetDelayTime(pingSettings.GetPingDelay());
114 if (pthread_create(&thread, NULL, Run, this))
116 errorStr = "Cannot start thread.";
117 logger("Cannot create thread.");
118 printfd(__FILE__, "Cannot start thread\n");
124 //-----------------------------------------------------------------------------
127 STG_LOCKER lock(&mutex);
134 //5 seconds to thread stops itself
135 struct timespec ts = {0, 200000000};
136 for (int i = 0; i < 25; i++)
141 nanosleep(&ts, NULL);
144 users->DelNotifierUserAdd(&onAddUserNotifier);
145 users->DelNotifierUserDel(&onDelUserNotifier);
147 std::list<USER_PTR>::iterator users_iter;
148 users_iter = usersList.begin();
149 while (users_iter != usersList.end())
151 UnSetUserNotifiers(*users_iter);
160 //-----------------------------------------------------------------------------
161 bool PING::IsRunning()
165 //-----------------------------------------------------------------------------
166 void * PING::Run(void * d)
169 sigfillset(&signalSet);
170 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
172 PING * ping = static_cast<PING *>(d);
173 ping->isRunning = true;
175 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
177 while (ping->nonstop)
179 std::list<USER_PTR>::iterator iter = ping->usersList.begin();
181 STG_LOCKER lock(&ping->mutex);
182 while (iter != ping->usersList.end())
184 if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
186 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
188 if (ping->pinger.GetIPTime(ip, &t) == 0)
191 (*iter)->UpdatePingTime(t);
196 uint32_t ip = (*iter)->GetCurrIP();
200 if (ping->pinger.GetIPTime(ip, &t) == 0)
203 (*iter)->UpdatePingTime(t);
210 struct timespec ts = {delay / 1000000000, delay % 1000000000};
211 for (int i = 0; i < 100; i++)
215 nanosleep(&ts, NULL);
220 ping->isRunning = false;
223 //-----------------------------------------------------------------------------
224 void PING::SetUserNotifiers(USER_PTR u)
226 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
227 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
229 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
230 ChgIPNotifierList.push_front(ChgIPNotifier);
232 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
233 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
235 //-----------------------------------------------------------------------------
236 void PING::UnSetUserNotifiers(USER_PTR u)
239 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
240 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
242 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
243 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
245 currIPter = find_if(ChgCurrIPNotifierList.begin(),
246 ChgCurrIPNotifierList.end(),
247 IsContainsUserCurrIP);
249 if (currIPter != ChgCurrIPNotifierList.end())
251 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
252 ChgCurrIPNotifierList.erase(currIPter);
254 // --- CurrIP end ---
257 IPIter = find_if(ChgIPNotifierList.begin(),
258 ChgIPNotifierList.end(),
261 if (IPIter != ChgIPNotifierList.end())
263 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
264 ChgIPNotifierList.erase(IPIter);
268 //-----------------------------------------------------------------------------
269 void PING::GetUsers()
271 STG_LOCKER lock(&mutex);
274 int h = users->OpenSearch();
275 assert(h && "USERS::OpenSearch is always correct");
277 while (users->SearchNext(h, &u) == 0)
279 usersList.push_back(u);
281 if (u->GetProperty().ips.ConstData().OnlyOneIP())
283 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
287 uint32_t ip = u->GetCurrIP();
295 users->CloseSearch(h);
297 //-----------------------------------------------------------------------------
298 void PING::AddUser(USER_PTR u)
300 STG_LOCKER lock(&mutex);
303 usersList.push_back(u);
305 //-----------------------------------------------------------------------------
306 void PING::DelUser(USER_PTR u)
308 STG_LOCKER lock(&mutex);
310 UnSetUserNotifiers(u);
312 std::list<USER_PTR>::iterator users_iter;
313 users_iter = usersList.begin();
315 while (users_iter != usersList.end())
317 if (u == *users_iter)
319 usersList.erase(users_iter);
325 //-----------------------------------------------------------------------------
326 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
328 ping.pinger.DelIP(oldIP);
331 ping.pinger.AddIP(newIP);
334 //-----------------------------------------------------------------------------
335 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
337 if (oldIPS.OnlyOneIP())
339 ping.pinger.DelIP(oldIPS[0].ip);
342 if (newIPS.OnlyOneIP())
344 ping.pinger.AddIP(newIPS[0].ip);
347 //-----------------------------------------------------------------------------
348 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
352 //-----------------------------------------------------------------------------
353 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
357 //-----------------------------------------------------------------------------