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()
46 int SMUX_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
49 std::vector<STG::ParamValue>::const_iterator pvi;
53 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
54 if (pvi == s.moduleParams.end() || pvi->value.empty())
56 errorStr = "Parameter \'Port\' not found.";
57 printfd(__FILE__, "Parameter 'Port' not found\n");
60 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
62 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
63 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
66 port = static_cast<uint16_t>(p);
68 pv.param = "Password";
69 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
70 if (pvi == s.moduleParams.end() || pvi->value.empty())
72 errorStr = "Parameter \'Password\' not found.";
73 printfd(__FILE__, "Parameter 'Password' not found\n");
78 password = pvi->value[0];
82 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
83 if (pvi == s.moduleParams.end() || pvi->value.empty())
85 errorStr = "Parameter \'Server\' not found.";
86 printfd(__FILE__, "Parameter 'Server' not found\n");
89 ip = inet_strington(pvi->value[0]);
102 needReconnect(false),
106 addUserNotifier(*this),
107 delUserNotifier(*this),
108 addDelTariffNotifier(*this),
109 logger(STG::PluginLogger::get("smux"))
111 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
112 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
113 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
114 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
116 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
117 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
118 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
124 Sensors::iterator it;
125 for (it = sensors.begin(); it != sensors.end(); ++it)
130 for (it = tables.begin(); it != tables.end(); ++it)
133 printfd(__FILE__, "SMUX::~SMUX()\n");
136 int SMUX::ParseSettings()
138 return smuxSettings.ParseSettings(settings);
143 assert(users != NULL && "users must not be NULL");
144 assert(tariffs != NULL && "tariffs must not be NULL");
145 assert(admins != NULL && "admins must not be NULL");
146 assert(services != NULL && "services must not be NULL");
147 assert(corporations != NULL && "corporations must not be NULL");
148 assert(traffcounter != NULL && "traffcounter must not be NULL");
151 needReconnect = true;
154 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
155 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
156 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
157 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
158 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
169 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
171 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
173 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
175 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
178 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);
184 Sensors::const_iterator it(sensors.begin());
185 while (it != sensors.end())
187 printfd(__FILE__, "%s = %s\n",
188 it->first.ToString().c_str(),
189 it->second->ToString().c_str());
194 if (!m_thread.joinable())
195 m_thread = std::jthread([this](auto token){ Run(token); });
202 printfd(__FILE__, "SMUX::Stop() - Before\n");
203 m_thread.request_stop();
207 //5 seconds to thread stops itself
208 for (int i = 0; i < 25 && !stopped; i++)
210 struct timespec ts = {0, 200000000};
211 nanosleep(&ts, NULL);
222 for (it = tables.begin(); it != tables.end(); ++it)
226 Sensors::iterator it;
227 for (it = sensors.begin(); it != sensors.end(); ++it)
231 tables.erase(tables.begin(), tables.end());
232 sensors.erase(sensors.begin(), sensors.end());
241 printfd(__FILE__, "SMUX::Stop() - After\n");
245 int SMUX::Reload(const STG::ModuleSettings & /*ms*/)
253 printfd(__FILE__, "SMUX reconnected succesfully.\n");
254 logger("Reconnected successfully.");
259 void SMUX::Run(std::stop_token token)
262 if (!SendOpenPDU(sock))
263 needReconnect = true;
264 if (!SendRReqPDU(sock))
265 needReconnect = true;
268 while (!token.stop_requested())
270 if (WaitPackets(sock) && !needReconnect)
272 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
276 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
278 else if (!token.stop_requested())
281 else if (!token.stop_requested() && needReconnect)
283 if (token.stop_requested())
290 bool SMUX::PrepareNet()
292 sock = socket(AF_INET, SOCK_STREAM, 0);
296 errorStr = "Cannot create socket.";
297 logger("Cannot create a socket: %s", strerror(errno));
298 printfd(__FILE__, "Cannot create socket\n");
302 struct sockaddr_in addr;
304 addr.sin_family = AF_INET;
305 addr.sin_port = htons(smuxSettings.GetPort());
306 addr.sin_addr.s_addr = smuxSettings.GetIP();
308 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
310 errorStr = "Cannot connect.";
311 logger("Cannot connect the socket: %s", strerror(errno));
312 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
319 bool SMUX::Reconnect()
321 if (needReconnect && difftime(time(NULL), lastReconnectTry) < reconnectTimeout)
324 time(&lastReconnectTry);
328 if (SendOpenPDU(sock))
329 if (SendRReqPDU(sock))
331 reconnectTimeout = 1;
332 needReconnect = false;
333 logger("Connected successfully");
334 printfd(__FILE__, "Connected successfully\n");
339 if (reconnectTimeout < 60)
340 reconnectTimeout *= 2;
342 needReconnect = true;
346 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
348 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
349 if (it != smuxHandlers.end())
351 return (this->*(it->second))(pdus);
356 switch (pdus->present)
358 case SMUX_PDUs_PR_NOTHING:
359 printfd(__FILE__, "PDUs: nothing\n");
361 case SMUX_PDUs_PR_open:
362 printfd(__FILE__, "PDUs: open\n");
364 case SMUX_PDUs_PR_registerRequest:
365 printfd(__FILE__, "PDUs: registerRequest\n");
368 printfd(__FILE__, "PDUs: undefined\n");
370 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
376 bool SMUX::UpdateTables()
380 Tables::iterator it(tables.begin());
381 while (it != tables.end())
385 it->second->UpdateSensors(newSensors);
387 catch (const std::runtime_error & ex)
390 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
391 it->first.c_str(), ex.what());
399 Sensors::iterator sit(newSensors.begin());
400 while (sit != newSensors.end())
409 while (it != tables.end())
411 std::pair<Sensors::iterator, Sensors::iterator> res;
412 res = std::equal_range(sensors.begin(),
414 std::pair<OID, Sensor *>(OID(it->first), NULL),
416 Sensors::iterator sit(res.first);
417 while (sit != res.second)
422 sensors.erase(res.first, res.second);
426 sensors.insert(newSensors.begin(), newSensors.end());
431 void SMUX::SetNotifier(UserPtr userPtr)
433 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
434 userPtr->GetProperties().tariffName.AddAfterNotifier(¬ifiers.back());
437 void SMUX::UnsetNotifier(UserPtr userPtr)
439 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
440 while (it != notifiers.end())
442 if (it->GetUserPtr() == userPtr)
444 userPtr->GetProperties().tariffName.DelAfterNotifier(&(*it));
452 void SMUX::SetNotifiers()
454 int h = users->OpenSearch();
455 assert(h && "USERS::OpenSearch is always correct");
458 while (!users->SearchNext(h, &u))
461 users->CloseSearch(h);
463 users->AddNotifierUserAdd(&addUserNotifier);
464 users->AddNotifierUserDel(&delUserNotifier);
466 tariffs->AddNotifierAdd(&addDelTariffNotifier);
467 tariffs->AddNotifierDel(&addDelTariffNotifier);
470 void SMUX::ResetNotifiers()
472 tariffs->DelNotifierDel(&addDelTariffNotifier);
473 tariffs->DelNotifierAdd(&addDelTariffNotifier);
475 users->DelNotifierUserDel(&delUserNotifier);
476 users->DelNotifierUserAdd(&addUserNotifier);
478 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
479 while (it != notifiers.end())
481 it->GetUserPtr()->GetProperties().tariffName.DelAfterNotifier(&(*it));