2 #include <sys/socket.h>
16 #include "stg/common.h"
17 #include "stg/plugin_creator.h"
22 PLUGIN_CREATOR<SMUX> smc;
26 return smc.GetPlugin();
29 bool SPrefixLess(const Sensors::value_type & a,
30 const Sensors::value_type & b)
32 return a.first.PrefixLess(b.first);
35 SMUX_SETTINGS::SMUX_SETTINGS()
40 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
43 std::vector<PARAM_VALUE>::const_iterator pvi;
47 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
48 if (pvi == s.moduleParams.end())
50 errorStr = "Parameter \'Port\' not found.";
51 printfd(__FILE__, "Parameter 'Port' not found\n");
54 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
56 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
57 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
62 pv.param = "Password";
63 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
64 if (pvi == s.moduleParams.end())
66 errorStr = "Parameter \'Password\' not found.";
67 printfd(__FILE__, "Parameter 'Password' not found\n");
72 password = pvi->value[0];
76 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
77 if (pvi == s.moduleParams.end())
79 errorStr = "Parameter \'Server\' not found.";
80 printfd(__FILE__, "Parameter 'Server' not found\n");
83 ip = inet_strington(pvi->value[0]);
96 pthread_mutex_init(&mutex, NULL);
98 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
99 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
100 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
101 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
103 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
104 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
105 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
111 Sensors::iterator it;
112 for (it = sensors.begin(); it != sensors.end(); ++it)
117 for (it = tables.begin(); it != tables.end(); ++it)
120 printfd(__FILE__, "SMUX::~SMUX()\n");
121 pthread_mutex_destroy(&mutex);
124 int SMUX::ParseSettings()
126 return smuxSettings.ParseSettings(settings);
135 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
136 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
137 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
138 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
139 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
140 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
141 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
142 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
143 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
144 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
145 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
147 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
150 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *users);
155 Sensors::const_iterator it(sensors.begin());
156 while (it != sensors.end())
158 printfd(__FILE__, "%s = %s\n",
159 it->first.ToString().c_str(),
160 it->second->ToString().c_str());
167 if (pthread_create(&thread, NULL, Runner, this))
169 errorStr = "Cannot create thread.";
170 printfd(__FILE__, "Cannot create thread\n");
180 printfd(__FILE__, "SMUX::Stop() - Before\n");
185 //5 seconds to thread stops itself
186 for (int i = 0; i < 25 && !stopped; i++)
188 struct timespec ts = {0, 200000000};
189 nanosleep(&ts, NULL);
192 //after 5 seconds waiting thread still running. now killing it
195 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
196 if (pthread_kill(thread, SIGINT))
198 errorStr = "Cannot kill thread.";
199 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
202 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
206 pthread_join(thread, NULL);
210 printfd(__FILE__, "SMUX::Stop() - After\n");
214 void * SMUX::Runner(void * d)
216 SMUX * smux = static_cast<SMUX *>(d);
231 if (WaitPackets(sock))
233 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
244 bool SMUX::PrepareNet()
246 sock = socket(AF_INET, SOCK_STREAM, 0);
250 errorStr = "Cannot create socket.";
251 printfd(__FILE__, "Cannot create socket\n");
255 struct sockaddr_in addr;
257 addr.sin_family = AF_INET;
258 addr.sin_port = htons(smuxSettings.GetPort());
259 addr.sin_addr.s_addr = smuxSettings.GetIP();
261 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
263 errorStr = "Cannot connect.";
264 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
271 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
273 SMUXHandlers::iterator it;
274 it = smuxHandlers.find(pdus->present);
275 if (it != smuxHandlers.end())
277 return (this->*(it->second))(pdus);
281 switch (pdus->present)
283 case SMUX_PDUs_PR_NOTHING:
284 printfd(__FILE__, "PDUs: nothing\n");
286 case SMUX_PDUs_PR_open:
287 printfd(__FILE__, "PDUs: open\n");
289 case SMUX_PDUs_PR_registerRequest:
290 printfd(__FILE__, "PDUs: registerRequest\n");
293 printfd(__FILE__, "PDUs: undefined\n");
295 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
300 bool SMUX::UpdateTables()
304 Tables::iterator it(tables.begin());
305 while (it != tables.end())
309 it->second->UpdateSensors(newSensors);
311 catch (const std::runtime_error & ex)
314 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
315 it->first.c_str(), ex.what());
323 Sensors::iterator it(newSensors.begin());
324 while (it != newSensors.end())
333 while (it != tables.end())
335 std::pair<Sensors::iterator, Sensors::iterator> res;
336 res = std::equal_range(sensors.begin(),
338 std::pair<OID, Sensor *>(OID(it->first), NULL),
340 Sensors::iterator sit(res.first);
341 while (sit != res.second)
346 sensors.erase(res.first, res.second);
350 sensors.insert(newSensors.begin(), newSensors.end());