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()
41 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
44 std::vector<PARAM_VALUE>::const_iterator pvi;
48 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
49 if (pvi == s.moduleParams.end())
51 errorStr = "Parameter \'Port\' not found.";
52 printfd(__FILE__, "Parameter 'Port' not found\n");
55 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
57 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
58 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
63 pv.param = "Password";
64 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
65 if (pvi == s.moduleParams.end())
67 errorStr = "Parameter \'Password\' not found.";
68 printfd(__FILE__, "Parameter 'Password' not found\n");
73 password = pvi->value[0];
77 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
78 if (pvi == s.moduleParams.end())
80 errorStr = "Parameter \'Server\' not found.";
81 printfd(__FILE__, "Parameter 'Server' not found\n");
84 ip = inet_strington(pvi->value[0]);
105 addUserNotifier(*this),
106 delUserNotifier(*this),
107 addDelTariffNotifier(*this)
109 pthread_mutex_init(&mutex, NULL);
111 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
112 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
113 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
114 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
116 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
117 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
118 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
124 Sensors::iterator it;
125 for (it = sensors.begin(); it != sensors.end(); ++it)
130 for (it = tables.begin(); it != tables.end(); ++it)
133 printfd(__FILE__, "SMUX::~SMUX()\n");
134 pthread_mutex_destroy(&mutex);
137 int SMUX::ParseSettings()
139 return smuxSettings.ParseSettings(settings);
144 assert(users != NULL && "users must not be NULL");
145 assert(tariffs != NULL && "tariffs must not be NULL");
146 assert(admins != NULL && "admins must not be NULL");
147 assert(services != NULL && "services must not be NULL");
148 assert(corporations != NULL && "corporations must not be NULL");
149 assert(traffcounter != NULL && "traffcounter must not be NULL");
155 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
156 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
157 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
158 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
169 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
171 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
173 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
175 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
178 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);
184 Sensors::const_iterator it(sensors.begin());
185 while (it != sensors.end())
187 printfd(__FILE__, "%s = %s\n",
188 it->first.ToString().c_str(),
189 it->second->ToString().c_str());
196 if (pthread_create(&thread, NULL, Runner, this))
198 errorStr = "Cannot create thread.";
199 printfd(__FILE__, "Cannot create thread\n");
209 printfd(__FILE__, "SMUX::Stop() - Before\n");
214 //5 seconds to thread stops itself
215 for (int i = 0; i < 25 && !stopped; i++)
217 struct timespec ts = {0, 200000000};
218 nanosleep(&ts, NULL);
221 //after 5 seconds waiting thread still running. now killing it
224 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
225 if (pthread_kill(thread, SIGINT))
227 errorStr = "Cannot kill thread.";
228 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
231 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
235 pthread_join(thread, NULL);
241 for (it = tables.begin(); it != tables.end(); ++it)
245 Sensors::iterator it;
246 for (it = sensors.begin(); it != sensors.end(); ++it)
250 tables.erase(tables.begin(), tables.end());
251 sensors.erase(sensors.begin(), sensors.end());
255 printfd(__FILE__, "SMUX::Stop() - After\n");
268 void * SMUX::Runner(void * d)
270 SMUX * smux = static_cast<SMUX *>(d);
286 if (WaitPackets(sock))
288 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
292 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
302 bool SMUX::PrepareNet()
304 sock = socket(AF_INET, SOCK_STREAM, 0);
308 errorStr = "Cannot create socket.";
309 printfd(__FILE__, "Cannot create socket\n");
313 struct sockaddr_in addr;
315 addr.sin_family = AF_INET;
316 addr.sin_port = htons(smuxSettings.GetPort());
317 addr.sin_addr.s_addr = smuxSettings.GetIP();
319 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
321 errorStr = "Cannot connect.";
322 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
329 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
331 SMUXHandlers::iterator it;
332 it = smuxHandlers.find(pdus->present);
333 if (it != smuxHandlers.end())
335 return (this->*(it->second))(pdus);
340 switch (pdus->present)
342 case SMUX_PDUs_PR_NOTHING:
343 printfd(__FILE__, "PDUs: nothing\n");
345 case SMUX_PDUs_PR_open:
346 printfd(__FILE__, "PDUs: open\n");
348 case SMUX_PDUs_PR_registerRequest:
349 printfd(__FILE__, "PDUs: registerRequest\n");
352 printfd(__FILE__, "PDUs: undefined\n");
354 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
360 bool SMUX::UpdateTables()
364 Tables::iterator it(tables.begin());
365 while (it != tables.end())
369 it->second->UpdateSensors(newSensors);
371 catch (const std::runtime_error & ex)
374 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
375 it->first.c_str(), ex.what());
383 Sensors::iterator it(newSensors.begin());
384 while (it != newSensors.end())
393 while (it != tables.end())
395 std::pair<Sensors::iterator, Sensors::iterator> res;
396 res = std::equal_range(sensors.begin(),
398 std::pair<OID, Sensor *>(OID(it->first), NULL),
400 Sensors::iterator sit(res.first);
401 while (sit != res.second)
406 sensors.erase(res.first, res.second);
410 sensors.insert(newSensors.begin(), newSensors.end());
415 void SMUX::SetNotifier(USER_PTR userPtr)
417 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
418 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
421 void SMUX::UnsetNotifier(USER_PTR userPtr)
423 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
424 while (it != notifiers.end())
426 if (it->GetUserPtr() == userPtr)
428 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
436 void SMUX::SetNotifiers()
438 int h = users->OpenSearch();
439 assert(h && "USERS::OpenSearch is always correct");
442 while (!users->SearchNext(h, &u))
445 users->CloseSearch(h);
447 users->AddNotifierUserAdd(&addUserNotifier);
448 users->AddNotifierUserDel(&delUserNotifier);
450 tariffs->AddNotifierAdd(&addDelTariffNotifier);
451 tariffs->AddNotifierDel(&addDelTariffNotifier);
454 void SMUX::ResetNotifiers()
456 tariffs->DelNotifierDel(&addDelTariffNotifier);
457 tariffs->DelNotifierAdd(&addDelTariffNotifier);
459 users->DelNotifierUserDel(&delUserNotifier);
460 users->DelNotifierUserAdd(&addUserNotifier);
462 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
463 while (it != notifiers.end())
465 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
470 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)