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]);
107 pthread_mutex_init(&mutex, NULL);
109 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
110 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
111 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
112 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
114 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
115 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
116 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
122 Sensors::iterator it;
123 for (it = sensors.begin(); it != sensors.end(); ++it)
128 for (it = tables.begin(); it != tables.end(); ++it)
131 printfd(__FILE__, "SMUX::~SMUX()\n");
132 pthread_mutex_destroy(&mutex);
135 int SMUX::ParseSettings()
137 return smuxSettings.ParseSettings(settings);
142 assert(users != NULL && "users must not be NULL");
143 assert(tariffs != NULL && "tariffs must not be NULL");
144 assert(admins != NULL && "admins must not be NULL");
145 assert(services != NULL && "services must not be NULL");
146 assert(corporations != NULL && "corporations must not be NULL");
147 assert(traffcounter != NULL && "traffcounter must not be NULL");
153 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
154 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
155 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
156 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
157 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
158 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
167 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
169 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
171 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
173 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
176 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *users);
181 Sensors::const_iterator it(sensors.begin());
182 while (it != sensors.end())
184 printfd(__FILE__, "%s = %s\n",
185 it->first.ToString().c_str(),
186 it->second->ToString().c_str());
193 if (pthread_create(&thread, NULL, Runner, this))
195 errorStr = "Cannot create thread.";
196 printfd(__FILE__, "Cannot create thread\n");
206 printfd(__FILE__, "SMUX::Stop() - Before\n");
211 //5 seconds to thread stops itself
212 for (int i = 0; i < 25 && !stopped; i++)
214 struct timespec ts = {0, 200000000};
215 nanosleep(&ts, NULL);
218 //after 5 seconds waiting thread still running. now killing it
221 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
222 if (pthread_kill(thread, SIGINT))
224 errorStr = "Cannot kill thread.";
225 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
228 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
232 pthread_join(thread, NULL);
236 printfd(__FILE__, "SMUX::Stop() - After\n");
240 void * SMUX::Runner(void * d)
242 SMUX * smux = static_cast<SMUX *>(d);
257 if (WaitPackets(sock))
259 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
263 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
273 bool SMUX::PrepareNet()
275 sock = socket(AF_INET, SOCK_STREAM, 0);
279 errorStr = "Cannot create socket.";
280 printfd(__FILE__, "Cannot create socket\n");
284 struct sockaddr_in addr;
286 addr.sin_family = AF_INET;
287 addr.sin_port = htons(smuxSettings.GetPort());
288 addr.sin_addr.s_addr = smuxSettings.GetIP();
290 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
292 errorStr = "Cannot connect.";
293 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
300 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
302 SMUXHandlers::iterator it;
303 it = smuxHandlers.find(pdus->present);
304 if (it != smuxHandlers.end())
306 return (this->*(it->second))(pdus);
310 switch (pdus->present)
312 case SMUX_PDUs_PR_NOTHING:
313 printfd(__FILE__, "PDUs: nothing\n");
315 case SMUX_PDUs_PR_open:
316 printfd(__FILE__, "PDUs: open\n");
318 case SMUX_PDUs_PR_registerRequest:
319 printfd(__FILE__, "PDUs: registerRequest\n");
322 printfd(__FILE__, "PDUs: undefined\n");
324 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
329 bool SMUX::UpdateTables()
333 Tables::iterator it(tables.begin());
334 while (it != tables.end())
338 it->second->UpdateSensors(newSensors);
340 catch (const std::runtime_error & ex)
343 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
344 it->first.c_str(), ex.what());
352 Sensors::iterator it(newSensors.begin());
353 while (it != newSensors.end())
362 while (it != tables.end())
364 std::pair<Sensors::iterator, Sensors::iterator> res;
365 res = std::equal_range(sensors.begin(),
367 std::pair<OID, Sensor *>(OID(it->first), NULL),
369 Sensors::iterator sit(res.first);
370 while (sit != res.second)
375 sensors.erase(res.first, res.second);
379 sensors.insert(newSensors.begin(), newSensors.end());