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");
268 void * SMUX::Runner(void * d)
270 SMUX * smux = static_cast<SMUX *>(d);
280 if (!SendOpenPDU(sock))
282 if (!SendRReqPDU(sock))
289 if (WaitPackets(sock))
291 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
295 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
305 bool SMUX::PrepareNet()
307 sock = socket(AF_INET, SOCK_STREAM, 0);
311 errorStr = "Cannot create socket.";
312 printfd(__FILE__, "Cannot create socket\n");
316 struct sockaddr_in addr;
318 addr.sin_family = AF_INET;
319 addr.sin_port = htons(smuxSettings.GetPort());
320 addr.sin_addr.s_addr = smuxSettings.GetIP();
322 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
324 errorStr = "Cannot connect.";
325 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
332 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
334 SMUXHandlers::iterator it;
335 it = smuxHandlers.find(pdus->present);
336 if (it != smuxHandlers.end())
338 return (this->*(it->second))(pdus);
343 switch (pdus->present)
345 case SMUX_PDUs_PR_NOTHING:
346 printfd(__FILE__, "PDUs: nothing\n");
348 case SMUX_PDUs_PR_open:
349 printfd(__FILE__, "PDUs: open\n");
351 case SMUX_PDUs_PR_registerRequest:
352 printfd(__FILE__, "PDUs: registerRequest\n");
355 printfd(__FILE__, "PDUs: undefined\n");
357 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
363 bool SMUX::UpdateTables()
367 Tables::iterator it(tables.begin());
368 while (it != tables.end())
372 it->second->UpdateSensors(newSensors);
374 catch (const std::runtime_error & ex)
377 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
378 it->first.c_str(), ex.what());
386 Sensors::iterator it(newSensors.begin());
387 while (it != newSensors.end())
396 while (it != tables.end())
398 std::pair<Sensors::iterator, Sensors::iterator> res;
399 res = std::equal_range(sensors.begin(),
401 std::pair<OID, Sensor *>(OID(it->first), NULL),
403 Sensors::iterator sit(res.first);
404 while (sit != res.second)
409 sensors.erase(res.first, res.second);
413 sensors.insert(newSensors.begin(), newSensors.end());
418 void SMUX::SetNotifier(USER_PTR userPtr)
420 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
421 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
424 void SMUX::UnsetNotifier(USER_PTR userPtr)
426 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
427 while (it != notifiers.end())
429 if (it->GetUserPtr() == userPtr)
431 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
439 void SMUX::SetNotifiers()
441 int h = users->OpenSearch();
442 assert(h && "USERS::OpenSearch is always correct");
445 while (!users->SearchNext(h, &u))
448 users->CloseSearch(h);
450 users->AddNotifierUserAdd(&addUserNotifier);
451 users->AddNotifierUserDel(&delUserNotifier);
453 tariffs->AddNotifierAdd(&addDelTariffNotifier);
454 tariffs->AddNotifierDel(&addDelTariffNotifier);
457 void SMUX::ResetNotifiers()
459 tariffs->DelNotifierDel(&addDelTariffNotifier);
460 tariffs->DelNotifierAdd(&addDelTariffNotifier);
462 users->DelNotifierUserDel(&delUserNotifier);
463 users->DelNotifierUserAdd(&addUserNotifier);
465 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
466 while (it != notifiers.end())
468 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
473 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)