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"
24 #include "stg/traffcounter.h"*/
29 PLUGIN_CREATOR<SMUX> smc;
33 return smc.GetPlugin();
36 bool SPrefixLess(const Sensors::value_type & a,
37 const Sensors::value_type & b)
39 return a.first.PrefixLess(b.first);
42 SMUX_SETTINGS::SMUX_SETTINGS()
47 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
50 std::vector<PARAM_VALUE>::const_iterator pvi;
54 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55 if (pvi == s.moduleParams.end())
57 errorStr = "Parameter \'Port\' not found.";
58 printfd(__FILE__, "Parameter 'Port' not found\n");
61 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
63 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
64 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
69 pv.param = "Password";
70 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
71 if (pvi == s.moduleParams.end())
73 errorStr = "Parameter \'Password\' not found.";
74 printfd(__FILE__, "Parameter 'Password' not found\n");
79 password = pvi->value[0];
83 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
84 if (pvi == s.moduleParams.end())
86 errorStr = "Parameter \'Server\' not found.";
87 printfd(__FILE__, "Parameter 'Server' not found\n");
90 ip = inet_strington(pvi->value[0]);
111 addDelNotifier(*this)
113 pthread_mutex_init(&mutex, NULL);
115 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
116 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
117 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
118 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
120 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
121 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
122 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
128 Sensors::iterator it;
129 for (it = sensors.begin(); it != sensors.end(); ++it)
134 for (it = tables.begin(); it != tables.end(); ++it)
137 printfd(__FILE__, "SMUX::~SMUX()\n");
138 pthread_mutex_destroy(&mutex);
141 int SMUX::ParseSettings()
143 return smuxSettings.ParseSettings(settings);
148 assert(users != NULL && "users must not be NULL");
149 assert(tariffs != NULL && "tariffs must not be NULL");
150 assert(admins != NULL && "admins must not be NULL");
151 assert(services != NULL && "services must not be NULL");
152 assert(corporations != NULL && "corporations must not be NULL");
153 assert(traffcounter != NULL && "traffcounter must not be NULL");
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
171 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
173 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
175 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
177 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
179 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
182 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *tariffs, *users);
186 users->AddNotifierUserAdd(&addDelNotifier);
187 users->AddNotifierUserDel(&addDelNotifier);
190 Sensors::const_iterator it(sensors.begin());
191 while (it != sensors.end())
193 printfd(__FILE__, "%s = %s\n",
194 it->first.ToString().c_str(),
195 it->second->ToString().c_str());
202 if (pthread_create(&thread, NULL, Runner, this))
204 errorStr = "Cannot create thread.";
205 printfd(__FILE__, "Cannot create thread\n");
215 printfd(__FILE__, "SMUX::Stop() - Before\n");
218 users->DelNotifierUserDel(&addDelNotifier);
219 users->DelNotifierUserAdd(&addDelNotifier);
224 //5 seconds to thread stops itself
225 for (int i = 0; i < 25 && !stopped; i++)
227 struct timespec ts = {0, 200000000};
228 nanosleep(&ts, NULL);
231 //after 5 seconds waiting thread still running. now killing it
234 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
235 if (pthread_kill(thread, SIGINT))
237 errorStr = "Cannot kill thread.";
238 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
241 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
245 pthread_join(thread, NULL);
249 printfd(__FILE__, "SMUX::Stop() - After\n");
253 void * SMUX::Runner(void * d)
255 SMUX * smux = static_cast<SMUX *>(d);
271 if (WaitPackets(sock))
273 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
277 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
287 bool SMUX::PrepareNet()
289 sock = socket(AF_INET, SOCK_STREAM, 0);
293 errorStr = "Cannot create socket.";
294 printfd(__FILE__, "Cannot create socket\n");
298 struct sockaddr_in addr;
300 addr.sin_family = AF_INET;
301 addr.sin_port = htons(smuxSettings.GetPort());
302 addr.sin_addr.s_addr = smuxSettings.GetIP();
304 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
306 errorStr = "Cannot connect.";
307 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
314 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
316 SMUXHandlers::iterator it;
317 it = smuxHandlers.find(pdus->present);
318 if (it != smuxHandlers.end())
320 return (this->*(it->second))(pdus);
324 switch (pdus->present)
326 case SMUX_PDUs_PR_NOTHING:
327 printfd(__FILE__, "PDUs: nothing\n");
329 case SMUX_PDUs_PR_open:
330 printfd(__FILE__, "PDUs: open\n");
332 case SMUX_PDUs_PR_registerRequest:
333 printfd(__FILE__, "PDUs: registerRequest\n");
336 printfd(__FILE__, "PDUs: undefined\n");
338 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
343 bool SMUX::UpdateTables()
347 Tables::iterator it(tables.begin());
348 while (it != tables.end())
352 it->second->UpdateSensors(newSensors);
354 catch (const std::runtime_error & ex)
357 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
358 it->first.c_str(), ex.what());
366 Sensors::iterator it(newSensors.begin());
367 while (it != newSensors.end())
376 while (it != tables.end())
378 std::pair<Sensors::iterator, Sensors::iterator> res;
379 res = std::equal_range(sensors.begin(),
381 std::pair<OID, Sensor *>(OID(it->first), NULL),
383 Sensors::iterator sit(res.first);
384 while (sit != res.second)
389 sensors.erase(res.first, res.second);
393 sensors.insert(newSensors.begin(), newSensors.end());
398 void SMUX::SetNotifiers()
401 int h = users->OpenSearch();
402 assert(h && "USERS::OpenSearch is always correct");
404 while (!users->SearchNext(h, &u))
406 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, u));
407 u->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
410 users->CloseSearch(h);
413 void SMUX::ResetNotifiers()
415 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
416 while (it != notifiers.end())
418 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
423 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
428 void ADD_DEL_USER_NOTIFIER::Notify(const USER_PTR &)