2 #include <sys/socket.h>
17 #include "stg/common.h"
18 #include "stg/plugin_creator.h"
19 /*#include "stg/users.h"
20 #include "stg/tariffs.h"
21 #include "stg/admins.h"
22 #include "stg/services.h"
23 #include "stg/corporations.h"
24 #include "stg/traffcounter.h"*/
29 PLUGIN_CREATOR<SMUX> smc;
33 return smc.GetPlugin();
36 bool SPrefixLess(const Sensors::value_type & a,
37 const Sensors::value_type & b)
39 return a.first.PrefixLess(b.first);
42 SMUX_SETTINGS::SMUX_SETTINGS()
47 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
50 std::vector<PARAM_VALUE>::const_iterator pvi;
54 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55 if (pvi == s.moduleParams.end())
57 errorStr = "Parameter \'Port\' not found.";
58 printfd(__FILE__, "Parameter 'Port' not found\n");
61 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
63 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
64 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
69 pv.param = "Password";
70 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
71 if (pvi == s.moduleParams.end())
73 errorStr = "Parameter \'Password\' not found.";
74 printfd(__FILE__, "Parameter 'Password' not found\n");
79 password = pvi->value[0];
83 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
84 if (pvi == s.moduleParams.end())
86 errorStr = "Parameter \'Server\' not found.";
87 printfd(__FILE__, "Parameter 'Server' not found\n");
90 ip = inet_strington(pvi->value[0]);
111 addUserNotifier(*this),
112 delUserNotifier(*this),
113 addDelTariffNotifier(*this)
115 pthread_mutex_init(&mutex, NULL);
117 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
118 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
119 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
120 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
122 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
123 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
124 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
130 Sensors::iterator it;
131 for (it = sensors.begin(); it != sensors.end(); ++it)
136 for (it = tables.begin(); it != tables.end(); ++it)
139 printfd(__FILE__, "SMUX::~SMUX()\n");
140 pthread_mutex_destroy(&mutex);
143 int SMUX::ParseSettings()
145 return smuxSettings.ParseSettings(settings);
150 assert(users != NULL && "users must not be NULL");
151 assert(tariffs != NULL && "tariffs must not be NULL");
152 assert(admins != NULL && "admins must not be NULL");
153 assert(services != NULL && "services must not be NULL");
154 assert(corporations != NULL && "corporations must not be NULL");
155 assert(traffcounter != NULL && "traffcounter must not be NULL");
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
170 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
171 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
173 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
175 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
177 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
179 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
181 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
184 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *tariffs, *users);
190 Sensors::const_iterator it(sensors.begin());
191 while (it != sensors.end())
193 printfd(__FILE__, "%s = %s\n",
194 it->first.ToString().c_str(),
195 it->second->ToString().c_str());
202 if (pthread_create(&thread, NULL, Runner, this))
204 errorStr = "Cannot create thread.";
205 printfd(__FILE__, "Cannot create thread\n");
215 printfd(__FILE__, "SMUX::Stop() - Before\n");
222 //5 seconds to thread stops itself
223 for (int i = 0; i < 25 && !stopped; i++)
225 struct timespec ts = {0, 200000000};
226 nanosleep(&ts, NULL);
229 //after 5 seconds waiting thread still running. now killing it
232 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
233 if (pthread_kill(thread, SIGINT))
235 errorStr = "Cannot kill thread.";
236 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
239 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
243 pthread_join(thread, NULL);
247 printfd(__FILE__, "SMUX::Stop() - After\n");
251 void * SMUX::Runner(void * d)
253 SMUX * smux = static_cast<SMUX *>(d);
269 if (WaitPackets(sock))
271 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
275 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
285 bool SMUX::PrepareNet()
287 sock = socket(AF_INET, SOCK_STREAM, 0);
291 errorStr = "Cannot create socket.";
292 printfd(__FILE__, "Cannot create socket\n");
296 struct sockaddr_in addr;
298 addr.sin_family = AF_INET;
299 addr.sin_port = htons(smuxSettings.GetPort());
300 addr.sin_addr.s_addr = smuxSettings.GetIP();
302 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
304 errorStr = "Cannot connect.";
305 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
312 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
314 SMUXHandlers::iterator it;
315 it = smuxHandlers.find(pdus->present);
316 if (it != smuxHandlers.end())
318 return (this->*(it->second))(pdus);
322 switch (pdus->present)
324 case SMUX_PDUs_PR_NOTHING:
325 printfd(__FILE__, "PDUs: nothing\n");
327 case SMUX_PDUs_PR_open:
328 printfd(__FILE__, "PDUs: open\n");
330 case SMUX_PDUs_PR_registerRequest:
331 printfd(__FILE__, "PDUs: registerRequest\n");
334 printfd(__FILE__, "PDUs: undefined\n");
336 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
341 bool SMUX::UpdateTables()
345 Tables::iterator it(tables.begin());
346 while (it != tables.end())
350 it->second->UpdateSensors(newSensors);
352 catch (const std::runtime_error & ex)
355 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
356 it->first.c_str(), ex.what());
364 Sensors::iterator it(newSensors.begin());
365 while (it != newSensors.end())
374 while (it != tables.end())
376 std::pair<Sensors::iterator, Sensors::iterator> res;
377 res = std::equal_range(sensors.begin(),
379 std::pair<OID, Sensor *>(OID(it->first), NULL),
381 Sensors::iterator sit(res.first);
382 while (sit != res.second)
387 sensors.erase(res.first, res.second);
391 sensors.insert(newSensors.begin(), newSensors.end());
396 void SMUX::SetNotifier(USER_PTR userPtr)
398 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
399 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
402 void SMUX::UnsetNotifier(USER_PTR userPtr)
404 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
405 while (it != notifiers.end())
407 if (it->GetUserPtr() == userPtr)
409 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
417 void SMUX::SetNotifiers()
419 int h = users->OpenSearch();
420 assert(h && "USERS::OpenSearch is always correct");
423 while (!users->SearchNext(h, &u))
426 users->CloseSearch(h);
428 users->AddNotifierUserAdd(&addUserNotifier);
429 users->AddNotifierUserDel(&delUserNotifier);
431 tariffs->AddNotifierAdd(&addDelTariffNotifier);
432 tariffs->AddNotifierDel(&addDelTariffNotifier);
435 void SMUX::ResetNotifiers()
437 tariffs->DelNotifierDel(&addDelTariffNotifier);
438 tariffs->DelNotifierAdd(&addDelTariffNotifier);
440 users->DelNotifierUserDel(&delUserNotifier);
441 users->DelNotifierUserAdd(&addUserNotifier);
443 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
444 while (it != notifiers.end())
446 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
451 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)