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");
283 printfd(__FILE__, "SMUX reconnected succesfully.\n");
284 logger("Reconnected successfully.");
289 void * SMUX::Runner(void * d)
292 sigfillset(&signalSet);
293 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
295 SMUX * smux = static_cast<SMUX *>(d);
305 if (!SendOpenPDU(sock))
306 needReconnect = true;
307 if (!SendRReqPDU(sock))
308 needReconnect = true;
314 if (WaitPackets(sock) && !needReconnect)
316 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
320 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
325 else if (running && needReconnect)
334 bool SMUX::PrepareNet()
336 sock = socket(AF_INET, SOCK_STREAM, 0);
340 errorStr = "Cannot create socket.";
341 logger("Cannot create a socket: %s", strerror(errno));
342 printfd(__FILE__, "Cannot create socket\n");
346 struct sockaddr_in addr;
348 addr.sin_family = AF_INET;
349 addr.sin_port = htons(smuxSettings.GetPort());
350 addr.sin_addr.s_addr = smuxSettings.GetIP();
352 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
354 errorStr = "Cannot connect.";
355 logger("Cannot connect the socket: %s", strerror(errno));
356 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
363 bool SMUX::Reconnect()
365 if (needReconnect && difftime(time(NULL), lastReconnectTry) < reconnectTimeout)
368 time(&lastReconnectTry);
372 if (SendOpenPDU(sock))
373 if (SendRReqPDU(sock))
375 reconnectTimeout = 1;
376 needReconnect = false;
377 logger("Connected successfully");
378 printfd(__FILE__, "Connected successfully\n");
383 if (reconnectTimeout < 60)
384 reconnectTimeout *= 2;
386 needReconnect = true;
390 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
392 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
393 if (it != smuxHandlers.end())
395 return (this->*(it->second))(pdus);
400 switch (pdus->present)
402 case SMUX_PDUs_PR_NOTHING:
403 printfd(__FILE__, "PDUs: nothing\n");
405 case SMUX_PDUs_PR_open:
406 printfd(__FILE__, "PDUs: open\n");
408 case SMUX_PDUs_PR_registerRequest:
409 printfd(__FILE__, "PDUs: registerRequest\n");
412 printfd(__FILE__, "PDUs: undefined\n");
414 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
420 bool SMUX::UpdateTables()
424 Tables::iterator it(tables.begin());
425 while (it != tables.end())
429 it->second->UpdateSensors(newSensors);
431 catch (const std::runtime_error & ex)
434 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
435 it->first.c_str(), ex.what());
443 Sensors::iterator it(newSensors.begin());
444 while (it != newSensors.end())
453 while (it != tables.end())
455 std::pair<Sensors::iterator, Sensors::iterator> res;
456 res = std::equal_range(sensors.begin(),
458 std::pair<OID, Sensor *>(OID(it->first), NULL),
460 Sensors::iterator sit(res.first);
461 while (sit != res.second)
466 sensors.erase(res.first, res.second);
470 sensors.insert(newSensors.begin(), newSensors.end());
475 void SMUX::SetNotifier(USER_PTR userPtr)
477 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
478 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
481 void SMUX::UnsetNotifier(USER_PTR userPtr)
483 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
484 while (it != notifiers.end())
486 if (it->GetUserPtr() == userPtr)
488 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
496 void SMUX::SetNotifiers()
498 int h = users->OpenSearch();
499 assert(h && "USERS::OpenSearch is always correct");
502 while (!users->SearchNext(h, &u))
505 users->CloseSearch(h);
507 users->AddNotifierUserAdd(&addUserNotifier);
508 users->AddNotifierUserDel(&delUserNotifier);
510 tariffs->AddNotifierAdd(&addDelTariffNotifier);
511 tariffs->AddNotifierDel(&addDelTariffNotifier);
514 void SMUX::ResetNotifiers()
516 tariffs->DelNotifierDel(&addDelTariffNotifier);
517 tariffs->DelNotifierAdd(&addDelTariffNotifier);
519 users->DelNotifierUserDel(&delUserNotifier);
520 users->DelNotifierUserAdd(&addUserNotifier);
522 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
523 while (it != notifiers.end())
525 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));