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 PING_SETTINGS::PING_SETTINGS()
44 //-----------------------------------------------------------------------------
45 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
48 vector<PARAM_VALUE>::const_iterator pvi;
50 pv.param = "PingDelay";
51 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
52 if (pvi == s.moduleParams.end())
54 errorStr = "Parameter \'PingDelay\' not found.";
55 printfd(__FILE__, "Parameter 'PingDelay' not found\n");
58 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
60 errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
61 printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
67 //-----------------------------------------------------------------------------
72 onAddUserNotifier(*this),
73 onDelUserNotifier(*this)
75 pthread_mutex_init(&mutex, NULL);
77 //-----------------------------------------------------------------------------
80 pthread_mutex_destroy(&mutex);
82 //-----------------------------------------------------------------------------
83 const std::string PING::GetVersion() const
85 return "Pinger v.1.01";
87 //-----------------------------------------------------------------------------
88 void PING::SetSettings(const MODULE_SETTINGS & s)
92 //-----------------------------------------------------------------------------
93 int PING::ParseSettings()
95 int ret = pingSettings.ParseSettings(settings);
97 errorStr = pingSettings.GetStrError();
100 //-----------------------------------------------------------------------------
101 void PING::SetUsers(USERS * u)
105 //-----------------------------------------------------------------------------
106 const std::string & PING::GetStrError() const
110 //-----------------------------------------------------------------------------
115 users->AddNotifierUserAdd(&onAddUserNotifier);
116 users->AddNotifierUserDel(&onDelUserNotifier);
120 pinger.SetDelayTime(pingSettings.GetPingDelay());
123 if (pthread_create(&thread, NULL, Run, this))
125 errorStr = "Cannot start thread.";
126 printfd(__FILE__, "Cannot start thread\n");
132 //-----------------------------------------------------------------------------
135 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
142 //5 seconds to thread stops itself
143 struct timespec ts = {0, 200000000};
144 for (int i = 0; i < 25; i++)
149 nanosleep(&ts, NULL);
152 //after 5 seconds waiting thread still running. now kill it
155 printfd(__FILE__, "kill PING thread.\n");
156 if (pthread_kill(thread, SIGINT))
158 errorStr = "Cannot kill PING thread.";
159 printfd(__FILE__, "Cannot kill PING thread.\n");
162 printfd(__FILE__, "PING killed\n");
165 users->DelNotifierUserAdd(&onAddUserNotifier);
166 users->DelNotifierUserDel(&onDelUserNotifier);
168 list<USER_PTR>::iterator users_iter;
169 users_iter = usersList.begin();
170 while (users_iter != usersList.end())
172 UnSetUserNotifiers(*users_iter);
178 //-----------------------------------------------------------------------------
179 bool PING::IsRunning()
183 //-----------------------------------------------------------------------------
184 void * PING::Run(void * d)
186 PING * ping = (PING *)d;
187 ping->isRunning = true;
189 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
191 while (ping->nonstop)
193 list<USER_PTR>::iterator iter = ping->usersList.begin();
195 STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
196 while (iter != ping->usersList.end())
198 if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
200 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
202 if (ping->pinger.GetIPTime(ip, &t) == 0)
205 (*iter)->UpdatePingTime(t);
210 uint32_t ip = (*iter)->GetCurrIP();
214 if (ping->pinger.GetIPTime(ip, &t) == 0)
217 (*iter)->UpdatePingTime(t);
224 struct timespec ts = {delay / 1000000000, delay % 1000000000};
225 for (int i = 0; i < 100; i++)
229 nanosleep(&ts, NULL);
234 ping->isRunning = false;
237 //-----------------------------------------------------------------------------
238 uint16_t PING::GetStartPosition() const
242 //-----------------------------------------------------------------------------
243 uint16_t PING::GetStopPosition() const
247 //-----------------------------------------------------------------------------
248 void PING::SetUserNotifiers(USER_PTR u)
250 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
251 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
253 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
254 ChgIPNotifierList.push_front(ChgIPNotifier);
256 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
257 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
259 //-----------------------------------------------------------------------------
260 void PING::UnSetUserNotifiers(USER_PTR u)
263 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
264 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
266 list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
267 list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
269 currIPter = find_if(ChgCurrIPNotifierList.begin(),
270 ChgCurrIPNotifierList.end(),
271 IsContainsUserCurrIP);
273 if (currIPter != ChgCurrIPNotifierList.end())
275 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
276 ChgCurrIPNotifierList.erase(currIPter);
278 // --- CurrIP end ---
281 IPIter = find_if(ChgIPNotifierList.begin(),
282 ChgIPNotifierList.end(),
285 if (IPIter != ChgIPNotifierList.end())
287 IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
288 ChgIPNotifierList.erase(IPIter);
292 //-----------------------------------------------------------------------------
293 void PING::GetUsers()
295 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
298 int h = users->OpenSearch();
301 printfd(__FILE__, "users->OpenSearch() error\n");
305 while (users->SearchNext(h, &u) == 0)
307 usersList.push_back(u);
309 if (u->GetProperty().ips.ConstData().OnlyOneIP())
311 pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
315 uint32_t ip = u->GetCurrIP();
323 users->CloseSearch(h);
325 //-----------------------------------------------------------------------------
326 void PING::AddUser(USER_PTR u)
328 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
331 usersList.push_back(u);
333 //-----------------------------------------------------------------------------
334 void PING::DelUser(USER_PTR u)
336 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
338 UnSetUserNotifiers(u);
340 list<USER_PTR>::iterator users_iter;
341 users_iter = usersList.begin();
343 while (users_iter != usersList.end())
345 if (u == *users_iter)
347 usersList.erase(users_iter);
353 //-----------------------------------------------------------------------------
354 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
356 ping.pinger.DelIP(oldIP);
359 ping.pinger.AddIP(newIP);
362 //-----------------------------------------------------------------------------
363 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
365 if (oldIPS.OnlyOneIP())
367 ping.pinger.DelIP(oldIPS[0].ip);
370 if (newIPS.OnlyOneIP())
372 ping.pinger.AddIP(newIPS[0].ip);
375 //-----------------------------------------------------------------------------
376 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
380 //-----------------------------------------------------------------------------
381 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
385 //-----------------------------------------------------------------------------