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