2 #include <sys/socket.h>
17 #include "stg/common.h"
18 #include "stg/plugin_creator.h"
23 PLUGIN_CREATOR<SMUX> smc;
27 return smc.GetPlugin();
30 bool SPrefixLess(const Sensors::value_type & a,
31 const Sensors::value_type & b)
33 return a.first.PrefixLess(b.first);
36 SMUX_SETTINGS::SMUX_SETTINGS()
43 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
46 std::vector<PARAM_VALUE>::const_iterator pvi;
50 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
51 if (pvi == s.moduleParams.end())
53 errorStr = "Parameter \'Port\' not found.";
54 printfd(__FILE__, "Parameter 'Port' not found\n");
57 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
59 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
60 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
65 pv.param = "Password";
66 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
67 if (pvi == s.moduleParams.end())
69 errorStr = "Parameter \'Password\' not found.";
70 printfd(__FILE__, "Parameter 'Password' not found\n");
75 password = pvi->value[0];
79 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end())
82 errorStr = "Parameter \'Server\' not found.";
83 printfd(__FILE__, "Parameter 'Server' not found\n");
86 ip = inet_strington(pvi->value[0]);
106 needReconnect(false),
115 addUserNotifier(*this),
116 delUserNotifier(*this),
117 addDelTariffNotifier(*this),
118 logger(GetPluginLogger(GetStgLogger(), "smux"))
120 pthread_mutex_init(&mutex, NULL);
122 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
123 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
124 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
125 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
127 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
128 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
129 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
135 Sensors::iterator it;
136 for (it = sensors.begin(); it != sensors.end(); ++it)
141 for (it = tables.begin(); it != tables.end(); ++it)
144 printfd(__FILE__, "SMUX::~SMUX()\n");
145 pthread_mutex_destroy(&mutex);
148 int SMUX::ParseSettings()
150 return smuxSettings.ParseSettings(settings);
155 assert(users != NULL && "users must not be NULL");
156 assert(tariffs != NULL && "tariffs must not be NULL");
157 assert(admins != NULL && "admins must not be NULL");
158 assert(services != NULL && "services must not be NULL");
159 assert(corporations != NULL && "corporations must not be NULL");
160 assert(traffcounter != NULL && "traffcounter must not be NULL");
163 needReconnect = true;
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
170 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
171 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
172 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
173 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
174 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
175 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
176 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
177 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
179 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
181 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
183 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
185 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
187 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
190 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);
196 Sensors::const_iterator it(sensors.begin());
197 while (it != sensors.end())
199 printfd(__FILE__, "%s = %s\n",
200 it->first.ToString().c_str(),
201 it->second->ToString().c_str());
208 if (pthread_create(&thread, NULL, Runner, this))
210 errorStr = "Cannot create thread.";
211 logger("Cannot create thread.");
212 printfd(__FILE__, "Cannot create thread\n");
222 printfd(__FILE__, "SMUX::Stop() - Before\n");
227 //5 seconds to thread stops itself
228 for (int i = 0; i < 25 && !stopped; i++)
230 struct timespec ts = {0, 200000000};
231 nanosleep(&ts, NULL);
236 pthread_join(thread, NULL);
242 for (it = tables.begin(); it != tables.end(); ++it)
246 Sensors::iterator it;
247 for (it = sensors.begin(); it != sensors.end(); ++it)
251 tables.erase(tables.begin(), tables.end());
252 sensors.erase(sensors.begin(), sensors.end());
262 printfd(__FILE__, "SMUX::Stop() - After\n");
275 void * SMUX::Runner(void * d)
278 sigfillset(&signalSet);
279 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
281 SMUX * smux = static_cast<SMUX *>(d);
291 if (!SendOpenPDU(sock))
292 needReconnect = true;
293 if (!SendRReqPDU(sock))
294 needReconnect = true;
300 if (WaitPackets(sock) && !needReconnect)
302 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
306 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
311 else if (running && needReconnect)
320 bool SMUX::PrepareNet()
322 sock = socket(AF_INET, SOCK_STREAM, 0);
326 errorStr = "Cannot create socket.";
327 logger("Cannot create a socket: %s", strerror(errno));
328 printfd(__FILE__, "Cannot create socket\n");
332 struct sockaddr_in addr;
334 addr.sin_family = AF_INET;
335 addr.sin_port = htons(smuxSettings.GetPort());
336 addr.sin_addr.s_addr = smuxSettings.GetIP();
338 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
340 errorStr = "Cannot connect.";
341 logger("Cannot connect the socket: %s", strerror(errno));
342 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
349 bool SMUX::Reconnect()
351 if (needReconnect && difftime(time(NULL), lastReconnectTry) < reconnectTimeout)
354 time(&lastReconnectTry);
358 if (SendOpenPDU(sock))
359 if (SendRReqPDU(sock))
361 reconnectTimeout = 1;
362 needReconnect = false;
363 logger("Connected successfully");
364 printfd(__FILE__, "Connected successfully\n");
369 if (reconnectTimeout < 60)
370 reconnectTimeout *= 2;
372 needReconnect = true;
376 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
378 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
379 if (it != smuxHandlers.end())
381 return (this->*(it->second))(pdus);
386 switch (pdus->present)
388 case SMUX_PDUs_PR_NOTHING:
389 printfd(__FILE__, "PDUs: nothing\n");
391 case SMUX_PDUs_PR_open:
392 printfd(__FILE__, "PDUs: open\n");
394 case SMUX_PDUs_PR_registerRequest:
395 printfd(__FILE__, "PDUs: registerRequest\n");
398 printfd(__FILE__, "PDUs: undefined\n");
400 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
406 bool SMUX::UpdateTables()
410 Tables::iterator it(tables.begin());
411 while (it != tables.end())
415 it->second->UpdateSensors(newSensors);
417 catch (const std::runtime_error & ex)
420 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
421 it->first.c_str(), ex.what());
429 Sensors::iterator it(newSensors.begin());
430 while (it != newSensors.end())
439 while (it != tables.end())
441 std::pair<Sensors::iterator, Sensors::iterator> res;
442 res = std::equal_range(sensors.begin(),
444 std::pair<OID, Sensor *>(OID(it->first), NULL),
446 Sensors::iterator sit(res.first);
447 while (sit != res.second)
452 sensors.erase(res.first, res.second);
456 sensors.insert(newSensors.begin(), newSensors.end());
461 void SMUX::SetNotifier(USER_PTR userPtr)
463 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
464 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
467 void SMUX::UnsetNotifier(USER_PTR userPtr)
469 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
470 while (it != notifiers.end())
472 if (it->GetUserPtr() == userPtr)
474 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
482 void SMUX::SetNotifiers()
484 int h = users->OpenSearch();
485 assert(h && "USERS::OpenSearch is always correct");
488 while (!users->SearchNext(h, &u))
491 users->CloseSearch(h);
493 users->AddNotifierUserAdd(&addUserNotifier);
494 users->AddNotifierUserDel(&delUserNotifier);
496 tariffs->AddNotifierAdd(&addDelTariffNotifier);
497 tariffs->AddNotifierDel(&addDelTariffNotifier);
500 void SMUX::ResetNotifiers()
502 tariffs->DelNotifierDel(&addDelTariffNotifier);
503 tariffs->DelNotifierAdd(&addDelTariffNotifier);
505 users->DelNotifierUserDel(&delUserNotifier);
506 users->DelNotifierUserAdd(&addUserNotifier);
508 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
509 while (it != notifiers.end())
511 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));