2 #include <sys/socket.h>
17 #include "stg/common.h"
25 bool SPrefixLess(const Sensors::value_type & a,
26 const Sensors::value_type & b)
28 return a.first.PrefixLess(b.first);
33 extern "C" STG::Plugin* GetPlugin()
39 SMUX_SETTINGS::SMUX_SETTINGS()
44 int SMUX_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
50 auto pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
51 if (pvi == s.moduleParams.end() || pvi->value.empty())
53 errorStr = "Parameter \'Port\' not found.";
54 printfd(__FILE__, "Parameter 'Port' not found\n");
57 if (ParseIntInRange(pvi->value[0], 2, 65535, &p) != 0)
59 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
60 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
63 port = static_cast<uint16_t>(p);
65 pv.param = "Password";
66 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
67 if (pvi == s.moduleParams.end() || pvi->value.empty())
69 errorStr = "Parameter \'Password\' not found.";
70 printfd(__FILE__, "Parameter 'Password' not found\n");
75 password = pvi->value[0];
79 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end() || pvi->value.empty())
82 errorStr = "Parameter \'Server\' not found.";
83 printfd(__FILE__, "Parameter 'Server' not found\n");
86 ip = inet_strington(pvi->value[0]);
96 corporations(nullptr),
97 traffcounter(nullptr),
103 logger(STG::PluginLogger::get("smux"))
105 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
106 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
107 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
108 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
110 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
111 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
112 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
117 for (auto& kv : sensors)
119 for (auto& kv : tables)
121 printfd(__FILE__, "SMUX::~SMUX()\n");
124 int SMUX::ParseSettings()
126 return smuxSettings.ParseSettings(settings);
131 assert(users != nullptr && "users must not be NULL");
132 assert(tariffs != nullptr && "tariffs must not be NULL");
133 assert(admins != nullptr && "admins must not be NULL");
134 assert(services != nullptr && "services must not be NULL");
135 assert(corporations != nullptr && "corporations must not be NULL");
136 assert(traffcounter != nullptr && "traffcounter must not be NULL");
139 needReconnect = true;
142 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
143 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
144 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
145 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
146 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
147 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
148 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
149 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
150 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
151 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
152 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
153 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
155 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
157 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
159 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
161 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
163 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
166 tables[".1.3.6.1.4.1.38313.1.2.2"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.2.2", *tariffs, *users);
172 auto it = sensors.begin();
173 while (it != sensors.end())
175 printfd(__FILE__, "%s = %s\n",
176 it->first.ToString().c_str(),
177 it->second->ToString().c_str());
182 if (!m_thread.joinable())
183 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
190 printfd(__FILE__, "SMUX::Stop() - Before\n");
191 m_thread.request_stop();
195 //5 seconds to thread stops itself
196 for (int i = 0; i < 25 && !stopped; i++)
198 struct timespec ts = {0, 200000000};
199 nanosleep(&ts, nullptr);
210 for (auto& kv : sensors)
212 for (auto& kv : tables)
215 tables.erase(tables.begin(), tables.end());
216 sensors.erase(sensors.begin(), sensors.end());
225 printfd(__FILE__, "SMUX::Stop() - After\n");
229 int SMUX::Reload(const STG::ModuleSettings & /*ms*/)
237 printfd(__FILE__, "SMUX reconnected succesfully.\n");
238 logger("Reconnected successfully.");
243 void SMUX::Run(std::stop_token token)
246 if (!SendOpenPDU(sock))
247 needReconnect = true;
248 if (!SendRReqPDU(sock))
249 needReconnect = true;
252 while (!token.stop_requested())
254 if (WaitPackets(sock) && !needReconnect)
256 auto* pdus = RecvSMUXPDUs(sock);
260 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
262 else if (!token.stop_requested())
265 else if (!token.stop_requested() && needReconnect)
267 if (token.stop_requested())
274 bool SMUX::PrepareNet()
276 sock = socket(AF_INET, SOCK_STREAM, 0);
280 errorStr = "Cannot create socket.";
281 logger("Cannot create a socket: %s", strerror(errno));
282 printfd(__FILE__, "Cannot create socket\n");
286 struct sockaddr_in addr;
288 addr.sin_family = AF_INET;
289 addr.sin_port = htons(smuxSettings.GetPort());
290 addr.sin_addr.s_addr = smuxSettings.GetIP();
292 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)) != 0)
294 errorStr = "Cannot connect.";
295 logger("Cannot connect the socket: %s", strerror(errno));
296 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
303 bool SMUX::Reconnect()
305 if (needReconnect && difftime(time(nullptr), lastReconnectTry) < reconnectTimeout)
308 time(&lastReconnectTry);
312 if (SendOpenPDU(sock))
313 if (SendRReqPDU(sock))
315 reconnectTimeout = 1;
316 needReconnect = false;
317 logger("Connected successfully");
318 printfd(__FILE__, "Connected successfully\n");
323 if (reconnectTimeout < 60)
324 reconnectTimeout *= 2;
326 needReconnect = true;
330 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
332 auto it = smuxHandlers.find(pdus->present);
333 if (it != smuxHandlers.end())
334 return (this->*(it->second))(pdus);
338 switch (pdus->present)
340 case SMUX_PDUs_PR_NOTHING:
341 printfd(__FILE__, "PDUs: nothing\n");
343 case SMUX_PDUs_PR_open:
344 printfd(__FILE__, "PDUs: open\n");
346 case SMUX_PDUs_PR_registerRequest:
347 printfd(__FILE__, "PDUs: registerRequest\n");
350 printfd(__FILE__, "PDUs: undefined\n");
352 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
358 bool SMUX::UpdateTables()
362 auto it = tables.begin();
363 while (it != tables.end())
367 it->second->UpdateSensors(newSensors);
369 catch (const std::runtime_error & ex)
372 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
373 it->first.c_str(), ex.what());
381 auto sit = newSensors.begin();
382 while (sit != newSensors.end())
391 while (it != tables.end())
393 auto res = std::equal_range(sensors.begin(),
395 std::pair<OID, Sensor *>(OID(it->first), nullptr),
397 auto sit = res.first;
398 while (sit != res.second)
403 sensors.erase(res.first, res.second);
407 sensors.insert(newSensors.begin(), newSensors.end());
412 void SMUX::SetNotifier(UserPtr userPtr)
414 notifiers.emplace_back(*this, userPtr);
415 userPtr->GetProperties().tariffName.AddAfterNotifier(¬ifiers.back());
418 void SMUX::UnsetNotifier(UserPtr userPtr)
420 auto it = notifiers.begin();
421 while (it != notifiers.end())
423 if (it->GetUserPtr() == userPtr)
425 userPtr->GetProperties().tariffName.DelAfterNotifier(&(*it));
433 void SMUX::SetNotifiers()
435 int h = users->OpenSearch();
436 assert(h && "USERS::OpenSearch is always correct");
439 while (users->SearchNext(h, &u) == 0)
442 users->CloseSearch(h);
444 m_onAddUserConn = users->onAdd([this](auto user){
448 m_onDelUserConn = users->onDel([this](auto user){
453 auto updateTables = [this](const STG::TariffData&){ UpdateTables(); };
454 m_onAddTariffConn = tariffs->onAdd(updateTables);
455 m_onDelTariffConn = tariffs->onDel(updateTables);
458 void SMUX::ResetNotifiers()
460 m_onAddTariffConn.disconnect();
461 m_onDelTariffConn.disconnect();
463 m_onAddUserConn.disconnect();
464 m_onDelUserConn.disconnect();
466 auto it = notifiers.begin();
467 while (it != notifiers.end())
469 it->GetUserPtr()->GetProperties().tariffName.DelAfterNotifier(&(*it));