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) != 0)
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 //-----------------------------------------------------------------------------
72 int PING::ParseSettings()
74 auto ret = pingSettings.ParseSettings(settings);
76 errorStr = pingSettings.GetStrError();
79 //-----------------------------------------------------------------------------
84 users->AddNotifierUserAdd(&onAddUserNotifier);
85 users->AddNotifierUserDel(&onDelUserNotifier);
87 pinger.SetDelayTime(pingSettings.GetPingDelay());
90 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
94 //-----------------------------------------------------------------------------
97 std::lock_guard lock(m_mutex);
99 if (!m_thread.joinable())
103 m_thread.request_stop();
104 //5 seconds to thread stops itself
105 struct timespec ts = {0, 200000000};
106 for (int i = 0; i < 25; i++)
111 nanosleep(&ts, nullptr);
114 users->DelNotifierUserAdd(&onAddUserNotifier);
115 users->DelNotifierUserDel(&onDelUserNotifier);
117 std::list<UserPtr>::iterator users_iter;
118 users_iter = usersList.begin();
119 while (users_iter != usersList.end())
121 UnSetUserNotifiers(*users_iter);
132 //-----------------------------------------------------------------------------
133 bool PING::IsRunning()
137 //-----------------------------------------------------------------------------
138 void PING::Run(std::stop_token token)
141 sigfillset(&signalSet);
142 pthread_sigmask(SIG_BLOCK, &signalSet, nullptr);
146 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
148 while (!token.stop_requested())
150 auto iter = usersList.begin();
152 std::lock_guard lock(m_mutex);
153 while (iter != usersList.end())
155 if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
157 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
159 if (pinger.GetIPTime(ip, &t) == 0)
162 (*iter)->UpdatePingTime(t);
167 uint32_t ip = (*iter)->GetCurrIP();
171 if (pinger.GetIPTime(ip, &t) == 0)
174 (*iter)->UpdatePingTime(t);
181 struct timespec ts = {delay / 1000000000, delay % 1000000000};
182 for (int i = 0; i < 100; i++)
184 if (!token.stop_requested())
186 nanosleep(&ts, nullptr);
193 //-----------------------------------------------------------------------------
194 void PING::SetUserNotifiers(UserPtr u)
196 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
197 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
199 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
200 ChgIPNotifierList.push_front(ChgIPNotifier);
202 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
203 u->GetProperties().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
205 //-----------------------------------------------------------------------------
206 void PING::UnSetUserNotifiers(UserPtr u)
209 HAS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
210 HAS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
212 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
213 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
215 currIPter = find_if(ChgCurrIPNotifierList.begin(),
216 ChgCurrIPNotifierList.end(),
217 IsContainsUserCurrIP);
219 if (currIPter != ChgCurrIPNotifierList.end())
221 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
222 ChgCurrIPNotifierList.erase(currIPter);
224 // --- CurrIP end ---
227 IPIter = find_if(ChgIPNotifierList.begin(),
228 ChgIPNotifierList.end(),
231 if (IPIter != ChgIPNotifierList.end())
233 IPIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*IPIter));
234 ChgIPNotifierList.erase(IPIter);
238 //-----------------------------------------------------------------------------
239 void PING::GetUsers()
241 std::lock_guard lock(m_mutex);
244 int h = users->OpenSearch();
245 assert(h && "USERS::OpenSearch is always correct");
247 while (users->SearchNext(h, &u) == 0)
249 usersList.push_back(u);
251 if (u->GetProperties().ips.ConstData().onlyOneIP())
253 pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
257 uint32_t ip = u->GetCurrIP();
263 users->CloseSearch(h);
265 //-----------------------------------------------------------------------------
266 void PING::AddUser(UserPtr u)
268 std::lock_guard lock(m_mutex);
271 usersList.push_back(u);
273 //-----------------------------------------------------------------------------
274 void PING::DelUser(UserPtr u)
276 std::lock_guard lock(m_mutex);
278 UnSetUserNotifiers(u);
280 std::list<UserPtr>::iterator users_iter;
281 users_iter = usersList.begin();
283 while (users_iter != usersList.end())
285 if (u == *users_iter)
287 usersList.erase(users_iter);
293 //-----------------------------------------------------------------------------
294 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
296 ping.pinger.DelIP(oldIP);
298 ping.pinger.AddIP(newIP);
300 //-----------------------------------------------------------------------------
301 void CHG_IPS_NOTIFIER_PING::Notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS)
303 if (oldIPS.onlyOneIP())
304 ping.pinger.DelIP(oldIPS[0].ip);
306 if (newIPS.onlyOneIP())
307 ping.pinger.AddIP(newIPS[0].ip);
309 //-----------------------------------------------------------------------------
310 void ADD_USER_NONIFIER_PING::Notify(const UserPtr & user)
314 //-----------------------------------------------------------------------------
315 void DEL_USER_NONIFIER_PING::Notify(const UserPtr & user)
319 //-----------------------------------------------------------------------------