2 #include <sys/socket.h>
13 #include "stg/common.h"
24 SMUX_CREATOR() : smux(new SMUX()) {}
25 ~SMUX_CREATOR() { delete smux; }
27 SMUX * GetPlugin() { return smux; }
34 return sac.GetPlugin();
37 int ParseIntInRange(const std::string & str,
42 if (str2x(str.c_str(), *val))
46 if (*val < min || *val > max)
53 SMUX_SETTINGS::SMUX_SETTINGS()
58 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
61 std::vector<PARAM_VALUE>::const_iterator pvi;
63 ///////////////////////////
65 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
66 if (pvi == s.moduleParams.end())
68 errorStr = "Parameter \'Port\' not found.";
69 printfd(__FILE__, "Parameter 'Port' not found\n");
72 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
74 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
75 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
80 pv.param = "Password";
81 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
82 if (pvi == s.moduleParams.end())
84 errorStr = "Parameter \'Password\' not found.";
85 printfd(__FILE__, "Parameter 'Password' not found\n");
90 password = pvi->value[0];
94 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
95 if (pvi == s.moduleParams.end())
97 errorStr = "Parameter \'Server\' not found.";
98 printfd(__FILE__, "Parameter 'Server' not found\n");
101 ip = inet_strington(pvi->value[0]);
114 pthread_mutex_init(&mutex, NULL);
116 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
117 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
118 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
119 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
121 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
122 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
123 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
128 Sensors::iterator it;
129 for (it = sensors.begin(); it != sensors.end(); ++it)
131 printfd(__FILE__, "SMUX::~SMUX()\n");
132 pthread_mutex_destroy(&mutex);
135 int SMUX::ParseSettings()
137 return smuxSettings.ParseSettings(settings);
146 sensors[".1.3.6.1.4.1.38313.1.1.1"] = new TotalUsersSensor(*users);
147 sensors[".1.3.6.1.4.1.38313.1.1.2"] = new ConnectedUsersSensor(*users);
148 sensors[".1.3.6.1.4.1.38313.1.1.3"] = new AuthorizedUsersSensor(*users);
149 sensors[".1.3.6.1.4.1.38313.1.1.4"] = new AlwaysOnlineUsersSensor(*users);
150 sensors[".1.3.6.1.4.1.38313.1.1.5"] = new NoCashUsersSensor(*users);
151 sensors[".1.3.6.1.4.1.38313.1.1.7"] = new DisabledDetailStatsUsersSensor(*users);
152 sensors[".1.3.6.1.4.1.38313.1.1.8"] = new DisabledUsersSensor(*users);
153 sensors[".1.3.6.1.4.1.38313.1.1.9"] = new PassiveUsersSensor(*users);
154 sensors[".1.3.6.1.4.1.38313.1.1.10"] = new CreditUsersSensor(*users);
155 sensors[".1.3.6.1.4.1.38313.1.1.11"] = new FreeMbUsersSensor(*users);
156 sensors[".1.3.6.1.4.1.38313.1.1.12"] = new TariffChangeUsersSensor(*users);
158 sensors[".1.3.6.1.4.1.38313.1.2.1"] = new TotalTariffsSensor(*tariffs);
162 if (pthread_create(&thread, NULL, Runner, this))
164 errorStr = "Cannot create thread.";
165 printfd(__FILE__, "Cannot create thread\n");
175 printfd(__FILE__, "SMUX::Stop() - Before\n");
180 //5 seconds to thread stops itself
181 for (int i = 0; i < 25 && !stopped; i++)
183 struct timespec ts = {0, 200000000};
184 nanosleep(&ts, NULL);
187 //after 5 seconds waiting thread still running. now killing it
190 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
191 if (pthread_kill(thread, SIGINT))
193 errorStr = "Cannot kill thread.";
194 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
197 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
201 pthread_join(thread, NULL);
205 printfd(__FILE__, "SMUX::Stop() - After\n");
209 void * SMUX::Runner(void * d)
211 SMUX * smux = static_cast<SMUX *>(d);
226 if (WaitPackets(sock))
228 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
239 bool SMUX::PrepareNet()
241 sock = socket(AF_INET, SOCK_STREAM, 0);
245 errorStr = "Cannot create socket.";
246 printfd(__FILE__, "Cannot create socket\n");
250 struct sockaddr_in addr;
252 addr.sin_family = AF_INET;
253 addr.sin_port = htons(smuxSettings.GetPort());
254 addr.sin_addr.s_addr = smuxSettings.GetIP();
256 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
258 errorStr = "Cannot connect.";
259 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
266 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
268 SMUXHandlers::iterator it;
269 it = smuxHandlers.find(pdus->present);
270 if (it != smuxHandlers.end())
272 return (this->*(it->second))(pdus);
276 switch (pdus->present)
278 case SMUX_PDUs_PR_NOTHING:
279 printfd(__FILE__, "PDUs: nothing\n");
281 case SMUX_PDUs_PR_open:
282 printfd(__FILE__, "PDUs: open\n");
284 case SMUX_PDUs_PR_registerRequest:
285 printfd(__FILE__, "PDUs: registerRequest\n");
288 printfd(__FILE__, "PDUs: undefined\n");
290 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);