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");
284 void * SMUX::Runner(void * d)
287 sigfillset(&signalSet);
288 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
290 SMUX * smux = static_cast<SMUX *>(d);
300 if (!SendOpenPDU(sock))
301 needReconnect = true;
302 if (!SendRReqPDU(sock))
303 needReconnect = true;
309 if (WaitPackets(sock) && !needReconnect)
311 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
315 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
320 else if (running && needReconnect)
329 bool SMUX::PrepareNet()
331 sock = socket(AF_INET, SOCK_STREAM, 0);
335 errorStr = "Cannot create socket.";
336 logger("Cannot create a socket: %s", strerror(errno));
337 printfd(__FILE__, "Cannot create socket\n");
341 struct sockaddr_in addr;
343 addr.sin_family = AF_INET;
344 addr.sin_port = htons(smuxSettings.GetPort());
345 addr.sin_addr.s_addr = smuxSettings.GetIP();
347 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
349 errorStr = "Cannot connect.";
350 logger("Cannot connect the socket: %s", strerror(errno));
351 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
358 bool SMUX::Reconnect()
360 if (needReconnect && difftime(time(NULL), lastReconnectTry) < reconnectTimeout)
363 time(&lastReconnectTry);
367 if (SendOpenPDU(sock))
368 if (SendRReqPDU(sock))
370 reconnectTimeout = 1;
371 needReconnect = false;
372 logger("Connected successfully");
373 printfd(__FILE__, "Connected successfully\n");
378 if (reconnectTimeout < 60)
379 reconnectTimeout *= 2;
381 needReconnect = true;
385 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
387 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
388 if (it != smuxHandlers.end())
390 return (this->*(it->second))(pdus);
395 switch (pdus->present)
397 case SMUX_PDUs_PR_NOTHING:
398 printfd(__FILE__, "PDUs: nothing\n");
400 case SMUX_PDUs_PR_open:
401 printfd(__FILE__, "PDUs: open\n");
403 case SMUX_PDUs_PR_registerRequest:
404 printfd(__FILE__, "PDUs: registerRequest\n");
407 printfd(__FILE__, "PDUs: undefined\n");
409 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
415 bool SMUX::UpdateTables()
419 Tables::iterator it(tables.begin());
420 while (it != tables.end())
424 it->second->UpdateSensors(newSensors);
426 catch (const std::runtime_error & ex)
429 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
430 it->first.c_str(), ex.what());
438 Sensors::iterator it(newSensors.begin());
439 while (it != newSensors.end())
448 while (it != tables.end())
450 std::pair<Sensors::iterator, Sensors::iterator> res;
451 res = std::equal_range(sensors.begin(),
453 std::pair<OID, Sensor *>(OID(it->first), NULL),
455 Sensors::iterator sit(res.first);
456 while (sit != res.second)
461 sensors.erase(res.first, res.second);
465 sensors.insert(newSensors.begin(), newSensors.end());
470 void SMUX::SetNotifier(USER_PTR userPtr)
472 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
473 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
476 void SMUX::UnsetNotifier(USER_PTR userPtr)
478 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
479 while (it != notifiers.end())
481 if (it->GetUserPtr() == userPtr)
483 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
491 void SMUX::SetNotifiers()
493 int h = users->OpenSearch();
494 assert(h && "USERS::OpenSearch is always correct");
497 while (!users->SearchNext(h, &u))
500 users->CloseSearch(h);
502 users->AddNotifierUserAdd(&addUserNotifier);
503 users->AddNotifierUserDel(&delUserNotifier);
505 tariffs->AddNotifierAdd(&addDelTariffNotifier);
506 tariffs->AddNotifierDel(&addDelTariffNotifier);
509 void SMUX::ResetNotifiers()
511 tariffs->DelNotifierDel(&addDelTariffNotifier);
512 tariffs->DelNotifierAdd(&addDelTariffNotifier);
514 users->DelNotifierUserDel(&delUserNotifier);
515 users->DelNotifierUserAdd(&addUserNotifier);
517 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
518 while (it != notifiers.end())
520 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));