2 #include <sys/socket.h>
16 #include "stg/common.h"
17 #include "stg/plugin_creator.h"
18 #include "stg/users.h"
19 #include "stg/tariffs.h"
20 #include "stg/admins.h"
21 #include "stg/services.h"
22 #include "stg/corporations.h"
27 PLUGIN_CREATOR<SMUX> smc;
31 return smc.GetPlugin();
34 bool SPrefixLess(const Sensors::value_type & a,
35 const Sensors::value_type & b)
37 return a.first.PrefixLess(b.first);
40 SMUX_SETTINGS::SMUX_SETTINGS()
45 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
48 std::vector<PARAM_VALUE>::const_iterator pvi;
52 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
53 if (pvi == s.moduleParams.end())
55 errorStr = "Parameter \'Port\' not found.";
56 printfd(__FILE__, "Parameter 'Port' not found\n");
59 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
61 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
62 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
67 pv.param = "Password";
68 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
69 if (pvi == s.moduleParams.end())
71 errorStr = "Parameter \'Password\' not found.";
72 printfd(__FILE__, "Parameter 'Password' not found\n");
77 password = pvi->value[0];
81 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
82 if (pvi == s.moduleParams.end())
84 errorStr = "Parameter \'Server\' not found.";
85 printfd(__FILE__, "Parameter 'Server' not found\n");
88 ip = inet_strington(pvi->value[0]);
104 pthread_mutex_init(&mutex, NULL);
106 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
107 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
108 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
109 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
111 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
112 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
113 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
119 Sensors::iterator it;
120 for (it = sensors.begin(); it != sensors.end(); ++it)
125 for (it = tables.begin(); it != tables.end(); ++it)
128 printfd(__FILE__, "SMUX::~SMUX()\n");
129 pthread_mutex_destroy(&mutex);
132 int SMUX::ParseSettings()
134 return smuxSettings.ParseSettings(settings);
143 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
144 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
145 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
146 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
147 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
148 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
149 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
150 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
151 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
152 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
153 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
155 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
157 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
159 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
161 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
164 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *users);
169 Sensors::const_iterator it(sensors.begin());
170 while (it != sensors.end())
172 printfd(__FILE__, "%s = %s\n",
173 it->first.ToString().c_str(),
174 it->second->ToString().c_str());
181 if (pthread_create(&thread, NULL, Runner, this))
183 errorStr = "Cannot create thread.";
184 printfd(__FILE__, "Cannot create thread\n");
194 printfd(__FILE__, "SMUX::Stop() - Before\n");
199 //5 seconds to thread stops itself
200 for (int i = 0; i < 25 && !stopped; i++)
202 struct timespec ts = {0, 200000000};
203 nanosleep(&ts, NULL);
206 //after 5 seconds waiting thread still running. now killing it
209 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
210 if (pthread_kill(thread, SIGINT))
212 errorStr = "Cannot kill thread.";
213 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
216 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
220 pthread_join(thread, NULL);
224 printfd(__FILE__, "SMUX::Stop() - After\n");
228 void * SMUX::Runner(void * d)
230 SMUX * smux = static_cast<SMUX *>(d);
245 if (WaitPackets(sock))
247 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
258 bool SMUX::PrepareNet()
260 sock = socket(AF_INET, SOCK_STREAM, 0);
264 errorStr = "Cannot create socket.";
265 printfd(__FILE__, "Cannot create socket\n");
269 struct sockaddr_in addr;
271 addr.sin_family = AF_INET;
272 addr.sin_port = htons(smuxSettings.GetPort());
273 addr.sin_addr.s_addr = smuxSettings.GetIP();
275 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
277 errorStr = "Cannot connect.";
278 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
285 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
287 SMUXHandlers::iterator it;
288 it = smuxHandlers.find(pdus->present);
289 if (it != smuxHandlers.end())
291 return (this->*(it->second))(pdus);
295 switch (pdus->present)
297 case SMUX_PDUs_PR_NOTHING:
298 printfd(__FILE__, "PDUs: nothing\n");
300 case SMUX_PDUs_PR_open:
301 printfd(__FILE__, "PDUs: open\n");
303 case SMUX_PDUs_PR_registerRequest:
304 printfd(__FILE__, "PDUs: registerRequest\n");
307 printfd(__FILE__, "PDUs: undefined\n");
309 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
314 bool SMUX::UpdateTables()
318 Tables::iterator it(tables.begin());
319 while (it != tables.end())
323 it->second->UpdateSensors(newSensors);
325 catch (const std::runtime_error & ex)
328 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
329 it->first.c_str(), ex.what());
337 Sensors::iterator it(newSensors.begin());
338 while (it != newSensors.end())
347 while (it != tables.end())
349 std::pair<Sensors::iterator, Sensors::iterator> res;
350 res = std::equal_range(sensors.begin(),
352 std::pair<OID, Sensor *>(OID(it->first), NULL),
354 Sensors::iterator sit(res.first);
355 while (sit != res.second)
360 sensors.erase(res.first, res.second);
364 sensors.insert(newSensors.begin(), newSensors.end());