4 #include "stg/locker.h"
5 #include "stg/user_property.h"
15 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 template <typename varType>
19 class HAS_USER: public std::binary_function<varType, UserPtr, bool>
22 explicit HAS_USER(const UserPtr & u) : user(u) {}
23 bool operator()(varType notifier) const
25 return notifier.GetUser() == user;
32 extern "C" STG::Plugin* GetPlugin()
37 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
40 int PING_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
43 std::vector<STG::ParamValue>::const_iterator pvi;
45 pv.param = "PingDelay";
46 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
47 if (pvi == s.moduleParams.end() || pvi->value.empty())
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 //-----------------------------------------------------------------------------
67 onAddUserNotifier(*this),
68 onDelUserNotifier(*this),
69 logger(STG::PluginLogger::get("ping"))
71 pthread_mutex_init(&mutex, NULL);
73 //-----------------------------------------------------------------------------
76 pthread_mutex_destroy(&mutex);
78 //-----------------------------------------------------------------------------
79 int PING::ParseSettings()
81 int ret = pingSettings.ParseSettings(settings);
83 errorStr = pingSettings.GetStrError();
86 //-----------------------------------------------------------------------------
91 users->AddNotifierUserAdd(&onAddUserNotifier);
92 users->AddNotifierUserDel(&onDelUserNotifier);
96 pinger.SetDelayTime(pingSettings.GetPingDelay());
99 if (pthread_create(&thread, NULL, Run, this))
101 errorStr = "Cannot start thread.";
102 logger("Cannot create thread.");
103 printfd(__FILE__, "Cannot start thread\n");
109 //-----------------------------------------------------------------------------
112 STG_LOCKER lock(&mutex);
119 //5 seconds to thread stops itself
120 struct timespec ts = {0, 200000000};
121 for (int i = 0; i < 25; i++)
126 nanosleep(&ts, NULL);
129 users->DelNotifierUserAdd(&onAddUserNotifier);
130 users->DelNotifierUserDel(&onDelUserNotifier);
132 std::list<UserPtr>::iterator users_iter;
133 users_iter = usersList.begin();
134 while (users_iter != usersList.end())
136 UnSetUserNotifiers(*users_iter);
145 //-----------------------------------------------------------------------------
146 bool PING::IsRunning()
150 //-----------------------------------------------------------------------------
151 void * PING::Run(void * d)
154 sigfillset(&signalSet);
155 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
157 PING * ping = static_cast<PING *>(d);
158 ping->isRunning = true;
160 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
162 while (ping->nonstop)
164 std::list<UserPtr>::iterator iter = ping->usersList.begin();
166 STG_LOCKER lock(&ping->mutex);
167 while (iter != ping->usersList.end())
169 if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
171 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
173 if (ping->pinger.GetIPTime(ip, &t) == 0)
176 (*iter)->UpdatePingTime(t);
181 uint32_t ip = (*iter)->GetCurrIP();
185 if (ping->pinger.GetIPTime(ip, &t) == 0)
188 (*iter)->UpdatePingTime(t);
195 struct timespec ts = {delay / 1000000000, delay % 1000000000};
196 for (int i = 0; i < 100; i++)
200 nanosleep(&ts, NULL);
205 ping->isRunning = false;
208 //-----------------------------------------------------------------------------
209 void PING::SetUserNotifiers(UserPtr u)
211 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
212 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
214 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
215 ChgIPNotifierList.push_front(ChgIPNotifier);
217 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
218 u->GetProperties().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
220 //-----------------------------------------------------------------------------
221 void PING::UnSetUserNotifiers(UserPtr u)
224 HAS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
225 HAS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
227 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
228 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
230 currIPter = find_if(ChgCurrIPNotifierList.begin(),
231 ChgCurrIPNotifierList.end(),
232 IsContainsUserCurrIP);
234 if (currIPter != ChgCurrIPNotifierList.end())
236 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
237 ChgCurrIPNotifierList.erase(currIPter);
239 // --- CurrIP end ---
242 IPIter = find_if(ChgIPNotifierList.begin(),
243 ChgIPNotifierList.end(),
246 if (IPIter != ChgIPNotifierList.end())
248 IPIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*IPIter));
249 ChgIPNotifierList.erase(IPIter);
253 //-----------------------------------------------------------------------------
254 void PING::GetUsers()
256 STG_LOCKER lock(&mutex);
259 int h = users->OpenSearch();
260 assert(h && "USERS::OpenSearch is always correct");
262 while (users->SearchNext(h, &u) == 0)
264 usersList.push_back(u);
266 if (u->GetProperties().ips.ConstData().onlyOneIP())
268 pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
272 uint32_t ip = u->GetCurrIP();
278 users->CloseSearch(h);
280 //-----------------------------------------------------------------------------
281 void PING::AddUser(UserPtr u)
283 STG_LOCKER lock(&mutex);
286 usersList.push_back(u);
288 //-----------------------------------------------------------------------------
289 void PING::DelUser(UserPtr u)
291 STG_LOCKER lock(&mutex);
293 UnSetUserNotifiers(u);
295 std::list<UserPtr>::iterator users_iter;
296 users_iter = usersList.begin();
298 while (users_iter != usersList.end())
300 if (u == *users_iter)
302 usersList.erase(users_iter);
308 //-----------------------------------------------------------------------------
309 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
311 ping.pinger.DelIP(oldIP);
313 ping.pinger.AddIP(newIP);
315 //-----------------------------------------------------------------------------
316 void CHG_IPS_NOTIFIER_PING::Notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS)
318 if (oldIPS.onlyOneIP())
319 ping.pinger.DelIP(oldIPS[0].ip);
321 if (newIPS.onlyOneIP())
322 ping.pinger.AddIP(newIPS[0].ip);
324 //-----------------------------------------------------------------------------
325 void ADD_USER_NONIFIER_PING::Notify(const UserPtr & user)
329 //-----------------------------------------------------------------------------
330 void DEL_USER_NONIFIER_PING::Notify(const UserPtr & user)
334 //-----------------------------------------------------------------------------