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 //-----------------------------------------------------------------------------
66 onAddUserNotifier(*this),
67 onDelUserNotifier(*this),
68 logger(STG::PluginLogger::get("ping"))
71 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 int PING::ParseSettings()
78 int ret = pingSettings.ParseSettings(settings);
80 errorStr = pingSettings.GetStrError();
83 //-----------------------------------------------------------------------------
88 users->AddNotifierUserAdd(&onAddUserNotifier);
89 users->AddNotifierUserDel(&onDelUserNotifier);
91 pinger.SetDelayTime(pingSettings.GetPingDelay());
94 m_thread = std::jthread([this](auto token){ Run(token); });
98 //-----------------------------------------------------------------------------
101 std::lock_guard lock(m_mutex);
103 if (!m_thread.joinable())
107 m_thread.request_stop();
108 //5 seconds to thread stops itself
109 struct timespec ts = {0, 200000000};
110 for (int i = 0; i < 25; i++)
115 nanosleep(&ts, NULL);
118 users->DelNotifierUserAdd(&onAddUserNotifier);
119 users->DelNotifierUserDel(&onDelUserNotifier);
121 std::list<UserPtr>::iterator users_iter;
122 users_iter = usersList.begin();
123 while (users_iter != usersList.end())
125 UnSetUserNotifiers(*users_iter);
134 //-----------------------------------------------------------------------------
135 bool PING::IsRunning()
139 //-----------------------------------------------------------------------------
140 void PING::Run(std::stop_token token)
143 sigfillset(&signalSet);
144 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
148 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
150 while (!token.stop_requested())
152 std::list<UserPtr>::iterator iter = usersList.begin();
154 std::lock_guard lock(m_mutex);
155 while (iter != usersList.end())
157 if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
159 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
161 if (pinger.GetIPTime(ip, &t) == 0)
164 (*iter)->UpdatePingTime(t);
169 uint32_t ip = (*iter)->GetCurrIP();
173 if (pinger.GetIPTime(ip, &t) == 0)
176 (*iter)->UpdatePingTime(t);
183 struct timespec ts = {delay / 1000000000, delay % 1000000000};
184 for (int i = 0; i < 100; i++)
186 if (!token.stop_requested())
188 nanosleep(&ts, NULL);
195 //-----------------------------------------------------------------------------
196 void PING::SetUserNotifiers(UserPtr u)
198 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
199 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
201 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
202 ChgIPNotifierList.push_front(ChgIPNotifier);
204 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
205 u->GetProperties().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
207 //-----------------------------------------------------------------------------
208 void PING::UnSetUserNotifiers(UserPtr u)
211 HAS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
212 HAS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
214 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
215 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
217 currIPter = find_if(ChgCurrIPNotifierList.begin(),
218 ChgCurrIPNotifierList.end(),
219 IsContainsUserCurrIP);
221 if (currIPter != ChgCurrIPNotifierList.end())
223 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
224 ChgCurrIPNotifierList.erase(currIPter);
226 // --- CurrIP end ---
229 IPIter = find_if(ChgIPNotifierList.begin(),
230 ChgIPNotifierList.end(),
233 if (IPIter != ChgIPNotifierList.end())
235 IPIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*IPIter));
236 ChgIPNotifierList.erase(IPIter);
240 //-----------------------------------------------------------------------------
241 void PING::GetUsers()
243 std::lock_guard lock(m_mutex);
246 int h = users->OpenSearch();
247 assert(h && "USERS::OpenSearch is always correct");
249 while (users->SearchNext(h, &u) == 0)
251 usersList.push_back(u);
253 if (u->GetProperties().ips.ConstData().onlyOneIP())
255 pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
259 uint32_t ip = u->GetCurrIP();
265 users->CloseSearch(h);
267 //-----------------------------------------------------------------------------
268 void PING::AddUser(UserPtr u)
270 std::lock_guard lock(m_mutex);
273 usersList.push_back(u);
275 //-----------------------------------------------------------------------------
276 void PING::DelUser(UserPtr u)
278 std::lock_guard lock(m_mutex);
280 UnSetUserNotifiers(u);
282 std::list<UserPtr>::iterator users_iter;
283 users_iter = usersList.begin();
285 while (users_iter != usersList.end())
287 if (u == *users_iter)
289 usersList.erase(users_iter);
295 //-----------------------------------------------------------------------------
296 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
298 ping.pinger.DelIP(oldIP);
300 ping.pinger.AddIP(newIP);
302 //-----------------------------------------------------------------------------
303 void CHG_IPS_NOTIFIER_PING::Notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS)
305 if (oldIPS.onlyOneIP())
306 ping.pinger.DelIP(oldIPS[0].ip);
308 if (newIPS.onlyOneIP())
309 ping.pinger.AddIP(newIPS[0].ip);
311 //-----------------------------------------------------------------------------
312 void ADD_USER_NONIFIER_PING::Notify(const UserPtr & user)
316 //-----------------------------------------------------------------------------
317 void DEL_USER_NONIFIER_PING::Notify(const UserPtr & user)
321 //-----------------------------------------------------------------------------