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"
28 PLUGIN_CREATOR<SMUX> smc;
32 return smc.GetPlugin();
35 bool SPrefixLess(const Sensors::value_type & a,
36 const Sensors::value_type & b)
38 return a.first.PrefixLess(b.first);
41 SMUX_SETTINGS::SMUX_SETTINGS()
46 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
49 std::vector<PARAM_VALUE>::const_iterator pvi;
53 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
54 if (pvi == s.moduleParams.end())
56 errorStr = "Parameter \'Port\' not found.";
57 printfd(__FILE__, "Parameter 'Port' not found\n");
60 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
62 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
63 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
68 pv.param = "Password";
69 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
70 if (pvi == s.moduleParams.end())
72 errorStr = "Parameter \'Password\' not found.";
73 printfd(__FILE__, "Parameter 'Password' not found\n");
78 password = pvi->value[0];
82 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
83 if (pvi == s.moduleParams.end())
85 errorStr = "Parameter \'Server\' not found.";
86 printfd(__FILE__, "Parameter 'Server' not found\n");
89 ip = inet_strington(pvi->value[0]);
105 pthread_mutex_init(&mutex, NULL);
107 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
108 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
109 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
110 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
112 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
113 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
114 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
120 Sensors::iterator it;
121 for (it = sensors.begin(); it != sensors.end(); ++it)
126 for (it = tables.begin(); it != tables.end(); ++it)
129 printfd(__FILE__, "SMUX::~SMUX()\n");
130 pthread_mutex_destroy(&mutex);
133 int SMUX::ParseSettings()
135 return smuxSettings.ParseSettings(settings);
140 assert(users != NULL && "users not NULL");
141 assert(tariffs != NULL && "tariffs not NULL");
142 assert(admins != NULL && "admins not NULL");
143 assert(services != NULL && "services not NULL");
144 assert(corporations != NULL && "corporations not NULL");
150 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
151 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
152 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
153 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
154 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
155 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
156 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
157 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
158 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
164 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
166 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
168 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
171 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *users);
176 Sensors::const_iterator it(sensors.begin());
177 while (it != sensors.end())
179 printfd(__FILE__, "%s = %s\n",
180 it->first.ToString().c_str(),
181 it->second->ToString().c_str());
188 if (pthread_create(&thread, NULL, Runner, this))
190 errorStr = "Cannot create thread.";
191 printfd(__FILE__, "Cannot create thread\n");
201 printfd(__FILE__, "SMUX::Stop() - Before\n");
206 //5 seconds to thread stops itself
207 for (int i = 0; i < 25 && !stopped; i++)
209 struct timespec ts = {0, 200000000};
210 nanosleep(&ts, NULL);
213 //after 5 seconds waiting thread still running. now killing it
216 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
217 if (pthread_kill(thread, SIGINT))
219 errorStr = "Cannot kill thread.";
220 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
223 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
227 pthread_join(thread, NULL);
231 printfd(__FILE__, "SMUX::Stop() - After\n");
235 void * SMUX::Runner(void * d)
237 SMUX * smux = static_cast<SMUX *>(d);
252 if (WaitPackets(sock))
254 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
265 bool SMUX::PrepareNet()
267 sock = socket(AF_INET, SOCK_STREAM, 0);
271 errorStr = "Cannot create socket.";
272 printfd(__FILE__, "Cannot create socket\n");
276 struct sockaddr_in addr;
278 addr.sin_family = AF_INET;
279 addr.sin_port = htons(smuxSettings.GetPort());
280 addr.sin_addr.s_addr = smuxSettings.GetIP();
282 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
284 errorStr = "Cannot connect.";
285 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
292 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
294 SMUXHandlers::iterator it;
295 it = smuxHandlers.find(pdus->present);
296 if (it != smuxHandlers.end())
298 return (this->*(it->second))(pdus);
302 switch (pdus->present)
304 case SMUX_PDUs_PR_NOTHING:
305 printfd(__FILE__, "PDUs: nothing\n");
307 case SMUX_PDUs_PR_open:
308 printfd(__FILE__, "PDUs: open\n");
310 case SMUX_PDUs_PR_registerRequest:
311 printfd(__FILE__, "PDUs: registerRequest\n");
314 printfd(__FILE__, "PDUs: undefined\n");
316 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
321 bool SMUX::UpdateTables()
325 Tables::iterator it(tables.begin());
326 while (it != tables.end())
330 it->second->UpdateSensors(newSensors);
332 catch (const std::runtime_error & ex)
335 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
336 it->first.c_str(), ex.what());
344 Sensors::iterator it(newSensors.begin());
345 while (it != newSensors.end())
354 while (it != tables.end())
356 std::pair<Sensors::iterator, Sensors::iterator> res;
357 res = std::equal_range(sensors.begin(),
359 std::pair<OID, Sensor *>(OID(it->first), NULL),
361 Sensors::iterator sit(res.first);
362 while (sit != res.second)
367 sensors.erase(res.first, res.second);
371 sensors.insert(newSensors.begin(), newSensors.end());