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 logger(STG::PluginLogger::get("ping"))
69 //-----------------------------------------------------------------------------
70 int PING::ParseSettings()
72 auto ret = pingSettings.ParseSettings(settings);
74 errorStr = pingSettings.GetStrError();
77 //-----------------------------------------------------------------------------
82 m_onAddUserConn = users->onUserAdd([this](auto user){ AddUser(user); });
83 m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); });
85 pinger.SetDelayTime(pingSettings.GetPingDelay());
88 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
92 //-----------------------------------------------------------------------------
95 std::lock_guard lock(m_mutex);
97 if (!m_thread.joinable())
101 m_thread.request_stop();
102 //5 seconds to thread stops itself
103 struct timespec ts = {0, 200000000};
104 for (int i = 0; i < 25; i++)
109 nanosleep(&ts, nullptr);
112 m_onAddUserConn.disconnect();
113 m_onDelUserConn.disconnect();
115 std::list<UserPtr>::iterator users_iter;
116 users_iter = usersList.begin();
117 while (users_iter != usersList.end())
119 UnSetUserNotifiers(*users_iter);
130 //-----------------------------------------------------------------------------
131 bool PING::IsRunning()
135 //-----------------------------------------------------------------------------
136 void PING::Run(std::stop_token token)
139 sigfillset(&signalSet);
140 pthread_sigmask(SIG_BLOCK, &signalSet, nullptr);
144 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
146 while (!token.stop_requested())
148 auto iter = usersList.begin();
150 std::lock_guard lock(m_mutex);
151 while (iter != usersList.end())
153 if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
155 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
157 if (pinger.GetIPTime(ip, &t) == 0)
160 (*iter)->UpdatePingTime(t);
165 uint32_t ip = (*iter)->GetCurrIP();
169 if (pinger.GetIPTime(ip, &t) == 0)
172 (*iter)->UpdatePingTime(t);
179 struct timespec ts = {delay / 1000000000, delay % 1000000000};
180 for (int i = 0; i < 100; i++)
182 if (!token.stop_requested())
184 nanosleep(&ts, nullptr);
191 //-----------------------------------------------------------------------------
192 void PING::SetUserNotifiers(UserPtr u)
194 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
195 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
197 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
198 ChgIPNotifierList.push_front(ChgIPNotifier);
200 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
201 u->GetProperties().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
203 //-----------------------------------------------------------------------------
204 void PING::UnSetUserNotifiers(UserPtr u)
207 HAS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
208 HAS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
210 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
211 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
213 currIPter = find_if(ChgCurrIPNotifierList.begin(),
214 ChgCurrIPNotifierList.end(),
215 IsContainsUserCurrIP);
217 if (currIPter != ChgCurrIPNotifierList.end())
219 currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
220 ChgCurrIPNotifierList.erase(currIPter);
222 // --- CurrIP end ---
225 IPIter = find_if(ChgIPNotifierList.begin(),
226 ChgIPNotifierList.end(),
229 if (IPIter != ChgIPNotifierList.end())
231 IPIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*IPIter));
232 ChgIPNotifierList.erase(IPIter);
236 //-----------------------------------------------------------------------------
237 void PING::GetUsers()
239 std::lock_guard lock(m_mutex);
242 int h = users->OpenSearch();
243 assert(h && "USERS::OpenSearch is always correct");
245 while (users->SearchNext(h, &u) == 0)
247 usersList.push_back(u);
249 if (u->GetProperties().ips.ConstData().onlyOneIP())
251 pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
255 uint32_t ip = u->GetCurrIP();
261 users->CloseSearch(h);
263 //-----------------------------------------------------------------------------
264 void PING::AddUser(UserPtr u)
266 std::lock_guard lock(m_mutex);
269 usersList.push_back(u);
271 //-----------------------------------------------------------------------------
272 void PING::DelUser(UserPtr u)
274 std::lock_guard lock(m_mutex);
276 UnSetUserNotifiers(u);
278 std::list<UserPtr>::iterator users_iter;
279 users_iter = usersList.begin();
281 while (users_iter != usersList.end())
283 if (u == *users_iter)
285 usersList.erase(users_iter);
291 //-----------------------------------------------------------------------------
292 void CHG_CURRIP_NOTIFIER_PING::notify(const uint32_t & oldIP, const uint32_t & newIP)
294 ping.pinger.DelIP(oldIP);
296 ping.pinger.AddIP(newIP);
298 //-----------------------------------------------------------------------------
299 void CHG_IPS_NOTIFIER_PING::notify(const STG::UserIPs & oldIPS, const STG::UserIPs & newIPS)
301 if (oldIPS.onlyOneIP())
302 ping.pinger.DelIP(oldIPS[0].ip);
304 if (newIPS.onlyOneIP())
305 ping.pinger.AddIP(newIPS[0].ip);