2 #include <sys/socket.h>
17 #include "stg/common.h"
18 #include "stg/plugin_creator.h"
25 PLUGIN_CREATOR<SMUX> smc;
27 bool SPrefixLess(const Sensors::value_type & a,
28 const Sensors::value_type & b);
30 bool SPrefixLess(const Sensors::value_type & a,
31 const Sensors::value_type & b)
33 return a.first.PrefixLess(b.first);
38 extern "C" PLUGIN * GetPlugin();
42 return smc.GetPlugin();
45 SMUX_SETTINGS::SMUX_SETTINGS()
52 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
55 std::vector<PARAM_VALUE>::const_iterator pvi;
59 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
60 if (pvi == s.moduleParams.end())
62 errorStr = "Parameter \'Port\' not found.";
63 printfd(__FILE__, "Parameter 'Port' not found\n");
66 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
68 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
69 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
72 port = static_cast<uint16_t>(p);
74 pv.param = "Password";
75 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
76 if (pvi == s.moduleParams.end())
78 errorStr = "Parameter \'Password\' not found.";
79 printfd(__FILE__, "Parameter 'Password' not found\n");
84 password = pvi->value[0];
88 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
89 if (pvi == s.moduleParams.end())
91 errorStr = "Parameter \'Server\' not found.";
92 printfd(__FILE__, "Parameter 'Server' not found\n");
95 ip = inet_strington(pvi->value[0]);
115 needReconnect(false),
124 addUserNotifier(*this),
125 delUserNotifier(*this),
126 addDelTariffNotifier(*this),
127 logger(GetPluginLogger(GetStgLogger(), "smux"))
129 pthread_mutex_init(&mutex, NULL);
131 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
132 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
133 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
134 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
136 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
137 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
138 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
144 Sensors::iterator it;
145 for (it = sensors.begin(); it != sensors.end(); ++it)
150 for (it = tables.begin(); it != tables.end(); ++it)
153 printfd(__FILE__, "SMUX::~SMUX()\n");
154 pthread_mutex_destroy(&mutex);
157 int SMUX::ParseSettings()
159 return smuxSettings.ParseSettings(settings);
164 assert(users != NULL && "users must not be NULL");
165 assert(tariffs != NULL && "tariffs must not be NULL");
166 assert(admins != NULL && "admins must not be NULL");
167 assert(services != NULL && "services must not be NULL");
168 assert(corporations != NULL && "corporations must not be NULL");
169 assert(traffcounter != NULL && "traffcounter must not be NULL");
172 needReconnect = true;
175 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
176 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
177 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
178 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
179 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
180 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
181 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
182 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
183 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
184 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
185 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
186 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
188 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
190 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
192 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
194 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
196 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
199 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);
205 Sensors::const_iterator it(sensors.begin());
206 while (it != sensors.end())
208 printfd(__FILE__, "%s = %s\n",
209 it->first.ToString().c_str(),
210 it->second->ToString().c_str());
217 if (pthread_create(&thread, NULL, Runner, this))
219 errorStr = "Cannot create thread.";
220 logger("Cannot create thread.");
221 printfd(__FILE__, "Cannot create thread\n");
231 printfd(__FILE__, "SMUX::Stop() - Before\n");
236 //5 seconds to thread stops itself
237 for (int i = 0; i < 25 && !stopped; i++)
239 struct timespec ts = {0, 200000000};
240 nanosleep(&ts, NULL);
245 pthread_join(thread, NULL);
251 for (it = tables.begin(); it != tables.end(); ++it)
255 Sensors::iterator it;
256 for (it = sensors.begin(); it != sensors.end(); ++it)
260 tables.erase(tables.begin(), tables.end());
261 sensors.erase(sensors.begin(), sensors.end());
271 printfd(__FILE__, "SMUX::Stop() - After\n");
281 logger("Reconnected successfully.");
285 void * SMUX::Runner(void * d)
288 sigfillset(&signalSet);
289 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
291 SMUX * smux = static_cast<SMUX *>(d);
301 if (!SendOpenPDU(sock))
302 needReconnect = true;
303 if (!SendRReqPDU(sock))
304 needReconnect = true;
310 if (WaitPackets(sock) && !needReconnect)
312 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
316 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
321 else if (running && needReconnect)
330 bool SMUX::PrepareNet()
332 sock = socket(AF_INET, SOCK_STREAM, 0);
336 errorStr = "Cannot create socket.";
337 logger("Cannot create a socket: %s", strerror(errno));
338 printfd(__FILE__, "Cannot create socket\n");
342 struct sockaddr_in addr;
344 addr.sin_family = AF_INET;
345 addr.sin_port = htons(smuxSettings.GetPort());
346 addr.sin_addr.s_addr = smuxSettings.GetIP();
348 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
350 errorStr = "Cannot connect.";
351 logger("Cannot connect the socket: %s", strerror(errno));
352 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
359 bool SMUX::Reconnect()
361 if (needReconnect && difftime(time(NULL), lastReconnectTry) < reconnectTimeout)
364 time(&lastReconnectTry);
368 if (SendOpenPDU(sock))
369 if (SendRReqPDU(sock))
371 reconnectTimeout = 1;
372 needReconnect = false;
373 logger("Connected successfully");
374 printfd(__FILE__, "Connected successfully\n");
379 if (reconnectTimeout < 60)
380 reconnectTimeout *= 2;
382 needReconnect = true;
386 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
388 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
389 if (it != smuxHandlers.end())
391 return (this->*(it->second))(pdus);
396 switch (pdus->present)
398 case SMUX_PDUs_PR_NOTHING:
399 printfd(__FILE__, "PDUs: nothing\n");
401 case SMUX_PDUs_PR_open:
402 printfd(__FILE__, "PDUs: open\n");
404 case SMUX_PDUs_PR_registerRequest:
405 printfd(__FILE__, "PDUs: registerRequest\n");
408 printfd(__FILE__, "PDUs: undefined\n");
410 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
416 bool SMUX::UpdateTables()
420 Tables::iterator it(tables.begin());
421 while (it != tables.end())
425 it->second->UpdateSensors(newSensors);
427 catch (const std::runtime_error & ex)
430 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
431 it->first.c_str(), ex.what());
439 Sensors::iterator it(newSensors.begin());
440 while (it != newSensors.end())
449 while (it != tables.end())
451 std::pair<Sensors::iterator, Sensors::iterator> res;
452 res = std::equal_range(sensors.begin(),
454 std::pair<OID, Sensor *>(OID(it->first), NULL),
456 Sensors::iterator sit(res.first);
457 while (sit != res.second)
462 sensors.erase(res.first, res.second);
466 sensors.insert(newSensors.begin(), newSensors.end());
471 void SMUX::SetNotifier(USER_PTR userPtr)
473 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
474 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
477 void SMUX::UnsetNotifier(USER_PTR userPtr)
479 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
480 while (it != notifiers.end())
482 if (it->GetUserPtr() == userPtr)
484 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
492 void SMUX::SetNotifiers()
494 int h = users->OpenSearch();
495 assert(h && "USERS::OpenSearch is always correct");
498 while (!users->SearchNext(h, &u))
501 users->CloseSearch(h);
503 users->AddNotifierUserAdd(&addUserNotifier);
504 users->AddNotifierUserDel(&delUserNotifier);
506 tariffs->AddNotifierAdd(&addDelTariffNotifier);
507 tariffs->AddNotifierDel(&addDelTariffNotifier);
510 void SMUX::ResetNotifiers()
512 tariffs->DelNotifierDel(&addDelTariffNotifier);
513 tariffs->DelNotifierAdd(&addDelTariffNotifier);
515 users->DelNotifierUserDel(&delUserNotifier);
516 users->DelNotifierUserAdd(&addUserNotifier);
518 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
519 while (it != notifiers.end())
521 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));