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)
116 pthread_mutex_init(&mutex, NULL);
118 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
119 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
120 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
121 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
123 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
124 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
125 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
131 Sensors::iterator it;
132 for (it = sensors.begin(); it != sensors.end(); ++it)
137 for (it = tables.begin(); it != tables.end(); ++it)
140 printfd(__FILE__, "SMUX::~SMUX()\n");
141 pthread_mutex_destroy(&mutex);
144 int SMUX::ParseSettings()
146 return smuxSettings.ParseSettings(settings);
151 assert(users != NULL && "users must not be NULL");
152 assert(tariffs != NULL && "tariffs must not be NULL");
153 assert(admins != NULL && "admins must not be NULL");
154 assert(services != NULL && "services must not be NULL");
155 assert(corporations != NULL && "corporations must not be NULL");
156 assert(traffcounter != NULL && "traffcounter must not be NULL");
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.6")] = new DisabledDetailStatsUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new PassiveUsersSensor(*users);
170 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new CreditUsersSensor(*users);
171 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new FreeMbUsersSensor(*users);
172 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.2.1")] = new TotalTariffsSensor(*tariffs);
176 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
178 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
180 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
182 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
185 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);
191 Sensors::const_iterator it(sensors.begin());
192 while (it != sensors.end())
194 printfd(__FILE__, "%s = %s\n",
195 it->first.ToString().c_str(),
196 it->second->ToString().c_str());
203 if (pthread_create(&thread, NULL, Runner, this))
205 errorStr = "Cannot create thread.";
206 printfd(__FILE__, "Cannot create thread\n");
216 printfd(__FILE__, "SMUX::Stop() - Before\n");
221 //5 seconds to thread stops itself
222 for (int i = 0; i < 25 && !stopped; i++)
224 struct timespec ts = {0, 200000000};
225 nanosleep(&ts, NULL);
230 pthread_join(thread, NULL);
236 for (it = tables.begin(); it != tables.end(); ++it)
240 Sensors::iterator it;
241 for (it = sensors.begin(); it != sensors.end(); ++it)
245 tables.erase(tables.begin(), tables.end());
246 sensors.erase(sensors.begin(), sensors.end());
256 printfd(__FILE__, "SMUX::Stop() - After\n");
269 void * SMUX::Runner(void * d)
272 sigfillset(&signalSet);
273 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
275 SMUX * smux = static_cast<SMUX *>(d);
285 if (!SendOpenPDU(sock))
287 if (!SendRReqPDU(sock))
294 if (WaitPackets(sock))
296 SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
300 ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
310 bool SMUX::PrepareNet()
312 sock = socket(AF_INET, SOCK_STREAM, 0);
316 errorStr = "Cannot create socket.";
317 printfd(__FILE__, "Cannot create socket\n");
321 struct sockaddr_in addr;
323 addr.sin_family = AF_INET;
324 addr.sin_port = htons(smuxSettings.GetPort());
325 addr.sin_addr.s_addr = smuxSettings.GetIP();
327 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
329 errorStr = "Cannot connect.";
330 printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
337 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
339 SMUXHandlers::iterator it;
340 it = smuxHandlers.find(pdus->present);
341 if (it != smuxHandlers.end())
343 return (this->*(it->second))(pdus);
348 switch (pdus->present)
350 case SMUX_PDUs_PR_NOTHING:
351 printfd(__FILE__, "PDUs: nothing\n");
353 case SMUX_PDUs_PR_open:
354 printfd(__FILE__, "PDUs: open\n");
356 case SMUX_PDUs_PR_registerRequest:
357 printfd(__FILE__, "PDUs: registerRequest\n");
360 printfd(__FILE__, "PDUs: undefined\n");
362 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
368 bool SMUX::UpdateTables()
372 Tables::iterator it(tables.begin());
373 while (it != tables.end())
377 it->second->UpdateSensors(newSensors);
379 catch (const std::runtime_error & ex)
382 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
383 it->first.c_str(), ex.what());
391 Sensors::iterator it(newSensors.begin());
392 while (it != newSensors.end())
401 while (it != tables.end())
403 std::pair<Sensors::iterator, Sensors::iterator> res;
404 res = std::equal_range(sensors.begin(),
406 std::pair<OID, Sensor *>(OID(it->first), NULL),
408 Sensors::iterator sit(res.first);
409 while (sit != res.second)
414 sensors.erase(res.first, res.second);
418 sensors.insert(newSensors.begin(), newSensors.end());
423 void SMUX::SetNotifier(USER_PTR userPtr)
425 notifiers.push_back(CHG_AFTER_NOTIFIER(*this, userPtr));
426 userPtr->GetProperty().tariffName.AddAfterNotifier(¬ifiers.back());
429 void SMUX::UnsetNotifier(USER_PTR userPtr)
431 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
432 while (it != notifiers.end())
434 if (it->GetUserPtr() == userPtr)
436 userPtr->GetProperty().tariffName.DelAfterNotifier(&(*it));
444 void SMUX::SetNotifiers()
446 int h = users->OpenSearch();
447 assert(h && "USERS::OpenSearch is always correct");
450 while (!users->SearchNext(h, &u))
453 users->CloseSearch(h);
455 users->AddNotifierUserAdd(&addUserNotifier);
456 users->AddNotifierUserDel(&delUserNotifier);
458 tariffs->AddNotifierAdd(&addDelTariffNotifier);
459 tariffs->AddNotifierDel(&addDelTariffNotifier);
462 void SMUX::ResetNotifiers()
464 tariffs->DelNotifierDel(&addDelTariffNotifier);
465 tariffs->DelNotifierAdd(&addDelTariffNotifier);
467 users->DelNotifierUserDel(&delUserNotifier);
468 users->DelNotifierUserAdd(&addUserNotifier);
470 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
471 while (it != notifiers.end())
473 it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
478 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)