]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/smux.cpp
Add tables update on user add and del
[stg.git] / projects / stargazer / plugins / other / smux / smux.cpp
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4
5 #include <cstring>
6 #include <cerrno>
7 #include <ctime>
8 #include <csignal>
9 #include <cassert>
10
11 #include <vector>
12 #include <algorithm>
13 #include <iterator>
14 #include <stdexcept>
15 #include <utility>
16
17 #include "stg/common.h"
18 #include "stg/plugin_creator.h"
19 /*#include "stg/users.h"
20 #include "stg/tariffs.h"
21 #include "stg/admins.h"
22 #include "stg/services.h"
23 #include "stg/corporations.h"
24 #include "stg/traffcounter.h"*/
25
26 #include "smux.h"
27 #include "utils.h"
28
29 PLUGIN_CREATOR<SMUX> smc;
30
31 PLUGIN * GetPlugin()
32 {
33 return smc.GetPlugin();
34 }
35
36 bool SPrefixLess(const Sensors::value_type & a,
37                  const Sensors::value_type & b)
38 {
39 return a.first.PrefixLess(b.first);
40 }
41
42 SMUX_SETTINGS::SMUX_SETTINGS()
43     : ip(0),
44       port(0)
45 {}
46
47 int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
48 {
49 PARAM_VALUE pv;
50 std::vector<PARAM_VALUE>::const_iterator pvi;
51 int p;
52
53 pv.param = "Port";
54 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55 if (pvi == s.moduleParams.end())
56     {
57     errorStr = "Parameter \'Port\' not found.";
58     printfd(__FILE__, "Parameter 'Port' not found\n");
59     return -1;
60     }
61 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
62     {
63     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
64     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
65     return -1;
66     }
67 port = p;
68
69 pv.param = "Password";
70 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
71 if (pvi == s.moduleParams.end())
72     {
73     errorStr = "Parameter \'Password\' not found.";
74     printfd(__FILE__, "Parameter 'Password' not found\n");
75     password = "";
76     }
77 else
78     {
79     password = pvi->value[0];
80     }
81
82 pv.param = "Server";
83 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
84 if (pvi == s.moduleParams.end())
85     {
86     errorStr = "Parameter \'Server\' not found.";
87     printfd(__FILE__, "Parameter 'Server' not found\n");
88     return -1;
89     }
90 ip = inet_strington(pvi->value[0]);
91
92 return 0;
93 }
94
95 SMUX::SMUX()
96     : PLUGIN(),
97       users(NULL),
98       tariffs(NULL),
99       admins(NULL),
100       services(NULL),
101       corporations(NULL),
102       traffcounter(NULL),
103       running(false),
104       stopped(true),
105       sock(-1),
106       smuxHandlers(),
107       pdusHandlers(),
108       sensors(),
109       tables(),
110       notifiers(),
111       addDelNotifier(*this)
112 {
113 pthread_mutex_init(&mutex, NULL);
114
115 smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
116 smuxHandlers[SMUX_PDUs_PR_registerResponse] = &SMUX::RegisterResponseHandler;
117 smuxHandlers[SMUX_PDUs_PR_pdus] = &SMUX::PDUsRequestHandler;
118 smuxHandlers[SMUX_PDUs_PR_commitOrRollback] = &SMUX::CommitOrRollbackHandler;
119
120 pdusHandlers[PDUs_PR_get_request] = &SMUX::GetRequestHandler;
121 pdusHandlers[PDUs_PR_get_next_request] = &SMUX::GetNextRequestHandler;
122 pdusHandlers[PDUs_PR_set_request] = &SMUX::SetRequestHandler;
123 }
124
125 SMUX::~SMUX()
126 {
127     {
128     Sensors::iterator it;
129     for (it = sensors.begin(); it != sensors.end(); ++it)
130         delete it->second;
131     }
132     {
133     Tables::iterator it;
134     for (it = tables.begin(); it != tables.end(); ++it)
135         delete it->second;
136     }
137 printfd(__FILE__, "SMUX::~SMUX()\n");
138 pthread_mutex_destroy(&mutex);
139 }
140
141 int SMUX::ParseSettings()
142 {
143 return smuxSettings.ParseSettings(settings);
144 }
145
146 int SMUX::Start()
147 {
148 assert(users != NULL && "users must not be NULL");
149 assert(tariffs != NULL && "tariffs must not be NULL");
150 assert(admins != NULL && "admins must not be NULL");
151 assert(services != NULL && "services must not be NULL");
152 assert(corporations != NULL && "corporations must not be NULL");
153 assert(traffcounter != NULL && "traffcounter must not be NULL");
154
155 if (PrepareNet())
156     return -1;
157
158 // Users
159 sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users);
160 sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users);
161 sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users);
162 sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users);
163 sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users);
164 sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users);
165 sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users);
166 sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users);
167 sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users);
168 sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users);
169 sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users);
170 // Tariffs
171 sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs);
172 // Admins
173 sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins);
174 // Services
175 sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services);
176 // Corporations
177 sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations);
178 // Traffcounter
179 sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
180
181 // Table data
182 tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *tariffs, *users);
183
184 UpdateTables();
185 SetNotifiers();
186 users->AddNotifierUserAdd(&addDelNotifier);
187 users->AddNotifierUserDel(&addDelNotifier);
188
189 #ifdef DEBUG
190 Sensors::const_iterator it(sensors.begin());
191 while (it != sensors.end())
192     {
193     printfd(__FILE__, "%s = %s\n",
194             it->first.ToString().c_str(),
195             it->second->ToString().c_str());
196     ++it;
197     }
198 #endif
199
200 if (!running)
201     {
202     if (pthread_create(&thread, NULL, Runner, this))
203         {
204         errorStr = "Cannot create thread.";
205         printfd(__FILE__, "Cannot create thread\n");
206         return -1;
207         }
208     }
209
210 return 0;
211 }
212
213 int SMUX::Stop()
214 {
215 printfd(__FILE__, "SMUX::Stop() - Before\n");
216 running = false;
217
218 users->DelNotifierUserDel(&addDelNotifier);
219 users->DelNotifierUserAdd(&addDelNotifier);
220 ResetNotifiers();
221
222 if (!stopped)
223     {
224     //5 seconds to thread stops itself
225     for (int i = 0; i < 25 && !stopped; i++)
226         {
227         struct timespec ts = {0, 200000000};
228         nanosleep(&ts, NULL);
229         }
230
231     //after 5 seconds waiting thread still running. now killing it
232     if (!stopped)
233         {
234         printfd(__FILE__, "SMUX::Stop() - failed to stop thread, killing it\n");
235         if (pthread_kill(thread, SIGINT))
236             {
237             errorStr = "Cannot kill thread.";
238             printfd(__FILE__, "SMUX::Stop() - Cannot kill thread\n");
239             return -1;
240             }
241         printfd(__FILE__, "SMUX::Stop() -  killed Run\n");
242         }
243     }
244
245 pthread_join(thread, NULL);
246
247 close(sock);
248
249 printfd(__FILE__, "SMUX::Stop() - After\n");
250 return 0;
251 }
252
253 void * SMUX::Runner(void * d)
254 {
255 SMUX * smux = static_cast<SMUX *>(d);
256
257 smux->Run();
258
259 return NULL;
260 }
261
262 void SMUX::Run()
263 {
264 SendOpenPDU(sock);
265 SendRReqPDU(sock);
266 running = true;
267 stopped = false;
268
269 while(running)
270     {
271     if (WaitPackets(sock))
272         {
273         SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
274         if (pdus)
275             {
276             DispatchPDUs(pdus);
277             ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
278             }
279         }
280     if (!running)
281         break;
282     }
283 SendClosePDU(sock);
284 stopped = true;
285 }
286
287 bool SMUX::PrepareNet()
288 {
289 sock = socket(AF_INET, SOCK_STREAM, 0);
290
291 if (sock < 0)
292     {
293     errorStr = "Cannot create socket.";
294     printfd(__FILE__, "Cannot create socket\n");
295     return true;
296     }
297
298 struct sockaddr_in addr;
299
300 addr.sin_family = AF_INET;
301 addr.sin_port = htons(smuxSettings.GetPort());
302 addr.sin_addr.s_addr = smuxSettings.GetIP();
303
304 if (connect(sock, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)))
305     {
306     errorStr = "Cannot connect.";
307     printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno));
308     return true;
309     }
310
311 return false;
312 }
313
314 bool SMUX::DispatchPDUs(const SMUX_PDUs_t * pdus)
315 {
316 SMUXHandlers::iterator it;
317 it = smuxHandlers.find(pdus->present);
318 if (it != smuxHandlers.end())
319     {
320     return (this->*(it->second))(pdus);
321     }
322 else
323     {
324     switch (pdus->present)
325         {
326         case SMUX_PDUs_PR_NOTHING:
327             printfd(__FILE__, "PDUs: nothing\n");
328             break;
329         case SMUX_PDUs_PR_open:
330             printfd(__FILE__, "PDUs: open\n");
331             break;
332         case SMUX_PDUs_PR_registerRequest:
333             printfd(__FILE__, "PDUs: registerRequest\n");
334             break;
335         default:
336             printfd(__FILE__, "PDUs: undefined\n");
337         }
338     asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
339     }
340 return false;
341 }
342
343 bool SMUX::UpdateTables()
344 {
345 Sensors newSensors;
346 bool done = true;
347 Tables::iterator it(tables.begin());
348 while (it != tables.end())
349     {
350     try
351         {
352         it->second->UpdateSensors(newSensors);
353         }
354     catch (const std::runtime_error & ex)
355         {
356         printfd(__FILE__,
357                 "SMUX::UpdateTables - failed to update table '%s': '%s'\n",
358                 it->first.c_str(), ex.what());
359         done = false;
360         break;
361         }
362     ++it;
363     }
364 if (!done)
365     {
366     Sensors::iterator it(newSensors.begin());
367     while (it != newSensors.end())
368         {
369         delete it->second;
370         ++it;
371         }
372     return false;
373     }
374
375 it = tables.begin();
376 while (it != tables.end())
377     {
378     std::pair<Sensors::iterator, Sensors::iterator> res;
379     res = std::equal_range(sensors.begin(),
380                            sensors.end(),
381                            std::pair<OID, Sensor *>(OID(it->first), NULL),
382                            SPrefixLess);
383     Sensors::iterator sit(res.first);
384     while (sit != res.second)
385         {
386         delete sit->second;
387         ++sit;
388         }
389     sensors.erase(res.first, res.second);
390     ++it;
391     }
392
393 sensors.insert(newSensors.begin(), newSensors.end());
394
395 return true;
396 }
397
398 void SMUX::SetNotifiers()
399 {
400 USER_PTR u;
401 int h = users->OpenSearch();
402 assert(h && "USERS::OpenSearch is always correct");
403
404 while (!users->SearchNext(h, &u))
405     {
406     notifiers.push_back(CHG_AFTER_NOTIFIER(*this, u));
407     u->GetProperty().tariffName.AddAfterNotifier(&notifiers.back());
408     }
409
410 users->CloseSearch(h);
411 }
412
413 void SMUX::ResetNotifiers()
414 {
415 std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
416 while (it != notifiers.end())
417     {
418     it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
419     ++it;
420     }
421 }
422
423 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
424 {
425 smux.UpdateTables();
426 }
427
428 void ADD_DEL_USER_NOTIFIER::Notify(const USER_PTR &)
429 {
430 smux.UpdateTables();
431 }