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");
216 //5 seconds to thread stops itself
217 for (int i = 0; i < 25 && !stopped; i++)
219 struct timespec ts = {0, 200000000};
220 nanosleep(&ts, NULL);
223 //after 5 seconds waiting thread still running. now killing it
226 printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
227 if (pthread_kill(thread, SIGINT))
229 errorStr = "Cannot kill thread.";
230 printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
233 printfd(__FILE__, "SMUX::Stop() - killed Run\n");
237 pthread_join(thread, NULL);
241 printfd(__FILE__, "SMUX::Stop() - After\n");
245 void * SMUX::Runner(void * d)
247 SMUX * smux = static_cast<SMUX *>(d);
263 if (WaitPackets(sock))
265 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
269 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
279 bool SMUX::PrepareNet()
281 sock = socket(AF_INET, SOCK_STREAM, 0);
285 errorStr = "Cannot create socket.";
286 printfd(__FILE__, "Cannot create socket\n");
290 struct sockaddr_in addr;
292 addr.sin_family = AF_INET;
293 addr.sin_port = htons(smuxSettings.GetPort());
294 addr.sin_addr.s_addr = smuxSettings.GetIP();
296 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
298 errorStr = "Cannot connect.";
299 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
306 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
308 SMUXHandlers::iterator it;
309 it = smuxHandlers.find(pdus->present);
310 if (it != smuxHandlers.end())
312 return (this->*(it->second))(pdus);
317 switch (pdus->present)
319 case SMUX_PDUs_PR_NOTHING:
320 printfd(__FILE__, "PDUs: nothing\n");
322 case SMUX_PDUs_PR_open:
323 printfd(__FILE__, "PDUs: open\n");
325 case SMUX_PDUs_PR_registerRequest:
326 printfd(__FILE__, "PDUs: registerRequest\n");
329 printfd(__FILE__, "PDUs: undefined\n");
331 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
337 bool SMUX::UpdateTables()
341 Tables::iterator it(tables.begin());
342 while (it != tables.end())
346 it->second->UpdateSensors(newSensors);
348 catch (const std::runtime_error & ex)
351 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
352 it->first.c_str(), ex.what());
360 Sensors::iterator it(newSensors.begin());
361 while (it != newSensors.end())
370 while (it != tables.end())
372 std::pair<Sensors::iterator, Sensors::iterator> res;
373 res = std::equal_range(sensors.begin(),
375 std::pair<OID, Sensor *>(OID(it->first), NULL),
377 Sensors::iterator sit(res.first);
378 while (sit != res.second)
383 sensors.erase(res.first, res.second);
387 sensors.insert(newSensors.begin(), newSensors.end());
392 void SMUX::SetNotifier(USER_PTR userPtr)
394 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
395 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
398 void SMUX::UnsetNotifier(USER_PTR userPtr)
400 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
401 while (it != notifiers.end())
403 if (it->GetUserPtr() == userPtr)
405 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
413 void SMUX::SetNotifiers()
415 int h = users->OpenSearch();
416 assert(h && "USERS::OpenSearch is always correct");
419 while (!users->SearchNext(h, &u))
422 users->CloseSearch(h);
424 users->AddNotifierUserAdd(&addUserNotifier);
425 users->AddNotifierUserDel(&delUserNotifier);
427 tariffs->AddNotifierAdd(&addDelTariffNotifier);
428 tariffs->AddNotifierDel(&addDelTariffNotifier);
431 void SMUX::ResetNotifiers()
433 tariffs->DelNotifierDel(&addDelTariffNotifier);
434 tariffs->DelNotifierAdd(&addDelTariffNotifier);
436 users->DelNotifierUserDel(&delUserNotifier);
437 users->DelNotifierUserAdd(&addUserNotifier);
439 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
440 while (it != notifiers.end())
442 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
447 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)