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);
270 bool SMUX::PrepareNet()
272 sock = socket(AF_INET, SOCK_STREAM, 0);
276 errorStr = "Cannot create socket.";
277 printfd(__FILE__, "Cannot create socket\n");
281 struct sockaddr_in addr;
283 addr.sin_family = AF_INET;
284 addr.sin_port = htons(smuxSettings.GetPort());
285 addr.sin_addr.s_addr = smuxSettings.GetIP();
287 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
289 errorStr = "Cannot connect.";
290 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
297 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
299 SMUXHandlers::iterator it;
300 it = smuxHandlers.find(pdus->present);
301 if (it != smuxHandlers.end())
303 return (this->*(it->second))(pdus);
307 switch (pdus->present)
309 case SMUX_PDUs_PR_NOTHING:
310 printfd(__FILE__, "PDUs: nothing\n");
312 case SMUX_PDUs_PR_open:
313 printfd(__FILE__, "PDUs: open\n");
315 case SMUX_PDUs_PR_registerRequest:
316 printfd(__FILE__, "PDUs: registerRequest\n");
319 printfd(__FILE__, "PDUs: undefined\n");
321 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
326 bool SMUX::UpdateTables()
330 Tables::iterator it(tables.begin());
331 while (it != tables.end())
335 it->second->UpdateSensors(newSensors);
337 catch (const std::runtime_error & ex)
340 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
341 it->first.c_str(), ex.what());
349 Sensors::iterator it(newSensors.begin());
350 while (it != newSensors.end())
359 while (it != tables.end())
361 std::pair<Sensors::iterator, Sensors::iterator> res;
362 res = std::equal_range(sensors.begin(),
364 std::pair<OID, Sensor *>(OID(it->first), NULL),
366 Sensors::iterator sit(res.first);
367 while (sit != res.second)
372 sensors.erase(res.first, res.second);
376 sensors.insert(newSensors.begin(), newSensors.end());