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 return a.first.PrefixLess(b.first);
35 extern "C" PLUGIN * GetPlugin();
39 return smc.GetPlugin();
42 SMUX_SETTINGS::SMUX_SETTINGS()
49 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
52 std::vector<PARAM_VALUE>::const_iterator pvi;
56 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
57 if (pvi == s.moduleParams.end() || pvi->value.empty())
59 errorStr = "Parameter \'Port\' not found.";
60 printfd(__FILE__, "Parameter 'Port' not found\n");
63 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
65 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
66 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
69 port = static_cast<uint16_t>(p);
71 pv.param = "Password";
72 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
73 if (pvi == s.moduleParams.end() || pvi->value.empty())
75 errorStr = "Parameter \'Password\' not found.";
76 printfd(__FILE__, "Parameter 'Password' not found\n");
81 password = pvi->value[0];
85 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
86 if (pvi == s.moduleParams.end() || pvi->value.empty())
88 errorStr = "Parameter \'Server\' not found.";
89 printfd(__FILE__, "Parameter 'Server' not found\n");
92 ip = inet_strington(pvi->value[0]);
112 needReconnect(false),
121 addUserNotifier(*this),
122 delUserNotifier(*this),
123 addDelTariffNotifier(*this),
124 logger(GetPluginLogger(GetStgLogger(), "smux"))
126 pthread_mutex_init(&mutex, NULL);
128 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
129 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
130 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
131 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
133 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
134 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
135 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
141 Sensors::iterator it;
142 for (it = sensors.begin(); it != sensors.end(); ++it)
147 for (it = tables.begin(); it != tables.end(); ++it)
150 printfd(__FILE__, "SMUX::~SMUX()\n");
151 pthread_mutex_destroy(&mutex);
154 int SMUX::ParseSettings()
156 return smuxSettings.ParseSettings(settings);
161 assert(users != NULL && "users must not be NULL");
162 assert(tariffs != NULL && "tariffs must not be NULL");
163 assert(admins != NULL && "admins must not be NULL");
164 assert(services != NULL && "services must not be NULL");
165 assert(corporations != NULL && "corporations must not be NULL");
166 assert(traffcounter != NULL && "traffcounter must not be NULL");
169 needReconnect = true;
172 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
173 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
174 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
175 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
176 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
177 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
178 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
179 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
180 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
181 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
182 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
183 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
185 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
187 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
189 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
191 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
193 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
196 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);
202 Sensors::const_iterator it(sensors.begin());
203 while (it != sensors.end())
205 printfd(__FILE__, "%s = %s\n",
206 it->first.ToString().c_str(),
207 it->second->ToString().c_str());
214 if (pthread_create(&thread, NULL, Runner, this))
216 errorStr = "Cannot create thread.";
217 logger("Cannot create thread.");
218 printfd(__FILE__, "Cannot create thread\n");
228 printfd(__FILE__, "SMUX::Stop() - Before\n");
233 //5 seconds to thread stops itself
234 for (int i = 0; i < 25 && !stopped; i++)
236 struct timespec ts = {0, 200000000};
237 nanosleep(&ts, NULL);
242 pthread_join(thread, NULL);
248 for (it = tables.begin(); it != tables.end(); ++it)
252 Sensors::iterator it;
253 for (it = sensors.begin(); it != sensors.end(); ++it)
257 tables.erase(tables.begin(), tables.end());
258 sensors.erase(sensors.begin(), sensors.end());
268 printfd(__FILE__, "SMUX::Stop() - After\n");
272 int SMUX::Reload(const MODULE_SETTINGS & /*ms*/)
280 printfd(__FILE__, "SMUX reconnected succesfully.\n");
281 logger("Reconnected successfully.");
286 void * SMUX::Runner(void * d)
289 sigfillset(&signalSet);
290 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
292 SMUX * smux = static_cast<SMUX *>(d);
302 if (!SendOpenPDU(sock))
303 needReconnect = true;
304 if (!SendRReqPDU(sock))
305 needReconnect = true;
311 if (WaitPackets(sock) && !needReconnect)
313 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
317 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
322 else if (running && needReconnect)
331 bool SMUX::PrepareNet()
333 sock = socket(AF_INET, SOCK_STREAM, 0);
337 errorStr = "Cannot create socket.";
338 logger("Cannot create a socket: %s", strerror(errno));
339 printfd(__FILE__, "Cannot create socket\n");
343 struct sockaddr_in addr;
345 addr.sin_family = AF_INET;
346 addr.sin_port = htons(smuxSettings.GetPort());
347 addr.sin_addr.s_addr = smuxSettings.GetIP();
349 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
351 errorStr = "Cannot connect.";
352 logger("Cannot connect the socket: %s", strerror(errno));
353 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
360 bool SMUX::Reconnect()
362 if (needReconnect && difftime(time(NULL), lastReconnectTry) < reconnectTimeout)
365 time(&lastReconnectTry);
369 if (SendOpenPDU(sock))
370 if (SendRReqPDU(sock))
372 reconnectTimeout = 1;
373 needReconnect = false;
374 logger("Connected successfully");
375 printfd(__FILE__, "Connected successfully\n");
380 if (reconnectTimeout < 60)
381 reconnectTimeout *= 2;
383 needReconnect = true;
387 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
389 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
390 if (it != smuxHandlers.end())
392 return (this->*(it->second))(pdus);
397 switch (pdus->present)
399 case SMUX_PDUs_PR_NOTHING:
400 printfd(__FILE__, "PDUs: nothing\n");
402 case SMUX_PDUs_PR_open:
403 printfd(__FILE__, "PDUs: open\n");
405 case SMUX_PDUs_PR_registerRequest:
406 printfd(__FILE__, "PDUs: registerRequest\n");
409 printfd(__FILE__, "PDUs: undefined\n");
411 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
417 bool SMUX::UpdateTables()
421 Tables::iterator it(tables.begin());
422 while (it != tables.end())
426 it->second->UpdateSensors(newSensors);
428 catch (const std::runtime_error & ex)
431 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
432 it->first.c_str(), ex.what());
440 Sensors::iterator it(newSensors.begin());
441 while (it != newSensors.end())
450 while (it != tables.end())
452 std::pair<Sensors::iterator, Sensors::iterator> res;
453 res = std::equal_range(sensors.begin(),
455 std::pair<OID, Sensor *>(OID(it->first), NULL),
457 Sensors::iterator sit(res.first);
458 while (sit != res.second)
463 sensors.erase(res.first, res.second);
467 sensors.insert(newSensors.begin(), newSensors.end());
472 void SMUX::SetNotifier(USER_PTR userPtr)
474 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
475 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
478 void SMUX::UnsetNotifier(USER_PTR userPtr)
480 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
481 while (it != notifiers.end())
483 if (it->GetUserPtr() == userPtr)
485 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
493 void SMUX::SetNotifiers()
495 int h = users->OpenSearch();
496 assert(h && "USERS::OpenSearch is always correct");
499 while (!users->SearchNext(h, &u))
502 users->CloseSearch(h);
504 users->AddNotifierUserAdd(&addUserNotifier);
505 users->AddNotifierUserDel(&delUserNotifier);
507 tariffs->AddNotifierAdd(&addDelTariffNotifier);
508 tariffs->AddNotifierDel(&addDelTariffNotifier);
511 void SMUX::ResetNotifiers()
513 tariffs->DelNotifierDel(&addDelTariffNotifier);
514 tariffs->DelNotifierAdd(&addDelTariffNotifier);
516 users->DelNotifierUserDel(&delUserNotifier);
517 users->DelNotifierUserAdd(&addUserNotifier);
519 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
520 while (it != notifiers.end())
522 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));