2 #include <sys/socket.h>
17 #include "stg/common.h"
25 bool SPrefixLess(const Sensors::value_type & a,
26 const Sensors::value_type & b)
28 return a.first.PrefixLess(b.first);
33 extern "C" STG::Plugin* GetPlugin()
39 SMUX_SETTINGS::SMUX_SETTINGS()
46 int SMUX_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
49 std::vector<STG::ParamValue>::const_iterator pvi;
53 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
54 if (pvi == s.moduleParams.end() || pvi->value.empty())
56 errorStr = "Parameter \'Port\' not found.";
57 printfd(__FILE__, "Parameter 'Port' not found\n");
60 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
62 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
63 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
66 port = static_cast<uint16_t>(p);
68 pv.param = "Password";
69 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
70 if (pvi == s.moduleParams.end() || pvi->value.empty())
72 errorStr = "Parameter \'Password\' not found.";
73 printfd(__FILE__, "Parameter 'Password' not found\n");
78 password = pvi->value[0];
82 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
83 if (pvi == s.moduleParams.end() || pvi->value.empty())
85 errorStr = "Parameter \'Server\' not found.";
86 printfd(__FILE__, "Parameter 'Server' not found\n");
89 ip = inet_strington(pvi->value[0]);
103 needReconnect(false),
107 addUserNotifier(*this),
108 delUserNotifier(*this),
109 addDelTariffNotifier(*this),
110 logger(STG::PluginLogger::get("smux"))
112 pthread_mutex_init(&mutex, NULL);
114 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
115 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
116 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
117 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
119 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
120 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
121 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
127 Sensors::iterator it;
128 for (it = sensors.begin(); it != sensors.end(); ++it)
133 for (it = tables.begin(); it != tables.end(); ++it)
136 printfd(__FILE__, "SMUX::~SMUX()\n");
137 pthread_mutex_destroy(&mutex);
140 int SMUX::ParseSettings()
142 return smuxSettings.ParseSettings(settings);
147 assert(users != NULL && "users must not be NULL");
148 assert(tariffs != NULL && "tariffs must not be NULL");
149 assert(admins != NULL && "admins must not be NULL");
150 assert(services != NULL && "services must not be NULL");
151 assert(corporations != NULL && "corporations must not be NULL");
152 assert(traffcounter != NULL && "traffcounter must not be NULL");
155 needReconnect = true;
158 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
171 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
173 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
175 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
177 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
179 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
182 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);
188 Sensors::const_iterator it(sensors.begin());
189 while (it != sensors.end())
191 printfd(__FILE__, "%s = %s\n",
192 it->first.ToString().c_str(),
193 it->second->ToString().c_str());
200 if (pthread_create(&thread, NULL, Runner, this))
202 errorStr = "Cannot create thread.";
203 logger("Cannot create thread.");
204 printfd(__FILE__, "Cannot create thread\n");
214 printfd(__FILE__, "SMUX::Stop() - Before\n");
219 //5 seconds to thread stops itself
220 for (int i = 0; i < 25 && !stopped; i++)
222 struct timespec ts = {0, 200000000};
223 nanosleep(&ts, NULL);
228 pthread_join(thread, NULL);
234 for (it = tables.begin(); it != tables.end(); ++it)
238 Sensors::iterator it;
239 for (it = sensors.begin(); it != sensors.end(); ++it)
243 tables.erase(tables.begin(), tables.end());
244 sensors.erase(sensors.begin(), sensors.end());
254 printfd(__FILE__, "SMUX::Stop() - After\n");
258 int SMUX::Reload(const STG::ModuleSettings & /*ms*/)
266 printfd(__FILE__, "SMUX reconnected succesfully.\n");
267 logger("Reconnected successfully.");
272 void * SMUX::Runner(void * d)
275 sigfillset(&signalSet);
276 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
278 SMUX * smux = static_cast<SMUX *>(d);
288 if (!SendOpenPDU(sock))
289 needReconnect = true;
290 if (!SendRReqPDU(sock))
291 needReconnect = true;
297 if (WaitPackets(sock) && !needReconnect)
299 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
303 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
308 else if (running && needReconnect)
317 bool SMUX::PrepareNet()
319 sock = socket(AF_INET, SOCK_STREAM, 0);
323 errorStr = "Cannot create socket.";
324 logger("Cannot create a socket: %s", strerror(errno));
325 printfd(__FILE__, "Cannot create socket\n");
329 struct sockaddr_in addr;
331 addr.sin_family = AF_INET;
332 addr.sin_port = htons(smuxSettings.GetPort());
333 addr.sin_addr.s_addr = smuxSettings.GetIP();
335 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
337 errorStr = "Cannot connect.";
338 logger("Cannot connect the socket: %s", strerror(errno));
339 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
346 bool SMUX::Reconnect()
348 if (needReconnect && difftime(time(NULL), lastReconnectTry) < reconnectTimeout)
351 time(&lastReconnectTry);
355 if (SendOpenPDU(sock))
356 if (SendRReqPDU(sock))
358 reconnectTimeout = 1;
359 needReconnect = false;
360 logger("Connected successfully");
361 printfd(__FILE__, "Connected successfully\n");
366 if (reconnectTimeout < 60)
367 reconnectTimeout *= 2;
369 needReconnect = true;
373 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
375 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
376 if (it != smuxHandlers.end())
378 return (this->*(it->second))(pdus);
383 switch (pdus->present)
385 case SMUX_PDUs_PR_NOTHING:
386 printfd(__FILE__, "PDUs: nothing\n");
388 case SMUX_PDUs_PR_open:
389 printfd(__FILE__, "PDUs: open\n");
391 case SMUX_PDUs_PR_registerRequest:
392 printfd(__FILE__, "PDUs: registerRequest\n");
395 printfd(__FILE__, "PDUs: undefined\n");
397 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
403 bool SMUX::UpdateTables()
407 Tables::iterator it(tables.begin());
408 while (it != tables.end())
412 it->second->UpdateSensors(newSensors);
414 catch (const std::runtime_error & ex)
417 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
418 it->first.c_str(), ex.what());
426 Sensors::iterator it(newSensors.begin());
427 while (it != newSensors.end())
436 while (it != tables.end())
438 std::pair<Sensors::iterator, Sensors::iterator> res;
439 res = std::equal_range(sensors.begin(),
441 std::pair<OID, Sensor *>(OID(it->first), NULL),
443 Sensors::iterator sit(res.first);
444 while (sit != res.second)
449 sensors.erase(res.first, res.second);
453 sensors.insert(newSensors.begin(), newSensors.end());
458 void SMUX::SetNotifier(UserPtr userPtr)
460 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
461 userPtr->GetProperties().tariffName.AddAfterNotifier(¬ifiers.back());
464 void SMUX::UnsetNotifier(UserPtr userPtr)
466 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
467 while (it != notifiers.end())
469 if (it->GetUserPtr() == userPtr)
471 userPtr->GetProperties().tariffName.DelAfterNotifier(&(*it));
479 void SMUX::SetNotifiers()
481 int h = users->OpenSearch();
482 assert(h && "USERS::OpenSearch is always correct");
485 while (!users->SearchNext(h, &u))
488 users->CloseSearch(h);
490 users->AddNotifierUserAdd(&addUserNotifier);
491 users->AddNotifierUserDel(&delUserNotifier);
493 tariffs->AddNotifierAdd(&addDelTariffNotifier);
494 tariffs->AddNotifierDel(&addDelTariffNotifier);
497 void SMUX::ResetNotifiers()
499 tariffs->DelNotifierDel(&addDelTariffNotifier);
500 tariffs->DelNotifierAdd(&addDelTariffNotifier);
502 users->DelNotifierUserDel(&delUserNotifier);
503 users->DelNotifierUserAdd(&addUserNotifier);
505 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
506 while (it != notifiers.end())
508 it->GetUserPtr()->GetProperties().tariffName.DelAfterNotifier(&(*it));