2 #include <sys/socket.h>
17 #include "stg/common.h"
18 #include "stg/plugin_creator.h"
23 PLUGIN_CREATOR<SMUX> smc;
27 return smc.GetPlugin();
30 bool SPrefixLess(const Sensors::value_type & a,
31 const Sensors::value_type & b)
33 return a.first.PrefixLess(b.first);
36 SMUX_SETTINGS::SMUX_SETTINGS()
41 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
44 std::vector<PARAM_VALUE>::const_iterator pvi;
48 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
49 if (pvi == s.moduleParams.end())
51 errorStr = "Parameter \'Port\' not found.";
52 printfd(__FILE__, "Parameter 'Port' not found\n");
55 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
57 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
58 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
63 pv.param = "Password";
64 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
65 if (pvi == s.moduleParams.end())
67 errorStr = "Parameter \'Password\' not found.";
68 printfd(__FILE__, "Parameter 'Password' not found\n");
73 password = pvi->value[0];
77 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
78 if (pvi == s.moduleParams.end())
80 errorStr = "Parameter \'Server\' not found.";
81 printfd(__FILE__, "Parameter 'Server' not found\n");
84 ip = inet_strington(pvi->value[0]);
105 addUserNotifier(*this),
106 delUserNotifier(*this),
107 addDelTariffNotifier(*this)
109 pthread_mutex_init(&mutex, NULL);
111 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
112 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
113 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
114 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
116 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
117 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
118 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
124 Sensors::iterator it;
125 for (it = sensors.begin(); it != sensors.end(); ++it)
130 for (it = tables.begin(); it != tables.end(); ++it)
133 printfd(__FILE__, "SMUX::~SMUX()\n");
134 pthread_mutex_destroy(&mutex);
137 int SMUX::ParseSettings()
139 return smuxSettings.ParseSettings(settings);
144 assert(users != NULL && "users must not be NULL");
145 assert(tariffs != NULL && "tariffs must not be NULL");
146 assert(admins != NULL && "admins must not be NULL");
147 assert(services != NULL && "services must not be NULL");
148 assert(corporations != NULL && "corporations must not be NULL");
149 assert(traffcounter != NULL && "traffcounter must not be NULL");
155 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
156 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
157 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
158 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
169 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
171 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
173 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
175 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
178 tables[".1.3.6.1.4.1.38313.1.2.2"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.2.2", *tariffs, *users);
184 Sensors::const_iterator it(sensors.begin());
185 while (it != sensors.end())
187 printfd(__FILE__, "%s = %s\n",
188 it->first.ToString().c_str(),
189 it->second->ToString().c_str());
196 if (pthread_create(&thread, NULL, Runner, this))
198 errorStr = "Cannot create thread.";
199 printfd(__FILE__, "Cannot create thread\n");
209 printfd(__FILE__, "SMUX::Stop() - Before\n");
214 //5 seconds to thread stops itself
215 for (int i = 0; i < 25 && !stopped; i++)
217 struct timespec ts = {0, 200000000};
218 nanosleep(&ts, NULL);
221 //after 5 seconds waiting thread still running. now killing it
224 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
225 if (pthread_kill(thread, SIGINT))
227 errorStr = "Cannot kill thread.";
228 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
231 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
235 pthread_join(thread, NULL);
241 for (it = tables.begin(); it != tables.end(); ++it)
245 Sensors::iterator it;
246 for (it = sensors.begin(); it != sensors.end(); ++it)
250 tables.erase(tables.begin(), tables.end());
251 sensors.erase(sensors.begin(), sensors.end());
255 printfd(__FILE__, "SMUX::Stop() - After\n");
259 void * SMUX::Runner(void * d)
261 SMUX * smux = static_cast<SMUX *>(d);
277 if (WaitPackets(sock))
279 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
283 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
293 bool SMUX::PrepareNet()
295 sock = socket(AF_INET, SOCK_STREAM, 0);
299 errorStr = "Cannot create socket.";
300 printfd(__FILE__, "Cannot create socket\n");
304 struct sockaddr_in addr;
306 addr.sin_family = AF_INET;
307 addr.sin_port = htons(smuxSettings.GetPort());
308 addr.sin_addr.s_addr = smuxSettings.GetIP();
310 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
312 errorStr = "Cannot connect.";
313 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
320 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
322 SMUXHandlers::iterator it;
323 it = smuxHandlers.find(pdus->present);
324 if (it != smuxHandlers.end())
326 return (this->*(it->second))(pdus);
331 switch (pdus->present)
333 case SMUX_PDUs_PR_NOTHING:
334 printfd(__FILE__, "PDUs: nothing\n");
336 case SMUX_PDUs_PR_open:
337 printfd(__FILE__, "PDUs: open\n");
339 case SMUX_PDUs_PR_registerRequest:
340 printfd(__FILE__, "PDUs: registerRequest\n");
343 printfd(__FILE__, "PDUs: undefined\n");
345 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
351 bool SMUX::UpdateTables()
355 Tables::iterator it(tables.begin());
356 while (it != tables.end())
360 it->second->UpdateSensors(newSensors);
362 catch (const std::runtime_error & ex)
365 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
366 it->first.c_str(), ex.what());
374 Sensors::iterator it(newSensors.begin());
375 while (it != newSensors.end())
384 while (it != tables.end())
386 std::pair<Sensors::iterator, Sensors::iterator> res;
387 res = std::equal_range(sensors.begin(),
389 std::pair<OID, Sensor *>(OID(it->first), NULL),
391 Sensors::iterator sit(res.first);
392 while (sit != res.second)
397 sensors.erase(res.first, res.second);
401 sensors.insert(newSensors.begin(), newSensors.end());
406 void SMUX::SetNotifier(USER_PTR userPtr)
408 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
409 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
412 void SMUX::UnsetNotifier(USER_PTR userPtr)
414 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
415 while (it != notifiers.end())
417 if (it->GetUserPtr() == userPtr)
419 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
427 void SMUX::SetNotifiers()
429 int h = users->OpenSearch();
430 assert(h && "USERS::OpenSearch is always correct");
433 while (!users->SearchNext(h, &u))
436 users->CloseSearch(h);
438 users->AddNotifierUserAdd(&addUserNotifier);
439 users->AddNotifierUserDel(&delUserNotifier);
441 tariffs->AddNotifierAdd(&addDelTariffNotifier);
442 tariffs->AddNotifierDel(&addDelTariffNotifier);
445 void SMUX::ResetNotifiers()
447 tariffs->DelNotifierDel(&addDelTariffNotifier);
448 tariffs->DelNotifierAdd(&addDelTariffNotifier);
450 users->DelNotifierUserDel(&delUserNotifier);
451 users->DelNotifierUserAdd(&addUserNotifier);
453 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
454 while (it != notifiers.end())
456 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
461 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)