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