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);
136 //-----------------------------------------------------------------------------
137 bool PING::IsRunning()
141 //-----------------------------------------------------------------------------
142 void PING::Run(std::stop_token token)
145 sigfillset(&signalSet);
146 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
150 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
152 while (!token.stop_requested())
154 std::list<UserPtr>::iterator iter = usersList.begin();
156 std::lock_guard lock(m_mutex);
157 while (iter != usersList.end())
159 if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
161 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
163 if (pinger.GetIPTime(ip, &t) == 0)
166 (*iter)->UpdatePingTime(t);
171 uint32_t ip = (*iter)->GetCurrIP();
175 if (pinger.GetIPTime(ip, &t) == 0)
178 (*iter)->UpdatePingTime(t);
185 struct timespec ts = {delay / 1000000000, delay % 1000000000};
186 for (int i = 0; i < 100; i++)
188 if (!token.stop_requested())
190 nanosleep(&ts, NULL);
197 //-----------------------------------------------------------------------------
198 void PING::SetUserNotifiers(UserPtr u)
200 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
201 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
203 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
204 ChgIPNotifierList.push_front(ChgIPNotifier);
206 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
207 u->GetProperties().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
209 //-----------------------------------------------------------------------------
210 void PING::UnSetUserNotifiers(UserPtr u)
213 HAS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
214 HAS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
216 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
217 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
219 currIPter = find_if(ChgCurrIPNotifierList.begin(),
220 ChgCurrIPNotifierList.end(),
221 IsContainsUserCurrIP);
223 if (currIPter != ChgCurrIPNotifierList.end())
225 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
226 ChgCurrIPNotifierList.erase(currIPter);
228 // --- CurrIP end ---
231 IPIter = find_if(ChgIPNotifierList.begin(),
232 ChgIPNotifierList.end(),
235 if (IPIter != ChgIPNotifierList.end())
237 IPIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*IPIter));
238 ChgIPNotifierList.erase(IPIter);
242 //-----------------------------------------------------------------------------
243 void PING::GetUsers()
245 std::lock_guard lock(m_mutex);
248 int h = users->OpenSearch();
249 assert(h && "USERS::OpenSearch is always correct");
251 while (users->SearchNext(h, &u) == 0)
253 usersList.push_back(u);
255 if (u->GetProperties().ips.ConstData().onlyOneIP())
257 pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
261 uint32_t ip = u->GetCurrIP();
267 users->CloseSearch(h);
269 //-----------------------------------------------------------------------------
270 void PING::AddUser(UserPtr u)
272 std::lock_guard lock(m_mutex);
275 usersList.push_back(u);
277 //-----------------------------------------------------------------------------
278 void PING::DelUser(UserPtr u)
280 std::lock_guard lock(m_mutex);
282 UnSetUserNotifiers(u);
284 std::list<UserPtr>::iterator users_iter;
285 users_iter = usersList.begin();
287 while (users_iter != usersList.end())
289 if (u == *users_iter)
291 usersList.erase(users_iter);
297 //-----------------------------------------------------------------------------
298 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
300 ping.pinger.DelIP(oldIP);
302 ping.pinger.AddIP(newIP);
304 //-----------------------------------------------------------------------------
305 void CHG_IPS_NOTIFIER_PING::Notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS)
307 if (oldIPS.onlyOneIP())
308 ping.pinger.DelIP(oldIPS[0].ip);
310 if (newIPS.onlyOneIP())
311 ping.pinger.AddIP(newIPS[0].ip);
313 //-----------------------------------------------------------------------------
314 void ADD_USER_NONIFIER_PING::Notify(const UserPtr & user)
318 //-----------------------------------------------------------------------------
319 void DEL_USER_NONIFIER_PING::Notify(const UserPtr & user)
323 //-----------------------------------------------------------------------------