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);
224 for (it = tables.begin(); it != tables.end(); ++it)
228 Sensors::iterator it;
229 for (it = sensors.begin(); it != sensors.end(); ++it)
233 tables.erase(tables.begin(), tables.end());
234 sensors.erase(sensors.begin(), sensors.end());
243 printfd(__FILE__, "SMUX::Stop() - After\n");
247 int SMUX::Reload(const STG::ModuleSettings & /*ms*/)
255 printfd(__FILE__, "SMUX reconnected succesfully.\n");
256 logger("Reconnected successfully.");
261 void SMUX::Run(std::stop_token token)
264 if (!SendOpenPDU(sock))
265 needReconnect = true;
266 if (!SendRReqPDU(sock))
267 needReconnect = true;
270 while (!token.stop_requested())
272 if (WaitPackets(sock) && !needReconnect)
274 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
278 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
280 else if (!token.stop_requested())
283 else if (!token.stop_requested() && needReconnect)
285 if (token.stop_requested())
292 bool SMUX::PrepareNet()
294 sock = socket(AF_INET, SOCK_STREAM, 0);
298 errorStr = "Cannot create socket.";
299 logger("Cannot create a socket: %s", strerror(errno));
300 printfd(__FILE__, "Cannot create socket\n");
304 struct sockaddr_in addr;
306 addr.sin_family = AF_INET;
307 addr.sin_port = htons(smuxSettings.GetPort());
308 addr.sin_addr.s_addr = smuxSettings.GetIP();
310 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
312 errorStr = "Cannot connect.";
313 logger("Cannot connect the socket: %s", strerror(errno));
314 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
321 bool SMUX::Reconnect()
323 if (needReconnect && difftime(time(NULL), lastReconnectTry) < reconnectTimeout)
326 time(&lastReconnectTry);
330 if (SendOpenPDU(sock))
331 if (SendRReqPDU(sock))
333 reconnectTimeout = 1;
334 needReconnect = false;
335 logger("Connected successfully");
336 printfd(__FILE__, "Connected successfully\n");
341 if (reconnectTimeout < 60)
342 reconnectTimeout *= 2;
344 needReconnect = true;
348 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
350 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
351 if (it != smuxHandlers.end())
353 return (this->*(it->second))(pdus);
358 switch (pdus->present)
360 case SMUX_PDUs_PR_NOTHING:
361 printfd(__FILE__, "PDUs: nothing\n");
363 case SMUX_PDUs_PR_open:
364 printfd(__FILE__, "PDUs: open\n");
366 case SMUX_PDUs_PR_registerRequest:
367 printfd(__FILE__, "PDUs: registerRequest\n");
370 printfd(__FILE__, "PDUs: undefined\n");
372 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
378 bool SMUX::UpdateTables()
382 Tables::iterator it(tables.begin());
383 while (it != tables.end())
387 it->second->UpdateSensors(newSensors);
389 catch (const std::runtime_error & ex)
392 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
393 it->first.c_str(), ex.what());
401 Sensors::iterator sit(newSensors.begin());
402 while (sit != newSensors.end())
411 while (it != tables.end())
413 std::pair<Sensors::iterator, Sensors::iterator> res;
414 res = std::equal_range(sensors.begin(),
416 std::pair<OID, Sensor *>(OID(it->first), NULL),
418 Sensors::iterator sit(res.first);
419 while (sit != res.second)
424 sensors.erase(res.first, res.second);
428 sensors.insert(newSensors.begin(), newSensors.end());
433 void SMUX::SetNotifier(UserPtr userPtr)
435 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
436 userPtr->GetProperties().tariffName.AddAfterNotifier(¬ifiers.back());
439 void SMUX::UnsetNotifier(UserPtr userPtr)
441 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
442 while (it != notifiers.end())
444 if (it->GetUserPtr() == userPtr)
446 userPtr->GetProperties().tariffName.DelAfterNotifier(&(*it));
454 void SMUX::SetNotifiers()
456 int h = users->OpenSearch();
457 assert(h && "USERS::OpenSearch is always correct");
460 while (!users->SearchNext(h, &u))
463 users->CloseSearch(h);
465 users->AddNotifierUserAdd(&addUserNotifier);
466 users->AddNotifierUserDel(&delUserNotifier);
468 tariffs->AddNotifierAdd(&addDelTariffNotifier);
469 tariffs->AddNotifierDel(&addDelTariffNotifier);
472 void SMUX::ResetNotifiers()
474 tariffs->DelNotifierDel(&addDelTariffNotifier);
475 tariffs->DelNotifierAdd(&addDelTariffNotifier);
477 users->DelNotifierUserDel(&delUserNotifier);
478 users->DelNotifierUserAdd(&addUserNotifier);
480 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
481 while (it != notifiers.end())
483 it->GetUserPtr()->GetProperties().tariffName.DelAfterNotifier(&(*it));