4 #include "stg/locker.h"
5 #include "stg/user_property.h"
14 using STG::PING_SETTINGS;
19 extern "C" STG::Plugin* GetPlugin()
26 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
29 int PING_SETTINGS::ParseSettings(const ModuleSettings & s)
32 std::vector<ParamValue>::const_iterator pvi;
34 pv.param = "PingDelay";
35 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
36 if (pvi == s.moduleParams.end() || pvi->value.empty())
38 errorStr = "Parameter \'PingDelay\' not found.";
39 printfd(__FILE__, "Parameter 'PingDelay' not found\n");
42 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay) != 0)
44 errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
45 printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
51 //-----------------------------------------------------------------------------
55 logger(PluginLogger::get("ping"))
58 //-----------------------------------------------------------------------------
59 int PING::ParseSettings()
61 auto ret = pingSettings.ParseSettings(settings);
63 errorStr = pingSettings.GetStrError();
66 //-----------------------------------------------------------------------------
71 m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); });
72 m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
74 pinger.SetDelayTime(pingSettings.GetPingDelay());
77 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
81 //-----------------------------------------------------------------------------
84 std::lock_guard lock(m_mutex);
86 if (!m_thread.joinable())
90 m_thread.request_stop();
91 //5 seconds to thread stops itself
92 struct timespec ts = {0, 200000000};
93 for (int i = 0; i < 25; i++)
98 nanosleep(&ts, nullptr);
101 m_onAddUserConn.disconnect();
102 m_onDelUserConn.disconnect();
113 //-----------------------------------------------------------------------------
114 bool PING::IsRunning()
118 //-----------------------------------------------------------------------------
119 void PING::Run(std::stop_token token)
122 sigfillset(&signalSet);
123 pthread_sigmask(SIG_BLOCK, &signalSet, nullptr);
127 long delay = (10000000 * pingSettings.GetPingDelay()) / 3 + 50000000;
129 while (!token.stop_requested())
131 auto iter = usersList.begin();
133 std::lock_guard lock(m_mutex);
134 while (iter != usersList.end())
136 if ((*iter)->GetProperties().ips.ConstData().onlyOneIP())
138 uint32_t ip = (*iter)->GetProperties().ips.ConstData()[0].ip;
140 if (pinger.GetIPTime(ip, &t) == 0)
143 (*iter)->UpdatePingTime(t);
148 uint32_t ip = (*iter)->GetCurrIP();
152 if (pinger.GetIPTime(ip, &t) == 0)
155 (*iter)->UpdatePingTime(t);
162 struct timespec ts = {delay / 1000000000, delay % 1000000000};
163 for (int i = 0; i < 100; i++)
165 if (!token.stop_requested())
167 nanosleep(&ts, nullptr);
174 //-----------------------------------------------------------------------------
175 void PING::SetUserNotifiers(UserPtr u)
177 m_conns.emplace_back(
179 u->afterCurrIPChange([this](auto oldVal, auto newVal){ updateCurrIP(oldVal, newVal); }),
180 u->GetProperties().ips.afterChange([this](const auto& oldVal, const auto& newVal){ updateIPs(oldVal, newVal); })
183 //-----------------------------------------------------------------------------
184 void PING::UnSetUserNotifiers(UserPtr u)
186 m_conns.erase(std::remove_if(m_conns.begin(), m_conns.end(),
187 [u](const auto& c){ return std::get<0>(c) == u->GetID(); }),
190 //-----------------------------------------------------------------------------
191 void PING::GetUsers()
193 std::lock_guard lock(m_mutex);
196 int h = users->OpenSearch();
197 assert(h && "USERS::OpenSearch is always correct");
199 while (users->SearchNext(h, &u) == 0)
201 usersList.push_back(u);
203 if (u->GetProperties().ips.ConstData().onlyOneIP())
205 pinger.AddIP(u->GetProperties().ips.ConstData()[0].ip);
209 uint32_t ip = u->GetCurrIP();
215 users->CloseSearch(h);
217 //-----------------------------------------------------------------------------
218 void PING::AddUser(UserPtr u)
220 std::lock_guard lock(m_mutex);
223 usersList.push_back(u);
225 //-----------------------------------------------------------------------------
226 void PING::DelUser(UserPtr u)
228 std::lock_guard lock(m_mutex);
230 UnSetUserNotifiers(u);
232 std::list<UserPtr>::iterator users_iter;
233 users_iter = usersList.begin();
235 while (users_iter != usersList.end())
237 if (u == *users_iter)
239 usersList.erase(users_iter);
245 //-----------------------------------------------------------------------------
246 void PING::updateCurrIP(uint32_t oldVal, uint32_t newVal)
248 pinger.DelIP(oldVal);
250 pinger.AddIP(newVal);
252 //-----------------------------------------------------------------------------
253 void PING::updateIPs(const UserIPs& oldVal, const UserIPs& newVal)
255 if (oldVal.onlyOneIP())
256 pinger.DelIP(oldVal[0].ip);
258 if (newVal.onlyOneIP())
259 pinger.AddIP(newVal[0].ip);