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()
43 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
46 std::vector<PARAM_VALUE>::const_iterator pvi;
50 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
51 if (pvi == s.moduleParams.end())
53 errorStr = "Parameter \'Port\' not found.";
54 printfd(__FILE__, "Parameter 'Port' not found\n");
57 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
59 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
60 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
65 pv.param = "Password";
66 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
67 if (pvi == s.moduleParams.end())
69 errorStr = "Parameter \'Password\' not found.";
70 printfd(__FILE__, "Parameter 'Password' not found\n");
75 password = pvi->value[0];
79 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
80 if (pvi == s.moduleParams.end())
82 errorStr = "Parameter \'Server\' not found.";
83 printfd(__FILE__, "Parameter 'Server' not found\n");
86 ip = inet_strington(pvi->value[0]);
112 addUserNotifier(*this),
113 delUserNotifier(*this),
114 addDelTariffNotifier(*this),
115 logger(GetPluginLogger(GetStgLogger(), "smux"))
117 pthread_mutex_init(&mutex, NULL);
119 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
120 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
121 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
122 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
124 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
125 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
126 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
132 Sensors::iterator it;
133 for (it = sensors.begin(); it != sensors.end(); ++it)
138 for (it = tables.begin(); it != tables.end(); ++it)
141 printfd(__FILE__, "SMUX::~SMUX()\n");
142 pthread_mutex_destroy(&mutex);
145 int SMUX::ParseSettings()
147 return smuxSettings.ParseSettings(settings);
152 assert(users != NULL && "users must not be NULL");
153 assert(tariffs != NULL && "tariffs must not be NULL");
154 assert(admins != NULL && "admins must not be NULL");
155 assert(services != NULL && "services must not be NULL");
156 assert(corporations != NULL && "corporations must not be NULL");
157 assert(traffcounter != NULL && "traffcounter must not be NULL");
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
170 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
171 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
172 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
173 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new TariffChangeUsersSensor(*users);
174 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new ActiveUsersSensor(*users);
176 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
178 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
180 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
182 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
184 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
187 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);
193 Sensors::const_iterator it(sensors.begin());
194 while (it != sensors.end())
196 printfd(__FILE__, "%s = %s\n",
197 it->first.ToString().c_str(),
198 it->second->ToString().c_str());
205 if (pthread_create(&thread, NULL, Runner, this))
207 errorStr = "Cannot create thread.";
208 logger("Cannot create thread.");
209 printfd(__FILE__, "Cannot create thread\n");
219 printfd(__FILE__, "SMUX::Stop() - Before\n");
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);
233 pthread_join(thread, NULL);
239 for (it = tables.begin(); it != tables.end(); ++it)
243 Sensors::iterator it;
244 for (it = sensors.begin(); it != sensors.end(); ++it)
248 tables.erase(tables.begin(), tables.end());
249 sensors.erase(sensors.begin(), sensors.end());
259 printfd(__FILE__, "SMUX::Stop() - After\n");
272 void * SMUX::Runner(void * d)
275 sigfillset(&signalSet);
276 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
278 SMUX * smux = static_cast<SMUX *>(d);
288 if (!SendOpenPDU(sock))
290 if (!SendRReqPDU(sock))
297 if (WaitPackets(sock))
299 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
303 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
313 bool SMUX::PrepareNet()
315 sock = socket(AF_INET, SOCK_STREAM, 0);
319 errorStr = "Cannot create socket.";
320 logger("Cannot create a socket: %s", strerror(errno));
321 printfd(__FILE__, "Cannot create socket\n");
325 struct sockaddr_in addr;
327 addr.sin_family = AF_INET;
328 addr.sin_port = htons(smuxSettings.GetPort());
329 addr.sin_addr.s_addr = smuxSettings.GetIP();
331 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
333 errorStr = "Cannot connect.";
334 logger("Cannot connect the socket: %s", strerror(errno));
335 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
342 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
344 SMUXHandlers::iterator it(smuxHandlers.find(pdus->present));
345 if (it != smuxHandlers.end())
347 return (this->*(it->second))(pdus);
352 switch (pdus->present)
354 case SMUX_PDUs_PR_NOTHING:
355 printfd(__FILE__, "PDUs: nothing\n");
357 case SMUX_PDUs_PR_open:
358 printfd(__FILE__, "PDUs: open\n");
360 case SMUX_PDUs_PR_registerRequest:
361 printfd(__FILE__, "PDUs: registerRequest\n");
364 printfd(__FILE__, "PDUs: undefined\n");
366 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
372 bool SMUX::UpdateTables()
376 Tables::iterator it(tables.begin());
377 while (it != tables.end())
381 it->second->UpdateSensors(newSensors);
383 catch (const std::runtime_error & ex)
386 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
387 it->first.c_str(), ex.what());
395 Sensors::iterator it(newSensors.begin());
396 while (it != newSensors.end())
405 while (it != tables.end())
407 std::pair<Sensors::iterator, Sensors::iterator> res;
408 res = std::equal_range(sensors.begin(),
410 std::pair<OID, Sensor *>(OID(it->first), NULL),
412 Sensors::iterator sit(res.first);
413 while (sit != res.second)
418 sensors.erase(res.first, res.second);
422 sensors.insert(newSensors.begin(), newSensors.end());
427 void SMUX::SetNotifier(USER_PTR userPtr)
429 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
430 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
433 void SMUX::UnsetNotifier(USER_PTR userPtr)
435 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
436 while (it != notifiers.end())
438 if (it->GetUserPtr() == userPtr)
440 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
448 void SMUX::SetNotifiers()
450 int h = users->OpenSearch();
451 assert(h && "USERS::OpenSearch is always correct");
454 while (!users->SearchNext(h, &u))
457 users->CloseSearch(h);
459 users->AddNotifierUserAdd(&addUserNotifier);
460 users->AddNotifierUserDel(&delUserNotifier);
462 tariffs->AddNotifierAdd(&addDelTariffNotifier);
463 tariffs->AddNotifierDel(&addDelTariffNotifier);
466 void SMUX::ResetNotifiers()
468 tariffs->DelNotifierDel(&addDelTariffNotifier);
469 tariffs->DelNotifierAdd(&addDelTariffNotifier);
471 users->DelNotifierUserDel(&delUserNotifier);
472 users->DelNotifierUserAdd(&addUserNotifier);
474 std::list<CHG_AFTER_NOTIFIER>::iterator it(notifiers.begin());
475 while (it != notifiers.end())
477 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));