2 #include <sys/socket.h>
13 #include "stg/common.h"
14 #include "stg/plugin_creator.h"
19 PLUGIN_CREATOR<SMUX> smc;
23 return smc.GetPlugin();
26 SMUX_SETTINGS::SMUX_SETTINGS()
31 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
34 std::vector<PARAM_VALUE>::const_iterator pvi;
38 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
39 if (pvi == s.moduleParams.end())
41 errorStr = "Parameter \'Port\' not found.";
42 printfd(__FILE__, "Parameter 'Port' not found\n");
45 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
47 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
48 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
53 pv.param = "Password";
54 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55 if (pvi == s.moduleParams.end())
57 errorStr = "Parameter \'Password\' not found.";
58 printfd(__FILE__, "Parameter 'Password' not found\n");
63 password = pvi->value[0];
67 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
68 if (pvi == s.moduleParams.end())
70 errorStr = "Parameter \'Server\' not found.";
71 printfd(__FILE__, "Parameter 'Server' not found\n");
74 ip = inet_strington(pvi->value[0]);
87 pthread_mutex_init(&mutex, NULL);
89 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
90 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
91 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
92 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
94 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
95 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
96 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
101 Sensors::iterator it;
102 for (it = sensors.begin(); it != sensors.end(); ++it)
104 printfd(__FILE__, "SMUX::~SMUX()\n");
105 pthread_mutex_destroy(&mutex);
108 int SMUX::ParseSettings()
110 return smuxSettings.ParseSettings(settings);
119 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
120 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
121 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
122 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
123 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
124 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
125 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
126 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
127 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
128 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
129 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
131 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
135 if (pthread_create(&thread, NULL, Runner, this))
137 errorStr = "Cannot create thread.";
138 printfd(__FILE__, "Cannot create thread\n");
148 printfd(__FILE__, "SMUX::Stop() - Before\n");
153 //5 seconds to thread stops itself
154 for (int i = 0; i < 25 && !stopped; i++)
156 struct timespec ts = {0, 200000000};
157 nanosleep(&ts, NULL);
160 //after 5 seconds waiting thread still running. now killing it
163 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
164 if (pthread_kill(thread, SIGINT))
166 errorStr = "Cannot kill thread.";
167 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
170 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
174 pthread_join(thread, NULL);
178 printfd(__FILE__, "SMUX::Stop() - After\n");
182 void * SMUX::Runner(void * d)
184 SMUX * smux = static_cast<SMUX *>(d);
199 if (WaitPackets(sock))
201 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
212 bool SMUX::PrepareNet()
214 sock = socket(AF_INET, SOCK_STREAM, 0);
218 errorStr = "Cannot create socket.";
219 printfd(__FILE__, "Cannot create socket\n");
223 struct sockaddr_in addr;
225 addr.sin_family = AF_INET;
226 addr.sin_port = htons(smuxSettings.GetPort());
227 addr.sin_addr.s_addr = smuxSettings.GetIP();
229 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
231 errorStr = "Cannot connect.";
232 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
239 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
241 SMUXHandlers::iterator it;
242 it = smuxHandlers.find(pdus->present);
243 if (it != smuxHandlers.end())
245 return (this->*(it->second))(pdus);
249 switch (pdus->present)
251 case SMUX_PDUs_PR_NOTHING:
252 printfd(__FILE__, "PDUs: nothing\n");
254 case SMUX_PDUs_PR_open:
255 printfd(__FILE__, "PDUs: open\n");
257 case SMUX_PDUs_PR_registerRequest:
258 printfd(__FILE__, "PDUs: registerRequest\n");
261 printfd(__FILE__, "PDUs: undefined\n");
263 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);