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),
78 logger(GetPluginLogger(GetStgLogger(), "ping"))
80 pthread_mutex_init(&mutex, NULL);
82 //-----------------------------------------------------------------------------
85 pthread_mutex_destroy(&mutex);
87 //-----------------------------------------------------------------------------
88 int PING::ParseSettings()
90 int ret = pingSettings.ParseSettings(settings);
92 errorStr = pingSettings.GetStrError();
95 //-----------------------------------------------------------------------------
100 users->AddNotifierUserAdd(&onAddUserNotifier);
101 users->AddNotifierUserDel(&onDelUserNotifier);
105 pinger.SetDelayTime(pingSettings.GetPingDelay());
108 if (pthread_create(&thread, NULL, Run, this))
110 errorStr = "Cannot start thread.";
111 printfd(__FILE__, "Cannot start thread\n");
117 //-----------------------------------------------------------------------------
120 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
127 //5 seconds to thread stops itself
128 struct timespec ts = {0, 200000000};
129 for (int i = 0; i < 25; i++)
134 nanosleep(&ts, NULL);
137 users->DelNotifierUserAdd(&onAddUserNotifier);
138 users->DelNotifierUserDel(&onDelUserNotifier);
140 list<USER_PTR>::iterator users_iter;
141 users_iter = usersList.begin();
142 while (users_iter != usersList.end())
144 UnSetUserNotifiers(*users_iter);
153 //-----------------------------------------------------------------------------
154 bool PING::IsRunning()
158 //-----------------------------------------------------------------------------
159 void * PING::Run(void * d)
162 sigfillset(&signalSet);
163 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
165 PING * ping = static_cast<PING *>(d);
166 ping->isRunning = true;
168 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
170 while (ping->nonstop)
172 list<USER_PTR>::iterator iter = ping->usersList.begin();
174 STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
175 while (iter != ping->usersList.end())
177 if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
179 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
181 if (ping->pinger.GetIPTime(ip, &t) == 0)
184 (*iter)->UpdatePingTime(t);
189 uint32_t ip = (*iter)->GetCurrIP();
193 if (ping->pinger.GetIPTime(ip, &t) == 0)
196 (*iter)->UpdatePingTime(t);
203 struct timespec ts = {delay / 1000000000, delay % 1000000000};
204 for (int i = 0; i < 100; i++)
208 nanosleep(&ts, NULL);
213 ping->isRunning = false;
216 //-----------------------------------------------------------------------------
217 void PING::SetUserNotifiers(USER_PTR u)
219 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
220 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
222 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
223 ChgIPNotifierList.push_front(ChgIPNotifier);
225 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
226 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
228 //-----------------------------------------------------------------------------
229 void PING::UnSetUserNotifiers(USER_PTR u)
232 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
233 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
235 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
236 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
238 currIPter = find_if(ChgCurrIPNotifierList.begin(),
239 ChgCurrIPNotifierList.end(),
240 IsContainsUserCurrIP);
242 if (currIPter != ChgCurrIPNotifierList.end())
244 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
245 ChgCurrIPNotifierList.erase(currIPter);
247 // --- CurrIP end ---
250 IPIter = find_if(ChgIPNotifierList.begin(),
251 ChgIPNotifierList.end(),
254 if (IPIter != ChgIPNotifierList.end())
256 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
257 ChgIPNotifierList.erase(IPIter);
261 //-----------------------------------------------------------------------------
262 void PING::GetUsers()
264 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
267 int h = users->OpenSearch();
268 assert(h && "USERS::OpenSearch is always correct");
270 while (users->SearchNext(h, &u) == 0)
272 usersList.push_back(u);
274 if (u->GetProperty().ips.ConstData().OnlyOneIP())
276 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
280 uint32_t ip = u->GetCurrIP();
288 users->CloseSearch(h);
290 //-----------------------------------------------------------------------------
291 void PING::AddUser(USER_PTR u)
293 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
296 usersList.push_back(u);
298 //-----------------------------------------------------------------------------
299 void PING::DelUser(USER_PTR u)
301 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
303 UnSetUserNotifiers(u);
305 list<USER_PTR>::iterator users_iter;
306 users_iter = usersList.begin();
308 while (users_iter != usersList.end())
310 if (u == *users_iter)
312 usersList.erase(users_iter);
318 //-----------------------------------------------------------------------------
319 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
321 ping.pinger.DelIP(oldIP);
324 ping.pinger.AddIP(newIP);
327 //-----------------------------------------------------------------------------
328 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
330 if (oldIPS.OnlyOneIP())
332 ping.pinger.DelIP(oldIPS[0].ip);
335 if (newIPS.OnlyOneIP())
337 ping.pinger.AddIP(newIPS[0].ip);
340 //-----------------------------------------------------------------------------
341 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
345 //-----------------------------------------------------------------------------
346 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
350 //-----------------------------------------------------------------------------